Skip to content

Commit

Permalink
Ensure a tag is present in ImageStream
Browse files Browse the repository at this point in the history
  • Loading branch information
lcarva committed Dec 16, 2016
1 parent 45827a3 commit d6c3875
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions osbs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,14 @@ def get_serviceaccount_tokens(self, username="~"):
def get_image_stream_tag(self, tag_id):
return self.os.get_image_stream_tag(tag_id)

@osbsapi
def put_image_stream_tag(self, tag_id, tag):
return self.os.put_image_stream_tag(tag_id, tag)

@osbsapi
def ensure_image_stream_tag(self, stream, tag_id, scheduled=False):
return self.os.ensure_image_stream_tag(stream, tag_id, scheduled)

@osbsapi
def get_image_stream(self, stream_id):
return self.os.get_image_stream(stream_id)
Expand Down
17 changes: 16 additions & 1 deletion osbs/build/build_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ def render_add_yum_repo_by_url(self):
"add_yum_repo_by_url", "inject_proxy",
self.spec.proxy.value)

def render_update_parent_image_stream_tag(self, use_auth=None):
phase = 'prebuild_plugins'
plugin = 'update_parent_image_stream_tag'
if not self.dj.dock_json_has_plugin_conf(phase, plugin):
return

self.dj.dock_json_set_arg(phase, plugin, 'openshift_url',
self.spec.builder_openshift_url.value)
self.dj.dock_json_set_arg(phase, plugin, 'image_stream_tag',
self.spec.trigger_imagestreamtag.value)
if use_auth is not None:
self.dj.dock_json_set_arg(phase, plugin, 'use_auth', use_auth)

def render_check_and_set_rebuild(self, use_auth=None):
if self.dj.dock_json_has_plugin_conf('prebuild_plugins',
'check_and_set_rebuild'):
Expand Down Expand Up @@ -364,7 +377,8 @@ def adjust_for_triggers(self):
"""
triggers = self.template['spec'].get('triggers', [])
if not triggers:
for when, which in [("prebuild_plugins", "check_and_set_rebuild"),
for when, which in [("prebuild_plugins", "update_parent_image_stream_tag"),
("prebuild_plugins", "check_and_set_rebuild"),
("prebuild_plugins", "stop_autorebuild_if_disabled"),
("postbuild_plugins", "import_image"),
("exit_plugins", "sendmail")]:
Expand Down Expand Up @@ -750,6 +764,7 @@ def render(self, validate=True):
self.render_add_yum_repo_by_url()

use_auth = self.spec.use_auth.value
self.render_update_parent_image_stream_tag(use_auth=use_auth)
self.render_check_and_set_rebuild(use_auth=use_auth)
self.render_store_metadata_in_osv3(use_auth=use_auth)

Expand Down
67 changes: 67 additions & 0 deletions osbs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@
logger = logging.getLogger(__name__)


# TODO: Create json for it
TAG_TEMPLATE = """\
{
"kind": "ImageStreamTag",
"apiVersion": "v1",
"metadata": {
"name": "{{IMAGE_STREAM_ID}}:{{TAG_ID}}"
},
"tag": {
"name": "{{TAG_ID}}",
"from": {
"kind": "DockerImage",
"name": "{{REPOSITORY}}:{{TAG_ID}}"
},
"importPolicy": {}
}
}
"""


def check_response(response):
if response.status_code not in (httplib.OK, httplib.CREATED):
if hasattr(response, 'content'):
Expand Down Expand Up @@ -705,6 +725,53 @@ def get_image_stream_tag(self, tag_id):
check_response(response)
return response

def put_image_stream_tag(self, tag_id, tag):
url = self._build_url("imagestreamtags/%s" % tag_id)
response = self._put(url, data=tag,
headers={"Content-Type": "application/json"})
check_response(response)
return response

def ensure_image_stream_tag(self, stream, tag_id, scheduled=False):
stream_id = stream['metadata']['name']
insecure = (stream['metadata']
.get('annotations', {})
.get('openshift.io/image.insecureRepository')
== 'true')
repo = stream['spec']['dockerImageRepository']
prefixed_tag_id = '{}:{}'.format(stream_id, tag_id)

changed = False
try:
tag = self.get_image_stream_tag(prefixed_tag_id).json()
logger.debug('image stream tag found: %s', prefixed_tag_id)
except OsbsResponseException as exc:
if exc.status_code != 404:
raise

logger.debug('image stream tag NOT found: %s', prefixed_tag_id)
tag = json.loads(TAG_TEMPLATE)
tag['metadata']['name'] = prefixed_tag_id
tag['tag']['name'] = tag_id
tag['tag']['from']['name'] = '{}:{}'.format(repo, tag_id)
changed = True

if insecure != tag['tag']['importPolicy'].get('insecure', False):
tag['tag']['importPolicy']['insecure'] = insecure
logger.debug('setting importPolicy.insecure to: %s', insecure)
changed = True

if scheduled != tag['tag']['importPolicy'].get('scheduled', False):
tag['tag']['importPolicy']['scheduled'] = scheduled
logger.debug('setting importPolicy.scheduled to: %s', scheduled)
changed = True

if changed:
logger.debug('modifying image stream tag: %s', prefixed_tag_id)
self.put_image_stream_tag(prefixed_tag_id, json.dumps(tag))

return changed

def get_image_stream(self, stream_id):
url = self._build_url("imagestreams/%s" % stream_id)
response = self._get(url)
Expand Down

0 comments on commit d6c3875

Please sign in to comment.