|
| 1 | +// Copyright 2020 The Kubernetes Authors. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package e2e |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "time" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + appsv1 "k8s.io/api/apps/v1" |
| 14 | + v1 "k8s.io/api/core/v1" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 16 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 17 | + "k8s.io/apimachinery/pkg/runtime" |
| 18 | + "k8s.io/apimachinery/pkg/types" |
| 19 | + "k8s.io/cli-runtime/pkg/genericclioptions" |
| 20 | + "k8s.io/kubectl/pkg/cmd/util" |
| 21 | + "k8s.io/kubectl/pkg/scheme" |
| 22 | + "sigs.k8s.io/cli-utils/pkg/apply" |
| 23 | + "sigs.k8s.io/cli-utils/pkg/apply/event" |
| 24 | + "sigs.k8s.io/cli-utils/pkg/common" |
| 25 | + "sigs.k8s.io/cli-utils/pkg/inventory" |
| 26 | + "sigs.k8s.io/cli-utils/pkg/provider" |
| 27 | + "sigs.k8s.io/cli-utils/pkg/util/factory" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/client/apiutil" |
| 31 | +) |
| 32 | + |
| 33 | +var _ = Describe("Applier", func() { |
| 34 | + |
| 35 | + var c client.Client |
| 36 | + |
| 37 | + BeforeSuite(func() { |
| 38 | + cfg, err := ctrl.GetConfig() |
| 39 | + Expect(err).NotTo(HaveOccurred()) |
| 40 | + |
| 41 | + mapper, err := apiutil.NewDynamicRESTMapper(cfg) |
| 42 | + Expect(err).NotTo(HaveOccurred()) |
| 43 | + |
| 44 | + c, err = client.New(cfg, client.Options{ |
| 45 | + Scheme: scheme.Scheme, |
| 46 | + Mapper: mapper, |
| 47 | + }) |
| 48 | + Expect(err).NotTo(HaveOccurred()) |
| 49 | + }) |
| 50 | + |
| 51 | + var namespaceName string |
| 52 | + var namespace *v1.Namespace |
| 53 | + var inventoryName string |
| 54 | + |
| 55 | + BeforeEach(func() { |
| 56 | + namespaceName = randomString("e2e-test-") |
| 57 | + namespace = &v1.Namespace{ |
| 58 | + TypeMeta: metav1.TypeMeta{ |
| 59 | + APIVersion: v1.SchemeGroupVersion.String(), |
| 60 | + Kind: "Namespace", |
| 61 | + }, |
| 62 | + ObjectMeta: metav1.ObjectMeta{ |
| 63 | + Name: namespaceName, |
| 64 | + }, |
| 65 | + } |
| 66 | + inventoryName = randomString("test-inv-") |
| 67 | + err := c.Create(context.TODO(), namespace) |
| 68 | + Expect(err).ToNot(HaveOccurred()) |
| 69 | + }) |
| 70 | + |
| 71 | + AfterEach(func() { |
| 72 | + err := c.Delete(context.TODO(), namespace) |
| 73 | + Expect(err).ToNot(HaveOccurred()) |
| 74 | + }) |
| 75 | + |
| 76 | + It("Apply and destroy", func() { |
| 77 | + By("Apply resources") |
| 78 | + applier := newApplier() |
| 79 | + err := applier.Initialize() |
| 80 | + Expect(err).NotTo(HaveOccurred()) |
| 81 | + |
| 82 | + inv := inventory.WrapInventoryInfoObj(inventoryManifest(inventoryName, namespaceName)) |
| 83 | + |
| 84 | + resources := []*unstructured.Unstructured{ |
| 85 | + deploymentManifest(namespaceName), |
| 86 | + } |
| 87 | + |
| 88 | + applyCh := applier.Run(context.TODO(), inv, resources, apply.Options{ |
| 89 | + ReconcileTimeout: 2 * time.Minute, |
| 90 | + EmitStatusEvents: true, |
| 91 | + }) |
| 92 | + |
| 93 | + for e := range applyCh { |
| 94 | + Expect(e.Type).NotTo(Equal(event.ErrorType)) |
| 95 | + } |
| 96 | + |
| 97 | + By("Verify inventory") |
| 98 | + var cm v1.ConfigMap |
| 99 | + err = c.Get(context.TODO(), types.NamespacedName{ |
| 100 | + Name: inventoryName, |
| 101 | + Namespace: namespaceName, |
| 102 | + }, &cm) |
| 103 | + Expect(err).ToNot(HaveOccurred()) |
| 104 | + |
| 105 | + data := cm.Data |
| 106 | + Expect(len(data)).To(Equal(1)) |
| 107 | + |
| 108 | + By("Destroy resources") |
| 109 | + destroyer := newDestroyer() |
| 110 | + err = destroyer.Initialize() |
| 111 | + Expect(err).NotTo(HaveOccurred()) |
| 112 | + |
| 113 | + destroyCh := destroyer.Run(inv) |
| 114 | + |
| 115 | + for e := range destroyCh { |
| 116 | + Expect(e.Type).NotTo(Equal(event.ErrorType)) |
| 117 | + } |
| 118 | + }) |
| 119 | +}) |
| 120 | + |
| 121 | +func inventoryManifest(name, namespace string) *unstructured.Unstructured { |
| 122 | + cm := &v1.ConfigMap{ |
| 123 | + TypeMeta: metav1.TypeMeta{ |
| 124 | + APIVersion: v1.SchemeGroupVersion.String(), |
| 125 | + Kind: "ConfigMap", |
| 126 | + }, |
| 127 | + ObjectMeta: metav1.ObjectMeta{ |
| 128 | + Name: name, |
| 129 | + Namespace: namespace, |
| 130 | + Labels: map[string]string{ |
| 131 | + common.InventoryLabel: "test", |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(cm) |
| 136 | + if err != nil { |
| 137 | + panic(err) |
| 138 | + } |
| 139 | + return &unstructured.Unstructured{ |
| 140 | + Object: u, |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func deploymentManifest(namespace string) *unstructured.Unstructured { |
| 145 | + dep := &appsv1.Deployment{ |
| 146 | + TypeMeta: metav1.TypeMeta{ |
| 147 | + APIVersion: appsv1.SchemeGroupVersion.String(), |
| 148 | + Kind: "Deployment", |
| 149 | + }, |
| 150 | + ObjectMeta: metav1.ObjectMeta{ |
| 151 | + Name: "nginx-deployment", |
| 152 | + Namespace: namespace, |
| 153 | + }, |
| 154 | + Spec: appsv1.DeploymentSpec{ |
| 155 | + Replicas: func() *int32 { r := int32(4); return &r }(), |
| 156 | + Selector: &metav1.LabelSelector{ |
| 157 | + MatchLabels: map[string]string{ |
| 158 | + "app": "nginx", |
| 159 | + }, |
| 160 | + }, |
| 161 | + Template: v1.PodTemplateSpec{ |
| 162 | + ObjectMeta: metav1.ObjectMeta{ |
| 163 | + Labels: map[string]string{ |
| 164 | + "app": "nginx", |
| 165 | + }, |
| 166 | + }, |
| 167 | + Spec: v1.PodSpec{ |
| 168 | + Containers: []v1.Container{ |
| 169 | + { |
| 170 | + Name: "nginx", |
| 171 | + Image: "nginx:1.19.6", |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + } |
| 178 | + u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(dep) |
| 179 | + if err != nil { |
| 180 | + panic(err) |
| 181 | + } |
| 182 | + return &unstructured.Unstructured{ |
| 183 | + Object: u, |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +func newApplier() *apply.Applier { |
| 188 | + return apply.NewApplier(newProvider()) |
| 189 | +} |
| 190 | + |
| 191 | +func newDestroyer() *apply.Destroyer { |
| 192 | + return apply.NewDestroyer(newProvider()) |
| 193 | +} |
| 194 | + |
| 195 | +func newProvider() provider.Provider { |
| 196 | + kubeConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag() |
| 197 | + matchVersionKubeConfigFlags := util.NewMatchVersionFlags(&factory.CachingRESTClientGetter{ |
| 198 | + Delegate: kubeConfigFlags, |
| 199 | + }) |
| 200 | + f := util.NewFactory(matchVersionKubeConfigFlags) |
| 201 | + return provider.NewProvider(f) |
| 202 | +} |
| 203 | + |
| 204 | +func randomString(prefix string) string { |
| 205 | + seed := time.Now().UTC().UnixNano() |
| 206 | + randomSuffix := common.RandomStr(seed) |
| 207 | + return fmt.Sprintf("%s%s", prefix, randomSuffix) |
| 208 | +} |
0 commit comments