Skip to content

Commit

Permalink
also hash binarydata in configmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
jabdoa2 committed Apr 27, 2024
1 parent de65b16 commit 3f1818d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 16 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
2 changes: 1 addition & 1 deletion pkg/controller/deployment/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,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 bd786f47ef9b79841ddba1059752f95c4fe21906df5e2964786b4658e02758d5"
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
2 changes: 1 addition & 1 deletion pkg/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,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 bd786f47ef9b79841ddba1059752f95c4fe21906df5e2964786b4658e02758d5"
Eventually(func() *corev1.EventList {
events := &corev1.EventList{}
m.Client.List(context.TODO(), events)
Expand Down
20 changes: 15 additions & 5 deletions pkg/core/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import (
func calculateConfigHash(children []configObject) (string, error) {
// hashSource contains all the data to be hashed
hashSource := struct {
ConfigMaps map[string]map[string]string `json:"configMaps"`
ConfigMaps map[string]map[string][]byte `json:"configMaps"`
Secrets map[string]map[string][]byte `json:"secrets"`
}{
ConfigMaps: make(map[string]map[string]string),
ConfigMaps: make(map[string]map[string][]byte),
Secrets: make(map[string]map[string][]byte),
}

Expand Down Expand Up @@ -65,14 +65,24 @@ func calculateConfigHash(children []configObject) (string, error) {

// getConfigMapData extracts all the relevant data from the ConfigMap, whether that is
// the whole ConfigMap or only the specified keys.
func getConfigMapData(child configObject) map[string]string {
func getConfigMapData(child configObject) map[string][]byte {
cm := *child.object.(*corev1.ConfigMap)
if child.allKeys {
return cm.Data
data := make(map[string][]byte)
for key := range cm.Data {
data[key] = []byte(cm.Data[key])
}
for key := range cm.BinaryData {
data[key] = cm.BinaryData[key]
}
return data
}
keyData := make(map[string]string)
keyData := make(map[string][]byte)
for key := range child.keys {
if value, exists := cm.Data[key]; exists {
keyData[key] = []byte(value)
}
if value, exists := cm.BinaryData[key]; exists {
keyData[key] = value
}
}
Expand Down
57 changes: 50 additions & 7 deletions pkg/core/hash_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 Down Expand Up @@ -97,7 +98,7 @@ var _ = Describe("Wave hash Suite", func() {
)
})

It("returns a different hash when an allKeys child's data is updated", func() {
It("returns a different hash when an allKeys child's configmap string data is updated", func() {
c := []configObject{
{object: cm1, allKeys: true},
{object: cm2, allKeys: true},
Expand All @@ -120,7 +121,7 @@ var _ = Describe("Wave hash Suite", func() {
Expect(h2).NotTo(Equal(h1))
})

It("returns a different hash when an all-field child's data is updated", func() {
It("returns a different hash when an allKeys child's configmap binary data is updated", func() {
c := []configObject{
{object: cm1, allKeys: true},
{object: cm2, allKeys: true},
Expand All @@ -133,7 +134,30 @@ var _ = Describe("Wave hash Suite", func() {

m.Update(cm1, func(obj client.Object) client.Object {
cm := obj.(*corev1.ConfigMap)
cm.Data["key1"] = modified
cm.BinaryData["binary_key1"] = []byte(modified)

return cm
}, timeout).Should(Succeed())
h2, err := calculateConfigHash(c)
Expect(err).NotTo(HaveOccurred())

Expect(h2).NotTo(Equal(h1))
})

It("returns a different hash when an allKeys child's secret data is updated", func() {
c := []configObject{
{object: cm1, allKeys: true},
{object: cm2, allKeys: true},
{object: s1, allKeys: true},
{object: s2, allKeys: true},
}

h1, err := calculateConfigHash(c)
Expect(err).NotTo(HaveOccurred())

m.Update(s1, func(obj client.Object) client.Object {
cm := obj.(*corev1.Secret)
cm.Data["key1"] = []byte("modified")

return cm
}, timeout).Should(Succeed())
Expand All @@ -146,7 +170,8 @@ var _ = Describe("Wave hash Suite", func() {
It("returns a different hash when a single-field child's data is updated", func() {
c := []configObject{
{object: cm1, allKeys: false, keys: map[string]struct{}{
"key1": {},
"key1": {},
"binary_key1": {},
},
},
{object: cm2, allKeys: true},
Expand All @@ -166,23 +191,40 @@ var _ = Describe("Wave hash Suite", func() {

return cm
}, timeout).Should(Succeed())
h2, err := calculateConfigHash(c)
Expect(err).NotTo(HaveOccurred())

m.Update(cm1, func(obj client.Object) client.Object {
cm := obj.(*corev1.ConfigMap)
cm.BinaryData["binary_key1"] = []byte(modified)

return cm
}, timeout).Should(Succeed())
h3, err := calculateConfigHash(c)
Expect(err).NotTo(HaveOccurred())

m.Update(s1, func(obj client.Object) client.Object {
s := obj.(*corev1.Secret)
s.Data["key1"] = []byte("modified")

return s
}, timeout).Should(Succeed())
h2, err := calculateConfigHash(c)
h4, err := calculateConfigHash(c)
Expect(err).NotTo(HaveOccurred())

Expect(h2).NotTo(Equal(h1))
Expect(h3).NotTo(Equal(h1))
Expect(h3).NotTo(Equal(h2))
Expect(h4).NotTo(Equal(h1))
Expect(h4).NotTo(Equal(h2))
Expect(h4).NotTo(Equal(h3))
})

It("returns the same hash when a single-field child's data is updated but not for that field", func() {
c := []configObject{
{object: cm1, allKeys: false, keys: map[string]struct{}{
"key1": {},
"key1": {},
"binary_key1": {},
},
},
{object: cm2, allKeys: true},
Expand All @@ -199,6 +241,7 @@ var _ = Describe("Wave hash Suite", func() {
m.Update(cm1, func(obj client.Object) client.Object {
cm := obj.(*corev1.ConfigMap)
cm.Data["key3"] = modified
cm.BinaryData["binary_key3"] = []byte("modified")

return cm
}, timeout).Should(Succeed())
Expand Down
5 changes: 5 additions & 0 deletions test/utils/test_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ var ExampleConfigMap1 = &corev1.ConfigMap{
"key2": "example1:key2",
"key3": "example1:key3",
},
BinaryData: map[string][]byte{
"binary_key1": []byte("example1:binary_key1"),
"binary_key2": []byte("example1:binary_key2"),
"binary_key3": []byte("example1:binary_key3"),
},
}

// ExampleConfigMap2 is an example ConfigMap object for use within test suites
Expand Down

0 comments on commit 3f1818d

Please sign in to comment.