Simplifies annotations for searching secrets for reload.

This commit is contained in:
Vlad Losev
2020-06-09 19:16:03 -07:00
parent 5980c91560
commit 17f8b81110
6 changed files with 24 additions and 83 deletions
+2 -2
View File
@@ -27,8 +27,8 @@ func NewReloaderCommand() *cobra.Command {
cmd.PersistentFlags().StringVar(&options.ConfigmapUpdateOnChangeAnnotation, "configmap-annotation", "configmap.reloader.stakater.com/reload", "annotation to detect changes in configmaps, specified by name")
cmd.PersistentFlags().StringVar(&options.SecretUpdateOnChangeAnnotation, "secret-annotation", "secret.reloader.stakater.com/reload", "annotation to detect changes in secrets, specified by name")
cmd.PersistentFlags().StringVar(&options.ReloaderAutoAnnotation, "auto-annotation", "reloader.stakater.com/auto", "annotation to detect changes in secrets")
cmd.PersistentFlags().StringVar(&options.ConfigmapUpdateAutoSearchAnnotation, "configmap-auto-by-annotation", "configmap.reloader.stakater.com/reload-by-annotation", "annotation to detect changes in configmaps, searched by annotation")
cmd.PersistentFlags().StringVar(&options.SecretUpdateAutoSearchAnnotation, "secret-auto-by-annotation", "secret.reloader.stakater.com/reload-by-annotation", "annotation to detect changes in secret, searched by annotations")
cmd.PersistentFlags().StringVar(&options.AutoSearchAnnotation, "auto-search-annotation", "reloader.stakater.com/annotated", "annotation to detect changes in configmaps or secrets tagged with special match annotation")
cmd.PersistentFlags().StringVar(&options.SearchMatchAnnotation, "search-match-annotation", "reloader.stakater.com/match", "annotation to mark secrets or configmapts to match the search")
cmd.PersistentFlags().StringVar(&options.LogFormat, "log-format", "", "Log format to use (empty string for text, or JSON")
cmd.PersistentFlags().StringSlice("resources-to-ignore", []string{}, "list of resources to ignore (valid options 'configMaps' or 'secrets')")
cmd.PersistentFlags().StringSlice("namespaces-to-ignore", []string{}, "list of namespaces to ignore")
+5 -11
View File
@@ -99,12 +99,12 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc
// find correct annotation and update the resource
annotations := upgradeFuncs.AnnotationsFunc(i)
annotationValue, found := annotations[config.Annotation]
searchAnnotationValue, foundSearchAnn := annotations[config.SearchAnnotation]
searchAnnotationValue, foundSearchAnn := annotations[options.AutoSearchAnnotation]
reloaderEnabledValue, foundAuto := annotations[options.ReloaderAutoAnnotation]
if !found && !foundAuto && !foundSearchAnn {
annotations = upgradeFuncs.PodAnnotationsFunc(i)
annotationValue = annotations[config.Annotation]
searchAnnotationValue = annotations[config.SearchAnnotation]
searchAnnotationValue = annotations[options.AutoSearchAnnotation]
reloaderEnabledValue = annotations[options.ReloaderAutoAnnotation]
}
result := constants.NotUpdated
@@ -125,15 +125,9 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc
}
}
if result != constants.Updated && searchAnnotationValue != "" {
keyValue := strings.Split(searchAnnotationValue, "=")
key := keyValue[0]
searchValue := ""
if len(keyValue) > 1 {
searchValue = keyValue[1]
}
value, found := config.ResourceAnnotations[key]
if found && searchValue == "" || value == searchValue {
if result != constants.Updated && searchAnnotationValue == "true" {
matchAnnotationValue := config.ResourceAnnotations[options.SearchMatchAnnotation]
if matchAnnotationValue == "true" {
result = updateContainers(upgradeFuncs, i, config, true)
}
}
+8 -51
View File
@@ -646,8 +646,8 @@ func createConfigMap(clients *kube.Clients, namespace, name string, annotations
}
func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotation(t *testing.T) {
annotatedConfigmapName := "testconfigmapAnnotated-handler-" + testutil.RandSeq(5)
configmap, err := createConfigMap(&clients, namespace, annotatedConfigmapName, map[string]string{"test-annotation": "test"})
annotatedConfigmapName := "testconfigmapAnnotated-handler"
configmap, err := createConfigMap(&clients, namespace, annotatedConfigmapName, map[string]string{"reloader.stakater.com/match": "true"})
if err != nil {
t.Errorf("Failed to create config map with annotation.")
}
@@ -656,7 +656,7 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotation(t *testing.
clients.KubernetesClient,
annotatedConfigmapName,
namespace,
map[string]string{options.ConfigmapUpdateAutoSearchAnnotation: "test-annotation=test"},
map[string]string{"reloader.stakater.com/annotated": "true"},
)
if err != nil {
t.Errorf("Failed to create deployment with search annotation.")
@@ -665,7 +665,6 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotation(t *testing.
shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, annotatedConfigmapName, "www.stakater.com")
config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, annotatedConfigmapName, shaData, "")
config.SearchAnnotation = options.ConfigmapUpdateAutoSearchAnnotation
config.ResourceAnnotations = configmap.Annotations
deploymentFuncs := GetDeploymentRollingUpgradeFuncs()
collectors := getCollectors()
@@ -686,9 +685,9 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotation(t *testing.
}
}
func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNoValue(t *testing.T) {
func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNoTriggers(t *testing.T) {
annotatedConfigmapName := "testconfigmapAnnotated-handler-" + testutil.RandSeq(5)
configmap, err := createConfigMap(&clients, namespace, annotatedConfigmapName, map[string]string{"test-annotation": "test"})
configmap, err := createConfigMap(&clients, namespace, annotatedConfigmapName, map[string]string{"reloader.stakater.com/match": "false"})
if err != nil {
t.Errorf("Failed to create config map with annotation.")
}
@@ -696,7 +695,7 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNoValue(t *t
clients.KubernetesClient,
annotatedConfigmapName,
namespace,
map[string]string{options.ConfigmapUpdateAutoSearchAnnotation: "test-annotation"},
map[string]string{"reloader.stakater.com/annotated": "true"},
)
if err != nil {
t.Errorf("Failed to create deployment with search annotation.")
@@ -705,47 +704,6 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNoValue(t *t
shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, annotatedConfigmapName, "www.stakater.com")
config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, annotatedConfigmapName, shaData, "")
config.SearchAnnotation = options.ConfigmapUpdateAutoSearchAnnotation
config.ResourceAnnotations = configmap.Annotations
deploymentFuncs := GetDeploymentRollingUpgradeFuncs()
collectors := getCollectors()
err = PerformRollingUpgrade(clients, config, deploymentFuncs, collectors)
if err != nil {
t.Errorf("Rolling upgrade failed for Deployment with Configmap")
}
logrus.Infof("Verifying deployment update")
updated := testutil.VerifyResourceUpdate(clients, config, constants.ConfigmapEnvVarPostfix, deploymentFuncs)
if !updated {
t.Errorf("Deployment was not updated")
}
if promtestutil.ToFloat64(collectors.Reloaded.With(labelSucceeded)) != 1 {
t.Errorf("Counter was not increased")
}
}
func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNotFound(t *testing.T) {
annotatedConfigmapName := "testconfigmapAnnotated-handler-" + testutil.RandSeq(5)
configmap, err := createConfigMap(&clients, namespace, annotatedConfigmapName, map[string]string{"test-annotation": "not-found"})
if err != nil {
t.Errorf("Failed to create config map with annotation.")
}
defer clients.KubernetesClient.CoreV1().ConfigMaps(namespace).Delete(configmap.Name, &v1.DeleteOptions{})
deployment, err := testutil.CreateDeploymentWithEnvVarSourceAndAnnotations(
clients.KubernetesClient,
annotatedConfigmapName,
namespace,
map[string]string{options.ConfigmapUpdateAutoSearchAnnotation: "test-annotation=test"},
)
if err != nil {
t.Errorf("Failed to create deployment with search annotation.")
}
defer clients.KubernetesClient.AppsV1().Deployments(namespace).Delete(deployment.Name, &v1.DeleteOptions{})
shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, annotatedConfigmapName, "www.stakater.com")
config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, annotatedConfigmapName, shaData, "")
config.SearchAnnotation = options.ConfigmapUpdateAutoSearchAnnotation
config.ResourceAnnotations = configmap.Annotations
deploymentFuncs := GetDeploymentRollingUpgradeFuncs()
collectors := getCollectors()
@@ -768,7 +726,7 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNotFound(t *
func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNotMapped(t *testing.T) {
annotatedConfigmapName := "testconfigmapAnnotated-handler-" + testutil.RandSeq(5)
configmap, err := createConfigMap(&clients, namespace, annotatedConfigmapName, map[string]string{"test-annotation": "test"})
configmap, err := createConfigMap(&clients, namespace, annotatedConfigmapName, map[string]string{"reloader.stakater.com/match": "true"})
if err != nil {
t.Errorf("Failed to create config map with annotation.")
}
@@ -777,7 +735,7 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNotMapped(t
clients.KubernetesClient,
annotatedConfigmapName+"-different",
namespace,
map[string]string{options.ConfigmapUpdateAutoSearchAnnotation: "test-annotation=test"},
map[string]string{"reloader.stakater.com/annotated": "true"},
)
if err != nil {
t.Errorf("Failed to create deployment with search annotation.")
@@ -786,7 +744,6 @@ func TestRollingUpgradeForDeploymentWithConfigmapViaSearchAnnotationNotMapped(t
shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, annotatedConfigmapName, "www.stakater.com")
config := getConfigWithAnnotations(constants.ConfigmapEnvVarPostfix, annotatedConfigmapName, shaData, "")
config.SearchAnnotation = options.ConfigmapUpdateAutoSearchAnnotation
config.ResourceAnnotations = configmap.Annotations
deploymentFuncs := GetDeploymentRollingUpgradeFuncs()
collectors := getCollectors()
+6 -6
View File
@@ -9,12 +9,12 @@ var (
SecretUpdateOnChangeAnnotation = "secret.reloader.stakater.com/reload"
// ReloaderAutoAnnotation is an annotation to detect changes in secrets
ReloaderAutoAnnotation = "reloader.stakater.com/auto"
// ConfigmapUpdateAutoSearchAnnotation is an annotation to detect changes in
// configmaps searched by annotation
ConfigmapUpdateAutoSearchAnnotation = "configmap.reloader.stakater.com/auto-by-annotation"
// SecretUpdateAutoSearchAnnotation is an annotation to detect changes in
// secrets searched by annotation
SecretUpdateAutoSearchAnnotation = "secret.reloader.stakater.com/auto-by-annotation"
// AutoSearchAnnotation is an annotation to detect changes in
// configmaps or triggers with the SearchMatchAnnotation
AutoSearchAnnotation = "reloader.stakater.com/annotated"
// SearchMatchAnnotation is an annotation to tag secrets to be found with
// AutoSearchAnnotation
SearchMatchAnnotation = "reloader.stakater.com/match"
// LogFormat is the log format to use (json, or empty string for default)
LogFormat = ""
)
+3 -10
View File
@@ -788,7 +788,7 @@ func VerifyResourceUpdate(clients kube.Clients, config util.Config, envVarPostfi
containers := upgradeFuncs.ContainersFunc(i)
// match statefulsets with the correct annotation
annotationValue := util.ToObjectMeta(i).Annotations[config.Annotation]
searchAnnotationValue := util.ToObjectMeta(i).Annotations[config.SearchAnnotation]
searchAnnotationValue := util.ToObjectMeta(i).Annotations[options.AutoSearchAnnotation]
reloaderEnabledValue := util.ToObjectMeta(i).Annotations[options.ReloaderAutoAnnotation]
reloaderEnabled, err := strconv.ParseBool(reloaderEnabledValue)
matches := false
@@ -802,15 +802,8 @@ func VerifyResourceUpdate(clients kube.Clients, config util.Config, envVarPostfi
break
}
}
} else if searchAnnotationValue != "" {
keyValue := strings.Split(searchAnnotationValue, "=")
key := keyValue[0]
valueToSearch := ""
if len(keyValue) > 1 {
valueToSearch = keyValue[1]
}
value, found := config.ResourceAnnotations[key]
if found && (valueToSearch == "" || value == valueToSearch) {
} else if searchAnnotationValue == "true" {
if config.ResourceAnnotations[options.SearchMatchAnnotation] == "true" {
matches = true
}
}
-3
View File
@@ -12,7 +12,6 @@ type Config struct {
ResourceName string
ResourceAnnotations map[string]string
Annotation string
SearchAnnotation string
SHAValue string
Type string
}
@@ -24,7 +23,6 @@ func GetConfigmapConfig(configmap *v1.ConfigMap) Config {
ResourceName: configmap.Name,
ResourceAnnotations: configmap.Annotations,
Annotation: options.ConfigmapUpdateOnChangeAnnotation,
SearchAnnotation: options.ConfigmapUpdateAutoSearchAnnotation,
SHAValue: GetSHAfromConfigmap(configmap.Data),
Type: constants.ConfigmapEnvVarPostfix,
}
@@ -37,7 +35,6 @@ func GetSecretConfig(secret *v1.Secret) Config {
ResourceName: secret.Name,
ResourceAnnotations: secret.Annotations,
Annotation: options.SecretUpdateOnChangeAnnotation,
SearchAnnotation: options.SecretUpdateAutoSearchAnnotation,
SHAValue: GetSHAfromSecret(secret.Data),
Type: constants.SecretEnvVarPostfix,
}