Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Oct 10, 2023
1 parent 7fb7ba8 commit 887b0c5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
47 changes: 24 additions & 23 deletions config/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,7 @@ func validateContext(c *configtypes.Context) error {
//
// Deprecated: GetCurrentContext is deprecated. Use GetActiveContext instead
func GetCurrentContext(target configtypes.Target) (c *configtypes.Context, err error) {
// Retrieve client config node
node, err := getClientConfigNode()
if err != nil {
return nil, err
}
return getActiveContext(node, configtypes.ConvertTargetToContextType(target))
return GetActiveContext(configtypes.ConvertTargetToContextType(target))
}

// GetActiveContext retrieves the active context for the specified contextType
Expand Down Expand Up @@ -192,8 +187,8 @@ func GetAllActiveContextsMap() (map[configtypes.ContextType]*configtypes.Context
return getAllActiveContextsMap(node)
}

// GetAllCurrentContextsList returns all current context names as list
func GetAllCurrentContextsList() ([]string, error) {
// GetAllActiveContextsList returns all active context names as list
func GetAllActiveContextsList() ([]string, error) {
currentContextsMap, err := GetAllCurrentContextsMap()
if err != nil {
return nil, err
Expand All @@ -205,6 +200,13 @@ func GetAllCurrentContextsList() ([]string, error) {
return serverNames, nil
}

// GetAllCurrentContextsList returns all current context names as list
//
// Deprecated: GetAllCurrentContextsList is deprecated. Use GetAllActiveContextsList instead
func GetAllCurrentContextsList() ([]string, error) {
return GetAllActiveContextsList()
}

// SetCurrentContext sets the current context to the specified name if context is present
func SetCurrentContext(name string) error {
// Retrieve client config node
Expand Down Expand Up @@ -245,7 +247,15 @@ func SetCurrentContext(name string) error {
}

// RemoveCurrentContext removed the current context of specified context type
//
// Deprecated: RemoveCurrentContext is deprecated. Use RemoveActiveContext instead
func RemoveCurrentContext(target configtypes.Target) error {
return RemoveActiveContext(configtypes.ConvertTargetToContextType(target))
}

// RemoveActiveContext removed the current context of specified context type
func RemoveActiveContext(contextType configtypes.ContextType) error {
target := configtypes.ConvertContextTypeToTarget(contextType)
// Retrieve client config node
AcquireTanzuConfigLock()
defer ReleaseTanzuConfigLock()
Expand All @@ -268,22 +278,17 @@ func RemoveCurrentContext(target configtypes.Target) error {
return persistConfig(node)
}

// RemoveActiveContext removed the current context of specified context type
func RemoveActiveContext(contextType configtypes.ContextType) error {
return RemoveCurrentContext(configtypes.ConvertContextTypeToTarget(contextType))
}

// EndpointFromContext retrieved the endpoint from the specified context
func EndpointFromContext(s *configtypes.Context) (endpoint string, err error) {
switch s.Target {
case configtypes.TargetK8s:
switch s.ContextType {
case configtypes.ContextTypeK8s:
return s.ClusterOpts.Endpoint, nil
case configtypes.TargetTMC:
case configtypes.ContextTypeTMC:
return s.GlobalOpts.Endpoint, nil
case configtypes.TargetTAE:
case configtypes.ContextTypeTAE:
return s.ClusterOpts.Endpoint, nil
default:
return endpoint, fmt.Errorf("unknown server type %q", s.Target)
return endpoint, fmt.Errorf("unknown context type %q", s.ContextType)
}
}

Expand All @@ -306,11 +311,7 @@ func getContext(node *yaml.Node, name string) (*configtypes.Context, error) {
}

func getCurrentContext(node *yaml.Node, target configtypes.Target) (*configtypes.Context, error) {
cfg, err := convertNodeToClientConfig(node)
if err != nil {
return nil, err
}
return cfg.GetCurrentContext(target) //nolint:staticcheck // Deprecated
return getActiveContext(node, configtypes.ConvertTargetToContextType(target))
}

func getActiveContext(node *yaml.Node, contextType configtypes.ContextType) (*configtypes.Context, error) {
Expand Down
26 changes: 14 additions & 12 deletions config/types/clientconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ import (
"strings"
)

// ContextType is a new Datatype introduced to represent the type of control plane
// ContextType defines the type of control plane endpoint a context represents
type ContextType string

const (
// ContextTypeK8s represents a type of control plane endpoint that is a Kubernetes cluster
ContextTypeK8s ContextType = "kubernetes"
contextTypeK8s ContextType = "k8s"

// ContextTypeTMC represents a type of control plane endpoint that is a TMC SaaS/self-managed endpoint
ContextTypeTMC ContextType = "mission-control"
contextTypeTMC ContextType = "tmc"

// ContextTypeTAE is a used to indicate the type of Context used to interact with
// Tanzu Application Engine (Unified Control Plane) endpoint
// Note!! Experimental, please expect changes
ContextTypeTAE ContextType = "application-engine"
contextTypeTAE ContextType = "tae"
)

// Target is the namespace of the CLI to which plugin is applicable
Expand Down Expand Up @@ -121,7 +124,7 @@ func (s *Server) IsManagementCluster() bool {

// GetCurrentServer returns the current server.
//
// Deprecated: This API is deprecated. Use GetActiveContext() instead.
// Deprecated: GetCurrentServer is deprecated. Use GetActiveContext() instead.
func (c *ClientConfig) GetCurrentServer() (*Server, error) {
for _, server := range c.KnownServers {
if server.Name == c.CurrentServer {
Expand Down Expand Up @@ -205,8 +208,8 @@ func (c *ClientConfig) GetAllActiveContextsMap() (map[ContextType]*Context, erro
return currentContexts, nil
}

// GetAllCurrentContextsList returns all current context names as list
func (c *ClientConfig) GetAllCurrentContextsList() ([]string, error) {
// GetAllActiveContextsList returns all active context names as list
func (c *ClientConfig) GetAllActiveContextsList() ([]string, error) {
var serverNames []string
currentContextsMap, err := c.GetAllCurrentContextsMap()
if err != nil {
Expand All @@ -219,6 +222,13 @@ func (c *ClientConfig) GetAllCurrentContextsList() ([]string, error) {
return serverNames, nil
}

// GetAllCurrentContextsList returns all current context names as list
//
// Deprecated: GetAllCurrentContextsList is deprecated. Use GetAllActiveContextsList instead
func (c *ClientConfig) GetAllCurrentContextsList() ([]string, error) {
return c.GetAllActiveContextsList()
}

// SetCurrentContext sets the current context for the given target.
//
// Deprecated: SetCurrentContext is deprecated. Use SetActiveContext instead
Expand Down Expand Up @@ -340,14 +350,6 @@ func (c *ClientConfig) SetEditionSelector(edition EditionSelector) {
c.ClientOptions.CLI.UnstableVersionSelector = EditionStandard
}

func SyncContextTypeAndTarget(ctx *Context) {
if ctx.ContextType == "" {
ctx.ContextType = ConvertTargetToContextType(ctx.Target)
} else if ctx.Target == "" {
ctx.Target = ConvertContextTypeToTarget(ctx.ContextType)
}
}

func ConvertTargetToContextType(target Target) ContextType {
switch target {
case TargetK8s:
Expand Down
12 changes: 12 additions & 0 deletions config/types/clientconfig_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,15 @@ func IsValidTarget(target string, allowGlobal, allowUnknown bool) bool {
(allowGlobal && target == string(TargetGlobal)) ||
(allowUnknown && target == string(TargetUnknown))
}

// StringToContextType converts string to ContextType
func StringToContextType(contextType string) ContextType {
if contextType == string(contextTypeK8s) || contextType == string(ContextTypeK8s) {
return ContextTypeK8s
} else if contextType == string(contextTypeTMC) || contextType == string(ContextTypeTMC) {
return ContextTypeTMC
} else if contextType == string(contextTypeTAE) || contextType == string(ContextTypeTAE) {
return ContextTypeTAE
}
return ""
}
2 changes: 1 addition & 1 deletion config/types/clientconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Server struct {
// Context configuration for a control plane. This can one of the following,
// 1. Kubernetes Cluster
// 2. Tanzu Mission Control endpoint
// 3. Unified Control Plane endpoint
// 3. Tanzu Application Engine endpoint
type Context struct {
// Name of the context.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Expand Down

0 comments on commit 887b0c5

Please sign in to comment.