|
1 | 1 | package controllerutil_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
| 5 | + |
4 | 6 | . "github.com/onsi/ginkgo"
|
5 | 7 | . "github.com/onsi/gomega"
|
6 | 8 | appsv1 "k8s.io/api/apps/v1"
|
| 9 | + corev1 "k8s.io/api/core/v1" |
7 | 10 | extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
8 | 11 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
9 | 12 | "k8s.io/apimachinery/pkg/runtime"
|
| 13 | + "k8s.io/apimachinery/pkg/types" |
10 | 14 | "k8s.io/client-go/kubernetes/scheme"
|
11 | 15 | "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
12 | 16 | )
|
@@ -92,10 +96,94 @@ var _ = Describe("Controllerutil", func() {
|
92 | 96 | }))
|
93 | 97 | })
|
94 | 98 | })
|
| 99 | + |
| 100 | + Describe("CreateOrUpdate", func() { |
| 101 | + |
| 102 | + It("creates a new object if one doesn't exists", func() { |
| 103 | + deplKey := types.NamespacedName{Name: "test-create", Namespace: "default"} |
| 104 | + depl := &appsv1.Deployment{} |
| 105 | + |
| 106 | + op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deplKey, depl, createDeployment) |
| 107 | + |
| 108 | + By("returning OperationCreated") |
| 109 | + Expect(op).Should(BeEquivalentTo(controllerutil.OperationCreated)) |
| 110 | + |
| 111 | + By("returning returning no error") |
| 112 | + Expect(err).ShouldNot(HaveOccurred()) |
| 113 | + |
| 114 | + By("actually having the deployment created") |
| 115 | + fetched := &appsv1.Deployment{} |
| 116 | + Expect(c.Get(context.TODO(), deplKey, fetched)).Should(Succeed()) |
| 117 | + }) |
| 118 | + |
| 119 | + It("update existing object", func() { |
| 120 | + deplKey := types.NamespacedName{Name: "test-update", Namespace: "default"} |
| 121 | + d, _ := createDeployment(&appsv1.Deployment{}) |
| 122 | + depl := d.(*appsv1.Deployment) |
| 123 | + depl.Name = "test-update" |
| 124 | + depl.Namespace = "default" |
| 125 | + |
| 126 | + var scale int32 = 2 |
| 127 | + |
| 128 | + Expect(c.Create(context.TODO(), depl)).Should(Succeed()) |
| 129 | + |
| 130 | + op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deplKey, &appsv1.Deployment{}, deploymentScaler(scale)) |
| 131 | + |
| 132 | + By("returning OperationUpdated") |
| 133 | + Expect(op).Should(BeEquivalentTo(controllerutil.OperationUpdated)) |
| 134 | + |
| 135 | + By("returning returning no error") |
| 136 | + Expect(err).ShouldNot(HaveOccurred()) |
| 137 | + |
| 138 | + By("actually having the deployment scaled") |
| 139 | + fetched := &appsv1.Deployment{} |
| 140 | + Expect(c.Get(context.TODO(), deplKey, fetched)).Should(Succeed()) |
| 141 | + Expect(*fetched.Spec.Replicas).To(Equal(scale)) |
| 142 | + }) |
| 143 | + |
| 144 | + It("updates only changed objects", func() { |
| 145 | + deplKey := types.NamespacedName{Name: "test-idempotency", Namespace: "default"} |
| 146 | + depl := &appsv1.Deployment{} |
| 147 | + |
| 148 | + op, err := controllerutil.CreateOrUpdate(context.TODO(), c, deplKey, depl, createDeployment) |
| 149 | + Expect(op).Should(BeEquivalentTo(controllerutil.OperationCreated)) |
| 150 | + Expect(err).ShouldNot(HaveOccurred()) |
| 151 | + |
| 152 | + op, err = controllerutil.CreateOrUpdate(context.TODO(), c, deplKey, depl, deploymentIdentity) |
| 153 | + |
| 154 | + By("returning OperationNoop") |
| 155 | + Expect(op).Should(BeEquivalentTo(controllerutil.OperationNoop)) |
| 156 | + |
| 157 | + By("returning returning no error") |
| 158 | + Expect(err).ShouldNot(HaveOccurred()) |
| 159 | + }) |
| 160 | + }) |
95 | 161 | })
|
96 | 162 |
|
97 | 163 | var _ metav1.Object = &errMetaObj{}
|
98 | 164 |
|
99 | 165 | type errMetaObj struct {
|
100 | 166 | metav1.ObjectMeta
|
101 | 167 | }
|
| 168 | + |
| 169 | +var createDeployment controllerutil.TransformFn = func(in runtime.Object) (runtime.Object, error) { |
| 170 | + out := in.(*appsv1.Deployment) |
| 171 | + out.Spec.Selector = &metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}} |
| 172 | + out.Spec.Template.ObjectMeta.Labels = map[string]string{"foo": "bar"} |
| 173 | + out.Spec.Template.Spec.Containers = []corev1.Container{corev1.Container{Name: "foo", Image: "busybox"}} |
| 174 | + return out, nil |
| 175 | +} |
| 176 | + |
| 177 | +var deploymentIdentity controllerutil.TransformFn = func(in runtime.Object) (runtime.Object, error) { |
| 178 | + return in, nil |
| 179 | +} |
| 180 | + |
| 181 | +func deploymentScaler(replicas int32) controllerutil.TransformFn { |
| 182 | + fn := func(in runtime.Object) (runtime.Object, error) { |
| 183 | + d, _ := createDeployment(in) |
| 184 | + out := d.(*appsv1.Deployment) |
| 185 | + out.Spec.Replicas = &replicas |
| 186 | + return out, nil |
| 187 | + } |
| 188 | + return fn |
| 189 | +} |
0 commit comments