Skip to content

Commit

Permalink
Merge pull request #194 from projectatomic/imagestream-naming
Browse files Browse the repository at this point in the history
Imagestream naming
  • Loading branch information
twaugh committed Aug 11, 2015
2 parents 7b8e4a4 + 2dc530f commit 2711e20
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 44 deletions.
9 changes: 9 additions & 0 deletions inputs/prod_inner.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@
"image_id": "BUILT_IMAGE_ID"
},
"name": "all_rpm_packages"
},
{
"args": {
"imagestream": "{{IMAGESTREAM}}",
"docker_image_repo": "{{DOCKER_IMAGE_REPO}}",
"url": "{{OPENSHIFT_URI}}",
"verify_ssl": false
},
"name": "import_image"
}
],
"exit_plugins": [
Expand Down
9 changes: 0 additions & 9 deletions inputs/simple_inner.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{
"prebuild_plugins": [
{
"args": {
"label_key": "is_autorebuild",
"label_value": "true",
"url": "{{OPENSHIFT_URI}}",
"verify_ssl": false
},
"name": "check_and_set_rebuild"
},
{
"name": "pull_base_image",
"args": {
Expand Down
20 changes: 12 additions & 8 deletions osbs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,16 @@ def _create_build_config_and_build(self, build_json, namespace):
@osbsapi
def create_prod_build(self, git_uri, git_ref, git_branch, user, component, target,
architecture, yum_repourls=None, namespace=DEFAULT_NAMESPACE, **kwargs):
base_image = utils.get_base_image(git_uri, git_ref)
df_parser = utils.get_df_parser(git_uri, git_ref)
build_request = self.get_build_request(PROD_BUILD_TYPE)
build_request.set_params(
git_uri=git_uri,
git_ref=git_ref,
git_branch=git_branch,
user=user,
component=component,
base_image=base_image,
base_image=df_parser.baseimage,
name_label=df_parser.labels['Name'],
registry_uri=self.build_conf.get_registry_uri(),
openshift_uri=self.os_conf.get_openshift_base_uri(),
kojiroot=self.build_conf.get_kojiroot(),
Expand All @@ -203,15 +204,16 @@ def create_prod_build(self, git_uri, git_ref, git_branch, user, component, targe
def create_prod_with_secret_build(self, git_uri, git_ref, git_branch, user, component,
target, architecture, yum_repourls=None,
namespace=DEFAULT_NAMESPACE, **kwargs):
base_image = utils.get_base_image(git_uri, git_ref)
df_parser = utils.get_df_parser(git_uri, git_ref)
build_request = self.get_build_request(PROD_WITH_SECRET_BUILD_TYPE)
build_request.set_params(
git_uri=git_uri,
git_ref=git_ref,
git_branch=git_branch,
user=user,
component=component,
base_image=base_image,
base_image=df_parser.baseimage,
name_label=df_parser.labels['Name'],
registry_uri=self.build_conf.get_registry_uri(),
openshift_uri=self.os_conf.get_openshift_base_uri(),
kojiroot=self.build_conf.get_kojiroot(),
Expand Down Expand Up @@ -239,15 +241,16 @@ def create_prod_with_secret_build(self, git_uri, git_ref, git_branch, user, comp
def create_prod_without_koji_build(self, git_uri, git_ref, git_branch, user, component,
architecture, yum_repourls=None,
namespace=DEFAULT_NAMESPACE, **kwargs):
base_image = utils.get_base_image(git_uri, git_ref)
df_parser = utils.get_df_parser(git_uri, git_ref)
build_request = self.get_build_request(PROD_BUILD_TYPE)
build_request.set_params(
git_uri=git_uri,
git_ref=git_ref,
git_branch=git_branch,
user=user,
component=component,
base_image=base_image,
base_image=df_parser.baseimage,
name_label=df_parser.labels['Name'],
registry_uri=self.build_conf.get_registry_uri(),
openshift_uri=self.os_conf.get_openshift_base_uri(),
sources_command=self.build_conf.get_sources_command(),
Expand All @@ -266,15 +269,16 @@ def create_prod_without_koji_build(self, git_uri, git_ref, git_branch, user, com
@osbsapi
def create_simple_build(self, git_uri, git_ref, git_branch, user, component, yum_repourls=None,
namespace=DEFAULT_NAMESPACE, **kwargs):
base_image = utils.get_base_image(git_uri, git_ref)
df_parser = utils.get_df_parser(git_uri, git_ref)
build_request = self.get_build_request(SIMPLE_BUILD_TYPE)
build_request.set_params(
git_uri=git_uri,
git_ref=git_ref,
git_branch=git_branch,
user=user,
component=component,
base_image=base_image,
base_image=df_parser.baseimage,
name_label=df_parser.labels['Name'],
registry_uri=self.build_conf.get_registry_uri(),
openshift_uri=self.os_conf.get_openshift_base_uri(),
yum_repourls=yum_repourls,
Expand Down
13 changes: 12 additions & 1 deletion osbs/build/build_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def render(self):
self.template['spec']['output']['to']['name'] = tag_with_registry
if 'triggers' in self.template['spec']:
self.template['spec']['triggers']\
[0]['imageChange']['from']['name'] = self.spec.base_image.value
[0]['imageChange']['from']['name'] = self.spec.trigger_imagestream_name.value

if (self.spec.yum_repourls.value is not None and
self.dj.dock_json_has_plugin_conf('prebuild_plugins', "add_yum_repo_by_url")):
Expand All @@ -192,6 +192,17 @@ def render(self):
self.dj.dock_json_set_arg('postbuild_plugins', "store_metadata_in_osv3",
"use_auth", self.spec.use_auth.value)

if self.dj.dock_json_has_plugin_conf('postbuild_plugins', 'import_image'):
self.dj.dock_json_set_arg('postbuild_plugins', 'import_image', 'imagestream',
self.spec.imagestream_name.value)
self.dj.dock_json_set_arg('postbuild_plugins', 'import_image', 'docker_image_repo',
self.spec.imagestream_url.value)
self.dj.dock_json_set_arg('postbuild_plugins', 'import_image', 'url',
self.spec.openshift_uri.value)
if self.spec.use_auth.value is not None:
self.dj.dock_json_set_arg('postbuild_plugins', 'import_image', 'use_auth',
self.spec.use_auth.value)

def validate_input(self):
self.spec.validate()

Expand Down
16 changes: 11 additions & 5 deletions osbs/build/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import re
from osbs.constants import DEFAULT_GIT_REF
from osbs.exceptions import OsbsValidationException
from osbs.utils import get_imagestream_name_from_image


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -110,7 +111,9 @@ class CommonSpec(BuildTypeSpec):
git_branch = BuildParam('git_branch')
user = UserParam()
component = BuildParam('component')
base_image = BuildParam('base_image')
trigger_imagestream_name = BuildParam('trigger_imagestream_name')
imagestream_name = BuildParam('imagestream_name')
imagestream_url = BuildParam('imagestream_url')
registry_uri = BuildParam('registry_uri')
openshift_uri = BuildParam('openshift_uri')
name = BuildIDParam()
Expand All @@ -128,8 +131,8 @@ def __init__(self):
]

def set_params(self, git_uri=None, git_ref=None, git_branch=None, registry_uri=None, user=None,
component=None, base_image=None, openshift_uri=None, yum_repourls=None,
use_auth=None, **kwargs):
component=None, base_image=None, name_label=None, openshift_uri=None,
yum_repourls=None, use_auth=None, **kwargs):
self.git_uri.value = git_uri
self.git_ref.value = git_ref
self.git_branch.value = git_branch
Expand All @@ -144,8 +147,11 @@ def set_params(self, git_uri=None, git_ref=None, git_branch=None, registry_uri=N
raise OsbsValidationException("yum_repourls must be a list")
self.yum_repourls.value = yum_repourls or []
self.use_auth.value = use_auth
self.name.value = '%s-%s' % (self.component.value, self.git_branch.value)
self.base_image.value = os.path.join(registry_uri, base_image)
self.name.value = name_label.replace('/', '-')
self.trigger_imagestream_name.value = get_imagestream_name_from_image(base_image)
# imagestream and buildconfig have precisely the same name
self.imagestream_name.value = self.name.value
self.imagestream_url.value = os.path.join(self.registry_uri.value, name_label)


class ProdSpec(CommonSpec):
Expand Down
24 changes: 17 additions & 7 deletions osbs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,22 @@ def checkout_git_repo(uri, commit):
return tmpdir


def get_base_image_from_dir(repo_dir):
df_path = os.path.join(repo_dir, 'Dockerfile')
df = DockerfileParser(df_path)
return df.baseimage
def get_df_parser(git_uri, git_ref):
code_dir = checkout_git_repo(git_uri, git_ref)
return DockerfileParser(os.path.join(code_dir, 'Dockerfile'))


def get_base_image(git_uri, git_ref):
code_dir = checkout_git_repo(git_uri, git_ref)
return get_base_image_from_dir(code_dir)
def get_imagestream_name_from_image(image):
# this duplicates some logic with atomic_reactor.util.ImageName,
# but I don't think it's worth it to depend on AR just for this
ret = image
parts = image.split('/', 2)
if len(parts) == 2:
if '.' in parts[0] or ':' in parts[0]:
ret = parts[1]
elif len(parts) == 3:
ret = '%s/%s' % (parts[1], parts[2])
if ':' in ret:
ret = ret[:ret.index(':')]

return ret.replace('/', '-')
3 changes: 1 addition & 2 deletions tests/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from __future__ import absolute_import, unicode_literals

TEST_BUILD = "test-build-123"
TEST_BUILD_CONFIG = "test-build-config-123"
TEST_BUILD_CONFIG = "fedora23-something"
TEST_GIT_URI = "git://hostname/path"
TEST_GIT_REF = "master"
TEST_USER = "user"
Expand All @@ -18,4 +18,3 @@
TEST_BUILD_POD = "build-test-build-123"
TEST_LABEL = "test-label"
TEST_LABEL_VALUE = "sample-value"

9 changes: 4 additions & 5 deletions tests/fake_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from osbs.http import Response
from osbs.conf import Configuration
from osbs.api import OSBS
from tests.constants import TEST_BUILD, TEST_COMPONENT, TEST_GIT_REF
from tests.constants import TEST_BUILD, TEST_COMPONENT, TEST_GIT_REF, TEST_BUILD_CONFIG
from tempfile import NamedTemporaryFile

try:
Expand Down Expand Up @@ -98,20 +98,19 @@ def buildconfig_not_found(content):
"file": "created_build_config_test-build-config-123.json",
}
},
"/osapi/v1beta3/namespaces/default/buildconfigs/%s-%s/instantiate" % \
(TEST_COMPONENT, TEST_GIT_REF): {
"/osapi/v1beta3/namespaces/default/buildconfigs/%s/instantiate" % TEST_BUILD_CONFIG: {
"post": {
"file": "instantiated_test-build-config-123.json",
}
},
# use both version with ending slash and without it
"/osapi/v1beta3/namespaces/default/buildconfigs/%s-%s" % (TEST_COMPONENT, TEST_GIT_REF): {
"/osapi/v1beta3/namespaces/default/buildconfigs/%s" % TEST_BUILD_CONFIG: {
"get": {
"custom_callback": buildconfig_not_found,
"file": "not_found_build-config-component-master.json",
}
},
"/osapi/v1beta3/namespaces/default/buildconfigs/%s-%s/" % (TEST_COMPONENT, TEST_GIT_REF): {
"/osapi/v1beta3/namespaces/default/buildconfigs/%s/" % TEST_BUILD_CONFIG: {
"get": {
"custom_callback": buildconfig_not_found,
"file": "not_found_build-config-component-master.json",
Expand Down
17 changes: 16 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
from osbs.utils import deep_update
import pytest

from osbs.utils import deep_update, get_imagestream_name_from_image


def test_deep_update():
x = {'a': 'A', 'b': {'b1': 'B1', 'b2': 'B2'}}
y = {'b': {'b1': 'newB1', 'b3': 'B3'}, 'c': 'C'}
deep_update(x, y)
assert x == {'a': 'A', 'b': {'b1': 'newB1', 'b2': 'B2', 'b3': 'B3'}, 'c': 'C'}


@pytest.mark.parametrize(('img', 'expected'), [
('fedora23', 'fedora23'),
('fedora23:sometag', 'fedora23'),
('fedora23/python', 'fedora23-python'),
('fedora23/python:sometag', 'fedora23-python'),
('docker.io/fedora23', 'fedora23'),
('docker.io/fedora23/python', 'fedora23-python'),
('docker.io/fedora23/python:sometag', 'fedora23-python'),
])
def test_get_imagestream_name_from_image(img, expected):
assert get_imagestream_name_from_image(img) == expected
Loading

0 comments on commit 2711e20

Please sign in to comment.