diff --git a/internal/pkg/cmd/reloader.go b/internal/pkg/cmd/reloader.go index bb45d35..f54d757 100644 --- a/internal/pkg/cmd/reloader.go +++ b/internal/pkg/cmd/reloader.go @@ -133,13 +133,13 @@ func startReloader(cmd *cobra.Command, args []string) { namespaceLabelSelector := "" if isGlobal { - namespaceLabelSelector, err = util.GetNamespaceLabelSelector() + namespaceLabelSelector, err = common.GetNamespaceLabelSelector(options.NamespaceSelectors) if err != nil { logrus.Fatal(err) } } - resourceLabelSelector, err := util.GetResourceLabelSelector() + resourceLabelSelector, err := common.GetResourceLabelSelector(options.ResourceSelectors) if err != nil { logrus.Fatal(err) } diff --git a/internal/pkg/controller/controller_test.go b/internal/pkg/controller/controller_test.go index 0876b2c..17eff94 100644 --- a/internal/pkg/controller/controller_test.go +++ b/internal/pkg/controller/controller_test.go @@ -15,6 +15,7 @@ import ( "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/common" "github.com/stakater/Reloader/pkg/kube" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -94,7 +95,7 @@ func TestControllerUpdatingConfigmapShouldCreatePodAnnotationInDeployment(t *tes // Verifying deployment update logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -147,7 +148,7 @@ func TestControllerUpdatingConfigmapShouldAutoCreatePodAnnotationInDeployment(t // Verifying deployment update logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -212,7 +213,7 @@ func TestControllerCreatingConfigmapShouldCreatePodAnnotationInDeployment(t *tes // Verifying deployment update logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -271,7 +272,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDeploymentUsingArs(t *testing // Verifying deployment update logrus.Infof("Verifying pod annotation has been updated") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "aurorasolutions.io") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -326,7 +327,7 @@ func TestControllerUpdatingConfigmapLabelsShouldNotCreateOrCreatePodAnnotationIn // Verifying deployment update logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.google.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -390,7 +391,7 @@ func TestControllerCreatingSecretShouldCreatePodAnnotationInDeployment(t *testin // Verifying Upgrade logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -443,7 +444,7 @@ func TestControllerUpdatingSecretShouldCreatePodAnnotationInDeployment(t *testin // Verifying Upgrade logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -501,7 +502,7 @@ func TestControllerUpdatingSecretShouldUpdatePodAnnotationInDeployment(t *testin // Verifying Upgrade logrus.Infof("Verifying pod annotation has been updated") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, updatedData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -552,7 +553,7 @@ func TestControllerUpdatingSecretLabelsShouldNotCreateOrUpdatePodAnnotationInDep // Verifying Upgrade logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, data) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -604,7 +605,7 @@ func TestControllerUpdatingConfigmapShouldCreatePodAnnotationInDaemonSet(t *test // Verifying DaemonSet update logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -667,7 +668,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDaemonSetUsingArs(t *testing. // Verifying DaemonSet update logrus.Infof("Verifying pod annotation has been updated") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "aurorasolutions.io") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -720,7 +721,7 @@ func TestControllerUpdatingSecretShouldCreatePodAnnotationInDaemonSet(t *testing // Verifying Upgrade logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -779,7 +780,7 @@ func TestControllerUpdatingSecretShouldUpdatePodAnnotationInDaemonSet(t *testing // Verifying Upgrade logrus.Infof("Verifying pod annotation has been updated") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, updatedData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -830,7 +831,7 @@ func TestControllerUpdatingSecretLabelsShouldNotCreateOrUpdatePodAnnotationInDae // Verifying Upgrade logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, data) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -882,7 +883,7 @@ func TestControllerUpdatingConfigmapShouldCreatePodAnnotationInStatefulSet(t *te // Verifying StatefulSet update logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -941,7 +942,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateStatefulSetUsingArs(t *testin // Verifying StatefulSet update logrus.Infof("Verifying pod annotation has been updated") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "aurorasolutions.io") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -994,7 +995,7 @@ func TestControllerUpdatingSecretShouldCreatePodAnnotationInStatefulSet(t *testi // Verifying Upgrade logrus.Infof("Verifying pod annotation has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1046,7 +1047,7 @@ func TestControllerUpdatingConfigmapShouldCreateEnvInDeployment(t *testing.T) { // Verifying deployment update logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1099,7 +1100,7 @@ func TestControllerUpdatingConfigmapShouldAutoCreateEnvInDeployment(t *testing.T // Verifying deployment update logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1164,7 +1165,7 @@ func TestControllerCreatingConfigmapShouldCreateEnvInDeployment(t *testing.T) { // Verifying deployment update logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1223,7 +1224,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDeploymentUsingErs(t *testing // Verifying deployment update logrus.Infof("Verifying env var has been updated") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "aurorasolutions.io") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1278,7 +1279,7 @@ func TestControllerUpdatingConfigmapLabelsShouldNotCreateOrUpdateEnvInDeployment // Verifying deployment update logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.google.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1342,7 +1343,7 @@ func TestControllerCreatingSecretShouldCreateEnvInDeployment(t *testing.T) { // Verifying Upgrade logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1395,7 +1396,7 @@ func TestControllerUpdatingSecretShouldCreateEnvInDeployment(t *testing.T) { // Verifying Upgrade logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1453,7 +1454,7 @@ func TestControllerUpdatingSecretShouldUpdateEnvInDeployment(t *testing.T) { // Verifying Upgrade logrus.Infof("Verifying env var has been updated") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, updatedData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1504,7 +1505,7 @@ func TestControllerUpdatingSecretLabelsShouldNotCreateOrUpdateEnvInDeployment(t // Verifying Upgrade logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, data) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1556,7 +1557,7 @@ func TestControllerUpdatingConfigmapShouldCreateEnvInDaemonSet(t *testing.T) { // Verifying DaemonSet update logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1619,7 +1620,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDaemonSetUsingErs(t *testing. // Verifying DaemonSet update logrus.Infof("Verifying env var has been updated") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "aurorasolutions.io") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1672,7 +1673,7 @@ func TestControllerUpdatingSecretShouldCreateEnvInDaemonSet(t *testing.T) { // Verifying Upgrade logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1731,7 +1732,7 @@ func TestControllerUpdatingSecretShouldUpdateEnvInDaemonSet(t *testing.T) { // Verifying Upgrade logrus.Infof("Verifying env var has been updated") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, updatedData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1782,7 +1783,7 @@ func TestControllerUpdatingSecretLabelsShouldNotCreateOrUpdateEnvInDaemonSet(t * // Verifying Upgrade logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, data) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -1834,7 +1835,7 @@ func TestControllerUpdatingConfigmapShouldCreateEnvInStatefulSet(t *testing.T) { // Verifying StatefulSet update logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1893,7 +1894,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateStatefulSetUsingErs(t *testin // Verifying StatefulSet update logrus.Infof("Verifying env var has been updated") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "aurorasolutions.io") - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: configmapName, SHAValue: shaData, @@ -1946,7 +1947,7 @@ func TestControllerUpdatingSecretShouldCreateEnvInStatefulSet(t *testing.T) { // Verifying Upgrade logrus.Infof("Verifying env var has been created") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, newData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -2004,7 +2005,7 @@ func TestControllerUpdatingSecretShouldUpdateEnvInStatefulSet(t *testing.T) { // Verifying Upgrade logrus.Infof("Verifying env var has been updated") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, updatedData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, @@ -2062,7 +2063,7 @@ func TestControllerUpdatingSecretShouldUpdatePodAnnotationInStatefulSet(t *testi // Verifying Upgrade logrus.Infof("Verifying pod annotation has been updated") shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, updatedData) - config := util.Config{ + config := common.Config{ Namespace: namespace, ResourceName: secretName, SHAValue: shaData, diff --git a/internal/pkg/handler/create.go b/internal/pkg/handler/create.go index a35da5e..fab7378 100644 --- a/internal/pkg/handler/create.go +++ b/internal/pkg/handler/create.go @@ -4,7 +4,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stakater/Reloader/internal/pkg/metrics" "github.com/stakater/Reloader/internal/pkg/options" - "github.com/stakater/Reloader/internal/pkg/util" + "github.com/stakater/Reloader/pkg/common" v1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/record" ) @@ -33,13 +33,13 @@ func (r ResourceCreatedHandler) Handle() error { } // GetConfig gets configurations containing SHA, annotations, namespace and resource name -func (r ResourceCreatedHandler) GetConfig() (util.Config, string) { +func (r ResourceCreatedHandler) GetConfig() (common.Config, string) { var oldSHAData string - var config util.Config + var config common.Config if _, ok := r.Resource.(*v1.ConfigMap); ok { - config = util.GetConfigmapConfig(r.Resource.(*v1.ConfigMap)) + config = common.GetConfigmapConfig(r.Resource.(*v1.ConfigMap)) } else if _, ok := r.Resource.(*v1.Secret); ok { - config = util.GetSecretConfig(r.Resource.(*v1.Secret)) + config = common.GetSecretConfig(r.Resource.(*v1.Secret)) } else { logrus.Warnf("Invalid resource: Resource should be 'Secret' or 'Configmap' but found, %v", r.Resource) } diff --git a/internal/pkg/handler/delete.go b/internal/pkg/handler/delete.go index 772cfca..65c671e 100644 --- a/internal/pkg/handler/delete.go +++ b/internal/pkg/handler/delete.go @@ -10,7 +10,7 @@ import ( "github.com/stakater/Reloader/internal/pkg/metrics" "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/common" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -42,20 +42,20 @@ func (r ResourceDeleteHandler) Handle() error { } // GetConfig gets configurations containing SHA, annotations, namespace and resource name -func (r ResourceDeleteHandler) GetConfig() (util.Config, string) { +func (r ResourceDeleteHandler) GetConfig() (common.Config, string) { var oldSHAData string - var config util.Config + var config common.Config if _, ok := r.Resource.(*v1.ConfigMap); ok { - config = util.GetConfigmapConfig(r.Resource.(*v1.ConfigMap)) + config = common.GetConfigmapConfig(r.Resource.(*v1.ConfigMap)) } else if _, ok := r.Resource.(*v1.Secret); ok { - config = util.GetSecretConfig(r.Resource.(*v1.Secret)) + config = common.GetSecretConfig(r.Resource.(*v1.Secret)) } else { logrus.Warnf("Invalid resource: Resource should be 'Secret' or 'Configmap' but found, %v", r.Resource) } return config, oldSHAData } -func invokeDeleteStrategy(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) InvokeStrategyResult { +func invokeDeleteStrategy(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) InvokeStrategyResult { if options.ReloadStrategy == constants.AnnotationsReloadStrategy { return removePodAnnotations(upgradeFuncs, item, config, autoReload) } @@ -63,12 +63,12 @@ func invokeDeleteStrategy(upgradeFuncs callbacks.RollingUpgradeFuncs, item runti return removeContainerEnvVars(upgradeFuncs, item, config, autoReload) } -func removePodAnnotations(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) InvokeStrategyResult { +func removePodAnnotations(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) InvokeStrategyResult { config.SHAValue = testutil.GetSHAfromEmptyData() return updatePodAnnotations(upgradeFuncs, item, config, autoReload) } -func removeContainerEnvVars(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) InvokeStrategyResult { +func removeContainerEnvVars(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) InvokeStrategyResult { envVar := getEnvVarName(config.ResourceName, config.Type) container := getContainerUsingResource(upgradeFuncs, item, config, autoReload) diff --git a/internal/pkg/handler/handler.go b/internal/pkg/handler/handler.go index 634e080..1f5858e 100644 --- a/internal/pkg/handler/handler.go +++ b/internal/pkg/handler/handler.go @@ -1,11 +1,9 @@ package handler -import ( - "github.com/stakater/Reloader/internal/pkg/util" -) +import "github.com/stakater/Reloader/pkg/common" // ResourceHandler handles the creation and update of resources type ResourceHandler interface { Handle() error - GetConfig() (util.Config, string) + GetConfig() (common.Config, string) } diff --git a/internal/pkg/handler/update.go b/internal/pkg/handler/update.go index 0575e19..ae0bb1e 100644 --- a/internal/pkg/handler/update.go +++ b/internal/pkg/handler/update.go @@ -5,6 +5,7 @@ import ( "github.com/stakater/Reloader/internal/pkg/metrics" "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/internal/pkg/util" + "github.com/stakater/Reloader/pkg/common" v1 "k8s.io/api/core/v1" "k8s.io/client-go/tools/record" ) @@ -36,15 +37,15 @@ func (r ResourceUpdatedHandler) Handle() error { } // GetConfig gets configurations containing SHA, annotations, namespace and resource name -func (r ResourceUpdatedHandler) GetConfig() (util.Config, string) { +func (r ResourceUpdatedHandler) GetConfig() (common.Config, string) { var oldSHAData string - var config util.Config + var config common.Config if _, ok := r.Resource.(*v1.ConfigMap); ok { oldSHAData = util.GetSHAfromConfigmap(r.OldResource.(*v1.ConfigMap)) - config = util.GetConfigmapConfig(r.Resource.(*v1.ConfigMap)) + config = common.GetConfigmapConfig(r.Resource.(*v1.ConfigMap)) } else if _, ok := r.Resource.(*v1.Secret); ok { oldSHAData = util.GetSHAfromSecret(r.OldResource.(*v1.Secret).Data) - config = util.GetSecretConfig(r.Resource.(*v1.Secret)) + config = common.GetSecretConfig(r.Resource.(*v1.Secret)) } else { logrus.Warnf("Invalid resource: Resource should be 'Secret' or 'Configmap' but found, %v", r.Resource) } diff --git a/internal/pkg/handler/upgrade.go b/internal/pkg/handler/upgrade.go index 914056d..57af252 100644 --- a/internal/pkg/handler/upgrade.go +++ b/internal/pkg/handler/upgrade.go @@ -138,7 +138,7 @@ func GetArgoRolloutRollingUpgradeFuncs() callbacks.RollingUpgradeFuncs { } } -func sendUpgradeWebhook(config util.Config, webhookUrl string) error { +func sendUpgradeWebhook(config common.Config, webhookUrl string) error { logrus.Infof("Changes detected in '%s' of type '%s' in namespace '%s', Sending webhook to '%s'", config.ResourceName, config.Type, config.Namespace, webhookUrl) @@ -169,7 +169,7 @@ func sendWebhook(url string) (string, []error) { return buffer.String(), nil } -func doRollingUpgrade(config util.Config, collectors metrics.Collectors, recorder record.EventRecorder, invoke invokeStrategy) error { +func doRollingUpgrade(config common.Config, collectors metrics.Collectors, recorder record.EventRecorder, invoke invokeStrategy) error { clients := kube.GetClients() err := rollingUpgrade(clients, config, GetDeploymentRollingUpgradeFuncs(), collectors, recorder, invoke) @@ -203,7 +203,7 @@ func doRollingUpgrade(config util.Config, collectors metrics.Collectors, recorde return nil } -func rollingUpgrade(clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, recorder record.EventRecorder, strategy invokeStrategy) error { +func rollingUpgrade(clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, recorder record.EventRecorder, strategy invokeStrategy) error { err := PerformAction(clients, config, upgradeFuncs, collectors, recorder, strategy) if err != nil { logrus.Errorf("Rolling upgrade for '%s' failed with error = %v", config.ResourceName, err) @@ -212,7 +212,7 @@ func rollingUpgrade(clients kube.Clients, config util.Config, upgradeFuncs callb } // PerformAction invokes the deployment if there is any change in configmap or secret data -func PerformAction(clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, recorder record.EventRecorder, strategy invokeStrategy) error { +func PerformAction(clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, recorder record.EventRecorder, strategy invokeStrategy) error { items := upgradeFuncs.ItemsFunc(clients, config.Namespace) for _, item := range items { @@ -249,7 +249,7 @@ func retryOnConflict(backoff wait.Backoff, fn func(_ bool) error) error { return err } -func upgradeResource(clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, recorder record.EventRecorder, strategy invokeStrategy, resource runtime.Object, fetchResource bool) error { +func upgradeResource(clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, recorder record.EventRecorder, strategy invokeStrategy, resource runtime.Object, fetchResource bool) error { accessor, err := meta.Accessor(resource) if err != nil { return err @@ -403,7 +403,7 @@ func getContainerWithEnvReference(containers []v1.Container, resourceName string return nil } -func getContainerUsingResource(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) *v1.Container { +func getContainerUsingResource(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) *v1.Container { volumes := upgradeFuncs.VolumesFunc(item) containers := upgradeFuncs.ContainersFunc(item) initContainers := upgradeFuncs.InitContainersFunc(item) @@ -464,16 +464,16 @@ type InvokeStrategyResult struct { Patch *Patch } -type invokeStrategy func(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) InvokeStrategyResult +type invokeStrategy func(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) InvokeStrategyResult -func invokeReloadStrategy(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) InvokeStrategyResult { +func invokeReloadStrategy(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) InvokeStrategyResult { if options.ReloadStrategy == constants.AnnotationsReloadStrategy { return updatePodAnnotations(upgradeFuncs, item, config, autoReload) } return updateContainerEnvVars(upgradeFuncs, item, config, autoReload) } -func updatePodAnnotations(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) InvokeStrategyResult { +func updatePodAnnotations(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) InvokeStrategyResult { container := getContainerUsingResource(upgradeFuncs, item, config, autoReload) if container == nil { return InvokeStrategyResult{constants.NoContainerFound, nil} @@ -481,7 +481,7 @@ func updatePodAnnotations(upgradeFuncs callbacks.RollingUpgradeFuncs, item runti // Generate reloaded annotations. Attaching this to the item's annotation will trigger a rollout // Note: the data on this struct is purely informational and is not used for future updates - reloadSource := util.NewReloadSourceFromConfig(config, []string{container.Name}) + reloadSource := common.NewReloadSourceFromConfig(config, []string{container.Name}) annotations, patch, err := createReloadedAnnotations(&reloadSource, upgradeFuncs) if err != nil { logrus.Errorf("Failed to create reloaded annotations for %s! error = %v", config.ResourceName, err) @@ -508,7 +508,7 @@ func getReloaderAnnotationKey() string { ) } -func createReloadedAnnotations(target *util.ReloadSource, upgradeFuncs callbacks.RollingUpgradeFuncs) (map[string]string, []byte, error) { +func createReloadedAnnotations(target *common.ReloadSource, upgradeFuncs callbacks.RollingUpgradeFuncs) (map[string]string, []byte, error) { if target == nil { return nil, nil, errors.New("target is required") } @@ -543,7 +543,7 @@ func getEnvVarName(resourceName string, typeName string) string { return constants.EnvVarPrefix + util.ConvertToEnvVarName(resourceName) + "_" + typeName } -func updateContainerEnvVars(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) InvokeStrategyResult { +func updateContainerEnvVars(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config common.Config, autoReload bool) InvokeStrategyResult { envVar := getEnvVarName(config.ResourceName, config.Type) container := getContainerUsingResource(upgradeFuncs, item, config, autoReload) diff --git a/internal/pkg/handler/upgrade_test.go b/internal/pkg/handler/upgrade_test.go index 3d2dcbf..7259fba 100644 --- a/internal/pkg/handler/upgrade_test.go +++ b/internal/pkg/handler/upgrade_test.go @@ -17,6 +17,7 @@ import ( "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/common" "github.com/stakater/Reloader/pkg/kube" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" @@ -1488,13 +1489,13 @@ func teardownErs() { } -func getConfigWithAnnotations(resourceType string, name string, shaData string, annotation string, typedAutoAnnotation string) util.Config { +func getConfigWithAnnotations(resourceType string, name string, shaData string, annotation string, typedAutoAnnotation string) common.Config { ns := ersNamespace if options.ReloadStrategy == constants.AnnotationsReloadStrategy { ns = arsNamespace } - return util.Config{ + return common.Config{ Namespace: ns, ResourceName: name, SHAValue: shaData, @@ -1511,7 +1512,7 @@ func getCollectors() metrics.Collectors { var labelSucceeded = prometheus.Labels{"success": "true"} var labelFailed = prometheus.Labels{"success": "false"} -func testRollingUpgradeInvokeDeleteStrategyArs(t *testing.T, clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { +func testRollingUpgradeInvokeDeleteStrategyArs(t *testing.T, clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { err := PerformAction(clients, config, upgradeFuncs, collectors, nil, invokeDeleteStrategy) time.Sleep(5 * time.Second) if err != nil { @@ -1529,7 +1530,7 @@ func testRollingUpgradeInvokeDeleteStrategyArs(t *testing.T, clients kube.Client } } -func testRollingUpgradeWithPatchAndInvokeDeleteStrategyArs(t *testing.T, clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { +func testRollingUpgradeWithPatchAndInvokeDeleteStrategyArs(t *testing.T, clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { err := PerformAction(clients, config, upgradeFuncs, collectors, nil, invokeDeleteStrategy) upgradeFuncs.PatchFunc = func(client kube.Clients, namespace string, resource runtime.Object, patchType patchtypes.PatchType, bytes []byte) error { assert.Equal(t, patchtypes.StrategicMergePatchType, patchType) @@ -2909,7 +2910,7 @@ func TestIgnoreAnnotationNoReloadUsingErs(t *testing.T) { } } -func testRollingUpgradeInvokeDeleteStrategyErs(t *testing.T, clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { +func testRollingUpgradeInvokeDeleteStrategyErs(t *testing.T, clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { err := PerformAction(clients, config, upgradeFuncs, collectors, nil, invokeDeleteStrategy) time.Sleep(5 * time.Second) if err != nil { @@ -2926,7 +2927,7 @@ func testRollingUpgradeInvokeDeleteStrategyErs(t *testing.T, clients kube.Client } } -func testRollingUpgradeWithPatchAndInvokeDeleteStrategyErs(t *testing.T, clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { +func testRollingUpgradeWithPatchAndInvokeDeleteStrategyErs(t *testing.T, clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs, collectors metrics.Collectors, envVarPostfix string) { assert.NotEmpty(t, upgradeFuncs.PatchTemplatesFunc().DeleteEnvVarTemplate) err := PerformAction(clients, config, upgradeFuncs, collectors, nil, invokeDeleteStrategy) diff --git a/internal/pkg/leadership/leadership_test.go b/internal/pkg/leadership/leadership_test.go index 1c916e5..eed0705 100644 --- a/internal/pkg/leadership/leadership_test.go +++ b/internal/pkg/leadership/leadership_test.go @@ -16,7 +16,7 @@ import ( "github.com/stakater/Reloader/internal/pkg/metrics" "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/common" "github.com/stakater/Reloader/pkg/kube" ) @@ -159,7 +159,7 @@ func TestRunLeaderElectionWithControllers(t *testing.T) { // Verifying deployment update logrus.Infof("Verifying pod envvars has been created") shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, testutil.Namespace, configmapName, "www.stakater.com") - config := util.Config{ + config := common.Config{ Namespace: testutil.Namespace, ResourceName: configmapName, SHAValue: shaData, @@ -186,7 +186,7 @@ func TestRunLeaderElectionWithControllers(t *testing.T) { // Verifying that the deployment was not updated as leadership has been lost logrus.Infof("Verifying pod envvars has not been updated") shaData = testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, testutil.Namespace, configmapName, "www.stakater.com/new") - config = util.Config{ + config = common.Config{ Namespace: testutil.Namespace, ResourceName: configmapName, SHAValue: shaData, diff --git a/internal/pkg/testutil/kube.go b/internal/pkg/testutil/kube.go index 7a1aae3..beaa297 100644 --- a/internal/pkg/testutil/kube.go +++ b/internal/pkg/testutil/kube.go @@ -21,6 +21,7 @@ import ( "github.com/stakater/Reloader/internal/pkg/metrics" "github.com/stakater/Reloader/internal/pkg/options" "github.com/stakater/Reloader/internal/pkg/util" + "github.com/stakater/Reloader/pkg/common" "github.com/stakater/Reloader/pkg/kube" appsv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" @@ -733,7 +734,7 @@ func GetResourceSHAFromAnnotation(podAnnotations map[string]string) string { return "" } - var last util.ReloadSource + var last common.ReloadSource bytes := []byte(annotationJson) err := json.Unmarshal(bytes, &last) if err != nil { @@ -1058,7 +1059,7 @@ func RandSeq(n int) string { } // VerifyResourceEnvVarUpdate verifies whether the rolling upgrade happened or not -func VerifyResourceEnvVarUpdate(clients kube.Clients, config util.Config, envVarPostfix string, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { +func VerifyResourceEnvVarUpdate(clients kube.Clients, config common.Config, envVarPostfix string, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { items := upgradeFuncs.ItemsFunc(clients, config.Namespace) for _, i := range items { containers := upgradeFuncs.ContainersFunc(i) @@ -1104,7 +1105,7 @@ func VerifyResourceEnvVarUpdate(clients kube.Clients, config util.Config, envVar } // VerifyResourceEnvVarRemoved verifies whether the rolling upgrade happened or not and all Envvars SKAKATER_name_CONFIGMAP/SECRET are removed -func VerifyResourceEnvVarRemoved(clients kube.Clients, config util.Config, envVarPostfix string, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { +func VerifyResourceEnvVarRemoved(clients kube.Clients, config common.Config, envVarPostfix string, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { items := upgradeFuncs.ItemsFunc(clients, config.Namespace) for _, i := range items { containers := upgradeFuncs.ContainersFunc(i) @@ -1153,7 +1154,7 @@ func VerifyResourceEnvVarRemoved(clients kube.Clients, config util.Config, envVa } // VerifyResourceAnnotationUpdate verifies whether the rolling upgrade happened or not -func VerifyResourceAnnotationUpdate(clients kube.Clients, config util.Config, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { +func VerifyResourceAnnotationUpdate(clients kube.Clients, config common.Config, upgradeFuncs callbacks.RollingUpgradeFuncs) bool { items := upgradeFuncs.ItemsFunc(clients, config.Namespace) for _, i := range items { podAnnotations := upgradeFuncs.PodAnnotationsFunc(i) diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 1d7ade5..d434bc1 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -8,13 +8,11 @@ import ( "sort" "strings" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/stakater/Reloader/internal/pkg/constants" "github.com/stakater/Reloader/internal/pkg/crypto" "github.com/stakater/Reloader/internal/pkg/options" v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/labels" ) // ConvertToEnvVarName converts the given text into a usable env var @@ -98,68 +96,6 @@ func ConfigureReloaderFlags(cmd *cobra.Command) { cmd.PersistentFlags().StringVar(&options.PProfAddr, "pprof-addr", ":6060", "Address to start pprof server on. Default is :6060") } -func GetNamespaceLabelSelector() (string, error) { - slice := options.NamespaceSelectors - - for i, kv := range slice { - // Legacy support for ":" as a delimiter and "*" for wildcard. - if strings.Contains(kv, ":") { - split := strings.Split(kv, ":") - if split[1] == "*" { - slice[i] = split[0] - } else { - slice[i] = split[0] + "=" + split[1] - } - } - // Convert wildcard to valid apimachinery operator - if strings.Contains(kv, "=") { - split := strings.Split(kv, "=") - if split[1] == "*" { - slice[i] = split[0] - } - } - } - - namespaceLabelSelector := strings.Join(slice[:], ",") - _, err := labels.Parse(namespaceLabelSelector) - if err != nil { - logrus.Fatal(err) - } - - return namespaceLabelSelector, nil -} - -func GetResourceLabelSelector() (string, error) { - slice := options.ResourceSelectors - - for i, kv := range slice { - // Legacy support for ":" as a delimiter and "*" for wildcard. - if strings.Contains(kv, ":") { - split := strings.Split(kv, ":") - if split[1] == "*" { - slice[i] = split[0] - } else { - slice[i] = split[0] + "=" + split[1] - } - } - // Convert wildcard to valid apimachinery operator - if strings.Contains(kv, "=") { - split := strings.Split(kv, "=") - if split[1] == "*" { - slice[i] = split[0] - } - } - } - - resourceLabelSelector := strings.Join(slice[:], ",") - _, err := labels.Parse(resourceLabelSelector) - if err != nil { - logrus.Fatal(err) - } - - return resourceLabelSelector, nil -} - func GetIgnoredResourcesList() (List, error) { ignoredResourcesList := options.ResourcesToIgnore // getStringSliceFromFlags(cmd, "resources-to-ignore") diff --git a/pkg/common/common.go b/pkg/common/common.go index c8685ad..715f002 100644 --- a/pkg/common/common.go +++ b/pkg/common/common.go @@ -10,8 +10,8 @@ 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/util" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/kubernetes" ) @@ -121,14 +121,67 @@ func PublishMetaInfoConfigmap(clientset kubernetes.Interface) { } } -func ShouldReload(config util.Config, resourceType string, annotations Map, podAnnotations Map, options *ReloaderOptions) ReloadCheckResult { - - if resourceType == "Rollout" && !options.IsArgoRollouts { - return ReloadCheckResult{ - ShouldReload: false, +func GetNamespaceLabelSelector(slice []string) (string, error) { + for i, kv := range slice { + // Legacy support for ":" as a delimiter and "*" for wildcard. + if strings.Contains(kv, ":") { + split := strings.Split(kv, ":") + if split[1] == "*" { + slice[i] = split[0] + } else { + slice[i] = split[0] + "=" + split[1] + } + } + // Convert wildcard to valid apimachinery operator + if strings.Contains(kv, "=") { + split := strings.Split(kv, "=") + if split[1] == "*" { + slice[i] = split[0] + } } } + namespaceLabelSelector := strings.Join(slice[:], ",") + _, err := labels.Parse(namespaceLabelSelector) + if err != nil { + logrus.Fatal(err) + } + + return namespaceLabelSelector, nil +} + +func GetResourceLabelSelector(slice []string) (string, error) { + for i, kv := range slice { + // Legacy support for ":" as a delimiter and "*" for wildcard. + if strings.Contains(kv, ":") { + split := strings.Split(kv, ":") + if split[1] == "*" { + slice[i] = split[0] + } else { + slice[i] = split[0] + "=" + split[1] + } + } + // Convert wildcard to valid apimachinery operator + if strings.Contains(kv, "=") { + split := strings.Split(kv, "=") + if split[1] == "*" { + slice[i] = split[0] + } + } + } + + resourceLabelSelector := strings.Join(slice[:], ",") + _, err := labels.Parse(resourceLabelSelector) + if err != nil { + logrus.Fatal(err) + } + + return resourceLabelSelector, nil +} + +// ShouldReload checks if a resource should be reloaded based on its annotations and the provided options. +func ShouldReload(config Config, resourceType string, annotations Map, podAnnotations Map, options *ReloaderOptions) ReloadCheckResult { + ignoreResourceAnnotatonValue := config.ResourceAnnotations[options.IgnoreResourceAnnotation] if ignoreResourceAnnotatonValue == "true" { return ReloadCheckResult{ @@ -258,6 +311,7 @@ func GetCommandLineOptions() *ReloaderOptions { CommandLineOptions.ReloadOnCreate = parseBool(options.ReloadOnCreate) CommandLineOptions.ReloadOnDelete = parseBool(options.ReloadOnDelete) CommandLineOptions.EnablePProf = options.EnablePProf + CommandLineOptions.PProfAddr = options.PProfAddr return CommandLineOptions } diff --git a/internal/pkg/util/config.go b/pkg/common/config.go similarity index 81% rename from internal/pkg/util/config.go rename to pkg/common/config.go index 08313ea..4227c2b 100644 --- a/internal/pkg/util/config.go +++ b/pkg/common/config.go @@ -1,8 +1,9 @@ -package util +package common import ( "github.com/stakater/Reloader/internal/pkg/constants" "github.com/stakater/Reloader/internal/pkg/options" + "github.com/stakater/Reloader/internal/pkg/util" v1 "k8s.io/api/core/v1" ) @@ -15,6 +16,7 @@ type Config struct { TypedAutoAnnotation string SHAValue string Type string + Labels map[string]string } // GetConfigmapConfig provides utility config for configmap @@ -25,8 +27,9 @@ func GetConfigmapConfig(configmap *v1.ConfigMap) Config { ResourceAnnotations: configmap.Annotations, Annotation: options.ConfigmapUpdateOnChangeAnnotation, TypedAutoAnnotation: options.ConfigmapReloaderAutoAnnotation, - SHAValue: GetSHAfromConfigmap(configmap), + SHAValue: util.GetSHAfromConfigmap(configmap), Type: constants.ConfigmapEnvVarPostfix, + Labels: configmap.Labels, } } @@ -38,7 +41,8 @@ func GetSecretConfig(secret *v1.Secret) Config { ResourceAnnotations: secret.Annotations, Annotation: options.SecretUpdateOnChangeAnnotation, TypedAutoAnnotation: options.SecretReloaderAutoAnnotation, - SHAValue: GetSHAfromSecret(secret.Data), + SHAValue: util.GetSHAfromSecret(secret.Data), Type: constants.SecretEnvVarPostfix, + Labels: secret.Labels, } } diff --git a/internal/pkg/util/reload_source.go b/pkg/common/reload_source.go similarity index 98% rename from internal/pkg/util/reload_source.go rename to pkg/common/reload_source.go index 8344d17..0938261 100644 --- a/internal/pkg/util/reload_source.go +++ b/pkg/common/reload_source.go @@ -1,4 +1,4 @@ -package util +package common import "time"