OpenShift : Waiting for image stream to be created

10/30/2019

I am creating an installation script that will create resources off of YAML files†. This script will do the equivalent of this command:

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

Three YAML files were created as follows:

  1. imagestream for 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 for repo - is-repo.yaml
apiVersion: v1
kind: ImageStream
metadata:
  labels:
    application: is-rp
  name: is-rp
  namespace: ns
  1. buildconfig for repo (output will be 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

When these commands are run one after another,

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

I get this error message,

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

But, when I run the commands with a sleep before start-build, the build is triggered correctly.

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

How do I trigger start-build without entering a sleep command? The oc wait seems to work only for --for=condition and --for=delete. I do not know what value is to be used for --for=condition.

† - I do not see a clear guideline on creating installation scripts - with YAML or equivalent oc commands only - for deploying applications on to OpenShift.

-- cogitoergosum
bash
git
kubernetes
nginx
openshift

2 Answers

11/5/2019

oc wait --for=condition=available only works when status object includes conditions, which is not the case for imagestreams.

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

Until openshift CLI implements builtin waiting command for imagestreams, what I used to do is: request imagestream object, parse status object for the expected tag and sleep few seconds if not ready. Something like this:

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

10/30/2019

Instead of running oc start-build, you should look into Image Change Triggers and Configuration Change Triggers

In your build config, you can point to an ImageStreamTag to start a build

type: "imageChange" 
imageChange: {}
type: "imageChange" 
imageChange:
  from:
    kind: "ImageStreamTag"
    name: "custom-image:latest"
-- Chris Bolton
Source: StackOverflow