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) {