Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eventsource: deterministic volume/volumeMount order #2811

Merged
merged 9 commits into from
Sep 25, 2023
9 changes: 9 additions & 0 deletions controllers/eventsource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"sort"

"github.com/imdario/mergo"
"go.uber.org/zap"
Expand Down Expand Up @@ -268,6 +269,14 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a
volumeMounts = append(volumeMounts, volCofigMapMounts...)
volumes = append(volumes, volConfigMaps...)

// Order volumes and volumemounts based on name to make the order deterministic
sort.Slice(volumes, func(i, j int) bool {
return volumes[i].Name < volumes[j].Name
})
sort.Slice(volumeMounts, func(i, j int) bool {
return volumeMounts[i].Name < volumeMounts[j].Name
})

deploymentSpec.Template.Spec.Containers[0].Env = append(deploymentSpec.Template.Spec.Containers[0].Env, env...)
deploymentSpec.Template.Spec.Containers[0].VolumeMounts = append(deploymentSpec.Template.Spec.Containers[0].VolumeMounts, volumeMounts...)
deploymentSpec.Template.Spec.Volumes = append(deploymentSpec.Template.Spec.Volumes, volumes...)
Expand Down
85 changes: 85 additions & 0 deletions controllers/eventsource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,91 @@ func Test_BuildDeployment(t *testing.T) {
assert.True(t, hasTLSSecretVolume)
assert.True(t, hasTLSSecretVolumeMount)
})

t.Run("test secret volume and volumemount order deterministic", func(t *testing.T) {
args := &AdaptorArgs{
Image: testImage,
EventSource: testEventSource,
Labels: testLabels,
}

webhooksWithSecrets := map[string]v1alpha1.WebhookEventSource{
"webhook4": {
WebhookContext: v1alpha1.WebhookContext{
URL: "http://a.b",
Endpoint: "/webhook4",
Port: "1234",
AuthSecret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "webhook4"},
Key: "secret",
},
},
},
"webhook3": {
WebhookContext: v1alpha1.WebhookContext{
URL: "http://a.b",
Endpoint: "/webhook3",
Port: "1234",
AuthSecret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "webhook3"},
Key: "secret",
},
},
},
"webhook1": {
WebhookContext: v1alpha1.WebhookContext{
URL: "http://a.b",
Endpoint: "/webhook1",
Port: "1234",
AuthSecret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "webhook1"},
Key: "secret",
},
},
},
"webhook2": {
WebhookContext: v1alpha1.WebhookContext{
URL: "http://a.b",
Endpoint: "/webhook2",
Port: "1234",
AuthSecret: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "webhook2"},
Key: "secret",
},
},
},
}
args.EventSource.Spec.Webhook = webhooksWithSecrets

wantVolumeNames := []string{"auth-volume", "cm-test-cm", "secret-test-secret", "secret-webhook1", "secret-webhook2", "secret-webhook3", "secret-webhook4", "tmp"}
wantVolumeMountNames := []string{"auth-volume", "cm-test-cm", "secret-test-secret", "secret-webhook1", "secret-webhook2", "secret-webhook3", "secret-webhook4", "tmp"}

deployment, err := buildDeployment(args, fakeEventBus)
assert.Nil(t, err)
assert.NotNil(t, deployment)
gotVolumes := deployment.Spec.Template.Spec.Volumes
gotVolumeMounts := deployment.Spec.Template.Spec.Containers[0].VolumeMounts

var gotVolumeNames []string
var gotVolumeMountNames []string

for _, v := range gotVolumes {
gotVolumeNames = append(gotVolumeNames, v.Name)
}
for _, v := range gotVolumeMounts {
gotVolumeMountNames = append(gotVolumeMountNames, v.Name)
}

assert.Equal(t, len(gotVolumes), len(wantVolumeNames))
assert.Equal(t, len(gotVolumeMounts), len(wantVolumeMountNames))

for i := range gotVolumeNames {
assert.Equal(t, gotVolumeNames[i], wantVolumeNames[i])
}
for i := range gotVolumeMountNames {
assert.Equal(t, gotVolumeMountNames[i], wantVolumeMountNames[i])
}
})
}

func TestResourceReconcile(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions controllers/sensor/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"sort"

"github.com/imdario/mergo"
"go.uber.org/zap"
Expand Down Expand Up @@ -228,6 +229,14 @@ func buildDeployment(args *AdaptorArgs, eventBus *eventbusv1alpha1.EventBus) (*a
volumeMounts = append(volumeMounts, volCofigMapMounts...)
volumes = append(volumes, volConfigMaps...)

// Order volumes and volumemounts based on name to make the order deterministic
sort.Slice(volumes, func(i, j int) bool {
return volumes[i].Name < volumes[j].Name
})
sort.Slice(volumeMounts, func(i, j int) bool {
return volumeMounts[i].Name < volumeMounts[j].Name
})

deploymentSpec.Template.Spec.Containers[0].Env = append(deploymentSpec.Template.Spec.Containers[0].Env, env...)
deploymentSpec.Template.Spec.Containers[0].VolumeMounts = append(deploymentSpec.Template.Spec.Containers[0].VolumeMounts, volumeMounts...)
deploymentSpec.Template.Spec.Volumes = append(deploymentSpec.Template.Spec.Volumes, volumes...)
Expand Down
37 changes: 37 additions & 0 deletions controllers/sensor/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,43 @@ func Test_BuildDeployment(t *testing.T) {
assert.True(t, hasTLSSecretVolume)
assert.True(t, hasTLSSecretVolumeMount)
})

t.Run("test secret volume and volumemount order deterministic", func(t *testing.T) {
args := &AdaptorArgs{
Image: testImage,
Sensor: sensorObj,
Labels: testLabels,
}

wantVolumeNames := []string{"test-data", "auth-volume", "tmp"}
wantVolumeMountNames := []string{"test-data", "auth-volume", "tmp"}

deployment, err := buildDeployment(args, fakeEventBus)
assert.Nil(t, err)
assert.NotNil(t, deployment)
gotVolumes := deployment.Spec.Template.Spec.Volumes
gotVolumeMounts := deployment.Spec.Template.Spec.Containers[0].VolumeMounts

var gotVolumeNames []string
var gotVolumeMountNames []string

for _, v := range gotVolumes {
gotVolumeNames = append(gotVolumeNames, v.Name)
}
for _, v := range gotVolumeMounts {
gotVolumeMountNames = append(gotVolumeMountNames, v.Name)
}

assert.Equal(t, len(gotVolumes), len(wantVolumeNames))
assert.Equal(t, len(gotVolumeMounts), len(wantVolumeMountNames))

for i := range gotVolumeNames {
assert.Equal(t, gotVolumeNames[i], wantVolumeNames[i])
}
for i := range gotVolumeMountNames {
assert.Equal(t, gotVolumeMountNames[i], wantVolumeMountNames[i])
}
})
}

func TestResourceReconcile(t *testing.T) {
Expand Down
Loading