Skip to content

Commit ba5a1a4

Browse files
authored
Merge pull request #193 from nan-yu/master
Fix the controller crashing issue
2 parents dacfd2a + 6bab51d commit ba5a1a4

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

controllers/application_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ func (r *ApplicationReconciler) getNewApplicationStatus(ctx context.Context, app
116116
func (r *ApplicationReconciler) fetchComponentListResources(ctx context.Context, groupKinds []metav1.GroupKind, selector *metav1.LabelSelector, namespace string, errs *[]error) []*unstructured.Unstructured {
117117
logger := getLoggerOrDie(ctx)
118118
var resources []*unstructured.Unstructured
119+
120+
if selector == nil {
121+
logger.Info("No selector is specified")
122+
return resources
123+
}
124+
119125
for _, gk := range groupKinds {
120126
mapping, err := r.Mapper.RESTMapping(schema.GroupKind{
121127
Group: appv1beta1.StripVersion(gk.Group),

controllers/application_controller_test.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ var _ = Describe("Application Reconciler", func() {
8080
objs = append(objs, service)
8181
objs = append(objs, statefulSet)
8282
objs = append(objs, createPod(labelSet2, namespace2))
83-
objs = append(objs, createDaemonSet(labelSet2, namespace2))
84-
objs = append(objs, createReplicaSet(labelSet2, namespace2))
83+
objs = append(objs, createDaemonSet(labelSet1, namespace2))
84+
objs = append(objs, createReplicaSet(labelSet1, namespace2))
8585
objs = append(objs, CreatePersistentVolumeClaim(labelSet2, namespace2))
8686
objs = append(objs, createPodDisruptionBudget(labelSet2, namespace2))
8787

@@ -135,10 +135,26 @@ var _ = Describe("Application Reconciler", func() {
135135
Expect(len(ns1List)).To(Equal(3))
136136
Expect(componentKinds(ns1List)).To(ConsistOf("StatefulSet", "Deployment", "Service"))
137137

138-
ns2List := applicationReconciler.fetchComponentListResources(ctx, groupKinds, metav1.SetAsLabelSelector(labelSet2), namespace2, &errs)
138+
ns2l1List := applicationReconciler.fetchComponentListResources(ctx, groupKinds, metav1.SetAsLabelSelector(labelSet1), namespace2, &errs)
139139
Expect(errs).To(BeNil())
140-
Expect(len(ns2List)).To(Equal(5))
141-
Expect(componentKinds(ns2List)).To(ConsistOf("ReplicaSet", "DaemonSet", "PersistentVolumeClaim", "Pod", "PodDisruptionBudget"))
140+
Expect(len(ns2l1List)).To(Equal(2))
141+
Expect(componentKinds(ns2l1List)).To(ConsistOf("ReplicaSet", "DaemonSet"))
142+
143+
ns2l2List := applicationReconciler.fetchComponentListResources(ctx, groupKinds, metav1.SetAsLabelSelector(labelSet2), namespace2, &errs)
144+
Expect(errs).To(BeNil())
145+
Expect(len(ns2l2List)).To(Equal(3))
146+
Expect(componentKinds(ns2l2List)).To(ConsistOf("PersistentVolumeClaim", "Pod", "PodDisruptionBudget"))
147+
148+
// Empty selector will select ALL resources in the namespace
149+
ns2AllList := applicationReconciler.fetchComponentListResources(ctx, groupKinds, metav1.SetAsLabelSelector(map[string]string{}), namespace2, &errs)
150+
Expect(errs).To(BeNil())
151+
Expect(len(ns2AllList)).To(Equal(5))
152+
Expect(componentKinds(ns2AllList)).To(ConsistOf("ReplicaSet", "DaemonSet", "PersistentVolumeClaim", "Pod", "PodDisruptionBudget"))
153+
154+
// No selector will select NO resources in the namespace
155+
ns2NoList := applicationReconciler.fetchComponentListResources(ctx, groupKinds, nil, namespace2, &errs)
156+
Expect(errs).To(BeNil())
157+
Expect(ns2NoList).To(BeNil())
142158

143159
})
144160

kalm-app-crd.yaml

Whitespace-only changes.

0 commit comments

Comments
 (0)