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
import (
"strings"
"testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
@@ -32,7 +33,7 @@ spec:
func writeComponentPatch(th kusttest_test.Harness) {
th.WriteF("/app/patch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
namePrefix: patched-
replicas:
@@ -117,13 +118,13 @@ spec:
`)
}
func TestMultipleComponentes(t *testing.T) {
func TestMultipleComponents(t *testing.T) {
th := kusttest_test.MakeHarness(t)
writeComponentBase(th)
writeComponentPatch(th)
writeComponentProd(th)
th.WriteF("/app/additionalpatch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
configMapGenerator:
- name: my-configmap
@@ -180,7 +181,7 @@ func TestNestedComponents(t *testing.T) {
writeComponentPatch(th)
writeComponentProd(th)
th.WriteF("/app/additionalpatch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- ../patch
@@ -300,7 +301,7 @@ func TestApplyingComponentDirectlySameAsKustomization(t *testing.T) {
writeComponentBase(th)
writeComponentPatch(th)
th.WriteF("/app/solopatch/kustomization.yaml", `
apiVersion: kustomize.config.k8s.io/v1beta1
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component
resources:
- ../base
@@ -332,3 +333,65 @@ metadata:
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 (
KustomizationVersion = "kustomize.config.k8s.io/v1beta1"
KustomizationKind = "Kustomization"
ComponentVersion = "kustomize.config.k8s.io/v1alpha1"
ComponentKind = "Component"
MetadataNamespacePath = "metadata/namespace"
)
@@ -129,23 +130,31 @@ type Kustomization struct {
// moving content of deprecated fields to newer
// fields.
func (k *Kustomization) FixKustomizationPostUnmarshalling() {
if k.APIVersion == "" {
k.APIVersion = KustomizationVersion
}
if k.Kind == "" {
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.Bases = nil
}
func (k *Kustomization) EnforceFields() []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 {
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
}