Skip to content

Commit

Permalink
Merge pull request #8343 from evhan/maintenance-job-env-from
Browse files Browse the repository at this point in the history
Copy "envFrom" from Velero server when creating maintenance jobs
  • Loading branch information
reasonerjt authored Nov 18, 2024
2 parents 74790d9 + f981dd4 commit e7da672
Show file tree
Hide file tree
Showing 9 changed files with 231 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
1 change: 1 addition & 0 deletions pkg/exposer/csi_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ func (e *csiSnapshotExposer) createBackupPod(
VolumeMounts: volumeMounts,
VolumeDevices: volumeDevices,
Env: podInfo.env,
EnvFrom: podInfo.envFrom,
Resources: resources,
},
},
Expand Down
1 change: 1 addition & 0 deletions pkg/exposer/generic_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ func (e *genericRestoreExposer) createRestorePod(ctx context.Context, ownerObjec
VolumeMounts: volumeMounts,
VolumeDevices: volumeDevices,
Env: podInfo.env,
EnvFrom: podInfo.envFrom,
Resources: resources,
},
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/exposer/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type inheritedPodInfo struct {
image string
serviceAccount string
env []v1.EnvVar
envFrom []v1.EnvFromSource
volumeMounts []v1.VolumeMount
volumes []v1.Volume
logLevelArgs []string
Expand All @@ -53,6 +54,7 @@ func getInheritedPodInfo(ctx context.Context, client kubernetes.Interface, veler
podInfo.serviceAccount = podSpec.ServiceAccountName

podInfo.env = podSpec.Containers[0].Env
podInfo.envFrom = podSpec.Containers[0].EnvFrom
podInfo.volumeMounts = podSpec.Containers[0].VolumeMounts
podInfo.volumes = podSpec.Volumes

Expand Down
64 changes: 64 additions & 0 deletions pkg/exposer/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ func TestGetInheritedPodInfo(t *testing.T) {
Value: "value-2",
},
},
EnvFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-configmap",
},
},
},
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-secret",
},
},
},
},
VolumeMounts: []v1.VolumeMount{
{
Name: "volume-1",
Expand Down Expand Up @@ -116,6 +132,22 @@ func TestGetInheritedPodInfo(t *testing.T) {
Value: "value-2",
},
},
EnvFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-configmap",
},
},
},
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-secret",
},
},
},
},
VolumeMounts: []v1.VolumeMount{
{
Name: "volume-1",
Expand Down Expand Up @@ -191,6 +223,22 @@ func TestGetInheritedPodInfo(t *testing.T) {
Value: "value-2",
},
},
envFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-configmap",
},
},
},
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-secret",
},
},
},
},
volumeMounts: []v1.VolumeMount{
{
Name: "volume-1",
Expand Down Expand Up @@ -228,6 +276,22 @@ func TestGetInheritedPodInfo(t *testing.T) {
Value: "value-2",
},
},
envFrom: []v1.EnvFromSource{
{
ConfigMapRef: &v1.ConfigMapEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-configmap",
},
},
},
{
SecretRef: &v1.SecretEnvSource{
LocalObjectReference: v1.LocalObjectReference{
Name: "test-secret",
},
},
},
},
volumeMounts: []v1.VolumeMount{
{
Name: "volume-1",
Expand Down
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 e7da672

Please sign in to comment.