From 20246998da33363cd933a3e73c1782a5dea5cab9 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 20 Jun 2024 12:48:17 -0400 Subject: [PATCH 01/10] fix: Duplicated restores --- pkg/configuration/backuprestore/backuprestore.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pkg/configuration/backuprestore/backuprestore.go b/pkg/configuration/backuprestore/backuprestore.go index b2deb9ca1..122225f59 100644 --- a/pkg/configuration/backuprestore/backuprestore.go +++ b/pkg/configuration/backuprestore/backuprestore.go @@ -180,12 +180,7 @@ func (bar *BackupAndRestore) Restore(jenkinsClient jenkinsclient.Jenkins) error if err != nil { return err } - //TODO fix me because we're doing two saves unatomically - jenkins.Spec.Restore.RecoveryOnce = 0 - err = bar.Client.Update(context.TODO(), jenkins) - if err != nil { - return err - } + key := types.NamespacedName{ Namespace: jenkins.Namespace, Name: jenkins.Name, @@ -194,6 +189,12 @@ func (bar *BackupAndRestore) Restore(jenkinsClient jenkinsclient.Jenkins) error if err != nil { return err } + + jenkins.Spec.Restore.RecoveryOnce = 0 + err = bar.Client.Update(context.TODO(), jenkins) + if err != nil { + return err + } bar.Configuration.Jenkins = jenkins jenkins.Status.RestoredBackup = backupNumber From ad2c908a3e877693d6be4a9450e4b7a7f885f4e2 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 20 Jun 2024 13:40:37 -0400 Subject: [PATCH 02/10] fix: Duplicate backups --- pkg/configuration/backuprestore/backuprestore.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/configuration/backuprestore/backuprestore.go b/pkg/configuration/backuprestore/backuprestore.go index 122225f59..ed203a55f 100644 --- a/pkg/configuration/backuprestore/backuprestore.go +++ b/pkg/configuration/backuprestore/backuprestore.go @@ -224,6 +224,15 @@ func (bar *BackupAndRestore) Backup(setBackupDoneBeforePodDeletion bool) error { _, _, err := bar.Exec(podName, jenkins.Spec.Backup.ContainerName, command) if err == nil { + key := types.NamespacedName{ + Namespace: jenkins.Namespace, + Name: jenkins.Name, + } + err = bar.Client.Get(context.TODO(), key, jenkins) + if err != nil { + return err + } + bar.logger.V(log.VDebug).Info(fmt.Sprintf("Backup completed '%d', updating status", backupNumber)) if jenkins.Status.RestoredBackup == 0 { jenkins.Status.RestoredBackup = backupNumber From adf438f66275419963ca215024744d7cc3341ff0 Mon Sep 17 00:00:00 2001 From: Dion Date: Thu, 20 Jun 2024 22:06:26 -0400 Subject: [PATCH 03/10] Trigger webhook From 80086306fd891f75c2c68242a5d5bedbf396ac14 Mon Sep 17 00:00:00 2001 From: Dion Date: Mon, 23 Sep 2024 17:54:36 -0400 Subject: [PATCH 04/10] fix(master): pass lifecycle configuration to master container --- pkg/configuration/base/resources/pod.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/configuration/base/resources/pod.go b/pkg/configuration/base/resources/pod.go index 783ccaa2b..e46eb5339 100644 --- a/pkg/configuration/base/resources/pod.go +++ b/pkg/configuration/base/resources/pod.go @@ -243,6 +243,7 @@ func NewJenkinsMasterContainer(jenkins *v1alpha2.Jenkins) corev1.Container { Image: jenkinsContainer.Image, ImagePullPolicy: jenkinsContainer.ImagePullPolicy, Command: jenkinsContainer.Command, + Lifecycle: jenkinsContainer.Lifecycle, LivenessProbe: jenkinsContainer.LivenessProbe, ReadinessProbe: jenkinsContainer.ReadinessProbe, Ports: []corev1.ContainerPort{ From c59901ba08a2da366d4c1ec7b20cfff846e10a64 Mon Sep 17 00:00:00 2001 From: Dion Date: Tue, 12 Nov 2024 06:25:12 -0500 Subject: [PATCH 05/10] set default empty Lifecycle --- controllers/jenkins_controller.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/controllers/jenkins_controller.go b/controllers/jenkins_controller.go index 7eb92454b..8569b6f25 100644 --- a/controllers/jenkins_controller.go +++ b/controllers/jenkins_controller.go @@ -378,6 +378,11 @@ func (r *JenkinsReconciler) setDefaults(jenkins *v1alpha2.Jenkins) (requeue bool changed = true jenkinsContainer.LivenessProbe = resources.NewProbe(containerProbeURI, containerProbePortName, corev1.URISchemeHTTP, 80, 5, 12) } + if jenkinsContainer.Lifecycle == nil { + logger.Info("Setting default Jenkins lifecycle") + changed = true + jenkinsContainer.Lifecycle = &corev1.Lifecycle{} + } if len(jenkinsContainer.Command) == 0 { logger.Info("Setting default Jenkins container command") changed = true From c773fa7f59d4b307f87d41751bf5afe1ca42e5d1 Mon Sep 17 00:00:00 2001 From: Dion Date: Tue, 12 Nov 2024 06:25:54 -0500 Subject: [PATCH 06/10] add jenkins lifecycle to helm chart --- chart/jenkins-operator/templates/jenkins.yaml | 3 +++ chart/jenkins-operator/values.yaml | 3 +++ 2 files changed, 6 insertions(+) diff --git a/chart/jenkins-operator/templates/jenkins.yaml b/chart/jenkins-operator/templates/jenkins.yaml index 72c18c239..0906891fd 100644 --- a/chart/jenkins-operator/templates/jenkins.yaml +++ b/chart/jenkins-operator/templates/jenkins.yaml @@ -134,6 +134,9 @@ spec: {{- with .Values.jenkins.env }} env: {{- toYaml . | nindent 8 }} {{- end }} + {{- with .Values.jenkins.lifecycle}} + lifecycle: {{ toYaml . | nindent 10 }} + {{- end }} {{- with .Values.jenkins.volumeMounts }} volumeMounts: {{- toYaml . | nindent 8 }} {{- end }} diff --git a/chart/jenkins-operator/values.yaml b/chart/jenkins-operator/values.yaml index c2a1cdd10..e40347989 100644 --- a/chart/jenkins-operator/values.yaml +++ b/chart/jenkins-operator/values.yaml @@ -44,6 +44,9 @@ jenkins: # imagePullPolicy defines policy for pulling images imagePullPolicy: Always + # lifecycle is used if you want to specify lifecycle hooks for the master container + lifecycle: {} + # priorityClassName indicates the importance of a Pod relative to other Pods # See: https://kubernetes.io/docs/concepts/configuration/pod-priority-preemption/ priorityClassName: "" From d25d29b058cf7a57cbea21a895edc6c661bed05c Mon Sep 17 00:00:00 2001 From: Dion Date: Tue, 12 Nov 2024 06:30:39 -0500 Subject: [PATCH 07/10] +lifecycle to container defaults comment --- api/v1alpha2/jenkins_types.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/v1alpha2/jenkins_types.go b/api/v1alpha2/jenkins_types.go index 09332af42..4b9f95cec 100644 --- a/api/v1alpha2/jenkins_types.go +++ b/api/v1alpha2/jenkins_types.go @@ -311,6 +311,7 @@ type JenkinsMaster struct { // periodSeconds: 10 // successThreshold: 1 // timeoutSeconds: 5 + // lifecycle: {} // name: jenkins-master // readinessProbe: // failureThreshold: 3 From 0bebe9fcb2c11c5680bf019396ea717abcf8e507 Mon Sep 17 00:00:00 2001 From: DionJones615 Date: Wed, 20 Nov 2024 23:10:54 -0500 Subject: [PATCH 08/10] add lifecyle test --- test/bats/2-deploy-with-more-options.bats | 26 ++++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/test/bats/2-deploy-with-more-options.bats b/test/bats/2-deploy-with-more-options.bats index 3b9aabeea..2f397d181 100644 --- a/test/bats/2-deploy-with-more-options.bats +++ b/test/bats/2-deploy-with-more-options.bats @@ -29,11 +29,10 @@ setup() { --set operator.image=${OPERATOR_IMAGE} \ --set jenkins.latestPlugins=true \ --set jenkins.nodeSelector.batstest=yep \ - --set jenkins.image="jenkins/jenkins:2.462.3-lts" \ - --set jenkins.imagePullPolicy="IfNotPresent" \ + --set jenkins.image="jenkins/jenkins:2.452.1-lts" \ --set jenkins.backup.makeBackupBeforePodDeletion=false \ - --set jenkins.backup.image=quay.io/jenkins-kubernetes-operator/backup-pvc:e2e-test \ - jenkins-operator/jenkins-operator --version=$(get_latest_chart_version) + --set jenkins.lifecycle.preStop.exec.command[0]="echo 'bats test'" \ + jenkins-operator/jenkins-operator --version=$(cat VERSION.txt | sed 's/v//') assert_success assert ${HELM} status options touch "chart/jenkins-operator/deploy.tmp" @@ -95,6 +94,15 @@ setup() { assert_output --partial "Backup completed '1', updating status" } +#bats test_tags=phase:helm,scenario:more-options +@test "2.5 Helm: check lifecycle" { + [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" + + run ${KUBECTL} get pod jenkins-jenkins -o jsonpath={.spec.containers[0].lifecycle.preStop.exec.command[0]} + assert_success + assert_output "echo 'bats test'" +} + #bats test_tags=phase:helm,scenario:more-options @test "2.9 Helm: upgrade from main branch same value" { run ${HELM} upgrade options \ @@ -103,11 +111,9 @@ setup() { --set operator.image=${OPERATOR_IMAGE} \ --set jenkins.latestPlugins=true \ --set jenkins.nodeSelector.batstest=yep \ - --set jenkins.image="jenkins/jenkins:2.462.3-lts" \ - --set jenkins.imagePullPolicy="IfNotPresent" \ + --set jenkins.image="jenkins/jenkins:2.452.1-lts" \ --set jenkins.backup.makeBackupBeforePodDeletion=false \ - --set jenkins.backup.image=quay.io/jenkins-kubernetes-operator/backup-pvc:e2e-test \ - chart/jenkins-operator --wait + chart/jenkins-operator assert_success assert ${HELM} status options } @@ -162,9 +168,9 @@ setup() { @test "2.14 Helm: clean" { [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" - run ${HELM} uninstall options --wait + run ${HELM} uninstall options assert_success - sleep 10 + sleep 30 run verify "there is 0 pvc named 'jenkins backup'" assert_success From aadfdd54e427424f0181f7db06c55bc7c53cf3bd Mon Sep 17 00:00:00 2001 From: Dion Date: Wed, 20 Nov 2024 23:15:14 -0500 Subject: [PATCH 09/10] Revert "add lifecyle test" This reverts commit 0bebe9fcb2c11c5680bf019396ea717abcf8e507. --- test/bats/2-deploy-with-more-options.bats | 26 +++++++++-------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/test/bats/2-deploy-with-more-options.bats b/test/bats/2-deploy-with-more-options.bats index 2f397d181..3b9aabeea 100644 --- a/test/bats/2-deploy-with-more-options.bats +++ b/test/bats/2-deploy-with-more-options.bats @@ -29,10 +29,11 @@ setup() { --set operator.image=${OPERATOR_IMAGE} \ --set jenkins.latestPlugins=true \ --set jenkins.nodeSelector.batstest=yep \ - --set jenkins.image="jenkins/jenkins:2.452.1-lts" \ + --set jenkins.image="jenkins/jenkins:2.462.3-lts" \ + --set jenkins.imagePullPolicy="IfNotPresent" \ --set jenkins.backup.makeBackupBeforePodDeletion=false \ - --set jenkins.lifecycle.preStop.exec.command[0]="echo 'bats test'" \ - jenkins-operator/jenkins-operator --version=$(cat VERSION.txt | sed 's/v//') + --set jenkins.backup.image=quay.io/jenkins-kubernetes-operator/backup-pvc:e2e-test \ + jenkins-operator/jenkins-operator --version=$(get_latest_chart_version) assert_success assert ${HELM} status options touch "chart/jenkins-operator/deploy.tmp" @@ -94,15 +95,6 @@ setup() { assert_output --partial "Backup completed '1', updating status" } -#bats test_tags=phase:helm,scenario:more-options -@test "2.5 Helm: check lifecycle" { - [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" - - run ${KUBECTL} get pod jenkins-jenkins -o jsonpath={.spec.containers[0].lifecycle.preStop.exec.command[0]} - assert_success - assert_output "echo 'bats test'" -} - #bats test_tags=phase:helm,scenario:more-options @test "2.9 Helm: upgrade from main branch same value" { run ${HELM} upgrade options \ @@ -111,9 +103,11 @@ setup() { --set operator.image=${OPERATOR_IMAGE} \ --set jenkins.latestPlugins=true \ --set jenkins.nodeSelector.batstest=yep \ - --set jenkins.image="jenkins/jenkins:2.452.1-lts" \ + --set jenkins.image="jenkins/jenkins:2.462.3-lts" \ + --set jenkins.imagePullPolicy="IfNotPresent" \ --set jenkins.backup.makeBackupBeforePodDeletion=false \ - chart/jenkins-operator + --set jenkins.backup.image=quay.io/jenkins-kubernetes-operator/backup-pvc:e2e-test \ + chart/jenkins-operator --wait assert_success assert ${HELM} status options } @@ -168,9 +162,9 @@ setup() { @test "2.14 Helm: clean" { [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" - run ${HELM} uninstall options + run ${HELM} uninstall options --wait assert_success - sleep 30 + sleep 10 run verify "there is 0 pvc named 'jenkins backup'" assert_success From de68377eed369fb1011c823278187db68a49ef8c Mon Sep 17 00:00:00 2001 From: Dion Date: Wed, 20 Nov 2024 23:19:48 -0500 Subject: [PATCH 10/10] add lifecycle test --- test/bats/2-deploy-with-more-options.bats | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/bats/2-deploy-with-more-options.bats b/test/bats/2-deploy-with-more-options.bats index 3b9aabeea..693bf4006 100644 --- a/test/bats/2-deploy-with-more-options.bats +++ b/test/bats/2-deploy-with-more-options.bats @@ -105,6 +105,7 @@ setup() { --set jenkins.nodeSelector.batstest=yep \ --set jenkins.image="jenkins/jenkins:2.462.3-lts" \ --set jenkins.imagePullPolicy="IfNotPresent" \ + --set jenkins.lifecycle.preStop.command='["echo bats test"]' \ --set jenkins.backup.makeBackupBeforePodDeletion=false \ --set jenkins.backup.image=quay.io/jenkins-kubernetes-operator/backup-pvc:e2e-test \ chart/jenkins-operator --wait @@ -137,6 +138,15 @@ setup() { assert_success } +#bats test_tags=phase:helm,scenario:more-options +@test "2.5 Helm: check lifecycle" { + [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" + + run ${KUBECTL} get pod jenkins-jenkins -o jsonpath={.spec.containers[0].lifecycle.preStop.exec.command[0]} + assert_success + assert_output "echo 'bats test'" +} + #bats test_tags=phase:helm,scenario:more-options @test "2.12 Helm: check node selector again" { [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly"