From 75b00733bf8612c5f96feff179f65a14ba27164e Mon Sep 17 00:00:00 2001 From: Joey Espinosa Date: Thu, 31 Jan 2019 16:14:16 -0500 Subject: [PATCH 1/3] feat: parameterize all annotations --- README.md | 11 ++++-- docs/How-it-works.md | 6 ++- docs/Reloader-vs-ConfigmapController.md | 3 +- docs/Reloader-vs-k8s-trigger-controller.md | 14 ++++++- internal/pkg/cmd/reloader.go | 7 ++++ internal/pkg/controller/controller_test.go | 37 ++++++++++--------- internal/pkg/handler/upgrade.go | 3 +- internal/pkg/handler/upgrade_test.go | 23 ++++++------ .../annotations.go => options/flags.go} | 4 +- internal/pkg/testutil/kube.go | 9 +++-- internal/pkg/util/config.go | 5 ++- 11 files changed, 76 insertions(+), 46 deletions(-) rename internal/pkg/{constants/annotations.go => options/flags.go} (94%) diff --git a/README.md b/README.md index 02599d76..9441910b 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Reloader can watch changes in `ConfigMap` and `Secret` and do rolling upgrades o ## How to use Reloader -For a `Deployment` called `foo` have a `ConfigMap` called `foo-configmap` or `Secret` called `foo-secret` or both. Then add this annotation to main metadata of your `Deployment` +For a `Deployment` called `foo` have a `ConfigMap` called `foo-configmap` or `Secret` called `foo-secret` or both. Then add your annotation (by default `reloader.stakater.com/auto`) to main metadata of your `Deployment` ```yaml kind: Deployment @@ -36,7 +36,7 @@ spec: This will discover deployments/daemonsets/statefulset automatically where `foo-configmap` or `foo-secret` is being used either via environment variable or from volume mount. And it will perform rolling upgrade on related pods when `foo-configmap` or `foo-secret`are updated. We can also specify a specific configmap or secret which would trigger rolling upgrade only upon change in our specified configmap or secret, this way, it will not trigger rolling upgrade upon changes in all configmaps or secrets used in a deployment, daemonset or statefulset. -To do this either set `reloader.stakater.com/auto: "false"` or remove this annotation altogather, and use annotations mentioned [here](#Configmap) or [here](#Secret) +To do this either set the auto annotation to `"false"` (`reloader.stakater.com/auto: "false"`) or remove it altogether, and use annotations mentioned [here](#Configmap) or [here](#Secret) ### Configmap @@ -94,8 +94,11 @@ spec: metadata: ``` -### NOTE -`reloader.stakater.com/auto: "true"` will always override when use with either `secret.reloader.stakater.com/reload` or `configmap.reloader.stakater.com/reload` annotation. +### NOTES +- `reloader.stakater.com/auto: "true"` will always override when use with either `secret.reloader.stakater.com/reload` or `configmap.reloader.stakater.com/reload` annotation. +- you may override the auto annotation with the `--auto-annotation` flag +- you may override the configmap annotation with the `--configmap-annotation` flag +- you may override the secret annotation with the `--secret-annotation` flag ## Deploying to Kubernetes diff --git a/docs/How-it-works.md b/docs/How-it-works.md index e33240f4..f724e3cf 100644 --- a/docs/How-it-works.md +++ b/docs/How-it-works.md @@ -17,23 +17,25 @@ The annotation value is comma separated list of `configmaps` or `secrets`. If a ### Annotation for Configmap -For a `Deployment` called `foo` have a `ConfigMap` called `foo`. Then add this annotation to your `Deployment` +For a `Deployment` called `foo` have a `ConfigMap` called `foo`. Then add this annotation* to your `Deployment` ```yaml metadata: annotations: configmap.reloader.stakater.com/reload: "foo" ``` +*the default annotation can be changed with the `--configmap-annotation` flag ### Annotation for Secret -For a `Deployment` called `foo` have a `Secret` called `foo`. Then add this annotation to your `Deployment` +For a `Deployment` called `foo` have a `Secret` called `foo`. Then add this annotation* to your `Deployment` ```yaml metadata: annotations: secret.reloader.stakater.com/reload: "foo" ``` +*the default annotation can be changed with the `--secret-annotation` flag Above mentioned annotation are also work for `Daemonsets` and `Statefulsets` diff --git a/docs/Reloader-vs-ConfigmapController.md b/docs/Reloader-vs-ConfigmapController.md index 1f7d2918..8fee3dd4 100644 --- a/docs/Reloader-vs-ConfigmapController.md +++ b/docs/Reloader-vs-ConfigmapController.md @@ -8,4 +8,5 @@ Reloader is inspired from [Configmapcontroller](https://github.com/fabric8io/con | Reloader can watch both `secrets` and `configmaps`. | ConfigmapController can only watch changes in `configmaps`. It cannot detect changes in other resources like `secrets`. | | Reloader can perform rolling upgrades on `deployments` as well as on `statefulsets` and `daemonsets` | ConfigmapController can only perform rolling upgrades on `deployments`. It currently does not support rolling upgrades on `statefulsets` and `daemonsets` | | Reloader provides both unit test cases and end to end integration test cases for future updates. So one can make sure that new changes do not break any old functionality. | Currently there are not any unit test cases or end to end integration test cases in configmap controller. It add difficulties for any additional updates in configmap controller and one can not know for sure whether new changes breaks any old functionality or not. | -| Reloader uses SHA1 to encode the change in configmap or secret. It then saves the SHA1 value in `STAKATER_FOO_CONFIGMAP` or `STAKATER_FOO_SECRET` environment variable depending upon where the change has happened. The use of SHA1 provides a concise 40 characters encoded value that is very less pron to collision. | Configmap controller uses `FABRICB_FOO_REVISION` environment variable to store any change in configmap controller. It does not encode it or convert it in suitable hash value to avoid data pollution in deployment. | \ No newline at end of file +| Reloader uses SHA1 to encode the change in configmap or secret. It then saves the SHA1 value in `STAKATER_FOO_CONFIGMAP` or `STAKATER_FOO_SECRET` environment variable depending upon where the change has happened. The use of SHA1 provides a concise 40 characters encoded value that is very less pron to collision. | Configmap controller uses `FABRICB_FOO_REVISION` environment variable to store any change in configmap controller. It does not encode it or convert it in suitable hash value to avoid data pollution in deployment. | +| Reloader allows you to customize your own annotation (for both Secrets and Configmaps) using command line flags | Configmap controller restricts you to only their provided annotation | \ No newline at end of file diff --git a/docs/Reloader-vs-k8s-trigger-controller.md b/docs/Reloader-vs-k8s-trigger-controller.md index f6bb564a..e9b64a93 100644 --- a/docs/Reloader-vs-k8s-trigger-controller.md +++ b/docs/Reloader-vs-k8s-trigger-controller.md @@ -25,4 +25,16 @@ Reloader supports deployment rollout as well as daemonsets and statefulsets roll k8s-trigger-controller stores the hash value in an annotation `trigger.k8s.io/[secret|configMap]-NAME-last-hash` #### Reloader: -Reloader stores the hash value in an environment variable `STAKATER_NAME_[SECRET|CONFIGMAP]` \ No newline at end of file +Reloader stores the hash value in an environment variable `STAKATER_NAME_[SECRET|CONFIGMAP]` + +### Customization + +#### k8s-trigger-controller: +k8s-trigger-controller restricts you to using the `trigger.k8s.io/[secret-configMap]-NAME-last-hash` annotation + +#### Reloader: +Reloader allows you to customize the annotation to fit your needs with command line flags: + +- `--auto-annotation ` +- `--configmap-annotation ` +- `--secret-annotation ` \ No newline at end of file diff --git a/internal/pkg/cmd/reloader.go b/internal/pkg/cmd/reloader.go index 05eceba3..70750527 100644 --- a/internal/pkg/cmd/reloader.go +++ b/internal/pkg/cmd/reloader.go @@ -6,6 +6,7 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/stakater/Reloader/internal/pkg/controller" + "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/pkg/kube" "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -17,6 +18,12 @@ func NewReloaderCommand() *cobra.Command { Short: "A watcher for your Kubernetes cluster", Run: startReloader, } + + // options + cmd.PersistentFlags().StringVar(&options.ConfigmapUpdateOnChangeAnnotation, "configmap-annotation", "configmap.reloader.stakater.com/reload", "annotation to detect changes in configmaps") + cmd.PersistentFlags().StringVar(&options.SecretUpdateOnChangeAnnotation, "secret-annotation", "secret.reloader.stakater.com/reload", "annotation to detect changes in secrets") + cmd.PersistentFlags().StringVar(&options.ReloaderAutoAnnotation, "auto-annotation", "reloader.stakater.com/auto", "annotation to detect changes in secrets") + return cmd } diff --git a/internal/pkg/controller/controller_test.go b/internal/pkg/controller/controller_test.go index ee06f832..624a9ed9 100644 --- a/internal/pkg/controller/controller_test.go +++ b/internal/pkg/controller/controller_test.go @@ -8,6 +8,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stakater/Reloader/internal/pkg/constants" "github.com/stakater/Reloader/internal/pkg/handler" + "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/internal/pkg/testutil" "github.com/stakater/Reloader/internal/pkg/util" "github.com/stakater/Reloader/pkg/kube" @@ -78,7 +79,7 @@ func TestControllerUpdatingConfigmapShouldCreateEnvInDeployment(t *testing.T) { Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, deploymentFuncs) @@ -130,7 +131,7 @@ func TestControllerUpdatingConfigmapShouldAutoCreateEnvInDeployment(t *testing.T Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, deploymentFuncs) @@ -191,7 +192,7 @@ func TestControllerCreatingConfigmapShouldCreateEnvInDeployment(t *testing.T) { Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, deploymentFuncs) @@ -248,7 +249,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDeployment(t *testing.T) { Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() @@ -301,7 +302,7 @@ func TestControllerUpdatingConfigmapLabelsShouldNotCreateorUpdateEnvInDeployment Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, deploymentFuncs) @@ -360,7 +361,7 @@ func TestControllerCreatingSecretShouldCreateEnvInDeployment(t *testing.T) { Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() time.Sleep(5 * time.Second) @@ -411,7 +412,7 @@ func TestControllerUpdatingSecretShouldCreateEnvInDeployment(t *testing.T) { Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, deploymentFuncs) @@ -467,7 +468,7 @@ func TestControllerUpdatingSecretShouldUpdateEnvInDeployment(t *testing.T) { Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, deploymentFuncs) @@ -516,7 +517,7 @@ func TestControllerUpdatingSecretLabelsShouldNotCreateorUpdateEnvInDeployment(t Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } deploymentFuncs := handler.GetDeploymentRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, deploymentFuncs) @@ -566,7 +567,7 @@ func TestControllerUpdatingConfigmapShouldCreateEnvInDaemonSet(t *testing.T) { Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } daemonSetFuncs := handler.GetDaemonSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, daemonSetFuncs) @@ -627,7 +628,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDaemonSet(t *testing.T) { Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } daemonSetFuncs := handler.GetDaemonSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, daemonSetFuncs) @@ -678,7 +679,7 @@ func TestControllerUpdatingSecretShouldCreateEnvInDaemonSet(t *testing.T) { Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } daemonSetFuncs := handler.GetDaemonSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, daemonSetFuncs) @@ -735,7 +736,7 @@ func TestControllerUpdatingSecretShouldUpdateEnvInDaemonSet(t *testing.T) { Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } daemonSetFuncs := handler.GetDaemonSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, daemonSetFuncs) @@ -784,7 +785,7 @@ func TestControllerUpdatingSecretLabelsShouldNotCreateorUpdateEnvInDaemonSet(t * Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } daemonSetFuncs := handler.GetDaemonSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, daemonSetFuncs) @@ -834,7 +835,7 @@ func TestControllerUpdatingConfigmapShouldCreateEnvInStatefulSet(t *testing.T) { Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } statefulSetFuncs := handler.GetStatefulSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, statefulSetFuncs) @@ -891,7 +892,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateStatefulSet(t *testing.T) { Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, } statefulSetFuncs := handler.GetStatefulSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.ConfigmapEnvVarPostfix, statefulSetFuncs) @@ -942,7 +943,7 @@ func TestControllerUpdatingSecretShouldCreateEnvInStatefulSet(t *testing.T) { Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } statefulSetFuncs := handler.GetStatefulSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, statefulSetFuncs) @@ -998,7 +999,7 @@ func TestControllerUpdatingSecretShouldUpdateEnvInStatefulSet(t *testing.T) { Namespace: namespace, ResourceName: secretName, SHAValue: shaData, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, } statefulSetFuncs := handler.GetStatefulSetRollingUpgradeFuncs() updated := testutil.VerifyResourceUpdate(client, config, constants.SecretEnvVarPostfix, statefulSetFuncs) diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index 0d059111..5ade0792 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -7,6 +7,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stakater/Reloader/internal/pkg/callbacks" "github.com/stakater/Reloader/internal/pkg/constants" + "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/internal/pkg/util" "github.com/stakater/Reloader/pkg/kube" v1 "k8s.io/api/core/v1" @@ -73,7 +74,7 @@ func PerformRollingUpgrade(client kubernetes.Interface, config util.Config, upgr volumes := upgradeFuncs.VolumesFunc(i) // find correct annotation and update the resource annotationValue := util.ToObjectMeta(i).Annotations[config.Annotation] - reloaderEnabledValue := util.ToObjectMeta(i).Annotations[constants.ReloaderAutoAnnotation] + reloaderEnabledValue := util.ToObjectMeta(i).Annotations[options.ReloaderAutoAnnotation] if len(containers) > 0 { resourceName := util.ToObjectMeta(i).Name result := constants.NotUpdated diff --git a/internal/pkg/handler/upgrade_test.go b/internal/pkg/handler/upgrade_test.go index 84f1a63a..6e2439c6 100644 --- a/internal/pkg/handler/upgrade_test.go +++ b/internal/pkg/handler/upgrade_test.go @@ -7,6 +7,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stakater/Reloader/internal/pkg/constants" + "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/internal/pkg/testutil" "github.com/stakater/Reloader/internal/pkg/util" testclient "k8s.io/client-go/kubernetes/fake" @@ -301,7 +302,7 @@ func getConfigWithAnnotations(resourceType string, name string, shaData string, func TestRollingUpgradeForDeploymentWithConfigmap(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapName, shaData, constants.ConfigmapUpdateOnChangeAnnotation) + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapName, shaData, options.ConfigmapUpdateOnChangeAnnotation) deploymentFuncs := GetDeploymentRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, deploymentFuncs) @@ -319,7 +320,7 @@ func TestRollingUpgradeForDeploymentWithConfigmap(t *testing.T) { func TestRollingUpgradeForDeploymentWithConfigmapAsEnvVar(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapWithEnvName, "www.stakater.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapWithEnvName, shaData, constants.ReloaderAutoAnnotation) + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapWithEnvName, shaData, options.ReloaderAutoAnnotation) deploymentFuncs := GetDeploymentRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, deploymentFuncs) @@ -337,7 +338,7 @@ func TestRollingUpgradeForDeploymentWithConfigmapAsEnvVar(t *testing.T) { func TestRollingUpgradeForDeploymentWithConfigmapAsEnvVarFrom(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapWithEnvFromName, "www.stakater.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapWithEnvFromName, shaData, constants.ReloaderAutoAnnotation) + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapWithEnvFromName, shaData, options.ReloaderAutoAnnotation) deploymentFuncs := GetDeploymentRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, deploymentFuncs) @@ -355,7 +356,7 @@ func TestRollingUpgradeForDeploymentWithConfigmapAsEnvVarFrom(t *testing.T) { func TestRollingUpgradeForDeploymentWithSecret(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, "dGVzdFVwZGF0ZWRTZWNyZXRFbmNvZGluZ0ZvclJlbG9hZGVy") - config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretName, shaData, constants.SecretUpdateOnChangeAnnotation) + config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretName, shaData, options.SecretUpdateOnChangeAnnotation) deploymentFuncs := GetDeploymentRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, deploymentFuncs) @@ -373,7 +374,7 @@ func TestRollingUpgradeForDeploymentWithSecret(t *testing.T) { func TestRollingUpgradeForDeploymentWithSecretAsEnvVar(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretWithEnvName, "dGVzdFVwZGF0ZWRTZWNyZXRFbmNvZGluZ0ZvclJlbG9hZGVy") - config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretWithEnvName, shaData, constants.ReloaderAutoAnnotation) + config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretWithEnvName, shaData, options.ReloaderAutoAnnotation) deploymentFuncs := GetDeploymentRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, deploymentFuncs) @@ -391,7 +392,7 @@ func TestRollingUpgradeForDeploymentWithSecretAsEnvVar(t *testing.T) { func TestRollingUpgradeForDeploymentWithSecretAsEnvVarFrom(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretWithEnvFromName, "dGVzdFVwZGF0ZWRTZWNyZXRFbmNvZGluZ0ZvclJlbG9hZGVy") - config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretWithEnvFromName, shaData, constants.ReloaderAutoAnnotation) + config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretWithEnvFromName, shaData, options.ReloaderAutoAnnotation) deploymentFuncs := GetDeploymentRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, deploymentFuncs) @@ -409,7 +410,7 @@ func TestRollingUpgradeForDeploymentWithSecretAsEnvVarFrom(t *testing.T) { func TestRollingUpgradeForDaemonSetWithConfigmap(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.facebook.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapName, shaData, constants.ConfigmapUpdateOnChangeAnnotation) + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapName, shaData, options.ConfigmapUpdateOnChangeAnnotation) daemonSetFuncs := GetDaemonSetRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, daemonSetFuncs) @@ -427,7 +428,7 @@ func TestRollingUpgradeForDaemonSetWithConfigmap(t *testing.T) { func TestRollingUpgradeForDaemonSetWithConfigmapAsEnvVar(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapWithEnvName, "www.facebook.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapWithEnvName, shaData, constants.ReloaderAutoAnnotation) + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapWithEnvName, shaData, options.ReloaderAutoAnnotation) daemonSetFuncs := GetDaemonSetRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, daemonSetFuncs) @@ -445,7 +446,7 @@ func TestRollingUpgradeForDaemonSetWithConfigmapAsEnvVar(t *testing.T) { func TestRollingUpgradeForDaemonSetWithSecret(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, "d3d3LmZhY2Vib29rLmNvbQ==") - config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretName, shaData, constants.SecretUpdateOnChangeAnnotation) + config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretName, shaData, options.SecretUpdateOnChangeAnnotation) daemonSetFuncs := GetDaemonSetRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, daemonSetFuncs) @@ -463,7 +464,7 @@ func TestRollingUpgradeForDaemonSetWithSecret(t *testing.T) { func TestRollingUpgradeForStatefulSetWithConfigmap(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.twitter.com") - config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapName, shaData, constants.ConfigmapUpdateOnChangeAnnotation) + config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, configmapName, shaData, options.ConfigmapUpdateOnChangeAnnotation) statefulSetFuncs := GetStatefulSetRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, statefulSetFuncs) @@ -481,7 +482,7 @@ func TestRollingUpgradeForStatefulSetWithConfigmap(t *testing.T) { func TestRollingUpgradeForStatefulSetWithSecret(t *testing.T) { shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, "d3d3LnR3aXR0ZXIuY29t") - config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretName, shaData, constants.SecretUpdateOnChangeAnnotation) + config := getConfigWithAnnotations(constants.SecretEnvVarPostfix, secretName, shaData, options.SecretUpdateOnChangeAnnotation) statefulSetFuncs := GetStatefulSetRollingUpgradeFuncs() err := PerformRollingUpgrade(client, config, statefulSetFuncs) diff --git a/internal/pkg/constants/annotations.go b/internal/pkg/options/flags.go similarity index 94% rename from internal/pkg/constants/annotations.go rename to internal/pkg/options/flags.go index 21554dbc..f150a11a 100644 --- a/internal/pkg/constants/annotations.go +++ b/internal/pkg/options/flags.go @@ -1,6 +1,6 @@ -package constants +package options -const ( +var ( // ConfigmapUpdateOnChangeAnnotation is an annotation to detect changes in configmaps ConfigmapUpdateOnChangeAnnotation = "configmap.reloader.stakater.com/reload" // SecretUpdateOnChangeAnnotation is an annotation to detect changes in secrets diff --git a/internal/pkg/testutil/kube.go b/internal/pkg/testutil/kube.go index 8612235c..b48a18c7 100644 --- a/internal/pkg/testutil/kube.go +++ b/internal/pkg/testutil/kube.go @@ -11,6 +11,7 @@ import ( "github.com/stakater/Reloader/internal/pkg/callbacks" "github.com/stakater/Reloader/internal/pkg/constants" "github.com/stakater/Reloader/internal/pkg/crypto" + "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/internal/pkg/util" "github.com/stakater/Reloader/pkg/kube" v1_beta1 "k8s.io/api/apps/v1beta1" @@ -69,12 +70,12 @@ func getObjectMeta(namespace string, name string, autoReload bool) metav1.Object func getAnnotations(name string, autoReload bool) map[string]string { if autoReload { return map[string]string{ - constants.ReloaderAutoAnnotation: "true"} + options.ReloaderAutoAnnotation: "true"} } return map[string]string{ - constants.ConfigmapUpdateOnChangeAnnotation: name, - constants.SecretUpdateOnChangeAnnotation: name} + options.ConfigmapUpdateOnChangeAnnotation: name, + options.SecretUpdateOnChangeAnnotation: name} } func getPodTemplateSpecWithEnvVars(name string) v1.PodTemplateSpec { @@ -536,7 +537,7 @@ func VerifyResourceUpdate(client kubernetes.Interface, config util.Config, envVa containers := upgradeFuncs.ContainersFunc(i) // match statefulsets with the correct annotation annotationValue := util.ToObjectMeta(i).Annotations[config.Annotation] - reloaderEnabledValue := util.ToObjectMeta(i).Annotations[constants.ReloaderAutoAnnotation] + reloaderEnabledValue := util.ToObjectMeta(i).Annotations[options.ReloaderAutoAnnotation] reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue) matches := false if err == nil && reloaderEnabled { diff --git a/internal/pkg/util/config.go b/internal/pkg/util/config.go index 24ef407f..e8de37e2 100644 --- a/internal/pkg/util/config.go +++ b/internal/pkg/util/config.go @@ -2,6 +2,7 @@ package util import ( "github.com/stakater/Reloader/internal/pkg/constants" + "github.com/stakater/Reloader/internal/pkg/options" v1 "k8s.io/api/core/v1" ) @@ -19,7 +20,7 @@ func GetConfigmapConfig(configmap *v1.ConfigMap) Config { return Config{ Namespace: configmap.Namespace, ResourceName: configmap.Name, - Annotation: constants.ConfigmapUpdateOnChangeAnnotation, + Annotation: options.ConfigmapUpdateOnChangeAnnotation, SHAValue: GetSHAfromConfigmap(configmap.Data), Type: constants.ConfigmapEnvVarPostfix, } @@ -30,7 +31,7 @@ func GetSecretConfig(secret *v1.Secret) Config { return Config{ Namespace: secret.Namespace, ResourceName: secret.Name, - Annotation: constants.SecretUpdateOnChangeAnnotation, + Annotation: options.SecretUpdateOnChangeAnnotation, SHAValue: GetSHAfromSecret(secret.Data), Type: constants.SecretEnvVarPostfix, } From 33443ccb29f77a58f7aa88a6016502cef45fe18f Mon Sep 17 00:00:00 2001 From: Joey Espinosa Date: Wed, 6 Feb 2019 12:46:17 -0500 Subject: [PATCH 2/3] feat: add cli flags to helm chart --- .../chart/reloader/templates/deployment.yaml | 15 +++++++++++++++ deployments/kubernetes/chart/reloader/values.yaml | 6 ++++++ 2 files changed, 21 insertions(+) diff --git a/deployments/kubernetes/chart/reloader/templates/deployment.yaml b/deployments/kubernetes/chart/reloader/templates/deployment.yaml index 0ad0018a..69383e20 100644 --- a/deployments/kubernetes/chart/reloader/templates/deployment.yaml +++ b/deployments/kubernetes/chart/reloader/templates/deployment.yaml @@ -70,4 +70,19 @@ spec: image: "{{ .Values.reloader.deployment.image.name }}:{{ .Values.reloader.deployment.image.tag }}" imagePullPolicy: {{ .Values.reloader.deployment.image.pullPolicy }} name: {{ template "reloader-name" . }} + {{- if .Values.reloader.custom_annotations }} + args: + {{- if .Values.reloader.custom_annotations.configmap }} + - "--configmap-annotation" + - "{{ .Values.reloader.custom_annotations.configmap }}" + {{- end }} + {{- if .Values.reloader.custom_annotations.secret }} + - "--secret-annotation" + - "{{ .Values.reloader.custom_annotations.secret }}" + {{- end }} + {{- if .Values.reloader.custom_annotations.auto }} + - "--auto-annotation" + - "{{ .Values.reloader.custom_annotations.auto }}" + {{- end }} + {{- end }} serviceAccountName: {{ template "serviceAccountName" . }} diff --git a/deployments/kubernetes/chart/reloader/values.yaml b/deployments/kubernetes/chart/reloader/values.yaml index 649dab35..e4c90640 100644 --- a/deployments/kubernetes/chart/reloader/values.yaml +++ b/deployments/kubernetes/chart/reloader/values.yaml @@ -35,3 +35,9 @@ reloader: # The name of the ServiceAccount to use. # If not set and create is true, a name is generated using the fullname template name: reloader + # Optional flags to pass to the Reloader entrypoint + # Example: + # custom_annotations: + # configmap: "my.company.com/configmap" + # secret: "my.company.com/secret" + custom_annotations: {} \ No newline at end of file From 16bce16f817d6d4291e642b9060006619dbd8778 Mon Sep 17 00:00:00 2001 From: kahootali Date: Fri, 8 Feb 2019 15:05:18 +0500 Subject: [PATCH 3/3] add custom annotation --- deployments/kubernetes/templates/chart/values.yaml.tmpl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/deployments/kubernetes/templates/chart/values.yaml.tmpl b/deployments/kubernetes/templates/chart/values.yaml.tmpl index 3e563d84..0ebcdd33 100644 --- a/deployments/kubernetes/templates/chart/values.yaml.tmpl +++ b/deployments/kubernetes/templates/chart/values.yaml.tmpl @@ -35,3 +35,9 @@ reloader: # The name of the ServiceAccount to use. # If not set and create is true, a name is generated using the fullname template name: reloader + # Optional flags to pass to the Reloader entrypoint + # Example: + # custom_annotations: + # configmap: "my.company.com/configmap" + # secret: "my.company.com/secret" + custom_annotations: {} \ No newline at end of file