From a125dc5d8a2ec823ddddfc418ff0010a087d184f Mon Sep 17 00:00:00 2001 From: Dmitry Ukov Date: Wed, 5 Feb 2020 11:37:01 +0400 Subject: [PATCH] Fix cluster name parsing For cluster names that contain underscore ('_') symbol config module removed some part of the name due to hardcoded indexes after name split Change-Id: Ibd1e9b3ef2fce7e43cbdf41f4b1852933a253868 --- cmd/config/set_cluster_test.go | 2 +- pkg/config/config.go | 22 ++++++++++++++----- pkg/config/test_utils.go | 12 +++++----- pkg/config/testdata/cluster-string.yaml | 2 +- pkg/config/testdata/config-string.yaml | 6 ++--- pkg/config/testdata/context-string.yaml | 4 ++-- pkg/config/testdata/prettycluster-string.yaml | 4 ++-- 7 files changed, 32 insertions(+), 20 deletions(-) diff --git a/cmd/config/set_cluster_test.go b/cmd/config/set_cluster_test.go index 0d53240f4..dc74df3c1 100644 --- a/cmd/config/set_cluster_test.go +++ b/cmd/config/set_cluster_test.go @@ -40,7 +40,7 @@ type setClusterTest struct { } const ( - testCluster = "my-new-cluster" + testCluster = "my_new-cluster" ) func TestSetClusterWithCAFile(t *testing.T) { diff --git a/pkg/config/config.go b/pkg/config/config.go index 6396647f4..2856e883c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -930,15 +930,27 @@ func (c *ClusterComplexName) validName() bool { err := ValidClusterType(c.clusterType) return c.clusterName != "" && err == nil } + func (c *ClusterComplexName) FromName(clusterName string) { - if clusterName != "" { - userNameSplit := strings.Split(clusterName, AirshipClusterNameSep) - if len(userNameSplit) == 2 { - c.clusterType = userNameSplit[1] + if clusterName == "" { + return + } + + userNameSplit := strings.Split(clusterName, AirshipClusterNameSep) + if len(userNameSplit) == 1 { + c.clusterName = clusterName + return + } + + for _, cType := range AllClusterTypes { + if userNameSplit[len(userNameSplit)-1] == cType { + c.clusterType = userNameSplit[len(userNameSplit)-1] + c.clusterName = strings.Join(userNameSplit[:len(userNameSplit)-1], AirshipClusterNameSep) + return } - c.clusterName = userNameSplit[0] } } + func (c *ClusterComplexName) WithType(clusterName string, clusterType string) { c.FromName(clusterName) c.SetClusterType(clusterType) diff --git a/pkg/config/test_utils.go b/pkg/config/test_utils.go index a285d94e8..5fa6dc0a5 100644 --- a/pkg/config/test_utils.go +++ b/pkg/config/test_utils.go @@ -50,20 +50,20 @@ func DummyConfig() *Config { kubeConfig: kubeconfig.NewConfig(), } dummyCluster := conf.Clusters["dummy_cluster"] - conf.KubeConfig().Clusters["dummycluster_target"] = dummyCluster.ClusterTypes[Target].KubeCluster() - conf.KubeConfig().Clusters["dummycluster_ephemeral"] = dummyCluster.ClusterTypes[Ephemeral].KubeCluster() + conf.KubeConfig().Clusters["dummy_cluster_target"] = dummyCluster.ClusterTypes[Target].KubeCluster() + conf.KubeConfig().Clusters["dummy_cluster_ephemeral"] = dummyCluster.ClusterTypes[Ephemeral].KubeCluster() return conf } // DummyContext , utility function used for tests func DummyContext() *Context { c := NewContext() - c.NameInKubeconf = "dummy_cluster" + c.NameInKubeconf = "dummy_cluster_ephemeral" c.Manifest = "dummy_manifest" context := kubeconfig.NewContext() context.Namespace = "dummy_namespace" context.AuthInfo = "dummy_user" - context.Cluster = "dummycluster_ephemeral" + context.Cluster = "dummy_cluster_ephemeral" c.SetKubeContext(context) return c @@ -78,7 +78,7 @@ func DummyCluster() *Cluster { cluster.InsecureSkipTLSVerify = false cluster.CertificateAuthority = "dummy_ca" c.SetKubeCluster(cluster) - c.NameInKubeconf = "dummycluster_target" + c.NameInKubeconf = "dummy_cluster_target" c.Bootstrap = "dummy_bootstrap_config" return c } @@ -125,7 +125,7 @@ func DummyModules() *Modules { func DummyClusterPurpose() *ClusterPurpose { cp := NewClusterPurpose() cp.ClusterTypes["ephemeral"] = DummyCluster() - cp.ClusterTypes["ephemeral"].NameInKubeconf = "dummycluster_ephemeral" + cp.ClusterTypes["ephemeral"].NameInKubeconf = "dummy_cluster_ephemeral" cp.ClusterTypes["target"] = DummyCluster() return cp } diff --git a/pkg/config/testdata/cluster-string.yaml b/pkg/config/testdata/cluster-string.yaml index 1173f9997..8da43a305 100644 --- a/pkg/config/testdata/cluster-string.yaml +++ b/pkg/config/testdata/cluster-string.yaml @@ -1,5 +1,5 @@ bootstrap-info: dummy_bootstrap_config -cluster-kubeconf: dummycluster_target +cluster-kubeconf: dummy_cluster_target LocationOfOrigin: "" certificate-authority: dummy_ca diff --git a/pkg/config/testdata/config-string.yaml b/pkg/config/testdata/config-string.yaml index 9aca29ac0..7274cd532 100644 --- a/pkg/config/testdata/config-string.yaml +++ b/pkg/config/testdata/config-string.yaml @@ -4,13 +4,13 @@ clusters: cluster-type: ephemeral: bootstrap-info: dummy_bootstrap_config - cluster-kubeconf: dummycluster_ephemeral + cluster-kubeconf: dummy_cluster_ephemeral target: bootstrap-info: dummy_bootstrap_config - cluster-kubeconf: dummycluster_target + cluster-kubeconf: dummy_cluster_target contexts: dummy_context: - context-kubeconf: dummy_cluster + context-kubeconf: dummy_cluster_ephemeral manifest: dummy_manifest current-context: dummy_context kind: Config diff --git a/pkg/config/testdata/context-string.yaml b/pkg/config/testdata/context-string.yaml index b4cca1a5b..0ec7bc8b8 100644 --- a/pkg/config/testdata/context-string.yaml +++ b/pkg/config/testdata/context-string.yaml @@ -1,7 +1,7 @@ -context-kubeconf: dummy_cluster +context-kubeconf: dummy_cluster_ephemeral manifest: dummy_manifest LocationOfOrigin: "" -cluster: dummycluster_ephemeral +cluster: dummy_cluster_ephemeral namespace: dummy_namespace user: dummy_user diff --git a/pkg/config/testdata/prettycluster-string.yaml b/pkg/config/testdata/prettycluster-string.yaml index 07ed63f8a..4e3d6b5fc 100644 --- a/pkg/config/testdata/prettycluster-string.yaml +++ b/pkg/config/testdata/prettycluster-string.yaml @@ -1,7 +1,7 @@ -Cluster: dummycluster +Cluster: dummy_cluster target: bootstrap-info: dummy_bootstrap_config -cluster-kubeconf: dummycluster_target +cluster-kubeconf: dummy_cluster_target LocationOfOrigin: "" certificate-authority: dummy_ca