From d919a2a11cf1b6417daaa414ad315cf3cc985fcc Mon Sep 17 00:00:00 2001 From: DionJones615 Date: Thu, 21 Nov 2024 19:03:01 -0500 Subject: [PATCH 1/3] fix(master): pass lifecycle configuration to master container (#1062) --- api/v1alpha2/jenkins_types.go | 1 + chart/jenkins-operator/templates/jenkins.yaml | 3 +++ chart/jenkins-operator/values.yaml | 3 +++ controllers/jenkins_controller.go | 5 +++++ pkg/configuration/base/resources/pod.go | 1 + test/bats/2-deploy-with-more-options.bats | 10 ++++++++++ 6 files changed, 23 insertions(+) diff --git a/api/v1alpha2/jenkins_types.go b/api/v1alpha2/jenkins_types.go index 09332af4..4b9f95ce 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 diff --git a/chart/jenkins-operator/templates/jenkins.yaml b/chart/jenkins-operator/templates/jenkins.yaml index 72c18c23..0906891f 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 c2a1cdd1..e4034798 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: "" diff --git a/controllers/jenkins_controller.go b/controllers/jenkins_controller.go index 7eb92454..8569b6f2 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 diff --git a/pkg/configuration/base/resources/pod.go b/pkg/configuration/base/resources/pod.go index 783ccaa2..e46eb533 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{ diff --git a/test/bats/2-deploy-with-more-options.bats b/test/bats/2-deploy-with-more-options.bats index 3b9aabee..693bf400 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" From f0da3494aecd5c4a22b958f7a9e8c495852f08ee Mon Sep 17 00:00:00 2001 From: Daniel Beck <1831569+daniel-beck@users.noreply.github.com> Date: Sat, 15 Mar 2025 11:09:10 +0100 Subject: [PATCH 2/3] Delete workflow using backdoored action https://news.ycombinator.com/item?id=43368870 https://github.com/tj-actions/changed-files/commit/0e58ed8671d6b60d0890c21b07f8835ace038e67 --- .github/workflows/auto-codespell.yml | 40 ---------------------------- 1 file changed, 40 deletions(-) delete mode 100644 .github/workflows/auto-codespell.yml diff --git a/.github/workflows/auto-codespell.yml b/.github/workflows/auto-codespell.yml deleted file mode 100644 index cb3c144d..00000000 --- a/.github/workflows/auto-codespell.yml +++ /dev/null @@ -1,40 +0,0 @@ -name: CI Lint -on: - push: - branches: - - master - - main - pull_request: - types: [opened, synchronize, ready_for_review, reopened] -jobs: - codespell: - name: Codespell - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Codespell - uses: codespell-project/actions-codespell@v2 - with: - check_filenames: true - ignore_words_list: aks,ags,startin,NotIn - skip: "*.js,package-lock.json,*.lock,*/Font-Awesome/*,*.toml,*.svg,*assets/vendor/bootstrap*,cert-manager.crds.yaml,*/docs/*" - pre-commit: - name: PreCommit - runs-on: ubuntu-latest - steps: - - uses: actions/setup-python@v5.1.1 - - uses: DeterminateSystems/nix-installer-action@v13 - with: - diagnostic-endpoint: "" - - uses: DeterminateSystems/magic-nix-cache-action@v7 - with: - diagnostic-endpoint: "" - - name: Checkout - uses: actions/checkout@v4 - - id: files - uses: tj-actions/changed-files@v44 - - name: nix checks - run: nix flake check - - name: nix pre-commit - run: nix develop . --command pre-commit run --files ${{ steps.files.outputs.all_changed_files }} --show-diff-on-failure From 1ec83b9de9b021a21d4a25e3e4592d2fc1d49570 Mon Sep 17 00:00:00 2001 From: Manish Bhasin <13490604+xmbhasin@users.noreply.github.com> Date: Wed, 26 Mar 2025 19:19:52 -0400 Subject: [PATCH 3/3] fix(bats-tests): lifecycle hook injection test failing (#1095) Co-authored-by: xmbhasin --- test/bats/2-deploy-with-more-options.bats | 34 ++++++++++++++++------- test/bats/test_helper.bash | 33 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/test/bats/2-deploy-with-more-options.bats b/test/bats/2-deploy-with-more-options.bats index 693bf400..195bc033 100644 --- a/test/bats/2-deploy-with-more-options.bats +++ b/test/bats/2-deploy-with-more-options.bats @@ -105,7 +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.lifecycle.preStop.exec.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 @@ -139,16 +139,15 @@ setup() { } #bats test_tags=phase:helm,scenario:more-options -@test "2.5 Helm: check lifecycle" { +@test "2.12 Helm: check lifecycle hook injection" { [[ ! -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]} + run try "at most 20 times every 10s to get pods named 'jenkins-jenkins' and verify that '.spec.containers[?(@.name==\"jenkins-master\")].lifecycle.preStop.exec.command[0]' is 'echo bats-test'" assert_success - assert_output "echo 'bats test'" } #bats test_tags=phase:helm,scenario:more-options -@test "2.12 Helm: check node selector again" { +@test "2.13 Helm: check node selector again" { [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" NODENAME=$(${KUBECTL} get pod jenkins-jenkins -o jsonpath={.spec.nodeName}) @@ -159,17 +158,32 @@ setup() { } #bats test_tags=phase:helm,scenario:more-options -@test "2.13 Helm: check jenkins-plugin-cli command again" { +@test "2.14 Helm: check jenkins-plugin-cli command again" { [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" - run ${KUBECTL} logs -c jenkins-master jenkins-jenkins + # Check logs for jenkins-plugin-cli command with retry. + # Retry is necessary here to reduce flakiness due to additional delays + # from reconciling and recreating jenkins pods after helm upgrade. + # We assert success only after the retry loop to reduce noise. + LOG_CMD="${KUBECTL} logs -c jenkins-master jenkins-jenkins" + + EXPECTED_LOG_LINE_BASE_PLUGINS="jenkins-plugin-cli --verbose --latest true -f /var/lib/jenkins/base-plugins.txt" + retry 10 10 "${LOG_CMD} | grep -e '${EXPECTED_LOG_LINE_BASE_PLUGINS}'" + + run $LOG_CMD assert_success - assert_output --partial 'jenkins-plugin-cli --verbose --latest true -f /var/lib/jenkins/base-plugins.txt' - assert_output --partial 'jenkins-plugin-cli --verbose --latest true -f /var/lib/jenkins/user-plugins.txt' + assert_output --partial "${EXPECTED_LOG_LINE_BASE_PLUGINS}" + + EXPECTED_LOG_LINE_USER_PLUGINS="jenkins-plugin-cli --verbose --latest true -f /var/lib/jenkins/user-plugins.txt" + retry 10 10 "${LOG_CMD} | grep -e '${EXPECTED_LOG_LINE_USER_PLUGINS}'" + + run $LOG_CMD + assert_success + assert_output --partial "${EXPECTED_LOG_LINE_USER_PLUGINS}" } #bats test_tags=phase:helm,scenario:more-options -@test "2.14 Helm: clean" { +@test "2.15 Helm: clean" { [[ ! -f "chart/jenkins-operator/deploy.tmp" ]] && skip "Jenkins helm chart have not been deployed correctly" run ${HELM} uninstall options --wait diff --git a/test/bats/test_helper.bash b/test/bats/test_helper.bash index 25977629..686ca58d 100644 --- a/test/bats/test_helper.bash +++ b/test/bats/test_helper.bash @@ -15,3 +15,36 @@ _common_setup() { get_latest_chart_version() { helm search repo jenkins-operator/jenkins --versions | awk 'NR==2 {print $2}' | sed 's/v//' } + +retry() { + # based on bats-detik's try function + + if [[ $# -ne 3 ]]; then + echo "[ERROR] Usage: retry " + return 1 + fi + + local times="$1" + local delay="$2" + local cmd="$3" + + code=0 + for ((i=1; i<=times; i++)); do + + # Run the command + eval "$cmd" && code=$? || code=$? + + # Break the loop prematurely? + if [[ "$code" == "0" ]]; then + break + elif [[ "$i" != "1" ]]; then + code=3 + sleep "$delay" + else + code=3 + fi + done + + ## Error code + return $code +}