Skip to content

Commit

Permalink
Fix secret equality checking (#1471)
Browse files Browse the repository at this point in the history
* Fix secret equality checking

* Remove stringdata checking

* Add back in verify-licenses
  • Loading branch information
khewonc authored Oct 22, 2024
1 parent d5201ff commit 342d9af
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 9 deletions.
12 changes: 3 additions & 9 deletions pkg/equality/equality.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,10 @@ func IsEqualAPIService(objA, objB client.Object) bool {

// IsEqualSecrets return true if the two Secrets are equal
func IsEqualSecrets(a, b client.Object) bool {
sA, okA := a.(*corev1.ConfigMap)
sB, okB := b.(*corev1.ConfigMap)
sA, okA := a.(*corev1.Secret)
sB, okB := b.(*corev1.Secret)
if okA && okB && sA != nil && sB != nil {
if !apiutils.IsEqualStruct(sA.Data, sB.Data) {
return false
}
if !apiutils.IsEqualStruct(sA.BinaryData, sA.BinaryData) {
return false
}
return true
return apiutils.IsEqualStruct(sA.Data, sB.Data)
}
return false
}
Expand Down
92 changes: 92 additions & 0 deletions pkg/equality/equality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"testing"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func TestIsEqualOperatorAnnotations(t *testing.T) {
Expand Down Expand Up @@ -160,3 +162,93 @@ func TestIsEqualOperatorLabels(t *testing.T) {
})
}
}
func TestIsEqualSecrets(t *testing.T) {
tests := []struct {
name string
objA client.Object
objB client.Object
want bool
}{
{
name: "objs equal",
objA: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
Data: map[string][]byte{
"foo": {1, 2, 3},
},
},
objB: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
Data: map[string][]byte{
"foo": {1, 2, 3},
},
},
want: true,
},
{
name: "objs not equal",
objA: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
Data: map[string][]byte{
"foo": {1, 2, 3},
},
},
objB: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
Data: map[string][]byte{
"foo": {3, 2, 1},
},
},
want: false,
},
{
name: "objs not equal, but data and stringdata equal",
objA: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
Type: corev1.SecretTypeBasicAuth,
Data: map[string][]byte{
"foo": {1, 2, 3},
},
},
objB: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
Type: corev1.SecretTypeBootstrapToken,
Data: map[string][]byte{
"foo": {1, 2, 3},
},
},
want: true,
},
{
name: "objs not secrets",
objA: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
},
objB: &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "secret-foo",
},
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, IsEqualSecrets(tt.objA, tt.objB))
})
}
}

0 comments on commit 342d9af

Please sign in to comment.