From 996fa6f2313e31775dce954ff1964494c2b00386 Mon Sep 17 00:00:00 2001 From: Kostiantyn Kalynovskyi Date: Thu, 9 Apr 2020 16:36:01 -0500 Subject: [PATCH] Add CurrentContextCluster Type and Name methods These are convinience methods, that may be helpful if any of our modules need to have understanding which cluster they are operating against. Change-Id: Ib5d32ac3d4cd3170544bbc4d5bf3044926b52955 Relates-To: #162 --- pkg/config/config.go | 20 ++++++++++++++++++++ pkg/config/config_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/pkg/config/config.go b/pkg/config/config.go index f3596326a..101a134b6 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -646,6 +646,22 @@ func (c *Config) CurrentContextEntryPoint(clusterType string, phase string) (str phase), nil } +func (c *Config) CurrentContextClusterType() (string, error) { + context, err := c.GetCurrentContext() + if err != nil { + return "", err + } + return context.ClusterType(), nil +} + +func (c *Config) CurrentContextClusterName() (string, error) { + context, err := c.GetCurrentContext() + if err != nil { + return "", err + } + return context.ClusterName(), nil +} + // GetAuthInfo returns an instance of authino // Credential or AuthInfo related methods func (c *Config) GetAuthInfo(aiName string) (*AuthInfo, error) { @@ -789,6 +805,10 @@ func (c *Context) ClusterType() string { return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Type } +func (c *Context) ClusterName() string { + return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Name +} + // AuthInfo functions func (c *AuthInfo) String() string { kauthinfo := c.KubeAuthInfo() diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 74548e6b4..d22cd531c 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -547,6 +547,42 @@ func TestCurrentContextEntryPoint(t *testing.T) { assert.Nil(t, nil, entryPoint) } +func TestCurrentContextClusterType(t *testing.T) { + conf, cleanup := testutil.InitConfig(t) + defer cleanup(t) + + expectedClusterType := "ephemeral" + + clusterTypeEmpty, err := conf.CurrentContextClusterType() + require.Error(t, err) + assert.Equal(t, "", clusterTypeEmpty) + + conf.CurrentContext = currentContextName + conf.Contexts[currentContextName].Manifest = defaultString + + actualClusterType, err := conf.CurrentContextClusterType() + require.NoError(t, err) + assert.Equal(t, expectedClusterType, actualClusterType) +} + +func TestCurrentContextClusterName(t *testing.T) { + conf, cleanup := testutil.InitConfig(t) + defer cleanup(t) + + expectedClusterName := "def" + + clusterNameEmpty, err := conf.CurrentContextClusterName() + require.Error(t, err) + assert.Equal(t, "", clusterNameEmpty) + + conf.CurrentContext = currentContextName + conf.Contexts[currentContextName].Manifest = defaultString + + actualClusterName, err := conf.CurrentContextClusterName() + require.NoError(t, err) + assert.Equal(t, expectedClusterName, actualClusterName) +} + // AuthInfo Related func TestGetAuthInfos(t *testing.T) {