diff --git a/.github/md_config.json b/.github/md_config.json index 465a3097..37715e39 100644 --- a/.github/md_config.json +++ b/.github/md_config.json @@ -3,5 +3,6 @@ { "pattern": "^(?!http).+" } - ] + ], + "retryOn429": true } diff --git a/.github/workflows/pull_request.yaml b/.github/workflows/pull_request.yaml index 01ab96e7..e4b1c6f2 100644 --- a/.github/workflows/pull_request.yaml +++ b/.github/workflows/pull_request.yaml @@ -80,11 +80,7 @@ jobs: make install - name: Run golangci-lint - uses: golangci/golangci-lint-action@v6 - with: - version: latest - only-new-issues: false - args: --timeout 10m + run: make lint - name: Helm Lint run: | diff --git a/.github/workflows/push-pr-image.yaml b/.github/workflows/push-pr-image.yaml index 9b4c3326..eff22f73 100644 --- a/.github/workflows/push-pr-image.yaml +++ b/.github/workflows/push-pr-image.yaml @@ -47,11 +47,7 @@ jobs: make install - name: Run golangci-lint - uses: golangci/golangci-lint-action@v6 - with: - version: latest - only-new-issues: false - args: --timeout 10m + run: make lint - name: Generate Tags id: generate_tag diff --git a/.github/workflows/push.yaml b/.github/workflows/push.yaml index 5dc7cf3c..dda9a1c1 100644 --- a/.github/workflows/push.yaml +++ b/.github/workflows/push.yaml @@ -53,11 +53,7 @@ jobs: make install - name: Run golangci-lint - uses: golangci/golangci-lint-action@v6 - with: - version: latest - only-new-issues: false - args: --timeout 10m + run: make lint - name: Install kubectl run: | diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 5fcc4654..6bd392f7 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -48,11 +48,7 @@ jobs: make install - name: Run golangci-lint - uses: golangci/golangci-lint-action@v6 - with: - version: latest - only-new-issues: false - args: --timeout 10m + run: make lint - name: Install kubectl run: | diff --git a/Dockerfile b/Dockerfile index 9708df58..8b4715c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ ARG BUILDER_IMAGE ARG BASE_IMAGE # Build the manager binary -FROM --platform=${BUILDPLATFORM} ${BUILDER_IMAGE:-golang:1.24.9} AS builder +FROM --platform=${BUILDPLATFORM} ${BUILDER_IMAGE:-golang:1.25.3} AS builder ARG TARGETOS ARG TARGETARCH diff --git a/Makefile b/Makefile index d8a3ec97..8444e1f7 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ YQ ?= $(LOCALBIN)/yq KUSTOMIZE_VERSION ?= v5.3.0 CONTROLLER_TOOLS_VERSION ?= v0.14.0 ENVTEST_VERSION ?= release-0.17 -GOLANGCI_LINT_VERSION ?= v1.57.2 +GOLANGCI_LINT_VERSION ?= v2.6.1 YQ_VERSION ?= v4.27.5 YQ_DOWNLOAD_URL = "https://github.com/mikefarah/yq/releases/download/$(YQ_VERSION)/yq_$(OS)_$(ARCH)" @@ -75,7 +75,7 @@ $(ENVTEST): $(LOCALBIN) .PHONY: golangci-lint golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary. $(GOLANGCI_LINT): $(LOCALBIN) - $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,${GOLANGCI_LINT_VERSION}) + $(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/v2/cmd/golangci-lint,${GOLANGCI_LINT_VERSION}) # go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist # $1 - target path with name of binary (ideally with version) @@ -102,6 +102,9 @@ run: build: "$(GOCMD)" build ${GOFLAGS} ${LDFLAGS} -o "${BINARY}" +lint: golangci-lint ## Run golangci-lint on the codebase + $(GOLANGCI_LINT) run ./... + build-image: docker buildx build \ --platform ${OS}/${ARCH} \ diff --git a/README.md b/README.md index 5a75ea07..ea1c77c9 100644 --- a/README.md +++ b/README.md @@ -433,11 +433,14 @@ _Repository GitHub releases_: As requested by the community in [issue 685](https To make a GitHub release: -1. Code owners create a release branch `release-vX.Y.Z` -1. Code owners run a dispatch mode workflow to automatically generate version and manifests on the release branch +1. Code owners create a release branch `release-vX.Y.Z` from `master` +1. Code owners run [Init Release](https://github.com/stakater/Reloader/actions/workflows/init-branch-release.yaml) workflow to automatically generate version and manifests on the release branch + - Set the `TARGET_BRANCH` parameter to release branch i.e. `release-vX.Y.Z` + - Set the `TARGET_VERSION` to release version without 'v' i.e. `X.Y.Z` 1. A PR is created to bump the image version on the release branch, example: [PR-798](https://github.com/stakater/Reloader/pull/798) 1. Code owners create a GitHub release with tag `vX.Y.Z` and target branch `release-vX.Y.Z`, which triggers creation of images -1. Code owners create a PR with `release/helm-chart` label to update the Helm chart version, example: [PR-846](https://github.com/stakater/Reloader/pull/846) +1. Code owners create another branch from `master` and bump the helm chart version as well as Reloader image version. + - Code owners create a PR with `release/helm-chart` label, example: [PR-846](https://github.com/stakater/Reloader/pull/846) _Repository git tagging_: Push to the main branch will create a merge-image and merge-tag named `merge-${{ github.event.number }}`, for example `merge-800` when pull request number 800 is merged. diff --git a/go.mod b/go.mod index d1160bf5..f3a9f824 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/stakater/Reloader -go 1.24.9 +go 1.25.3 require ( github.com/argoproj/argo-rollouts v1.8.3 diff --git a/go.sum b/go.sum index 1a469e0e..59339eaf 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/argoproj/argo-rollouts v1.8.2 h1:DBvkYvFTEH/zJ9MxJerqz/NMWEgZcHY5vxztyCBS5ak= -github.com/argoproj/argo-rollouts v1.8.2/go.mod h1:xZIw+dg+B4IqMv5fNPenIBUiPb9xljL2st1xxkjhaC0= github.com/argoproj/argo-rollouts v1.8.3 h1:blbtQva4IK9r6gFh+dWkCrLnFdPOWiv9ubQYu36qeaA= github.com/argoproj/argo-rollouts v1.8.3/go.mod h1:kCAUvIfMGfOyVf3lvQbBt0nqQn4Pd+zB5/YwKv+UBa8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= @@ -106,12 +104,8 @@ github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo= github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg= github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM= -github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo= -github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0= github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s= github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0= -github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= -github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/internal/pkg/alerts/alert.go b/internal/pkg/alerts/alert.go index 57914486..6b9568ff 100644 --- a/internal/pkg/alerts/alert.go +++ b/internal/pkg/alerts/alert.go @@ -9,6 +9,15 @@ import ( "github.com/sirupsen/logrus" ) +type AlertSink string + +const ( + AlertSinkSlack AlertSink = "slack" + AlertSinkTeams AlertSink = "teams" + AlertSinkGoogleChat AlertSink = "gchat" + AlertSinkRaw AlertSink = "raw" +) + // function to send alert msg to webhook service func SendWebhookAlert(msg string) { webhook_url, ok := os.LookupEnv("ALERT_WEBHOOK_URL") @@ -31,14 +40,15 @@ func SendWebhookAlert(msg string) { msg = fmt.Sprintf("%s : %s", alert_additional_info, msg) } - if alert_sink == "slack" { + switch AlertSink(alert_sink) { + case AlertSinkSlack: sendSlackAlert(webhook_url, webhook_proxy, msg) - } else if alert_sink == "teams" { + case AlertSinkTeams: sendTeamsAlert(webhook_url, webhook_proxy, msg) - } else if alert_sink == "gchat" { + case AlertSinkGoogleChat: sendGoogleChatAlert(webhook_url, webhook_proxy, msg) - } else { - msg = strings.Replace(msg, "*", "", -1) + default: + msg = strings.ReplaceAll(msg, "*", "") sendRawWebhookAlert(webhook_url, webhook_proxy, msg) } } diff --git a/internal/pkg/callbacks/rolling_upgrade.go b/internal/pkg/callbacks/rolling_upgrade.go index c7b2e5cc..13e5a63c 100644 --- a/internal/pkg/callbacks/rolling_upgrade.go +++ b/internal/pkg/callbacks/rolling_upgrade.go @@ -83,9 +83,9 @@ func GetDeploymentItem(clients kube.Clients, name string, namespace string) (run return nil, err } - if deployment.Spec.Template.ObjectMeta.Annotations == nil { + if deployment.Spec.Template.Annotations == nil { annotations := make(map[string]string) - deployment.Spec.Template.ObjectMeta.Annotations = annotations + deployment.Spec.Template.Annotations = annotations } return deployment, nil @@ -101,9 +101,9 @@ func GetDeploymentItems(clients kube.Clients, namespace string) []runtime.Object items := make([]runtime.Object, len(deployments.Items)) // Ensure we always have pod annotations to add to for i, v := range deployments.Items { - if v.Spec.Template.ObjectMeta.Annotations == nil { + if v.Spec.Template.Annotations == nil { annotations := make(map[string]string) - deployments.Items[i].Spec.Template.ObjectMeta.Annotations = annotations + deployments.Items[i].Spec.Template.Annotations = annotations } items[i] = &deployments.Items[i] } @@ -132,9 +132,9 @@ func GetCronJobItems(clients kube.Clients, namespace string) []runtime.Object { items := make([]runtime.Object, len(cronjobs.Items)) // Ensure we always have pod annotations to add to for i, v := range cronjobs.Items { - if v.Spec.JobTemplate.Spec.Template.ObjectMeta.Annotations == nil { + if v.Spec.JobTemplate.Spec.Template.Annotations == nil { annotations := make(map[string]string) - cronjobs.Items[i].Spec.JobTemplate.Spec.Template.ObjectMeta.Annotations = annotations + cronjobs.Items[i].Spec.JobTemplate.Spec.Template.Annotations = annotations } items[i] = &cronjobs.Items[i] } @@ -163,9 +163,9 @@ func GetJobItems(clients kube.Clients, namespace string) []runtime.Object { items := make([]runtime.Object, len(jobs.Items)) // Ensure we always have pod annotations to add to for i, v := range jobs.Items { - if v.Spec.Template.ObjectMeta.Annotations == nil { + if v.Spec.Template.Annotations == nil { annotations := make(map[string]string) - jobs.Items[i].Spec.Template.ObjectMeta.Annotations = annotations + jobs.Items[i].Spec.Template.Annotations = annotations } items[i] = &jobs.Items[i] } @@ -194,8 +194,8 @@ func GetDaemonSetItems(clients kube.Clients, namespace string) []runtime.Object items := make([]runtime.Object, len(daemonSets.Items)) // Ensure we always have pod annotations to add to for i, v := range daemonSets.Items { - if v.Spec.Template.ObjectMeta.Annotations == nil { - daemonSets.Items[i].Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if v.Spec.Template.Annotations == nil { + daemonSets.Items[i].Spec.Template.Annotations = make(map[string]string) } items[i] = &daemonSets.Items[i] } @@ -224,8 +224,8 @@ func GetStatefulSetItems(clients kube.Clients, namespace string) []runtime.Objec items := make([]runtime.Object, len(statefulSets.Items)) // Ensure we always have pod annotations to add to for i, v := range statefulSets.Items { - if v.Spec.Template.ObjectMeta.Annotations == nil { - statefulSets.Items[i].Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if v.Spec.Template.Annotations == nil { + statefulSets.Items[i].Spec.Template.Annotations = make(map[string]string) } items[i] = &statefulSets.Items[i] } @@ -254,8 +254,8 @@ func GetRolloutItems(clients kube.Clients, namespace string) []runtime.Object { items := make([]runtime.Object, len(rollouts.Items)) // Ensure we always have pod annotations to add to for i, v := range rollouts.Items { - if v.Spec.Template.ObjectMeta.Annotations == nil { - rollouts.Items[i].Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if v.Spec.Template.Annotations == nil { + rollouts.Items[i].Spec.Template.Annotations = make(map[string]string) } items[i] = &rollouts.Items[i] } @@ -265,98 +265,98 @@ func GetRolloutItems(clients kube.Clients, namespace string) []runtime.Object { // GetDeploymentAnnotations returns the annotations of given deployment func GetDeploymentAnnotations(item runtime.Object) map[string]string { - if item.(*appsv1.Deployment).ObjectMeta.Annotations == nil { - item.(*appsv1.Deployment).ObjectMeta.Annotations = make(map[string]string) + if item.(*appsv1.Deployment).Annotations == nil { + item.(*appsv1.Deployment).Annotations = make(map[string]string) } - return item.(*appsv1.Deployment).ObjectMeta.Annotations + return item.(*appsv1.Deployment).Annotations } // GetCronJobAnnotations returns the annotations of given cronjob func GetCronJobAnnotations(item runtime.Object) map[string]string { - if item.(*batchv1.CronJob).ObjectMeta.Annotations == nil { - item.(*batchv1.CronJob).ObjectMeta.Annotations = make(map[string]string) + if item.(*batchv1.CronJob).Annotations == nil { + item.(*batchv1.CronJob).Annotations = make(map[string]string) } - return item.(*batchv1.CronJob).ObjectMeta.Annotations + return item.(*batchv1.CronJob).Annotations } // GetJobAnnotations returns the annotations of given job func GetJobAnnotations(item runtime.Object) map[string]string { - if item.(*batchv1.Job).ObjectMeta.Annotations == nil { - item.(*batchv1.Job).ObjectMeta.Annotations = make(map[string]string) + if item.(*batchv1.Job).Annotations == nil { + item.(*batchv1.Job).Annotations = make(map[string]string) } - return item.(*batchv1.Job).ObjectMeta.Annotations + return item.(*batchv1.Job).Annotations } // GetDaemonSetAnnotations returns the annotations of given daemonSet func GetDaemonSetAnnotations(item runtime.Object) map[string]string { - if item.(*appsv1.DaemonSet).ObjectMeta.Annotations == nil { - item.(*appsv1.DaemonSet).ObjectMeta.Annotations = make(map[string]string) + if item.(*appsv1.DaemonSet).Annotations == nil { + item.(*appsv1.DaemonSet).Annotations = make(map[string]string) } - return item.(*appsv1.DaemonSet).ObjectMeta.Annotations + return item.(*appsv1.DaemonSet).Annotations } // GetStatefulSetAnnotations returns the annotations of given statefulSet func GetStatefulSetAnnotations(item runtime.Object) map[string]string { - if item.(*appsv1.StatefulSet).ObjectMeta.Annotations == nil { - item.(*appsv1.StatefulSet).ObjectMeta.Annotations = make(map[string]string) + if item.(*appsv1.StatefulSet).Annotations == nil { + item.(*appsv1.StatefulSet).Annotations = make(map[string]string) } - return item.(*appsv1.StatefulSet).ObjectMeta.Annotations + return item.(*appsv1.StatefulSet).Annotations } // GetRolloutAnnotations returns the annotations of given rollout func GetRolloutAnnotations(item runtime.Object) map[string]string { - if item.(*argorolloutv1alpha1.Rollout).ObjectMeta.Annotations == nil { - item.(*argorolloutv1alpha1.Rollout).ObjectMeta.Annotations = make(map[string]string) + if item.(*argorolloutv1alpha1.Rollout).Annotations == nil { + item.(*argorolloutv1alpha1.Rollout).Annotations = make(map[string]string) } - return item.(*argorolloutv1alpha1.Rollout).ObjectMeta.Annotations + return item.(*argorolloutv1alpha1.Rollout).Annotations } // GetDeploymentPodAnnotations returns the pod's annotations of given deployment func GetDeploymentPodAnnotations(item runtime.Object) map[string]string { - if item.(*appsv1.Deployment).Spec.Template.ObjectMeta.Annotations == nil { - item.(*appsv1.Deployment).Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if item.(*appsv1.Deployment).Spec.Template.Annotations == nil { + item.(*appsv1.Deployment).Spec.Template.Annotations = make(map[string]string) } - return item.(*appsv1.Deployment).Spec.Template.ObjectMeta.Annotations + return item.(*appsv1.Deployment).Spec.Template.Annotations } // GetCronJobPodAnnotations returns the pod's annotations of given cronjob func GetCronJobPodAnnotations(item runtime.Object) map[string]string { - if item.(*batchv1.CronJob).Spec.JobTemplate.Spec.Template.ObjectMeta.Annotations == nil { - item.(*batchv1.CronJob).Spec.JobTemplate.Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if item.(*batchv1.CronJob).Spec.JobTemplate.Spec.Template.Annotations == nil { + item.(*batchv1.CronJob).Spec.JobTemplate.Spec.Template.Annotations = make(map[string]string) } - return item.(*batchv1.CronJob).Spec.JobTemplate.Spec.Template.ObjectMeta.Annotations + return item.(*batchv1.CronJob).Spec.JobTemplate.Spec.Template.Annotations } // GetJobPodAnnotations returns the pod's annotations of given job func GetJobPodAnnotations(item runtime.Object) map[string]string { - if item.(*batchv1.Job).Spec.Template.ObjectMeta.Annotations == nil { - item.(*batchv1.Job).Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if item.(*batchv1.Job).Spec.Template.Annotations == nil { + item.(*batchv1.Job).Spec.Template.Annotations = make(map[string]string) } - return item.(*batchv1.Job).Spec.Template.ObjectMeta.Annotations + return item.(*batchv1.Job).Spec.Template.Annotations } // GetDaemonSetPodAnnotations returns the pod's annotations of given daemonSet func GetDaemonSetPodAnnotations(item runtime.Object) map[string]string { - if item.(*appsv1.DaemonSet).Spec.Template.ObjectMeta.Annotations == nil { - item.(*appsv1.DaemonSet).Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if item.(*appsv1.DaemonSet).Spec.Template.Annotations == nil { + item.(*appsv1.DaemonSet).Spec.Template.Annotations = make(map[string]string) } - return item.(*appsv1.DaemonSet).Spec.Template.ObjectMeta.Annotations + return item.(*appsv1.DaemonSet).Spec.Template.Annotations } // GetStatefulSetPodAnnotations returns the pod's annotations of given statefulSet func GetStatefulSetPodAnnotations(item runtime.Object) map[string]string { - if item.(*appsv1.StatefulSet).Spec.Template.ObjectMeta.Annotations == nil { - item.(*appsv1.StatefulSet).Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if item.(*appsv1.StatefulSet).Spec.Template.Annotations == nil { + item.(*appsv1.StatefulSet).Spec.Template.Annotations = make(map[string]string) } - return item.(*appsv1.StatefulSet).Spec.Template.ObjectMeta.Annotations + return item.(*appsv1.StatefulSet).Spec.Template.Annotations } // GetRolloutPodAnnotations returns the pod's annotations of given rollout func GetRolloutPodAnnotations(item runtime.Object) map[string]string { - if item.(*argorolloutv1alpha1.Rollout).Spec.Template.ObjectMeta.Annotations == nil { - item.(*argorolloutv1alpha1.Rollout).Spec.Template.ObjectMeta.Annotations = make(map[string]string) + if item.(*argorolloutv1alpha1.Rollout).Spec.Template.Annotations == nil { + item.(*argorolloutv1alpha1.Rollout).Spec.Template.Annotations = make(map[string]string) } - return item.(*argorolloutv1alpha1.Rollout).Spec.Template.ObjectMeta.Annotations + return item.(*argorolloutv1alpha1.Rollout).Spec.Template.Annotations } // GetDeploymentContainers returns the containers of given deployment @@ -481,9 +481,9 @@ func ReCreateJobFromjob(clients kube.Clients, namespace string, resource runtime } // Remove fields that should not be specified when creating a new Job - job.ObjectMeta.ResourceVersion = "" - job.ObjectMeta.UID = "" - job.ObjectMeta.CreationTimestamp = meta_v1.Time{} + job.ResourceVersion = "" + job.UID = "" + job.CreationTimestamp = meta_v1.Time{} job.Status = batchv1.JobStatus{} // Remove problematic labels diff --git a/internal/pkg/callbacks/rolling_upgrade_test.go b/internal/pkg/callbacks/rolling_upgrade_test.go index b9f48f3f..452867f4 100644 --- a/internal/pkg/callbacks/rolling_upgrade_test.go +++ b/internal/pkg/callbacks/rolling_upgrade_test.go @@ -373,19 +373,19 @@ func TestPatchResources(t *testing.T) { assert.NoError(t, err) patchedResource, err := callbacks.GetDeploymentItem(clients, "test-deployment", fixtures.namespace) assert.NoError(t, err) - assert.Equal(t, "test", patchedResource.(*appsv1.Deployment).ObjectMeta.Annotations["test"]) + assert.Equal(t, "test", patchedResource.(*appsv1.Deployment).Annotations["test"]) }}, {"DaemonSet", createTestDaemonSetWithAnnotations, callbacks.PatchDaemonSet, deleteTestDaemonSet, func(err error) { assert.NoError(t, err) patchedResource, err := callbacks.GetDaemonSetItem(clients, "test-daemonset", fixtures.namespace) assert.NoError(t, err) - assert.Equal(t, "test", patchedResource.(*appsv1.DaemonSet).ObjectMeta.Annotations["test"]) + assert.Equal(t, "test", patchedResource.(*appsv1.DaemonSet).Annotations["test"]) }}, {"StatefulSet", createTestStatefulSetWithAnnotations, callbacks.PatchStatefulSet, deleteTestStatefulSet, func(err error) { assert.NoError(t, err) patchedResource, err := callbacks.GetStatefulSetItem(clients, "test-statefulset", fixtures.namespace) assert.NoError(t, err) - assert.Equal(t, "test", patchedResource.(*appsv1.StatefulSet).ObjectMeta.Annotations["test"]) + assert.Equal(t, "test", patchedResource.(*appsv1.StatefulSet).Annotations["test"]) }}, {"CronJob", createTestCronJobWithAnnotations, callbacks.PatchCronJob, deleteTestCronJob, func(err error) { assert.EqualError(t, err, "not supported patching: CronJob") @@ -621,17 +621,17 @@ func deleteTestStatefulSets(clients kube.Clients, namespace string) error { func createResourceWithPodAnnotations(obj runtime.Object, annotations map[string]string) runtime.Object { switch v := obj.(type) { case *appsv1.Deployment: - v.Spec.Template.ObjectMeta.Annotations = annotations + v.Spec.Template.Annotations = annotations case *appsv1.DaemonSet: - v.Spec.Template.ObjectMeta.Annotations = annotations + v.Spec.Template.Annotations = annotations case *appsv1.StatefulSet: - v.Spec.Template.ObjectMeta.Annotations = annotations + v.Spec.Template.Annotations = annotations case *batchv1.CronJob: - v.Spec.JobTemplate.Spec.Template.ObjectMeta.Annotations = annotations + v.Spec.JobTemplate.Spec.Template.Annotations = annotations case *batchv1.Job: - v.Spec.Template.ObjectMeta.Annotations = annotations + v.Spec.Template.Annotations = annotations case *argorolloutv1alpha1.Rollout: - v.Spec.Template.ObjectMeta.Annotations = annotations + v.Spec.Template.Annotations = annotations } return obj } diff --git a/internal/pkg/controller/controller.go b/internal/pkg/controller/controller.go index 7dc7664e..15b2e0f1 100644 --- a/internal/pkg/controller/controller.go +++ b/internal/pkg/controller/controller.go @@ -124,9 +124,9 @@ func (c *Controller) Add(obj interface{}) { func (c *Controller) resourceInIgnoredNamespace(raw interface{}) bool { switch object := raw.(type) { case *v1.ConfigMap: - return c.ignoredNamespaces.Contains(object.ObjectMeta.Namespace) + return c.ignoredNamespaces.Contains(object.Namespace) case *v1.Secret: - return c.ignoredNamespaces.Contains(object.ObjectMeta.Namespace) + return c.ignoredNamespaces.Contains(object.Namespace) } return false } @@ -212,7 +212,7 @@ func (c *Controller) Run(threadiness int, stopCh chan struct{}) { // Wait for all involved caches to be synced, before processing items from the queue is started if !cache.WaitForCacheSync(stopCh, c.informer.HasSynced) { - runtime.HandleError(fmt.Errorf("Timed out waiting for caches to sync")) + runtime.HandleError(fmt.Errorf("timed out waiting for caches to sync")) return } @@ -226,9 +226,9 @@ func (c *Controller) Run(threadiness int, stopCh chan struct{}) { func (c *Controller) runWorker() { // At this point the controller is fully initialized and we can start processing the resources - if c.resource == "secrets" { + if c.resource == string(v1.ResourceSecrets) { secretControllerInitialized = true - } else if c.resource == "configMaps" { + } else if c.resource == string(v1.ResourceConfigMaps) { configmapControllerInitialized = true } diff --git a/internal/pkg/controller/controller_test.go b/internal/pkg/controller/controller_test.go index 17eff947..63e6be3e 100644 --- a/internal/pkg/controller/controller_test.go +++ b/internal/pkg/controller/controller_test.go @@ -2341,7 +2341,7 @@ func TestController_resourceInNamespaceSelector(t *testing.T) { indexer: tt.fields.indexer, queue: tt.fields.queue, informer: tt.fields.informer, - namespace: tt.fields.namespace.ObjectMeta.Name, + namespace: tt.fields.namespace.Name, namespaceSelector: tt.fields.namespaceSelector, } diff --git a/internal/pkg/handler/pause_deployment_test.go b/internal/pkg/handler/pause_deployment_test.go index 076b0b25..c14cbfcb 100644 --- a/internal/pkg/handler/pause_deployment_test.go +++ b/internal/pkg/handler/pause_deployment_test.go @@ -9,7 +9,6 @@ import ( "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/pkg/kube" "github.com/stretchr/testify/assert" - app "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -374,14 +373,14 @@ func TestPauseDeployment(t *testing.T) { } // Simple helper function for test cases -func FindDeploymentByName(deployments []runtime.Object, deploymentName string) (*app.Deployment, error) { +func FindDeploymentByName(deployments []runtime.Object, deploymentName string) (*appsv1.Deployment, error) { for _, deployment := range deployments { accessor, err := meta.Accessor(deployment) if err != nil { return nil, fmt.Errorf("error getting accessor for item: %v", err) } if accessor.GetName() == deploymentName { - deploymentObj, ok := deployment.(*app.Deployment) + deploymentObj, ok := deployment.(*appsv1.Deployment) if !ok { return nil, fmt.Errorf("failed to cast to Deployment") } diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index 9f661dab..6f185f1e 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -160,7 +160,12 @@ func sendWebhook(url string) (string, []error) { // the reloader seems to retry automatically so no retry logic added return "", err } - defer resp.Body.Close() + defer func() { + closeErr := resp.Body.Close() + if closeErr != nil { + logrus.Error(closeErr) + } + }() var buffer bytes.Buffer _, bufferErr := io.Copy(&buffer, resp.Body) if bufferErr != nil { @@ -350,7 +355,8 @@ func upgradeResource(clients kube.Clients, config common.Config, upgradeFuncs ca func getVolumeMountName(volumes []v1.Volume, mountType string, volumeName string) string { for i := range volumes { - if mountType == constants.ConfigmapEnvVarPostfix { + switch mountType { + case constants.ConfigmapEnvVarPostfix: if volumes[i].ConfigMap != nil && volumes[i].ConfigMap.Name == volumeName { return volumes[i].Name } @@ -362,7 +368,7 @@ func getVolumeMountName(volumes []v1.Volume, mountType string, volumeName string } } } - } else if mountType == constants.SecretEnvVarPostfix { + case constants.SecretEnvVarPostfix: if volumes[i].Secret != nil && volumes[i].Secret.SecretName == volumeName { return volumes[i].Name } @@ -399,9 +405,9 @@ func getContainerWithEnvReference(containers []v1.Container, resourceName string for j := range envs { envVarSource := envs[j].ValueFrom if envVarSource != nil { - if resourceType == constants.SecretEnvVarPostfix && envVarSource.SecretKeyRef != nil && envVarSource.SecretKeyRef.LocalObjectReference.Name == resourceName { + if resourceType == constants.SecretEnvVarPostfix && envVarSource.SecretKeyRef != nil && envVarSource.SecretKeyRef.Name == resourceName { return &containers[i] - } else if resourceType == constants.ConfigmapEnvVarPostfix && envVarSource.ConfigMapKeyRef != nil && envVarSource.ConfigMapKeyRef.LocalObjectReference.Name == resourceName { + } else if resourceType == constants.ConfigmapEnvVarPostfix && envVarSource.ConfigMapKeyRef != nil && envVarSource.ConfigMapKeyRef.Name == resourceName { return &containers[i] } } @@ -409,9 +415,9 @@ func getContainerWithEnvReference(containers []v1.Container, resourceName string envsFrom := containers[i].EnvFrom for j := range envsFrom { - if resourceType == constants.SecretEnvVarPostfix && envsFrom[j].SecretRef != nil && envsFrom[j].SecretRef.LocalObjectReference.Name == resourceName { + if resourceType == constants.SecretEnvVarPostfix && envsFrom[j].SecretRef != nil && envsFrom[j].SecretRef.Name == resourceName { return &containers[i] - } else if resourceType == constants.ConfigmapEnvVarPostfix && envsFrom[j].ConfigMapRef != nil && envsFrom[j].ConfigMapRef.LocalObjectReference.Name == resourceName { + } else if resourceType == constants.ConfigmapEnvVarPostfix && envsFrom[j].ConfigMapRef != nil && envsFrom[j].ConfigMapRef.Name == resourceName { return &containers[i] } } diff --git a/internal/pkg/testutil/kube.go b/internal/pkg/testutil/kube.go index beaa2978..1ad43e18 100644 --- a/internal/pkg/testutil/kube.go +++ b/internal/pkg/testutil/kube.go @@ -94,7 +94,7 @@ func getAnnotations(name string, autoReload bool, secretAutoReload bool, configm annotations[options.ConfigmapReloaderAutoAnnotation] = "true" } - if !(len(annotations) > 0) { + if len(annotations) == 0 { annotations = map[string]string{ options.ConfigmapUpdateOnChangeAnnotation: name, options.SecretUpdateOnChangeAnnotation: name} @@ -479,18 +479,19 @@ func GetDeploymentWithPodAnnotations(namespace string, deploymentName string, bo }, } if !both { - deployment.ObjectMeta.Annotations = nil + deployment.Annotations = nil } - deployment.Spec.Template.ObjectMeta.Annotations = getAnnotations(deploymentName, true, false, false, map[string]string{}) + deployment.Spec.Template.Annotations = getAnnotations(deploymentName, true, false, false, map[string]string{}) return deployment } func GetDeploymentWithTypedAutoAnnotation(namespace string, deploymentName string, resourceType string) *appsv1.Deployment { replicaset := int32(1) var objectMeta metav1.ObjectMeta - if resourceType == SecretResourceType { + switch resourceType { + case SecretResourceType: objectMeta = getObjectMeta(namespace, deploymentName, false, true, false, map[string]string{}) - } else if resourceType == ConfigmapResourceType { + case ConfigmapResourceType: objectMeta = getObjectMeta(namespace, deploymentName, false, false, true, map[string]string{}) } @@ -514,9 +515,10 @@ func GetDeploymentWithExcludeAnnotation(namespace string, deploymentName string, annotation := map[string]string{} - if resourceType == SecretResourceType { + switch resourceType { + case SecretResourceType: annotation[options.SecretExcludeReloaderAnnotation] = deploymentName - } else if resourceType == ConfigmapResourceType { + case ConfigmapResourceType: annotation[options.ConfigmapExcludeReloaderAnnotation] = deploymentName } @@ -747,12 +749,13 @@ func GetResourceSHAFromAnnotation(podAnnotations map[string]string) string { // ConvertResourceToSHA generates SHA from secret or configmap data func ConvertResourceToSHA(resourceType string, namespace string, resourceName string, data string) string { values := []string{} - if resourceType == SecretResourceType { + switch resourceType { + case SecretResourceType: secret := GetSecret(namespace, resourceName, data) for k, v := range secret.Data { values = append(values, k+"="+string(v[:])) } - } else if resourceType == ConfigmapResourceType { + case ConfigmapResourceType: configmap := GetConfigmap(namespace, resourceName, data) for k, v := range configmap.Data { values = append(values, k+"="+v)