Require version kustomize.config.k8s.io/v1alpha1 for Component

This commit is contained in:
Paul Martin
2020-05-14 21:01:02 +01:00
parent 1079d8604c
commit fdfb58cc3e
2 changed files with 83 additions and 11 deletions

View File

@@ -4,6 +4,7 @@
package krusty_test package krusty_test
import ( import (
"strings"
"testing" "testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
@@ -32,7 +33,7 @@ spec:
func writeComponentPatch(th kusttest_test.Harness) { func writeComponentPatch(th kusttest_test.Harness) {
th.WriteF("/app/patch/kustomization.yaml", ` th.WriteF("/app/patch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component kind: Component
namePrefix: patched- namePrefix: patched-
replicas: replicas:
@@ -117,13 +118,13 @@ spec:
`) `)
} }
func TestMultipleComponentes(t *testing.T) { func TestMultipleComponents(t *testing.T) {
th := kusttest_test.MakeHarness(t) th := kusttest_test.MakeHarness(t)
writeComponentBase(th) writeComponentBase(th)
writeComponentPatch(th) writeComponentPatch(th)
writeComponentProd(th) writeComponentProd(th)
th.WriteF("/app/additionalpatch/kustomization.yaml", ` th.WriteF("/app/additionalpatch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component kind: Component
configMapGenerator: configMapGenerator:
- name: my-configmap - name: my-configmap
@@ -180,7 +181,7 @@ func TestNestedComponents(t *testing.T) {
writeComponentPatch(th) writeComponentPatch(th)
writeComponentProd(th) writeComponentProd(th)
th.WriteF("/app/additionalpatch/kustomization.yaml", ` th.WriteF("/app/additionalpatch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component kind: Component
resources: resources:
- ../patch - ../patch
@@ -300,7 +301,7 @@ func TestApplyingComponentDirectlySameAsKustomization(t *testing.T) {
writeComponentBase(th) writeComponentBase(th)
writeComponentPatch(th) writeComponentPatch(th)
th.WriteF("/app/solopatch/kustomization.yaml", ` th.WriteF("/app/solopatch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component kind: Component
resources: resources:
- ../base - ../base
@@ -332,3 +333,65 @@ metadata:
name: my-configmap-t86ktk6tdk name: my-configmap-t86ktk6tdk
`) `)
} }
func TestMissingOptionalComponentApiVersion(t *testing.T) {
th := kusttest_test.MakeHarness(t)
writeComponentBase(th)
writeComponentProd(th)
th.WriteF("/app/patch/kustomization.yaml", `
kind: Component
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
- otherValue=9
`)
m := th.Run("/app/prod", th.MakeDefaultOptions())
th.AssertActualEqualsExpected(m, `
apiVersion: v1
kind: Deployment
metadata:
name: storefront
spec:
replicas: 1
---
apiVersion: v1
data:
otherValue: "9"
testValue: "1"
kind: ConfigMap
metadata:
annotations: {}
labels: {}
name: my-configmap-72cfg2mg5d
---
apiVersion: v1
kind: Deployment
metadata:
name: db
spec:
type: Logical
`)
}
func TestInvalidComponentApiVersion(t *testing.T) {
th := kusttest_test.MakeHarness(t)
writeComponentBase(th)
writeComponentProd(th)
th.WriteF("/app/patch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Component
configMapGenerator:
- name: my-configmap
behavior: merge
literals:
- otherValue=9
`)
err := th.RunWithErr("/app/prod", th.MakeDefaultOptions())
if !strings.Contains(
err.Error(),
"apiVersion should be kustomize.config.k8s.io/v1alpha1") {
t.Fatalf("unexpected error: %s", err)
}
}

View File

@@ -6,6 +6,7 @@ package types
const ( const (
KustomizationVersion = "kustomize.config.k8s.io/v1beta1" KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
KustomizationKind = "Kustomization" KustomizationKind = "Kustomization"
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
ComponentKind = "Component" ComponentKind = "Component"
MetadataNamespacePath = "metadata/namespace" MetadataNamespacePath = "metadata/namespace"
) )
@@ -129,23 +130,31 @@ type Kustomization struct {
// moving content of deprecated fields to newer // moving content of deprecated fields to newer
// fields. // fields.
func (k *Kustomization) FixKustomizationPostUnmarshalling() { func (k *Kustomization) FixKustomizationPostUnmarshalling() {
if k.APIVersion == "" {
k.APIVersion = KustomizationVersion
}
if k.Kind == "" { if k.Kind == "" {
k.Kind = KustomizationKind k.Kind = KustomizationKind
} }
if k.APIVersion == "" {
if k.Kind == ComponentKind {
k.APIVersion = ComponentVersion
} else {
k.APIVersion = KustomizationVersion
}
}
k.Resources = append(k.Resources, k.Bases...) k.Resources = append(k.Resources, k.Bases...)
k.Bases = nil k.Bases = nil
} }
func (k *Kustomization) EnforceFields() []string { func (k *Kustomization) EnforceFields() []string {
var errs []string var errs []string
if k.APIVersion != "" && k.APIVersion != KustomizationVersion {
errs = append(errs, "apiVersion should be "+KustomizationVersion)
}
if k.Kind != "" && k.Kind != KustomizationKind && k.Kind != ComponentKind { if k.Kind != "" && k.Kind != KustomizationKind && k.Kind != ComponentKind {
errs = append(errs, "kind should be "+KustomizationKind+" or "+ComponentKind) errs = append(errs, "kind should be "+KustomizationKind+" or "+ComponentKind)
} }
requiredVersion := KustomizationVersion
if k.Kind == ComponentKind {
requiredVersion = ComponentVersion
}
if k.APIVersion != "" && k.APIVersion != requiredVersion {
errs = append(errs, "apiVersion should be "+requiredVersion)
}
return errs return errs
} }