Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional image building #794

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions applications/nfsserver/deploy/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ harness:
auto: false
deployment:
auto: false
image: gcr.io/metacellllc/cloudharness/nfsserver:1.0


# nfs server pvc disk size (/exports)
Expand Down
1 change: 1 addition & 0 deletions applications/samples/deploy/values-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ harness:
- events
- common
- jupyterhub
- volumemanager
accounts:
roles:
- role1
Expand Down
79 changes: 4 additions & 75 deletions deployment/codefresh-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ steps:
working_directory: .
commands:
- bash cloud-harness/install.sh
- 'harness-deployment . -n test-${{NAMESPACE_BASENAME}} -d ${{DOMAIN}} -r ${{REGISTRY}}
-rs ${{REGISTRY_SECRET}} -e test --write-env -N '
- harness-deployment . -n test-${{NAMESPACE_BASENAME}} -d ${{DOMAIN}} -r ${{REGISTRY}}
-rs ${{REGISTRY_SECRET}} -e test --write-env -N -i samples
- cat deployment/.env >> ${{CF_VOLUME_PATH}}/env_vars_to_export
- cat ${{CF_VOLUME_PATH}}/env_vars_to_export
prepare_deployment_view:
Expand Down Expand Up @@ -124,49 +124,6 @@ steps:
type: parallel
stage: build
steps:
nfsserver:
type: build
stage: build
dockerfile: Dockerfile
registry: '${{CODEFRESH_REGISTRY}}'
buildkit: true
build_arguments:
- DOMAIN=${{DOMAIN}}
- NOCACHE=${{CF_BUILD_ID}}
- REGISTRY=${{REGISTRY}}/cloudharness/
image_name: cloudharness/nfsserver
title: Nfsserver
working_directory: ./applications/nfsserver
tag: '${{NFSSERVER_TAG}}'
when:
condition:
any:
buildDoesNotExist: includes('${{NFSSERVER_TAG_EXISTS}}', '{{NFSSERVER_TAG_EXISTS}}')
== true
forceNoCache: includes('${{NFSSERVER_TAG_FORCE_BUILD}}', '{{NFSSERVER_TAG_FORCE_BUILD}}')
== false
notifications:
type: build
stage: build
dockerfile: Dockerfile
registry: '${{CODEFRESH_REGISTRY}}'
buildkit: true
build_arguments:
- DOMAIN=${{DOMAIN}}
- NOCACHE=${{CF_BUILD_ID}}
- REGISTRY=${{REGISTRY}}/cloudharness/
- CLOUDHARNESS_BASE=${{REGISTRY}}/cloudharness/cloudharness-base:${{CLOUDHARNESS_BASE_TAG}}
image_name: cloudharness/notifications
title: Notifications
working_directory: ./applications/notifications/server
tag: '${{NOTIFICATIONS_TAG}}'
when:
condition:
any:
buildDoesNotExist: includes('${{NOTIFICATIONS_TAG_EXISTS}}', '{{NOTIFICATIONS_TAG_EXISTS}}')
== true
forceNoCache: includes('${{NOTIFICATIONS_TAG_FORCE_BUILD}}', '{{NOTIFICATIONS_TAG_FORCE_BUILD}}')
== false
accounts:
type: build
stage: build
Expand Down Expand Up @@ -210,27 +167,6 @@ steps:
== true
forceNoCache: includes('${{VOLUMEMANAGER_TAG_FORCE_BUILD}}', '{{VOLUMEMANAGER_TAG_FORCE_BUILD}}')
== false
sentry:
type: build
stage: build
dockerfile: Dockerfile
registry: '${{CODEFRESH_REGISTRY}}'
buildkit: true
build_arguments:
- DOMAIN=${{DOMAIN}}
- NOCACHE=${{CF_BUILD_ID}}
- REGISTRY=${{REGISTRY}}/cloudharness/
image_name: cloudharness/sentry
title: Sentry
working_directory: ./applications/sentry
tag: '${{SENTRY_TAG}}'
when:
condition:
any:
buildDoesNotExist: includes('${{SENTRY_TAG_EXISTS}}', '{{SENTRY_TAG_EXISTS}}')
== true
forceNoCache: includes('${{SENTRY_TAG_FORCE_BUILD}}', '{{SENTRY_TAG_FORCE_BUILD}}')
== false
jupyterhub:
type: build
stage: build
Expand Down Expand Up @@ -466,7 +402,7 @@ steps:
working_directory: ./${{CF_REPO_NAME}}
title: Installing chart
arguments:
helm_version: 3.11.0
helm_version: 3.6.2
chart_name: deployment/helm
release_name: test-${{NAMESPACE_BASENAME}}
kube_context: '${{CLUSTER_NAME}}'
Expand All @@ -476,11 +412,6 @@ steps:
custom_value_files:
- ./deployment/helm/values.yaml
custom_values:
- apps_notifications_harness_secrets_email-user=${{EMAIL-USER}}
- apps_notifications_harness_secrets_email-password=${{EMAIL-PASSWORD}}
- apps_sentry_harness_secrets_email-server=${{EMAIL-SERVER}}
- apps_sentry_harness_secrets_email-user=${{EMAIL-USER}}
- apps_sentry_harness_secrets_email-password=${{EMAIL-PASSWORD}}
- apps_samples_harness_secrets_asecret=${{ASECRET}}
build_test_images:
title: Build test images
Expand Down Expand Up @@ -537,10 +468,8 @@ steps:
commands:
- kubectl config use-context ${{CLUSTER_NAME}}
- kubectl config set-context --current --namespace=test-${{NAMESPACE_BASENAME}}
- kubectl rollout status deployment/notifications
- kubectl rollout status deployment/accounts
- kubectl rollout status deployment/volumemanager
- kubectl rollout status deployment/sentry
- kubectl rollout status deployment/argo-server-gk
- kubectl rollout status deployment/samples
- kubectl rollout status deployment/samples-gk
Expand Down Expand Up @@ -617,7 +546,7 @@ steps:
image: '${{REGISTRY}}/cloudharness/test-e2e:${{TEST_E2E_TAG}}'
fail_fast: false
commands:
- npx puppeteer browsers install chrome
- npx puppeteer browsers install chrome
- yarn test
scale:
jupyterhub_e2e_test:
Expand Down
9 changes: 7 additions & 2 deletions tools/deployment-cli-tools/ch_cli_tools/codefresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ def codefresh_steps_from_base_path(base_path, build_step, fixed_context=None, in
# Skip excluded apps
continue

if app_config and not helm_values.apps[app_key].get('build', True):
continue

if app_config and app_config.dependencies and app_config.dependencies.git:
for dep in app_config.dependencies.git:
step_name = f"clone_{basename(dep.url).replace('.', '_')}_{basename(dockerfile_relative_to_root).replace('.', '_')}"
Expand Down Expand Up @@ -319,11 +322,13 @@ def add_unit_test_step(app_config: ApplicationHarnessConfig):
steps = codefresh["steps"]
if CD_E2E_TEST_STEP in steps and not steps[CD_E2E_TEST_STEP]["scale"]:
del steps[CD_E2E_TEST_STEP]
del steps[CD_BUILD_STEP_TEST]["steps"]["test-e2e"]
if CD_BUILD_STEP_TEST in steps and 'test-e2e' in steps[CD_BUILD_STEP_TEST]["steps"]:
del steps[CD_BUILD_STEP_TEST]["steps"]["test-e2e"]

if CD_API_TEST_STEP in steps and not steps[CD_API_TEST_STEP]["scale"]:
del steps[CD_API_TEST_STEP]
del steps[CD_BUILD_STEP_TEST]["steps"]["test-api"]
if CD_BUILD_STEP_TEST in steps and 'test-api' in steps[CD_BUILD_STEP_TEST]["steps"]:
del steps[CD_BUILD_STEP_TEST]["steps"]["test-api"]

if CD_BUILD_STEP_TEST in steps and not steps[CD_BUILD_STEP_TEST]["steps"]:
del steps[CD_BUILD_STEP_TEST]
Expand Down
8 changes: 5 additions & 3 deletions tools/deployment-cli-tools/ch_cli_tools/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ def create_app_values_spec(self, app_name, app_path, base_image_name=None, helm_
else:
build_dependencies = []

if len(image_paths) > 0:
deployment_values = values.get(KEY_HARNESS, {}).get(KEY_DEPLOYMENT, {})
deployment_image = deployment_values.get('image', None) or values.get('image', None)
values['build'] = not bool(deployment_image) # Used by skaffold and ci/cd to determine if the image should be built
if len(image_paths) > 0 and not deployment_image:
image_name = image_name_from_dockerfile_path(os.path.relpath(
image_paths[0], os.path.dirname(app_path)), base_image_name)

values['image'] = self.image_tag(
image_name, build_context_path=app_path, dependencies=build_dependencies)
elif KEY_HARNESS in values and not values[KEY_HARNESS].get(KEY_DEPLOYMENT, {}).get('image', None) and values[
elif KEY_HARNESS in values and not deployment_image and values[
KEY_HARNESS].get(KEY_DEPLOYMENT, {}).get('auto', False):
raise Exception(f"At least one Dockerfile must be specified on application {app_name}. "
f"Specify harness.deployment.image value if you intend to use a prebuilt image.")
Expand Down
4 changes: 4 additions & 0 deletions tools/deployment-cli-tools/ch_cli_tools/skaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def process_build_dockerfile(
if app_name is None:
app_name = app_name_from_path(basename(dockerfile_path))
app_key = app_name.replace("-", "_")
if app_key in helm_values.apps and not helm_values.apps[app_key]['build']:
return
if app_name in helm_values[KEY_TASK_IMAGES] or app_key in helm_values.apps:
context_path = relpath_if(root_path, output_path) if global_context else relpath_if(dockerfile_path, output_path)

Expand Down Expand Up @@ -157,6 +159,8 @@ def process_build_dockerfile(
# app_image_tag, app_relative_to_skaffold, build_requirements)
process_build_dockerfile(dockerfile_path, root_path, requirements=build_requirements, app_name=app_name)
app = apps[app_key]
if not app['build']:
continue
if app[KEY_HARNESS][KEY_DEPLOYMENT]['image']:
release_config['artifactOverrides']['apps'][app_key] = \
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
harness:
dependencies:
build: []
deployment:
image: "custom-image"
36 changes: 36 additions & 0 deletions tools/deployment-cli-tools/tests/test_codefresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,39 @@ def test_create_codefresh_configuration_tests():

finally:
shutil.rmtree(BUILD_MERGE_DIR)


def test_create_codefresh_configuration_nobuild():
values = create_helm_chart(
[RESOURCES],
output_path=OUT,
include=['myapp'],
exclude=['events'],
domain="my.local",
namespace='test',
env=['dev', 'nobuild'],
local=False,
tag=1,
registry='reg'
)

root_paths = preprocess_build_overrides(
root_paths=[CLOUD_HARNESS_PATH, RESOURCES],
helm_values=values,
merge_build_path=BUILD_MERGE_DIR
)

build_included = [app['harness']['name']
for app in values['apps'].values() if 'harness' in app]

cf = create_codefresh_deployment_scripts(root_paths, include=build_included,
envs=['dev', 'nobuild'],
base_image_name=values['name'],
helm_values=values, save=False)
l1_steps = cf['steps']
steps = l1_steps["build_application_images"]["steps"]
assert len(steps) == 1
assert "myapp" not in steps
assert "myapp-mytask" in steps
assert "publish_myapp" not in l1_steps["publish"]["steps"]
assert "publish_myapp-mytask" in l1_steps["publish"]["steps"]
24 changes: 17 additions & 7 deletions tools/deployment-cli-tools/tests/test_helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ def test_collect_helm_values(tmp_path):
exclude=['events'], domain="my.local",
namespace='test', env='dev', local=False, tag=1, registry='reg')

# Auto values
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['deployment']['image'] == 'reg/cloudharness/myapp:1'
assert values.apps['myapp'].harness.deployment.image == 'reg/cloudharness/myapp:1'
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['name'] == 'myapp'
assert values[KEY_APPS]['legacy'][KEY_HARNESS]['name'] == 'legacy'
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['image'] == 'reg/cloudharness/accounts:1'

# First level include apps
assert 'samples' in values[KEY_APPS]
assert 'myapp' in values[KEY_APPS]
Expand All @@ -41,6 +34,14 @@ def test_collect_helm_values(tmp_path):
# Explicit exclude overrides include
assert 'events' not in values[KEY_APPS]

# Auto values
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['deployment']['image'] == 'reg/cloudharness/myapp:1'
assert values[KEY_APPS]['myapp']['build'] == True
assert values.apps['myapp'].harness.deployment.image == 'reg/cloudharness/myapp:1'
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['name'] == 'myapp'
assert values[KEY_APPS]['legacy'][KEY_HARNESS]['name'] == 'legacy'
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['deployment']['image'] == 'reg/cloudharness/accounts:1'

# Base values kept
assert values[KEY_APPS]['accounts'][KEY_HARNESS]['subdomain'] == 'accounts'

Expand Down Expand Up @@ -79,6 +80,15 @@ def test_collect_helm_values(tmp_path):
assert 'cloudharness-base-debian' not in values[KEY_TASK_IMAGES]


def test_collect_nobuild(tmp_path):
out_folder = tmp_path / 'test_collect_helm_values'
values = create_helm_chart([RESOURCES], output_path=out_folder, include=['myapp'],
exclude=['events'], domain="my.local",
namespace='test', env='nobuild', local=False, tag=1, registry='reg')
assert values[KEY_APPS]['myapp'][KEY_HARNESS]['deployment']['image'] == 'custom-image'
assert values[KEY_APPS]['myapp']['build'] == False


def test_collect_helm_values_noreg_noinclude(tmp_path):
out_path = tmp_path / 'test_collect_helm_values_noreg_noinclude'
values = create_helm_chart([CLOUDHARNESS_ROOT, RESOURCES], output_path=out_path, domain="my.local",
Expand Down
34 changes: 34 additions & 0 deletions tools/deployment-cli-tools/tests/test_skaffold.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,37 @@ def test_create_skaffold_configuration_with_conflicting_dependencies_requirement

myapp_config = release['overrides']['apps']['myapp2']
assert myapp_config['harness']['deployment']['args'][0] == '/usr/src/app/myapp_code/__main__.py'


def test_create_skaffold_configuration_nobuild():
values = create_helm_chart(
[RESOURCES],
output_path=OUT,
include=['myapp'],
domain="my.local",
namespace='test',
env='nobuild',
local=False,
tag=1,
registry='reg'
)

BUILD_DIR = "/tmp/build"
root_paths = preprocess_build_overrides(
root_paths=[CLOUDHARNESS_ROOT, RESOURCES],
helm_values=values,
merge_build_path=BUILD_DIR
)

sk = create_skaffold_configuration(
root_paths=root_paths,
helm_values=values,
output_path=OUT
)
releases = sk['deploy']['helm']['releases']

assert len(sk['build']['artifacts']) == 1
assert len(releases) == 1 # Ensure we only found 1 deployment (for myapp)

release = releases[0]
assert 'myapp' not in release['overrides']['apps']
Loading