Skip to content

Commit

Permalink
Move Kustomize patch tests to Go test suite
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hello@hidde.co>
hiddeco committed Apr 6, 2021
1 parent efecc8a commit 5a8d502
Showing 10 changed files with 378 additions and 316 deletions.
20 changes: 0 additions & 20 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -79,26 +79,6 @@ jobs:
kubectl -n impersonation wait kustomizations/podinfo --for=condition=ready --timeout=4m
kubectl -n impersonation delete kustomizations/podinfo
until kubectl -n impersonation get deploy/podinfo 2>&1 | grep NotFound ; do sleep 2; done
- name: Run images override tests
run: |
kubectl -n images-test apply -f ./config/testdata/overrides/images.yaml
kubectl -n images-test wait kustomizations/podinfo --for=condition=ready --timeout=1m
ACTUAL_TAG=$(kubectl -n images-test get deployments podinfo -o jsonpath='{.spec.template.spec.containers[0].image}' | cut -f2 -d ":")
if [[ $ACTUAL_TAG != "5.0.0" ]]; then echo "Image tag was not overwritten" && exit 1; fi
- name: Run patches override tests
run: |
kubectl -n patches-test apply -f ./config/testdata/overrides/patches.yaml
kubectl -n patches-test wait kustomizations/podinfo --for=condition=ready --timeout=1m
WANT="xxxx"
RESULT=$(kubectl -n patches-test get deployment podinfo -o jsonpath='{.metadata.labels.yyyy}')
if [ "$RESULT" != "$WANT" ]; then
echo -e "$RESULT\n\ndoes not equal\n\n$WANT" && exit 1
fi
WANT="yyyy"
RESULT=$(kubectl -n patches-test get deployment podinfo -o jsonpath='{.metadata.labels.xxxx}')
if [ "$RESULT" != "$WANT" ]; then
echo -e "$RESULT\n\ndoes not equal\n\$WANT" && exit 1
fi
- name: Logs
run: |
kubectl -n kustomize-system logs deploy/source-controller
34 changes: 0 additions & 34 deletions config/testdata/overrides/images.yaml

This file was deleted.

47 changes: 0 additions & 47 deletions config/testdata/overrides/patches.yaml

This file was deleted.

195 changes: 195 additions & 0 deletions controllers/kustomization_controller_patch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
"context"
"fmt"
"os"
"time"

sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/fluxcd/pkg/apis/kustomize"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/testserver"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1"
)

const timeout = 10 * time.Second

var _ = Describe("KustomizationReconciler", func() {
var (
artifactServer *testserver.ArtifactServer
)

BeforeEach(func() {
var err error
artifactServer, err = testserver.NewTempArtifactServer()
Expect(err).ToNot(HaveOccurred())
artifactServer.Start()
})

AfterEach(func() {
artifactServer.Stop()
os.RemoveAll(artifactServer.Root())
})

Context("Kustomize patches", func() {
var (
namespace *corev1.Namespace
kubeconfig *kustomizev1.KubeConfig
artifactFile string
artifactChecksum string
artifactURL string
kustomization *kustomizev1.Kustomization
)
BeforeEach(func() {
namespace = &corev1.Namespace{}
namespace.Name = "patch-" + randStringRunes(5)
Expect(k8sClient.Create(context.Background(), namespace)).To(Succeed())

kubecfgSecret, err := kubeConfigSecret()
Expect(err).ToNot(HaveOccurred())
kubecfgSecret.Namespace = namespace.Name
Expect(k8sClient.Create(context.Background(), kubecfgSecret)).To(Succeed())
kubeconfig = &kustomizev1.KubeConfig{
SecretRef: meta.LocalObjectReference{
Name: kubecfgSecret.Name,
},
}

artifactFile = "patch-" + randStringRunes(5)
artifactChecksum, err = initArtifact(artifactServer, "testdata/patch", artifactFile)
Expect(err).ToNot(HaveOccurred())
artifactURL, err = artifactServer.URLForFile(artifactFile)
Expect(err).ToNot(HaveOccurred())

gitRepoKey := client.ObjectKey{
Name: fmt.Sprintf("patch-%s", randStringRunes(5)),
Namespace: namespace.Name,
}
gitRepo := readyGitRepository(gitRepoKey, artifactURL, "main/"+artifactChecksum, artifactChecksum)
Expect(k8sClient.Create(context.Background(), gitRepo)).To(Succeed())
Expect(k8sClient.Status().Update(context.Background(), gitRepo)).To(Succeed())

kustomizationKey := types.NamespacedName{
Name: "patch-" + randStringRunes(5),
Namespace: namespace.Name,
}
kustomization = &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Spec: kustomizev1.KustomizationSpec{
Path: "./",
KubeConfig: kubeconfig,
SourceRef: kustomizev1.CrossNamespaceSourceReference{
Name: gitRepoKey.Name,
Namespace: gitRepoKey.Namespace,
Kind: sourcev1.GitRepositoryKind,
},
TargetNamespace: namespace.Name,
},
}
})

AfterEach(func() {
Expect(k8sClient.Delete(context.Background(), namespace)).To(Succeed())
})

It("patches images", func() {
kustomization.Spec.Images = []kustomize.Image{
{
Name: "podinfo",
NewName: "ghcr.io/stefanprodan/podinfo",
NewTag: "5.2.0",
},
}
Expect(k8sClient.Create(context.TODO(), kustomization)).To(Succeed())

Eventually(func() bool {
var obj kustomizev1.Kustomization
_ = k8sClient.Get(context.Background(), ObjectKey(kustomization), &obj)
return obj.Status.LastAppliedRevision == "main/"+artifactChecksum
}, timeout, time.Second).Should(BeTrue())

var deployment appsv1.Deployment
Expect(k8sClient.Get(context.TODO(), client.ObjectKey{Name: "podinfo", Namespace: namespace.Name}, &deployment)).To(Succeed())
Expect(deployment.Spec.Template.Spec.Containers[0].Image).To(Equal("ghcr.io/stefanprodan/podinfo:5.2.0"))
})

It("strategic merge patches", func() {
kustomization.Spec.PatchesStrategicMerge = []apiextensionsv1.JSON{
{
Raw: []byte(`{"kind":"Deployment","apiVersion":"apps/v1","metadata":{"name":"podinfo","labels":{"xxxx":"yyyy"}}}`),
},
}
Expect(k8sClient.Create(context.TODO(), kustomization)).To(Succeed())

Eventually(func() bool {
var obj kustomizev1.Kustomization
_ = k8sClient.Get(context.Background(), ObjectKey(kustomization), &obj)
return obj.Status.LastAppliedRevision == "main/"+artifactChecksum
}, timeout, time.Second).Should(BeTrue())

var deployment appsv1.Deployment
Expect(k8sClient.Get(context.TODO(), client.ObjectKey{Name: "podinfo", Namespace: namespace.Name}, &deployment)).To(Succeed())
Expect(deployment.ObjectMeta.Labels["xxxx"]).To(Equal("yyyy"))
})

It("JSON6902 patches", func() {
kustomization.Spec.PatchesJSON6902 = []kustomize.JSON6902Patch{
{

Patch: []kustomize.JSON6902{
{Op: "add", Path: "/metadata/labels/yyyy", Value: &apiextensionsv1.JSON{Raw: []byte(`"xxxx"`)}},
{Op: "replace", Path: "/spec/replicas", Value: &apiextensionsv1.JSON{Raw: []byte("2")}},
},
Target: kustomize.Selector{
Group: "apps",
Version: "v1",
Kind: "Deployment",
Name: "podinfo",
},
},
}
Expect(k8sClient.Create(context.TODO(), kustomization)).To(Succeed())

Eventually(func() bool {
var obj kustomizev1.Kustomization
_ = k8sClient.Get(context.Background(), ObjectKey(kustomization), &obj)
return obj.Status.LastAppliedRevision == "main/"+artifactChecksum
}, timeout, time.Second).Should(BeTrue())

var deployment appsv1.Deployment
Expect(k8sClient.Get(context.TODO(), client.ObjectKey{Name: "podinfo", Namespace: namespace.Name}, &deployment)).To(Succeed())
Expect(deployment.ObjectMeta.Labels["yyyy"]).To(Equal("xxxx"))
Expect(*deployment.Spec.Replicas).To(Equal(int32(2)))
})
})
})
Loading

0 comments on commit 5a8d502

Please sign in to comment.