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
This commit is contained in:
Kostiantyn Kalynovskyi 2020-04-09 16:36:01 -05:00
parent f555af3bf6
commit 996fa6f231
2 changed files with 56 additions and 0 deletions

View File

@ -646,6 +646,22 @@ func (c *Config) CurrentContextEntryPoint(clusterType string, phase string) (str
phase), nil 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 // GetAuthInfo returns an instance of authino
// Credential or AuthInfo related methods // Credential or AuthInfo related methods
func (c *Config) GetAuthInfo(aiName string) (*AuthInfo, error) { func (c *Config) GetAuthInfo(aiName string) (*AuthInfo, error) {
@ -789,6 +805,10 @@ func (c *Context) ClusterType() string {
return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Type return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Type
} }
func (c *Context) ClusterName() string {
return NewClusterComplexNameFromKubeClusterName(c.NameInKubeconf).Name
}
// AuthInfo functions // AuthInfo functions
func (c *AuthInfo) String() string { func (c *AuthInfo) String() string {
kauthinfo := c.KubeAuthInfo() kauthinfo := c.KubeAuthInfo()

View File

@ -547,6 +547,42 @@ func TestCurrentContextEntryPoint(t *testing.T) {
assert.Nil(t, nil, entryPoint) 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 // AuthInfo Related
func TestGetAuthInfos(t *testing.T) { func TestGetAuthInfos(t *testing.T) {