From 914d84f7eaf3ce4fb17d36374effbc0531d88e9e Mon Sep 17 00:00:00 2001 From: "michal.kempski@sap.com" Date: Fri, 9 Aug 2019 11:36:54 +0200 Subject: [PATCH] Delete addons configs migration --- cmd/cm2cac/main.go | 58 -------------- cmd/cm2cac/migration.go | 82 ------------------- cmd/cm2cac/migration_test.go | 149 ----------------------------------- 3 files changed, 289 deletions(-) delete mode 100644 cmd/cm2cac/main.go delete mode 100644 cmd/cm2cac/migration.go delete mode 100644 cmd/cm2cac/migration_test.go diff --git a/cmd/cm2cac/main.go b/cmd/cm2cac/main.go deleted file mode 100644 index 75c8e6f1..00000000 --- a/cmd/cm2cac/main.go +++ /dev/null @@ -1,58 +0,0 @@ -package main - -import ( - "os" - - "github.com/sirupsen/logrus" - v1 "k8s.io/api/core/v1" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" - "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/kyma-project/helm-broker/pkg/apis/addons/v1alpha1" -) - -func main() { - sch, err := v1alpha1.SchemeBuilder.Build() - fatalOnError(err) - err = v1.AddToScheme(sch) - fatalOnError(err) - - namespace := os.Getenv("NAMESPACE") - if namespace == "" { - logrus.Fatal("Environment variable NAMESPACE must be set") - } - defaultCm := os.Getenv("DEFAULT_CONFIGMAP") - if defaultCm == "" { - logrus.Fatal("Environment variable DEFAULT_CONFIGMAP must be set") - } - k8sConfig, err := newRestClientConfig(os.Getenv("KUBECONFIG")) - fatalOnError(err) - - cli, err := client.New(k8sConfig, client.Options{ - Scheme: sch, - }) - fatalOnError(err) - - logrus.Info("Start ConfigMap to ClusterAddonsConfiguration migration process") - - migrationService := NewMigrationService(cli, namespace, defaultCm) - err = migrationService.Migrate() - fatalOnError(err) - - logrus.Info("Migration done") -} - -func newRestClientConfig(kubeConfigPath string) (*rest.Config, error) { - if kubeConfigPath != "" { - return clientcmd.BuildConfigFromFlags("", kubeConfigPath) - } - - return rest.InClusterConfig() -} - -func fatalOnError(err error) { - if err != nil { - logrus.Fatal(err.Error()) - } -} diff --git a/cmd/cm2cac/migration.go b/cmd/cm2cac/migration.go deleted file mode 100644 index c011e013..00000000 --- a/cmd/cm2cac/migration.go +++ /dev/null @@ -1,82 +0,0 @@ -package main - -import ( - "context" - "fmt" - "strings" - - "github.com/kyma-project/helm-broker/pkg/apis/addons/v1alpha1" - "github.com/sirupsen/logrus" - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/labels" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -// MigrationService is responsible for migration from ConfigMap to Cl -type MigrationService struct { - cli client.Client - namespace string - defaultCm string -} - -// NewMigrationService creates a new instance of MigrationService -func NewMigrationService(cli client.Client, namespace string, defaultCm string) *MigrationService { - return &MigrationService{ - cli: cli, - namespace: namespace, - defaultCm: defaultCm, - } -} - -// Migrate performs ConfigMap with URLs to ClusterAddonConfiguration migration. The method tries to migrate all config maps. It does not stop if any errors occurs. -func (s *MigrationService) Migrate() error { - configMaps := &v1.ConfigMapList{} - - err := s.cli.List(context.TODO(), &client.ListOptions{ - Namespace: s.namespace, - LabelSelector: labels.SelectorFromValidatedSet( - labels.Set(map[string]string{ - "helm-broker-repo": "true", - }))}, configMaps) - if err != nil { - return err - } - - for _, cm := range configMaps.Items { - if cm.Name == s.defaultCm { - continue - } - logrus.Infof("Migrating ConfigMap %s/%s", cm.Namespace, cm.Name) - err := s.migrateConfigMap(&cm) - if err != nil { - logrus.Errorf("could not migrate ConfigMap %s/%s, error: %s", cm.Namespace, cm.Name, err.Error()) - } - } - - return nil -} - -func (s *MigrationService) migrateConfigMap(cm *v1.ConfigMap) error { - urlsAsString, exists := cm.Data["URLs"] - if !exists { - return fmt.Errorf("could not find URLs fired in the 'Data'") - } - cac := &v1alpha1.ClusterAddonsConfiguration{} - cac.Spec.Repositories = []v1alpha1.SpecRepository{} - cac.Name = cm.Name - - for _, url := range strings.Split(urlsAsString, "\n") { - if strings.HasSuffix(url, "/") { - url = url + "index.yaml" - } - cac.Spec.Repositories = append(cac.Spec.Repositories, v1alpha1.SpecRepository{ - URL: url, - }) - } - - err := s.cli.Create(context.TODO(), cac) - if err != nil { - return err - } - return s.cli.Delete(context.TODO(), cm) -} diff --git a/cmd/cm2cac/migration_test.go b/cmd/cm2cac/migration_test.go deleted file mode 100644 index 61474fd1..00000000 --- a/cmd/cm2cac/migration_test.go +++ /dev/null @@ -1,149 +0,0 @@ -package main - -import ( - "context" - "strings" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - v1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - - "github.com/kyma-project/helm-broker/pkg/apis/addons/v1alpha1" -) - -const namespace = "stage" - -func TestMigrationService_MigrateOneURL(t *testing.T) { - // GIVEN - sch, err := v1alpha1.SchemeBuilder.Build() - v1.AddToScheme(sch) - require.NoError(t, err) - - repoURL := "http://url.to.repo.com/" - repoConfigMap := fixConfigMapWithRepoURLs("main", []string{repoURL}) - - cli := fake.NewFakeClientWithScheme(sch, repoConfigMap) - svc := NewMigrationService(cli, namespace, "default-repo-url") - - // WHEN - err = svc.Migrate() - require.NoError(t, err) - - // THEN - expCAC := &v1alpha1.ClusterAddonsConfiguration{} - err = cli.Get(context.TODO(), types.NamespacedName{Name: "main", Namespace: ""}, expCAC) - require.NoError(t, err) - assert.Equal(t, 1, len(expCAC.Spec.Repositories)) - assert.Equal(t, "http://url.to.repo.com/index.yaml", expCAC.Spec.Repositories[0].URL) - - assertNumberOfConfigMaps(t, cli, 0) -} - -func TestMigrationService_MigrateTwoURLs(t *testing.T) { - // GIVEN - sch, err := v1alpha1.SchemeBuilder.Build() - v1.AddToScheme(sch) - require.NoError(t, err) - - repoURL := "http://url.to.repo.com/index.yaml\nhttps://repo.com/prod.yaml" - repoConfigMap := fixConfigMapWithRepoURLs("main", []string{repoURL}) - - cli := fake.NewFakeClientWithScheme(sch, repoConfigMap) - svc := NewMigrationService(cli, namespace, "default-repo-url") - - // WHEN - err = svc.Migrate() - require.NoError(t, err) - - // THEN - expCAC := &v1alpha1.ClusterAddonsConfiguration{} - err = cli.Get(context.TODO(), types.NamespacedName{Name: "main"}, expCAC) - require.NoError(t, err) - assert.Equal(t, 2, len(expCAC.Spec.Repositories)) - assert.Equal(t, "http://url.to.repo.com/index.yaml", expCAC.Spec.Repositories[0].URL) - assert.Equal(t, "https://repo.com/prod.yaml", expCAC.Spec.Repositories[1].URL) - assertNumberOfConfigMaps(t, cli, 0) -} - -func TestMigrationService_MigrateTwoConfigMaps(t *testing.T) { - // GIVEN - sch, err := v1alpha1.SchemeBuilder.Build() - v1.AddToScheme(sch) - require.NoError(t, err) - - expectedURLs := map[string]string{ - "first": "http://frist.com/index.yaml", - "second": "http://second.com/index.yaml", - } - firstCM := fixConfigMapWithRepoURLs("first", []string{expectedURLs["first"]}) - secondCM := fixConfigMapWithRepoURLs("second", []string{expectedURLs["second"]}) - otherCM := &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "other"}} - - cli := fake.NewFakeClientWithScheme(sch, firstCM, secondCM, otherCM) - svc := NewMigrationService(cli, namespace, "default-repo-url") - - // WHEN - err = svc.Migrate() - require.NoError(t, err) - - // THEN - cacList := &v1alpha1.ClusterAddonsConfigurationList{} - err = cli.List(context.TODO(), &client.ListOptions{}, cacList) - require.NoError(t, err) - assert.Equal(t, 2, len(cacList.Items)) - - for _, cac := range cacList.Items { - expected, found := expectedURLs[cac.Name] - assert.True(t, found) - assert.Equal(t, expected, cac.Spec.Repositories[0].URL) - } - assertNumberOfConfigMaps(t, cli, 1) -} - -func TestMigrationService_NotMigrateDefaultConfiguration(t *testing.T) { - // GIVEN - sch, err := v1alpha1.SchemeBuilder.Build() - v1.AddToScheme(sch) - require.NoError(t, err) - - repoURL := "http://url.to.repo.com/" - repoConfigMap := fixConfigMapWithRepoURLs("main", []string{repoURL}) - - cli := fake.NewFakeClientWithScheme(sch, repoConfigMap) - svc := NewMigrationService(cli, namespace, "main") - - // WHEN - err = svc.Migrate() - require.NoError(t, err) - - // THEN - expCAC := &v1alpha1.ClusterAddonsConfiguration{} - err = cli.Get(context.TODO(), types.NamespacedName{Name: "main", Namespace: ""}, expCAC) - require.Error(t, err) - - assertNumberOfConfigMaps(t, cli, 1) -} - -func fixConfigMapWithRepoURLs(name string, urls []string) *v1.ConfigMap { - joinedURLs := strings.Join(urls, "\n") - return &v1.ConfigMap{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - Labels: map[string]string{"helm-broker-repo": "true"}, - }, - Data: map[string]string{"URLs": joinedURLs}, - } -} - -func assertNumberOfConfigMaps(t *testing.T, cli client.Client, expected int) { - cmList := &v1.ConfigMapList{} - err := cli.List(context.TODO(), &client.ListOptions{}, cmList) - require.NoError(t, err) - assert.Equal(t, expected, len(cmList.Items)) -}