From 94a0c8a319833fac20a4151fc03f5ef6e139a8b9 Mon Sep 17 00:00:00 2001 From: Tobias Schuhmacher Date: Mon, 28 Aug 2023 17:24:41 +0200 Subject: [PATCH] ignore CRDs of skipped components (#1388) * Ignore CRDs of skipped components * add test for env-var skip-comp logic * add logging * add correlation ID --- pkg/model/clusterconfigentity.go | 20 ++++++------ pkg/model/clusterconfigentity_test.go | 7 +++-- pkg/reconciler/service/install.go | 28 ++++++++++++++--- pkg/reconciler/service/install_test.go | 42 ++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 pkg/reconciler/service/install_test.go diff --git a/pkg/model/clusterconfigentity.go b/pkg/model/clusterconfigentity.go index 23cfe109b..87716708b 100644 --- a/pkg/model/clusterconfigentity.go +++ b/pkg/model/clusterconfigentity.go @@ -4,6 +4,11 @@ import ( "context" "encoding/json" "fmt" + "os" + "reflect" + "strings" + "time" + log "github.com/kyma-incubator/reconciler/pkg/logger" "github.com/kyma-incubator/reconciler/pkg/scheduler/config" "go.uber.org/zap" @@ -13,20 +18,17 @@ import ( "k8s.io/client-go/dynamic" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" - "os" - "reflect" - "strings" - "time" "github.com/kyma-incubator/reconciler/pkg/db" "github.com/kyma-incubator/reconciler/pkg/keb" ) const ( - CRDComponent = "CRDs" - CleanupComponent = "cleaner" - DeleteStrategyKey = "delete_strategy" - tblConfiguration string = "inventory_cluster_configs" + CRDComponent = "CRDs" + CleanupComponent = "cleaner" + DeleteStrategyKey = "delete_strategy" + tblConfiguration = "inventory_cluster_configs" + SkippedComponentEnvVarPrefix = "SKIP_COMPONENT_" ) var ( @@ -179,7 +181,7 @@ func (c *ClusterConfigurationEntity) nonMigratedComponents(cfg *ReconciliationSe continue } //ignore component if the SKIP_COMPONENT_XYZ env-var is defined - envVar := fmt.Sprintf("SKIP_COMPONENT_%s", strings.ReplaceAll(strings.ToUpper(comp.Component), "-", "_")) + envVar := fmt.Sprintf("%s%s", SkippedComponentEnvVarPrefix, strings.ReplaceAll(strings.ToUpper(comp.Component), "-", "_")) skipComp := os.Getenv(envVar) if strings.ToLower(skipComp) == "true" || skipComp == "1" { logger.Infof("Skipping component %s (env-var: %s = %s)", comp.Component, envVar, skipComp) diff --git a/pkg/model/clusterconfigentity_test.go b/pkg/model/clusterconfigentity_test.go index 27a84619f..751de6a72 100644 --- a/pkg/model/clusterconfigentity_test.go +++ b/pkg/model/clusterconfigentity_test.go @@ -3,12 +3,13 @@ package model import ( "context" "fmt" + "os" + "testing" + log "github.com/kyma-incubator/reconciler/pkg/logger" "github.com/kyma-incubator/reconciler/pkg/reconciler/kubernetes" "github.com/kyma-incubator/reconciler/pkg/scheduler/config" "github.com/kyma-incubator/reconciler/pkg/test" - "os" - "testing" "github.com/kyma-incubator/reconciler/pkg/keb" "github.com/stretchr/testify/require" @@ -510,7 +511,7 @@ func TestReconciliationSequenceWithMigratedComponents(t *testing.T) { }, }, err: nil, - envVars: map[string]string{"SKIP_COMPONENT_COMP3": "1"}, + envVars: map[string]string{fmt.Sprintf("%s%s", SkippedComponentEnvVarPrefix, "COMP3"): "1"}, }, } diff --git a/pkg/reconciler/service/install.go b/pkg/reconciler/service/install.go index db1d27bbf..aa566acbd 100644 --- a/pkg/reconciler/service/install.go +++ b/pkg/reconciler/service/install.go @@ -3,6 +3,9 @@ package service import ( "context" "fmt" + "os" + "strings" + "github.com/kyma-incubator/reconciler/pkg/model" "github.com/kyma-incubator/reconciler/pkg/reconciler" "github.com/kyma-incubator/reconciler/pkg/reconciler/chart" @@ -113,12 +116,12 @@ func (r *Install) renderManifest(chartProvider chart.Provider, model *reconciler func (r *Install) renderCRDs(chartProvider chart.Provider, model *reconciler.Task) (string, error) { var crdManifests []*chart.Manifest var err error + var skippedComps = r.skippedComps(model) if r.ignoreIstioCRD(model) { - r.logger.Info("Istio CRDs will be ignored from reconciliation") - crdManifests, err = chartProvider.RenderCRDFiltered(model.Version, []string{"istio"}) - } else { - crdManifests, err = chartProvider.RenderCRD(model.Version) + r.logger.Infof("Istio CRDs will be ignored from reconciliation (correlation-ID: %s)", model.CorrelationID) + skippedComps = append(skippedComps, "istio") } + crdManifests, err = chartProvider.RenderCRDFiltered(model.Version, skippedComps) if err != nil { msg := fmt.Sprintf("Failed to get CRD manifests for Kyma version '%s'", model.Version) r.logger.Errorf("%s: %s", msg, err) @@ -166,3 +169,20 @@ func (r *Install) ignoreIstioCRD(task *reconciler.Task) bool { return false } + +func (r *Install) skippedComps(task *reconciler.Task) []string { + envVars := os.Environ() + skippedComps := []string{} + //Search for skipped components by checking all env-vars + for _, envVar := range envVars { + envPair := strings.SplitN(envVar, "=", 2) + //extract the component name from the env-var and append it to the slice of skippedComps + if strings.HasPrefix(envPair[0], model.SkippedComponentEnvVarPrefix) && (envPair[1] == "1" || strings.ToLower(envPair[1]) == "true") { + compNameRaw := strings.Replace(envPair[0], model.SkippedComponentEnvVarPrefix, "", 1) + compName := strings.ToLower(strings.ReplaceAll(compNameRaw, "_", "-")) + skippedComps = append(skippedComps, compName) + r.logger.Infof("%s CRDs will be ignored from reconciliation (skipped by env-var, correlation-ID: %s)", compName, task.CorrelationID) + } + } + return skippedComps +} diff --git a/pkg/reconciler/service/install_test.go b/pkg/reconciler/service/install_test.go new file mode 100644 index 000000000..79ff45c46 --- /dev/null +++ b/pkg/reconciler/service/install_test.go @@ -0,0 +1,42 @@ +package service + +import ( + "fmt" + "os" + "testing" + + "github.com/kyma-incubator/reconciler/pkg/logger" + "github.com/kyma-incubator/reconciler/pkg/model" + "github.com/kyma-incubator/reconciler/pkg/reconciler" + "github.com/stretchr/testify/require" +) + +func TestInstall(t *testing.T) { + tests := []struct { + envVars map[string]string + expected []string + }{ + { + envVars: map[string]string{ + "IM_INVALID": "true", + fmt.Sprintf("%s%s", model.SkippedComponentEnvVarPrefix, "ABC"): "tRue", + fmt.Sprintf("%s%s", model.SkippedComponentEnvVarPrefix, "DE_F"): "1", + fmt.Sprintf("%s%s", model.SkippedComponentEnvVarPrefix, "GH"): "0", + fmt.Sprintf("%s%s", model.SkippedComponentEnvVarPrefix, "XYZ"): "truee", + }, + expected: []string{"abc", "de-f"}, + }, + } + + for _, testCase := range tests { + install := NewInstall(logger.NewTestLogger(t)) + for envKey, envValue := range testCase.envVars { + os.Setenv(envKey, envValue) + } + got := install.skippedComps(&reconciler.Task{ + CorrelationID: "unit-test-corrID", + }) + require.Len(t, got, len(testCase.expected)) + require.ElementsMatch(t, testCase.expected, got) + } +}