From bf119bf5b7ab3b51cba5845832ae1cc9807d553e Mon Sep 17 00:00:00 2001 From: Jeffrey Regan Date: Mon, 18 Nov 2019 08:52:14 -0800 Subject: [PATCH] Add test elaborating on custom config. --- .../customconfigofbuiltinplugin_test.go | 119 +---------- .../target/customconfigreusable_test.go | 186 ++++++++++++++++++ 2 files changed, 188 insertions(+), 117 deletions(-) create mode 100644 api/internal/target/customconfigreusable_test.go diff --git a/api/internal/target/customconfigofbuiltinplugin_test.go b/api/internal/target/customconfigofbuiltinplugin_test.go index 01a365508..824ba7f7b 100644 --- a/api/internal/target/customconfigofbuiltinplugin_test.go +++ b/api/internal/target/customconfigofbuiltinplugin_test.go @@ -10,8 +10,8 @@ import ( ) // Demo custom configuration of a builtin transformation. -// This is a NamePrefixer that only touches Deployments -// and Services. +// This is a NamePrefixer that touches Deployments +// and Services exclusively. func TestCustomNamePrefixer(t *testing.T) { tc := kusttest_test.NewPluginTestEnv(t).Set() defer tc.Reset() @@ -99,118 +99,3 @@ metadata: name: zzz-myService `) } - -// Demo custom configuration as a base. -func TestReusableCustomNamePrefixer(t *testing.T) { - tc := kusttest_test.NewPluginTestEnv(t).Set() - defer tc.Reset() - - tc.BuildGoPlugin( - "builtin", "", "PrefixSuffixTransformer") - tc.BuildGoPlugin( - "builtin", "", "LabelTransformer") - - th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app/foo") - - // This kustomization file contains resources that - // all happen to be plugin configurations. This makes - // these plugins all available as part of a base, - // re-usable in any number of other kustomizations. - // Just specify the path (or URL) to this base in the - // "transformers:" field (not the "resources" field). - th.WriteK("/app/mytransformers", ` -resources: -- prefixer.yaml -- labeller.yaml -`) - th.WriteF("/app/mytransformers/prefixer.yaml", ` -apiVersion: builtin -kind: PrefixSuffixTransformer -metadata: - name: myPrefixer -prefix: zzz- -fieldSpecs: -- kind: Deployment - path: metadata/name -- kind: Service - path: metadata/name -`) - th.WriteF("/app/mytransformers/labeller.yaml", ` -apiVersion: builtin -kind: LabelTransformer -metadata: - name: myLabeller -labels: - company: acmeCorp -fieldSpecs: -- path: spec/template/metadata/labels - kind: Deployment -`) - - th.WriteK("/app/foo", ` -resources: -- deployment.yaml -- role.yaml -- service.yaml -transformers: -- ../mytransformers -`) - th.WriteF("/app/foo/deployment.yaml", ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: myDeployment -spec: - template: - metadata: - labels: - backend: awesome - spec: - containers: - - name: whatever - image: whatever -`) - th.WriteF("/app/foo/role.yaml", ` -apiVersion: v1 -kind: Role -metadata: - name: myRole -`) - th.WriteF("/app/foo/service.yaml", ` -apiVersion: v1 -kind: Service -metadata: - name: myService -`) - - m, err := th.MakeKustTarget().MakeCustomizedResMap() - if err != nil { - t.Fatalf("Err: %v", err) - } - th.AssertActualEqualsExpected(m, ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: zzz-myDeployment -spec: - template: - metadata: - labels: - backend: awesome - company: acmeCorp - spec: - containers: - - image: whatever - name: whatever ---- -apiVersion: v1 -kind: Role -metadata: - name: myRole ---- -apiVersion: v1 -kind: Service -metadata: - name: zzz-myService -`) -} diff --git a/api/internal/target/customconfigreusable_test.go b/api/internal/target/customconfigreusable_test.go new file mode 100644 index 000000000..e9c6988c0 --- /dev/null +++ b/api/internal/target/customconfigreusable_test.go @@ -0,0 +1,186 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package target_test + +import ( + "testing" + + kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" +) + +// Demo custom configuration as a base. +// This test shows usage of three custom configurations sitting +// in a base, allowing them to be reused in any number of +// kustomizations. +func TestReusableCustomTransformers(t *testing.T) { + tc := kusttest_test.NewPluginTestEnv(t).Set() + defer tc.Reset() + + tc.BuildGoPlugin( + "builtin", "", "PrefixSuffixTransformer") + tc.BuildGoPlugin( + "builtin", "", "AnnotationsTransformer") + tc.BuildGoPlugin( + "builtin", "", "LabelTransformer") + + th := kusttest_test.NewKustTestHarnessAllowPlugins(t, "/app/staging") + + // First write three custom configurations for builtin plugins. + + // A custom name prefixer that only touches Deployments and Services. + th.WriteF("/app/mytransformers/deploymentServicePrefixer.yaml", ` +apiVersion: builtin +kind: PrefixSuffixTransformer +metadata: + name: myPrefixer +prefix: bob- +fieldSpecs: +- kind: Deployment + path: metadata/name +- kind: Service + path: metadata/name +`) + + // A custom annotator exclusively annotating Roles. + th.WriteF("/app/mytransformers/roleAnnotator.yaml", ` +apiVersion: builtin +kind: AnnotationsTransformer +metadata: + name: myAnnotator +annotations: + # Imagine that SRE has not approved this role yet. + status: probationary +fieldSpecs: +- path: metadata/annotations + create: true + kind: Role +`) + + // A custom labeller that only labels Deployments, + // and only labels them at their top metadata level + // exclusively. It does not modify selectors or + // add labels to pods in the template. + th.WriteF("/app/mytransformers/deploymentLabeller.yaml", ` +apiVersion: builtin +kind: LabelTransformer +metadata: + name: myLabeller +labels: + pager: 867-5301 +fieldSpecs: +- path: metadata/labels + create: true + kind: Deployment +`) + + // Combine these custom transformers in one kustomization file. + // This kustomization file contains only resources that + // all happen to be plugin configurations. This makes + // these plugins re-usable as a group in any number of other + // kustomizations. + th.WriteK("/app/mytransformers", ` +resources: +- deploymentServicePrefixer.yaml +- roleAnnotator.yaml +- deploymentLabeller.yaml +`) + + // Finally, define the kustomization for the (arbitrarily named) + // staging environment. + th.WriteK("/app/staging", ` + +# Bring in the custom transformers. +transformers: +- ../mytransformers + +# Also use the "classic" labeller, which behind the scenes uses +# the LabelTransformer, but with a broad configuration targeting +# many resources and fields (including selector fields). +# It's a big hammer - probably too big; the output shows all the +# places 'acmeCorp' now appears as a result. To avoid this, +# define your own config for your own LabelTransformer instance +# as shown above. +commonLabels: + company: acmeCorp + +# Specify the resources to modify. +resources: +- deployment.yaml +- role.yaml +- service.yaml +`) + th.WriteF("/app/staging/deployment.yaml", ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: myDeployment +spec: + template: + metadata: + labels: + backend: flawless + spec: + containers: + - name: whatever + image: whatever +`) + th.WriteF("/app/staging/role.yaml", ` +apiVersion: v1 +kind: Role +metadata: + name: myRole +`) + th.WriteF("/app/staging/service.yaml", ` +apiVersion: v1 +kind: Service +metadata: + name: myService +`) + + m, err := th.MakeKustTarget().MakeCustomizedResMap() + if err != nil { + t.Fatalf("Err: %v", err) + } + th.AssertActualEqualsExpected(m, ` +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + company: acmeCorp + pager: 867-5301 + name: bob-myDeployment +spec: + selector: + matchLabels: + company: acmeCorp + template: + metadata: + labels: + backend: flawless + company: acmeCorp + spec: + containers: + - image: whatever + name: whatever +--- +apiVersion: v1 +kind: Role +metadata: + annotations: + status: probationary + labels: + company: acmeCorp + name: myRole +--- +apiVersion: v1 +kind: Service +metadata: + labels: + company: acmeCorp + name: bob-myService +spec: + selector: + company: acmeCorp +`) +}