Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

ignore CRDs of skipped components #1388

Merged
merged 4 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pkg/model/clusterconfigentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions pkg/model/clusterconfigentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"},
},
}

Expand Down
28 changes: 24 additions & 4 deletions pkg/reconciler/service/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
42 changes: 42 additions & 0 deletions pkg/reconciler/service/install_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}