Skip to content

Commit

Permalink
Copy "envFrom" from Velero server when creating maintenance jobs
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Hanson <[email protected]>
  • Loading branch information
a-really-great-username committed Oct 30, 2024
1 parent ebbeb7a commit 70d8890
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/8343-evhan
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Copy "envFrom" from Velero server when creating maintenance jobs
4 changes: 4 additions & 0 deletions pkg/repository/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ func (m *manager) buildMaintenanceJob(
// Get the environment variables from the Velero server deployment
envVars := veleroutil.GetEnvVarsFromVeleroServer(deployment)

// Get the referenced storage from the Velero server deployment
envFromSources := veleroutil.GetEnvFromSourcesFromVeleroServer(deployment)

// Get the volume mounts from the Velero server deployment
volumeMounts := veleroutil.GetVolumeMountsFromVeleroServer(deployment)

Expand Down Expand Up @@ -433,6 +436,7 @@ func (m *manager) buildMaintenanceJob(
Args: args,
ImagePullPolicy: v1.PullIfNotPresent,
Env: envVars,
EnvFrom: envFromSources,
VolumeMounts: volumeMounts,
Resources: resources,
},
Expand Down
50 changes: 50 additions & 0 deletions pkg/repository/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func TestBuildMaintenanceJob(t *testing.T) {
logFormat *logging.FormatFlag
expectedJobName string
expectedError bool
expectedEnv []v1.EnvVar
expectedEnvFrom []v1.EnvFromSource
}{
{
name: "Valid maintenance job",
Expand All @@ -93,6 +95,28 @@ func TestBuildMaintenanceJob(t *testing.T) {
{
Name: "velero-repo-maintenance-container",
Image: "velero-image",
Env: []v1.EnvVar{
{
Name: "test-name",
Value: "test-value",
},
},
EnvFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-configmap",
},
},
},
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-secret",
},
},
},
},
},
},
},
Expand All @@ -103,6 +127,28 @@ func TestBuildMaintenanceJob(t *testing.T) {
logFormat: logging.NewFormatFlag(),
expectedJobName: "test-123-maintain-job",
expectedError: false,
expectedEnv: []v1.EnvVar{
{
Name: "test-name",
Value: "test-value",
},
},
expectedEnvFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-configmap",
},
},
},
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-secret",
},
},
},
},
},
{
name: "Error getting Velero server deployment",
Expand Down Expand Up @@ -191,6 +237,10 @@ func TestBuildMaintenanceJob(t *testing.T) {
assert.Equal(t, "velero-image", container.Image)
assert.Equal(t, v1.PullIfNotPresent, container.ImagePullPolicy)

// Check container env
assert.Equal(t, tc.expectedEnv, container.Env)
assert.Equal(t, tc.expectedEnvFrom, container.EnvFrom)

// Check resources
expectedResources := v1.ResourceRequirements{
Requests: v1.ResourceList{
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/velero/velero.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ func GetEnvVarsFromVeleroServer(deployment *appsv1.Deployment) []v1.EnvVar {
return nil
}

// GetEnvFromSourcesFromVeleroServer get the environment sources from the Velero server deployment
func GetEnvFromSourcesFromVeleroServer(deployment *appsv1.Deployment) []v1.EnvFromSource {
for _, container := range deployment.Spec.Template.Spec.Containers {
// We only have one container in the Velero server deployment
return container.EnvFrom
}
return nil
}

// GetVolumeMountsFromVeleroServer get the volume mounts from the Velero server deployment
func GetVolumeMountsFromVeleroServer(deployment *appsv1.Deployment) []v1.VolumeMount {
for _, container := range deployment.Spec.Template.Spec.Containers {
Expand Down
99 changes: 99 additions & 0 deletions pkg/util/velero/velero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,105 @@ func TestGetEnvVarsFromVeleroServer(t *testing.T) {
}
}

func TestGetEnvFromSourcesFromVeleroServer(t *testing.T) {
tests := []struct {
name string
deploy *appsv1.Deployment
expected []v1.EnvFromSource
}{
{
name: "no env vars",
deploy: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
EnvFrom: []v1.EnvFromSource{},
},
},
},
},
},
},
expected: []v1.EnvFromSource{},
},
{
name: "configmap",
deploy: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
EnvFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "foo",
},
},
},
},
},
},
},
},
},
},
expected: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "foo",
},
},
},
},
},
{
name: "secret",
deploy: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: v1.PodTemplateSpec{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
EnvFrom: []v1.EnvFromSource{
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "foo",
},
},
},
},
},
},
},
},
},
},
expected: []v1.EnvFromSource{
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "foo",
},
},
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := GetEnvFromSourcesFromVeleroServer(test.deploy)
assert.Equal(t, test.expected, result)
})
}
}

func TestGetVolumeMountsFromVeleroServer(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 70d8890

Please sign in to comment.