Skip to content

Commit

Permalink
Removes cluster from kubeconfig when deleting
Browse files Browse the repository at this point in the history
If the kubeconfig contains an eksctl-created cluster, deleting the cluster will now remove the cluster from the kubeconfig.
  • Loading branch information
Kirsten Schumy committed Sep 28, 2018
1 parent 13286de commit 4264452
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/eksctl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func doDeleteCluster(cfg *api.ClusterConfig, name string) error {

ctl.MaybeDeletePublicSSHKey()

kubeconfig.MaybeDeleteConfig(cfg.ClusterName)
kubeconfig.MaybeDeleteConfig(cfg)

if len(deletedResources) == 0 {
logger.Warning("no EKS cluster resources were found for %q", ctl.Spec.ClusterName)
Expand Down
52 changes: 47 additions & 5 deletions pkg/utils/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// if certificateAuthorityPath is no empty, it is used instead of
// embedded certificate-authority-data
func New(spec *api.ClusterConfig, username, certificateAuthorityPath string) (*clientcmdapi.Config, string, string) {
clusterName := fmt.Sprintf("%s.%s.eksctl.io", spec.ClusterName, spec.Region)
clusterName := getCompleteClusterName(spec)
contextName := fmt.Sprintf("%s@%s", username, clusterName)

c := &clientcmdapi.Config{
Expand Down Expand Up @@ -95,6 +95,10 @@ func Write(path string, newConfig *clientcmdapi.Config, setContext bool) (string
return configAccess.GetDefaultFilename(), nil
}

func getCompleteClusterName(spec *api.ClusterConfig) string {
return fmt.Sprintf("%s.%s.eksctl.io", spec.ClusterName, spec.Region)
}

func getConfigAccess(explicitPath string) clientcmd.ConfigAccess {
pathOptions := clientcmd.NewDefaultPathOptions()
if explicitPath != "" && explicitPath != DefaultPath {
Expand Down Expand Up @@ -156,8 +160,8 @@ func tryDeleteConfig(p, name string) {
}
}

func MaybeDeleteConfig(name string) {
p := AutoPath(name)
func MaybeDeleteConfig(ctl *api.ClusterConfig) {
p := AutoPath(ctl.ClusterName)

autoConfExists, err := utils.FileExists(p)
if err != nil {
Expand All @@ -171,6 +175,44 @@ func MaybeDeleteConfig(name string) {
return
}

// Print message to manually remove from config file
logger.Warning("as you are not using the auto-generated kubeconfig file you will need to remove the details of cluster %s manually", name)
configAccess := getConfigAccess(DefaultPath)
config, _ := configAccess.GetStartingConfig()
if !deleteClusterInfo(config, ctl) {
return
}

if err := clientcmd.ModifyConfig(configAccess, *config, true); err != nil {
logger.Debug("ignoring error while failing to update config file %q: %s", DefaultPath, err)
} else {
logger.Success("kubeconfig has been updated")
}
}

// deleteClusterInfo removes a cluster's information from the kubeconfig if the cluster name
// provided by ctl matches a eksctl-created cluster in the kubeconfig
// returns 'true' if the existing config has changes and 'false' otherwise
func deleteClusterInfo(existing *clientcmdapi.Config, ctl *api.ClusterConfig) bool {
isChanged := false
clusterName := getCompleteClusterName(ctl)

if existing.Clusters[clusterName] != nil {
delete(existing.Clusters, clusterName)
logger.Debug("removed cluster %q from kubeconfig", clusterName)
isChanged = true
}

for username, context := range existing.Contexts {
if context.Cluster == clusterName {
delete(existing.Contexts, username)
logger.Debug("removed context for %q from kubeconfig", username)
isChanged = true
if existing.AuthInfos[username] != nil {
delete(existing.AuthInfos, username)
logger.Debug("removed auth info for %q from kubeconfig", username)
}
break
}
}

return isChanged
}

0 comments on commit 4264452

Please sign in to comment.