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: update spec with new scripts dir when updating kotsadm #5085

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions pkg/kotsadm/objects/kotsadm_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`
}
Comment on lines +172 to +174
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should create the map if it is nil, but this is probably fine in practice


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 {
Expand Down Expand Up @@ -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)
Expand Down
228 changes: 228 additions & 0 deletions pkg/kotsadm/objects/kotsadm_objects_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
}
Loading