Skip to content

Commit

Permalink
Merge pull request #152 from jabdoa2/handle_projections
Browse files Browse the repository at this point in the history
Handle projections
  • Loading branch information
toelke authored Apr 29, 2024
2 parents de65b16 + 6e1a1af commit 9f93638
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 31 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/daemonset/daemonset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ var _ = Describe("DaemonSet controller Suite", func() {
eventMessage := func(event *corev1.Event) string {
return event.Message
}
hashMessage := "Configuration hash updated to ebabf80ef45218b27078a41ca16b35a4f91cb5672f389e520ae9da6ee3df3b1c"
hashMessage := "Configuration hash updated to bd786f47ef9b79841ddba1059752f95c4fe21906df5e2964786b4658e02758d5"
Eventually(func() *corev1.EventList {
events := &corev1.EventList{}
m.Client.List(context.TODO(), events)
Expand Down
26 changes: 25 additions & 1 deletion pkg/controller/deployment/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ var _ = Describe("Deployment controller Suite", func() {
var cm1 *corev1.ConfigMap
var cm2 *corev1.ConfigMap
var cm3 *corev1.ConfigMap
var cm4 *corev1.ConfigMap
var cm5 *corev1.ConfigMap
var cm6 *corev1.ConfigMap
var s1 *corev1.Secret
var s2 *corev1.Secret
var s3 *corev1.Secret
var s4 *corev1.Secret
var s5 *corev1.Secret
var s6 *corev1.Secret

const modified = "modified"

Expand Down Expand Up @@ -97,22 +103,40 @@ var _ = Describe("Deployment controller Suite", func() {
cm1 = utils.ExampleConfigMap1.DeepCopy()
cm2 = utils.ExampleConfigMap2.DeepCopy()
cm3 = utils.ExampleConfigMap3.DeepCopy()
cm4 = utils.ExampleConfigMap4.DeepCopy()
cm5 = utils.ExampleConfigMap5.DeepCopy()
cm6 = utils.ExampleConfigMap6.DeepCopy()
s1 = utils.ExampleSecret1.DeepCopy()
s2 = utils.ExampleSecret2.DeepCopy()
s3 = utils.ExampleSecret3.DeepCopy()
s4 = utils.ExampleSecret4.DeepCopy()
s5 = utils.ExampleSecret5.DeepCopy()
s6 = utils.ExampleSecret6.DeepCopy()

m.Create(cm1).Should(Succeed())
m.Create(cm2).Should(Succeed())
m.Create(cm3).Should(Succeed())
m.Create(cm4).Should(Succeed())
m.Create(cm5).Should(Succeed())
m.Create(cm6).Should(Succeed())
m.Create(s1).Should(Succeed())
m.Create(s2).Should(Succeed())
m.Create(s3).Should(Succeed())
m.Create(s4).Should(Succeed())
m.Create(s5).Should(Succeed())
m.Create(s6).Should(Succeed())
m.Get(cm1, timeout).Should(Succeed())
m.Get(cm2, timeout).Should(Succeed())
m.Get(cm3, timeout).Should(Succeed())
m.Get(cm4, timeout).Should(Succeed())
m.Get(cm5, timeout).Should(Succeed())
m.Get(cm6, timeout).Should(Succeed())
m.Get(s1, timeout).Should(Succeed())
m.Get(s2, timeout).Should(Succeed())
m.Get(s3, timeout).Should(Succeed())
m.Get(s4, timeout).Should(Succeed())
m.Get(s5, timeout).Should(Succeed())
m.Get(s6, timeout).Should(Succeed())

deployment = utils.ExampleDeployment.DeepCopy()

Expand Down Expand Up @@ -204,7 +228,7 @@ var _ = Describe("Deployment controller Suite", func() {
eventMessage := func(event *corev1.Event) string {
return event.Message
}
hashMessage := "Configuration hash updated to ebabf80ef45218b27078a41ca16b35a4f91cb5672f389e520ae9da6ee3df3b1c"
hashMessage := "Configuration hash updated to 421778c325761f51dbf7a23a20eb9c1bc516ffd4aa7362ebec03175d427d7557"
Eventually(func() *corev1.EventList {
events := &corev1.EventList{}
m.Client.List(context.TODO(), events)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/statefulset/statefulset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ var _ = Describe("StatefulSet controller Suite", func() {
eventMessage := func(event *corev1.Event) string {
return event.Message
}
hashMessage := "Configuration hash updated to ebabf80ef45218b27078a41ca16b35a4f91cb5672f389e520ae9da6ee3df3b1c"
hashMessage := "Configuration hash updated to bd786f47ef9b79841ddba1059752f95c4fe21906df5e2964786b4658e02758d5"
Eventually(func() *corev1.EventList {
events := &corev1.EventList{}
m.Client.List(context.TODO(), events)
Expand Down
27 changes: 27 additions & 0 deletions pkg/core/children.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ func getChildNamesByType(obj podController) (map[string]configMetadata, map[stri
if s := vol.VolumeSource.Secret; s != nil {
secrets[s.SecretName] = configMetadata{required: isRequired(s.Optional), allKeys: true}
}

if projection := vol.VolumeSource.Projected; projection != nil {
for _, source := range projection.Sources {
if cm := source.ConfigMap; cm != nil {
if cm.Items == nil {
configMaps[cm.Name] = configMetadata{required: isRequired(cm.Optional), allKeys: true}
} else {
keys := make(map[string]struct{})
for _, item := range cm.Items {
keys[item.Key] = struct{}{}
}
configMaps[cm.Name] = configMetadata{required: isRequired(cm.Optional), allKeys: false, keys: keys}
}
}
if s := source.Secret; s != nil {
if s.Items == nil {
secrets[s.Name] = configMetadata{required: isRequired(s.Optional), allKeys: true}
} else {
keys := make(map[string]struct{})
for _, item := range s.Items {
keys[item.Key] = struct{}{}
}
secrets[s.Name] = configMetadata{required: isRequired(s.Optional), allKeys: false, keys: keys}
}
}
}
}
}

// Range through all Containers and their respective EnvFrom,
Expand Down
84 changes: 69 additions & 15 deletions pkg/core/children_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ limitations under the License.
package core

import (
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sync"
"time"

metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/wave-k8s/wave/test/utils"
Expand All @@ -38,7 +39,6 @@ var _ = Describe("Wave children Suite", func() {
var m utils.Matcher
var deploymentObject *appsv1.Deployment
var podControllerDeployment podController
var existingChildren []Object
var currentChildren []configObject
var mgrStopped *sync.WaitGroup
var stopMgr chan struct{}
Expand All @@ -49,10 +49,14 @@ var _ = Describe("Wave children Suite", func() {
var cm2 *corev1.ConfigMap
var cm3 *corev1.ConfigMap
var cm4 *corev1.ConfigMap
var cm5 *corev1.ConfigMap
var cm6 *corev1.ConfigMap
var s1 *corev1.Secret
var s2 *corev1.Secret
var s3 *corev1.Secret
var s4 *corev1.Secret
var s5 *corev1.Secret
var s6 *corev1.Secret

BeforeEach(func() {
mgr, err := manager.New(cfg, manager.Options{
Expand All @@ -75,19 +79,27 @@ var _ = Describe("Wave children Suite", func() {
cm2 = utils.ExampleConfigMap2.DeepCopy()
cm3 = utils.ExampleConfigMap3.DeepCopy()
cm4 = utils.ExampleConfigMap4.DeepCopy()
cm5 = utils.ExampleConfigMap5.DeepCopy()
cm6 = utils.ExampleConfigMap6.DeepCopy()
s1 = utils.ExampleSecret1.DeepCopy()
s2 = utils.ExampleSecret2.DeepCopy()
s3 = utils.ExampleSecret3.DeepCopy()
s4 = utils.ExampleSecret4.DeepCopy()
s5 = utils.ExampleSecret5.DeepCopy()
s6 = utils.ExampleSecret6.DeepCopy()

m.Create(cm1).Should(Succeed())
m.Create(cm2).Should(Succeed())
m.Create(cm3).Should(Succeed())
m.Create(cm4).Should(Succeed())
m.Create(cm5).Should(Succeed())
m.Create(cm6).Should(Succeed())
m.Create(s1).Should(Succeed())
m.Create(s2).Should(Succeed())
m.Create(s3).Should(Succeed())
m.Create(s4).Should(Succeed())
m.Create(s5).Should(Succeed())
m.Create(s6).Should(Succeed())

deploymentObject = utils.ExampleDeployment.DeepCopy()
podControllerDeployment = &deployment{deploymentObject}
Expand All @@ -101,10 +113,14 @@ var _ = Describe("Wave children Suite", func() {
m.Get(cm2, timeout).Should(Succeed())
m.Get(cm3, timeout).Should(Succeed())
m.Get(cm4, timeout).Should(Succeed())
m.Get(cm5, timeout).Should(Succeed())
m.Get(cm6, timeout).Should(Succeed())
m.Get(s1, timeout).Should(Succeed())
m.Get(s2, timeout).Should(Succeed())
m.Get(s3, timeout).Should(Succeed())
m.Get(s4, timeout).Should(Succeed())
m.Get(s5, timeout).Should(Succeed())
m.Get(s6, timeout).Should(Succeed())
m.Get(deploymentObject, timeout).Should(Succeed())
})

Expand Down Expand Up @@ -134,6 +150,23 @@ var _ = Describe("Wave children Suite", func() {
}))
})

It("returns ConfigMaps referenced in Volume Projections", func() {
Expect(currentChildren).To(ContainElement(configObject{
object: cm6,
required: true,
allKeys: false,
keys: map[string]struct{}{
"example6_key1": {},
"example6_key3": {},
},
}))
Expect(currentChildren).To(ContainElement(configObject{
object: cm5,
required: false,
allKeys: true,
}))
})

It("returns ConfigMaps referenced in EnvFrom", func() {
Expect(currentChildren).To(ContainElement(configObject{
object: cm2,
Expand Down Expand Up @@ -171,6 +204,23 @@ var _ = Describe("Wave children Suite", func() {
}))
})

It("returns Secrets referenced in Volume Projections", func() {
Expect(currentChildren).To(ContainElement(configObject{
object: s6,
required: true,
allKeys: false,
keys: map[string]struct{}{
"example6_key1": {},
"example6_key3": {},
},
}))
Expect(currentChildren).To(ContainElement(configObject{
object: s5,
required: false,
allKeys: true,
}))
})

It("returns Secrets referenced in EnvFrom", func() {
Expect(currentChildren).To(ContainElement(configObject{
object: s2,
Expand Down Expand Up @@ -201,7 +251,7 @@ var _ = Describe("Wave children Suite", func() {
})

It("does not return duplicate children", func() {
Expect(currentChildren).To(HaveLen(8))
Expect(currentChildren).To(HaveLen(12))
})

It("returns an error if one of the referenced children is missing", func() {
Expand Down Expand Up @@ -300,8 +350,8 @@ var _ = Describe("Wave children Suite", func() {
})

It("does not return extra children", func() {
Expect(configMaps).To(HaveLen(7))
Expect(secrets).To(HaveLen(7))
Expect(configMaps).To(HaveLen(9))
Expect(secrets).To(HaveLen(9))
})
})

Expand All @@ -319,32 +369,36 @@ var _ = Describe("Wave children Suite", func() {
}

var err error
existingChildren, err = h.getExistingChildren(podControllerDeployment)
_, err = h.getExistingChildren(podControllerDeployment)
Expect(err).NotTo(HaveOccurred())
})

existingChildrenFn := func() ([]Object, error) {
return h.getExistingChildren(podControllerDeployment)
}

It("returns ConfigMaps with the correct OwnerReference", func() {
Expect(existingChildren).To(ContainElement(cm1))
Eventually(existingChildrenFn).Should(ContainElement(cm1))
})

It("doesn't return ConfigMaps without OwnerReferences", func() {
Expect(existingChildren).NotTo(ContainElement(cm2))
Expect(existingChildren).NotTo(ContainElement(cm3))
Expect(existingChildren).NotTo(ContainElement(cm4))
Eventually(existingChildrenFn).ShouldNot(ContainElement(cm2))
Eventually(existingChildrenFn).ShouldNot(ContainElement(cm3))
Eventually(existingChildrenFn).ShouldNot(ContainElement(cm4))
})

It("returns Secrets with the correct OwnerReference", func() {
Expect(existingChildren).To(ContainElement(s1))
Eventually(existingChildrenFn).Should(ContainElement(s1))
})

It("doesn't return Secrets without OwnerReferences", func() {
Expect(existingChildren).NotTo(ContainElement(s2))
Expect(existingChildren).NotTo(ContainElement(s3))
Expect(existingChildren).NotTo(ContainElement(s4))
Eventually(existingChildrenFn).ShouldNot(ContainElement(s2))
Eventually(existingChildrenFn).ShouldNot(ContainElement(s3))
Eventually(existingChildrenFn).ShouldNot(ContainElement(s4))
})

It("does not return duplicate children", func() {
Expect(existingChildren).To(HaveLen(2))
Eventually(existingChildrenFn).Should(HaveLen(2))
})
})

Expand Down
26 changes: 25 additions & 1 deletion pkg/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ var _ = Describe("Wave controller Suite", func() {
var cm1 *corev1.ConfigMap
var cm2 *corev1.ConfigMap
var cm3 *corev1.ConfigMap
var cm4 *corev1.ConfigMap
var cm5 *corev1.ConfigMap
var cm6 *corev1.ConfigMap
var s1 *corev1.Secret
var s2 *corev1.Secret
var s3 *corev1.Secret
var s4 *corev1.Secret
var s5 *corev1.Secret
var s6 *corev1.Secret

var modified = "modified"

Expand All @@ -79,22 +85,40 @@ var _ = Describe("Wave controller Suite", func() {
cm1 = utils.ExampleConfigMap1.DeepCopy()
cm2 = utils.ExampleConfigMap2.DeepCopy()
cm3 = utils.ExampleConfigMap3.DeepCopy()
cm4 = utils.ExampleConfigMap4.DeepCopy()
cm5 = utils.ExampleConfigMap5.DeepCopy()
cm6 = utils.ExampleConfigMap6.DeepCopy()
s1 = utils.ExampleSecret1.DeepCopy()
s2 = utils.ExampleSecret2.DeepCopy()
s3 = utils.ExampleSecret3.DeepCopy()
s4 = utils.ExampleSecret4.DeepCopy()
s5 = utils.ExampleSecret5.DeepCopy()
s6 = utils.ExampleSecret6.DeepCopy()

m.Create(cm1).Should(Succeed())
m.Create(cm2).Should(Succeed())
m.Create(cm3).Should(Succeed())
m.Create(cm4).Should(Succeed())
m.Create(cm5).Should(Succeed())
m.Create(cm6).Should(Succeed())
m.Create(s1).Should(Succeed())
m.Create(s2).Should(Succeed())
m.Create(s3).Should(Succeed())
m.Create(s4).Should(Succeed())
m.Create(s5).Should(Succeed())
m.Create(s6).Should(Succeed())
m.Get(cm1, timeout).Should(Succeed())
m.Get(cm2, timeout).Should(Succeed())
m.Get(cm3, timeout).Should(Succeed())
m.Get(cm4, timeout).Should(Succeed())
m.Get(cm5, timeout).Should(Succeed())
m.Get(cm6, timeout).Should(Succeed())
m.Get(s1, timeout).Should(Succeed())
m.Get(s2, timeout).Should(Succeed())
m.Get(s3, timeout).Should(Succeed())
m.Get(s4, timeout).Should(Succeed())
m.Get(s5, timeout).Should(Succeed())
m.Get(s6, timeout).Should(Succeed())

deployment = utils.ExampleDeployment.DeepCopy()

Expand Down Expand Up @@ -194,7 +218,7 @@ var _ = Describe("Wave controller Suite", func() {
eventMessage := func(event *corev1.Event) string {
return event.Message
}
hashMessage := "Configuration hash updated to ebabf80ef45218b27078a41ca16b35a4f91cb5672f389e520ae9da6ee3df3b1c"
hashMessage := "Configuration hash updated to 421778c325761f51dbf7a23a20eb9c1bc516ffd4aa7362ebec03175d427d7557"
Eventually(func() *corev1.EventList {
events := &corev1.EventList{}
m.Client.List(context.TODO(), events)
Expand Down
Loading

0 comments on commit 9f93638

Please sign in to comment.