Skip to content

Commit

Permalink
correct handling of deps runtimes (#109)
Browse files Browse the repository at this point in the history
* +use images or image pushes in k8s_deploy images parameter
+use dict {"name": ":image" } or [ ":image" ] for images parameter
+more tests

* correct handling of dependency runtimes

* test fixes

* use template to generate expected file

* cleanup

* buildifier

* buildifier nit

* explain execution_requirements rationale

* buildifier

Co-authored-by: Konstantin Zadorozhny <[email protected]>
  • Loading branch information
apesternikov and kzadorozhny authored Nov 28, 2022
1 parent 5659063 commit 041ba72
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 29 deletions.
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,11 @@ http_archive(
load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_dependencies")

buildifier_dependencies()

load("//skylib:k8s.bzl", "kubeconfig")

kubeconfig(
name = "k8s_test",
cluster = "mycluster",
use_host_config = True,
)
2 changes: 2 additions & 0 deletions gitops/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ GitOps rules public interface
"""

load("@com_adobe_rules_gitops//skylib:k8s.bzl", _k8s_deploy = "k8s_deploy", _k8s_test_setup = "k8s_test_setup")
load("@com_adobe_rules_gitops//skylib:external_image.bzl", _external_iamge = "external_image")

k8s_deploy = _k8s_deploy
k8s_test_setup = _k8s_test_setup
external_image = _external_iamge
176 changes: 174 additions & 2 deletions gitops/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,191 @@
# OF ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.

load("//skylib:k8s.bzl", "k8s_deploy")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("//gitops:defs.bzl", "external_image", "k8s_deploy", "k8s_test_setup")
load("//skylib:push.bzl", "k8s_container_push")
load("//skylib:templates.bzl", "expand_template")

licenses(["notice"]) # Apache 2.0

k8s_deploy(
name = "testdeployment",
name = "legacy_alias",
cluster = "testcluster",
deployment_branch = "test1",
gitops = 1,
images = {
"img": "//skylib/kustomize/tests:image",
},
manifests = [
":deployment_legacy.yaml",
],
namespace = "ci",
release_branch_prefix = "gitops_test_release_branch",
visibility = ["//visibility:public"],
)

expand_template(
name = "generate_deployment_expected",
out = "deployment_expected.txt",
deps_aliases = {
"digest": "//skylib/kustomize/tests:image.digest",
},
substitutions = {},
template = ":deployment_expected_template.txt",
deps = ["//skylib/kustomize/tests:image.digest"],
)

expand_template(
name = "generate_deployment2_expected",
out = "deployment2_expected.txt",
deps_aliases = {
"digest": "//skylib/kustomize/tests:image.digest",
},
substitutions = {},
template = ":deployment2_expected_template.txt",
deps = ["//skylib/kustomize/tests:image.digest"],
)

diff_test(
name = "legacy_alias_test",
file1 = ":legacy_alias",
file2 = "deployment_expected.txt",
)

k8s_deploy(
name = "legacy_label",
cluster = "testcluster",
deployment_branch = "test1",
gitops = 1,
images = {
"img": "//skylib/kustomize/tests:image",
},
manifests = [
":deployment.yaml",
],
namespace = "ci",
release_branch_prefix = "gitops_test_release_branch",
visibility = ["//visibility:public"],
)

diff_test(
name = "legacy_label_test",
file1 = ":legacy_label",
file2 = "deployment_expected.txt",
)

k8s_deploy(
name = "label",
cluster = "testcluster",
deployment_branch = "test1",
gitops = 1,
images = [
"//skylib/kustomize/tests:image",
],
manifests = [
":deployment.yaml",
],
namespace = "ci",
release_branch_prefix = "gitops_test_release_branch",
visibility = ["//visibility:public"],
)

diff_test(
name = "label_test",
file1 = ":label",
file2 = "deployment_expected.txt",
)

# using image from external repo
external_image(
name = "external_image",
digest = "sha:1234567890",
image = "gcr.io/repo/someimage:thetag",
)

k8s_deploy(
name = "external_image_label",
cluster = "testcluster",
deployment_branch = "test1",
gitops = 1,
images = [
":external_image",
],
manifests = [
":deployment1.yaml",
],
namespace = "ci",
release_branch_prefix = "gitops_test_release_branch",
visibility = ["//visibility:public"],
)

diff_test(
name = "external_image_label_test",
file1 = ":external_image_label",
file2 = "deployment1_expected.txt",
)

k8s_deploy(
name = "external_image_legacy",
cluster = "testcluster",
deployment_branch = "test1",
gitops = 1,
images = {
"img": ":external_image",
},
manifests = [
":deployment_legacy.yaml",
],
namespace = "ci",
release_branch_prefix = "gitops_test_release_branch",
visibility = ["//visibility:public"],
)

diff_test(
name = "external_image_legacy_test",
file1 = ":external_image_legacy",
file2 = "deployment1_expected.txt",
)

k8s_container_push(
name = "pushed_image",
image = "//skylib/kustomize/tests:image",
registry = "gcr.io",
repository = "repo/imagethere",
tag = "thetag",
)

# rename(
# name = "//skylib/kustomize/tests:image",

# )

k8s_deploy(
name = "legacy_renamed_alias",
cluster = "testcluster",
deployment_branch = "test1",
gitops = 1,
images = {
"img": ":pushed_image",
},
manifests = [
":deployment_legacy.yaml",
],
namespace = "ci",
release_branch_prefix = "gitops_test_release_branch",
visibility = ["//visibility:public"],
)

diff_test(
name = "legacy_renamed_alias_test",
file1 = ":legacy_renamed_alias",
file2 = "deployment2_expected.txt",
)

k8s_test_setup(
name = "legacy_renamed_alias_it.setup",
# kubeconfig = "@k8s_test//:kubeconfig",
objects = [
":legacy_renamed_alias",
],
)
16 changes: 16 additions & 0 deletions gitops/testing/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: //skylib/kustomize/tests:image
16 changes: 16 additions & 0 deletions gitops/testing/deployment1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: //gitops/testing:external_image
17 changes: 17 additions & 0 deletions gitops/testing/deployment1_expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: ci
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: gcr.io/repo/someimage@sha:1234567890
name: myapp
17 changes: 17 additions & 0 deletions gitops/testing/deployment2_expected_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: ci
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: gcr.io/repo/imagethere@{{imports.digest}}
name: myapp
17 changes: 17 additions & 0 deletions gitops/testing/deployment_expected_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: ci
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: docker.io/skylib/kustomize/tests/image@{{imports.digest}}
name: myapp
17 changes: 17 additions & 0 deletions gitops/testing/deployment_legacy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: img

40 changes: 40 additions & 0 deletions skylib/external_image.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Implementation of external image information provider suitable for injection into manifests
"""

load(":push.bzl", "K8sPushInfo")

def _external_image_impl(ctx):
sv = ctx.attr.image.split("@", 1)
if (len(sv) == 1) and (not ctx.attr.digest):
fail("digest must be specified either in image or as a separate attribute")
s = sv[0].split(":", 1)[0] #drop tag
registry, repository = s.split("/", 1)

#write digest to a file
digest_file = ctx.actions.declare_file(ctx.label.name + ".digest")
ctx.actions.write(
output = digest_file,
content = ctx.attr.digest,
)
return [
DefaultInfo(
files = depset([digest_file]),
),
K8sPushInfo(
image_label = ctx.label,
legacy_image_name = ctx.attr.image_name,
registry = registry,
repository = repository,
digestfile = digest_file,
),
]

external_image = rule(
implementation = _external_image_impl,
attrs = {
"image": attr.string(mandatory = True, doc = "The image location, e.g. gcr.io/foo/bar:baz"),
"image_name": attr.string(doc = "Image name, e.g. exernalserver. DEPRECATED: Use full target label instead, e.g. //images:externalserver"),
"digest": attr.string(mandatory = True, doc = "The image digest, e.g. sha256:deadbeef"),
},
)
Loading

0 comments on commit 041ba72

Please sign in to comment.