OpenShift:等待图像流创建

时间:2019-10-30 10:26:21

标签: bash git nginx kubernetes openshift

我正在创建一个安装脚本,该脚本将根据YAML文件创建资源†。该脚本将执行以下命令:

oc new-app registry.access.redhat.com/rhscl/nginx-114-rhel7~http://github.com/username/repo.git

创建了三个YAML文件,如下所示:

  1. imagestream代表nginx-114-rhel7-is-nginx.yaml
apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
  labels:
    build: build-repo
  name: nginx-114-rhel7
  namespace: ns
spec:
  tags:
    - annotations: null
      from:
        kind: DockerImage
        name: registry.access.redhat.com/rhscl/nginx-114-rhel7
      name: latest
      referencePolicy:
        type: Source
  1. imagestream代表repo-is-repo.yaml
apiVersion: v1
kind: ImageStream
metadata:
  labels:
    application: is-rp
  name: is-rp
  namespace: ns
  1. buildconfig for repo(输出将是imagestream for repo)-bc-repo.yaml

apiVersion: v1
kind: BuildConfig
metadata:
  labels:
    build: rp
  name: bc-rp
  namespace: ns
spec:
  output:
    to:
      kind: ImageStreamTag
      name: 'is-rp:latest'
  postCommit: {}
  resources: {}
  runPolicy: Serial
  source:
    git:
      ref: dev_1.0
      uri: 'http://github.com/username/repo.git'
    type: Git
  strategy:
    sourceStrategy:
      from:
        kind: ImageStreamTag
        name: 'nginx-114-rhel7:latest'
        namespace: flo
    type: Source
  successfulBuildsHistoryLimit: 5

这些命令一个接一个地运行时,

oc create -f is-nginx.yaml;oc create -f is-repo.yaml;oc create -f bc-repo.yaml;oc start-build bc/bc-rep --wait

我收到此错误消息,

The ImageStreamTag "nginx-114-rhel7:latest" is invalid: from: Error resolving ImageStreamTag nginx-114-rhel7:latest in namespace ns: unable to find latest tagged image

但是,当我在sleep之前使用start-build运行命令时,将正确触发构建。

oc create -f is-nginx.yaml;oc create -f is-repo.yaml;oc create -f bc-repo.yaml;sleep 5;oc start-build bc/bc-rep

如何在不输入start-build命令的情况下触发sleepoc wait似乎仅适用于--for=condition--for=delete。我不知道--for=condition使用什么值。

†-我没有看到关于使用YAML或等效的oc命令(仅 )创建安装脚本的明确指南,该命令用于将应用程序部署到OpenShift。

2 个答案:

答案 0 :(得分:0)

应该运行Image Change TriggersConfiguration Change Triggers

,而不要运行oc start-build

在构建配置中,您可以指向ImageStreamTag来开始构建

type: "imageChange" 
imageChange: {}
type: "imageChange" 
imageChange:
  from:
    kind: "ImageStreamTag"
    name: "custom-image:latest"

答案 1 :(得分:0)

oc wait --for=condition=available仅在状态对象包含条件时才有效,而图像流则不是如此。

status:
  dockerImageRepository: image-registry.openshift-image-registry.svc:5000/test/s2i-openresty-centos7
  tags:
  - items:
    - created: "2019-11-05T11:23:45Z"
      dockerImageReference: quay.io/openresty/openresty-centos7@sha256:db224d642ad4001ab971b934a6444da16bb87ddfcc9c048bbf68febafcac52db
      generation: 2
      image: sha256:db224d642ad4001ab971b934a6444da16bb87ddfcc9c048bbf68febafcac52db
    tag: builder
  - items:
    - created: "2019-11-05T11:23:45Z"
      dockerImageReference: qquay.io/openresty/openresty-centos7@sha256:082ee75ed83f161375d0d281f235b7271176b1d129f5ed9972c7d31923e08660
      generation: 2
      image: sha256:082ee75ed83f161375d0d281f235b7271176b1d129f5ed9972c7d31923e08660
    tag: runtime

直到openshift CLI为图像流实现了内置的等待命令,我用来做的是:请求图像流对象,解析状态对象以获得所需的标记,如果尚未准备就绪,则休眠几秒钟。像这样:

until oc get is nginx-114-rhel7 -o json || echo '{}' | jq '[.status.tags[] | select(.tag == "latest")] | length == 1' --exit-status; do
    sleep 1
done