-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcontroller_integration_test.go
170 lines (145 loc) · 5.51 KB
/
controller_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright 2018 The Kubernetes 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 controller_test
import (
"context"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
var _ = Describe("controller", func() {
var reconciled chan reconcile.Request
var stop chan struct{}
BeforeEach(func() {
stop = make(chan struct{})
reconciled = make(chan reconcile.Request)
Expect(cfg).NotTo(BeNil())
})
AfterEach(func() {
close(stop)
})
Describe("controller", func() {
// TODO(directxman12): write a whole suite of controller-client interaction tests
It("should reconcile", func(done Done) {
By("Creating the Manager")
cm, err := manager.New(cfg, manager.Options{})
Expect(err).NotTo(HaveOccurred())
By("Creating the Controller")
instance, err := controller.New("foo-controller", cm, controller.Options{
Reconciler: reconcile.Func(
func(request reconcile.Request) (reconcile.Result, error) {
reconciled <- request
return reconcile.Result{}, nil
}),
})
Expect(err).NotTo(HaveOccurred())
By("Watching Resources")
err = instance.Watch(&source.Kind{Type: &appsv1.ReplicaSet{}}, &handler.EnqueueRequestForOwner{
OwnerType: &appsv1.Deployment{},
})
Expect(err).NotTo(HaveOccurred())
err = instance.Watch(&source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForObject{})
Expect(err).NotTo(HaveOccurred())
err = cm.GetClient().Get(context.Background(), types.NamespacedName{Name: "foo"}, &corev1.Namespace{})
Expect(err).To(Equal(&cache.ErrCacheNotStarted{}))
err = cm.GetClient().List(context.Background(), &corev1.NamespaceList{})
Expect(err).To(Equal(&cache.ErrCacheNotStarted{}))
By("Starting the Manager")
go func() {
defer GinkgoRecover()
Expect(cm.Start(stop)).NotTo(HaveOccurred())
}()
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "deployment-name"},
Spec: appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"foo": "bar"}},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "nginx",
Image: "nginx",
},
},
},
},
},
}
expectedReconcileRequest := reconcile.Request{NamespacedName: types.NamespacedName{
Namespace: "default",
Name: "deployment-name",
}}
By("Invoking Reconciling for Create")
deployment, err = clientset.AppsV1().Deployments("default").Create(deployment)
Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))
By("Invoking Reconciling for Update")
newDeployment := deployment.DeepCopy()
newDeployment.Labels = map[string]string{"foo": "bar"}
newDeployment, err = clientset.AppsV1().Deployments("default").Update(newDeployment)
Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))
By("Invoking Reconciling for an OwnedObject when it is created")
replicaset := &appsv1.ReplicaSet{
ObjectMeta: metav1.ObjectMeta{
Name: "rs-name",
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(deployment, schema.GroupVersionKind{
Group: "apps",
Version: "v1",
Kind: "Deployment",
}),
},
},
Spec: appsv1.ReplicaSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"foo": "bar"},
},
Template: deployment.Spec.Template,
},
}
replicaset, err = clientset.AppsV1().ReplicaSets("default").Create(replicaset)
Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))
By("Invoking Reconciling for an OwnedObject when it is updated")
newReplicaset := replicaset.DeepCopy()
newReplicaset.Labels = map[string]string{"foo": "bar"}
newReplicaset, err = clientset.AppsV1().ReplicaSets("default").Update(newReplicaset)
Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))
By("Invoking Reconciling for an OwnedObject when it is deleted")
err = clientset.AppsV1().ReplicaSets("default").Delete(replicaset.Name, &metav1.DeleteOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))
By("Invoking Reconciling for Delete")
err = clientset.AppsV1().Deployments("default").
Delete("deployment-name", &metav1.DeleteOptions{})
Expect(err).NotTo(HaveOccurred())
Expect(<-reconciled).To(Equal(expectedReconcileRequest))
close(done)
}, 5)
})
})