diff --git a/pkg/kotsadm/objects/kotsadm_objects.go b/pkg/kotsadm/objects/kotsadm_objects.go index 9c683e94c2..ab2f4df280 100644 --- a/pkg/kotsadm/objects/kotsadm_objects.go +++ b/pkg/kotsadm/objects/kotsadm_objects.go @@ -163,11 +163,37 @@ func UpdateKotsadmDeployment(existingDeployment *appsv1.Deployment, desiredDeplo existingDeployment.Spec.Template.Spec.Containers[containerIdx].VolumeMounts = desiredVolumeMounts existingDeployment.Spec.Template.Spec.Containers[containerIdx].Env = desiredDeployment.Spec.Template.Spec.Containers[0].Env - existingDeployment.Spec.Template.Annotations["pre.hook.backup.velero.io/command"] = `["/scripts/backup.sh"]` + updateKotsadmDeploymentScriptsPath(existingDeployment) return nil } +func updateKotsadmDeploymentScriptsPath(existing *appsv1.Deployment) { + if existing.Spec.Template.Annotations != nil { + existing.Spec.Template.Annotations["pre.hook.backup.velero.io/command"] = `["/scripts/backup.sh"]` + } + + for i, c := range existing.Spec.Template.Spec.Containers { + for j, env := range c.Env { + if env.Name == "POSTGRES_SCHEMA_DIR" { + existing.Spec.Template.Spec.Containers[i].Env[j].Value = "/scripts/postgres/tables" + } + } + } + + for i, c := range existing.Spec.Template.Spec.InitContainers { + if c.Name == "restore-db" { + existing.Spec.Template.Spec.InitContainers[i].Command = []string{ + "/scripts/restore-db.sh", + } + } else if c.Name == "restore-s3" { + existing.Spec.Template.Spec.InitContainers[i].Command = []string{ + "/scripts/restore-s3.sh", + } + } + } +} + func KotsadmDeployment(deployOptions types.DeployOptions) (*appsv1.Deployment, error) { securityContext := k8sutil.SecurePodContext(1001, 1001, deployOptions.StrictSecurityContext) if deployOptions.IsOpenShift { @@ -729,11 +755,29 @@ func UpdateKotsadmStatefulSet(existingStatefulset *appsv1.StatefulSet, desiredSt existingStatefulset.Spec.Template.Spec.Containers[containerIdx].VolumeMounts = desiredVolumeMounts existingStatefulset.Spec.Template.Spec.Containers[containerIdx].Env = desiredStatefulSet.Spec.Template.Spec.Containers[0].Env - existingStatefulset.Spec.Template.Annotations["pre.hook.backup.velero.io/command"] = `["/scripts/backup.sh"]` + updateKotsadmStatefulSetScriptsPath(existingStatefulset) return nil } +func updateKotsadmStatefulSetScriptsPath(existing *appsv1.StatefulSet) { + if existing.Spec.Template.Annotations != nil { + existing.Spec.Template.Annotations["pre.hook.backup.velero.io/command"] = `["/scripts/backup.sh"]` + } + + for i, c := range existing.Spec.Template.Spec.InitContainers { + if c.Name == "restore-data" { + existing.Spec.Template.Spec.InitContainers[i].Command = []string{ + "/scripts/restore.sh", + } + } else if c.Name == "migrate-s3" { + existing.Spec.Template.Spec.InitContainers[i].Command = []string{ + "/scripts/migrate-s3.sh", + } + } + } +} + // TODO add configmap for additional CAs func KotsadmStatefulSet(deployOptions types.DeployOptions, size resource.Quantity) (*appsv1.StatefulSet, error) { securityContext := k8sutil.SecurePodContext(1001, 1001, deployOptions.StrictSecurityContext) diff --git a/pkg/kotsadm/objects/kotsadm_objects_test.go b/pkg/kotsadm/objects/kotsadm_objects_test.go new file mode 100644 index 0000000000..3d7638290c --- /dev/null +++ b/pkg/kotsadm/objects/kotsadm_objects_test.go @@ -0,0 +1,228 @@ +package kotsadm + +import ( + "testing" + + "github.com/stretchr/testify/assert" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func Test_updateKotsadmStatefulSetScriptsPath(t *testing.T) { + type args struct { + existing *appsv1.StatefulSet + } + tests := []struct { + name string + args args + want *appsv1.StatefulSet + }{ + { + name: "migrate scripts dir", + args: args{ + existing: &appsv1.StatefulSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "kotsadm", + }, + Spec: appsv1.StatefulSetSpec{ + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "backup.velero.io/backup-volumes": "backup", + "pre.hook.backup.velero.io/command": `["/backup.sh"]`, + "pre.hook.backup.velero.io/timeout": "10m", + }, + }, + Spec: corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "some-other-init-container", + }, + { + Name: "restore-data", + Command: []string{ + "/restore.sh", + }, + }, + { + Name: "migrate-s3", + Command: []string{ + "/migrate-s3.sh", + }, + }, + }, + Containers: []corev1.Container{ + { + Name: "kotsadm", + }, + }, + }, + }, + }, + }, + }, + want: &appsv1.StatefulSet{ + ObjectMeta: metav1.ObjectMeta{ + Name: "kotsadm", + }, + Spec: appsv1.StatefulSetSpec{ + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "backup.velero.io/backup-volumes": "backup", + "pre.hook.backup.velero.io/command": `["/scripts/backup.sh"]`, + "pre.hook.backup.velero.io/timeout": "10m", + }, + }, + Spec: corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "some-other-init-container", + }, + { + Name: "restore-data", + Command: []string{ + "/scripts/restore.sh", + }, + }, + { + Name: "migrate-s3", + Command: []string{ + "/scripts/migrate-s3.sh", + }, + }, + }, + Containers: []corev1.Container{ + { + Name: "kotsadm", + }, + }, + }, + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + updateKotsadmStatefulSetScriptsPath(tt.args.existing) + assert.Equal(t, tt.want, tt.args.existing) + }) + } +} + +func Test_updateKotsadmDeploymentScriptsPath(t *testing.T) { + type args struct { + existing *appsv1.Deployment + } + tests := []struct { + name string + args args + want *appsv1.Deployment + }{ + { + name: "migrate scripts dir", + args: args{ + existing: &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "kotsadm", + }, + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "backup.velero.io/backup-volumes": "backup", + "pre.hook.backup.velero.io/command": `["/backup.sh"]`, + "pre.hook.backup.velero.io/timeout": "10m", + }, + }, + Spec: corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "some-other-init-container", + }, + { + Name: "restore-db", + Command: []string{ + "/restore-db.sh", + }, + }, + { + Name: "restore-s3", + Command: []string{ + "/restore-s3.sh", + }, + }, + }, + Containers: []corev1.Container{ + { + Name: "kotsadm", + Env: []corev1.EnvVar{ + { + Name: "POSTGRES_SCHEMA_DIR", + Value: "/postgres/tables", + }, + }, + }, + }, + }, + }, + }, + }, + }, + want: &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "kotsadm", + }, + Spec: appsv1.DeploymentSpec{ + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + "backup.velero.io/backup-volumes": "backup", + "pre.hook.backup.velero.io/command": `["/scripts/backup.sh"]`, + "pre.hook.backup.velero.io/timeout": "10m", + }, + }, + Spec: corev1.PodSpec{ + InitContainers: []corev1.Container{ + { + Name: "some-other-init-container", + }, + { + Name: "restore-db", + Command: []string{ + "/scripts/restore-db.sh", + }, + }, + { + Name: "restore-s3", + Command: []string{ + "/scripts/restore-s3.sh", + }, + }, + }, + Containers: []corev1.Container{ + { + Name: "kotsadm", + Env: []corev1.EnvVar{ + { + Name: "POSTGRES_SCHEMA_DIR", + Value: "/scripts/postgres/tables", + }, + }, + }, + }, + }, + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + updateKotsadmDeploymentScriptsPath(tt.args.existing) + assert.Equal(t, tt.want, tt.args.existing) + }) + } +}