Skip to content

Commit

Permalink
Cleanup current-context after cluster deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffwan committed Feb 12, 2019
1 parent b6d02e1 commit bb552c8
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ To delete a cluster, run:
```
eksctl delete cluster --name=<name> [--region=<region>]
```
> NOTE: Cluster info will be cleaned up in kubernetes config file. Please run `kubectl config get-contexts` to select right context.

### Managing nodegroups

Expand Down
1 change: 1 addition & 0 deletions humans.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Yusuke Kuoka @mumoshu
Daniel Herman @dcherman
Takuma Hashimoto @af12066
Yasuhiro Hara @toricls
Jiaxin Shan @Jeffwan

/* Thanks */

Expand Down
20 changes: 14 additions & 6 deletions pkg/utils/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,26 @@ func deleteClusterInfo(existing *clientcmdapi.Config, cl *api.ClusterMeta) bool
isChanged = true
}

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

if existing.CurrentContext == currentContextName {
existing.CurrentContext = ""
logger.Debug("removed current-context %q from kubeconfig", currentContextName)
isChanged = true
}

return isChanged
}
46 changes: 40 additions & 6 deletions pkg/utils/kubeconfig/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ import (
var _ = Describe("Kubeconfig", func() {
var configFile *os.File

var contextName = "test-context"

var testConfig = api.Config{
AuthInfos: map[string]*api.AuthInfo{
"test-user": {Token: "test-token"}},
Clusters: map[string]*api.Cluster{
"test-cluster": {Server: "https://127.0.0.1:8443"}},
Contexts: map[string]*api.Context{
"test-context": {AuthInfo: "test-user", Cluster: "test-cluster", Namespace: "test-ns"}},
contextName: {AuthInfo: "test-user", Cluster: "test-cluster", Namespace: "test-ns"}},
CurrentContext: contextName,
}

BeforeEach(func() {
Expand Down Expand Up @@ -168,10 +171,12 @@ var _ = Describe("Kubeconfig", func() {
}

var (
oneClusterAsBytes []byte
twoClustersAsBytes []byte
kubeconfigPathToRestore string
hasKubeconfigPath bool
emptyClusterAsBytes []byte
oneClusterAsBytes []byte
twoClustersAsBytes []byte
oneClusterWithoutContextAsBytes []byte
kubeconfigPathToRestore string
hasKubeconfigPath bool
)

// Returns an ClusterConfig with a cluster name equal to the provided clusterName.
Expand Down Expand Up @@ -203,6 +208,10 @@ var _ = Describe("Kubeconfig", func() {

var err error

if emptyClusterAsBytes, err = ioutil.ReadFile("testdata/empty_cluster.golden"); err != nil {
GinkgoT().Fatalf("failed reading .golden: %v", err)
}

if oneClusterAsBytes, err = ioutil.ReadFile("testdata/one_cluster.golden"); err != nil {
GinkgoT().Fatalf("failed reading .golden: %v", err)
}
Expand All @@ -211,6 +220,10 @@ var _ = Describe("Kubeconfig", func() {
GinkgoT().Fatalf("failed reading .golden: %v", err)
}

if oneClusterWithoutContextAsBytes, err = ioutil.ReadFile("testdata/one_cluster_without_context.golden"); err != nil {
GinkgoT().Fatalf("failed reading .golden: %v", err)
}

_, err = configFile.Write(twoClustersAsBytes)
Expect(err).To(BeNil())
})
Expand All @@ -219,7 +232,28 @@ var _ = Describe("Kubeconfig", func() {
RestoreKubeconfig()
})

It("removes a cluster from the kubeconfig if the kubeconfig file includes the cluster", func() {
It("removes the only current cluster from the kubeconfig if the kubeconfig file includes the cluster", func() {
_, err := configFile.Write(oneClusterAsBytes)
Expect(err).To(BeNil())

existingClusterConfig := GetClusterConfig("cluster-one")
kubeconfig.MaybeDeleteConfig(existingClusterConfig.Metadata)

configFileAsBytes, err := ioutil.ReadFile(configFile.Name())
Expect(err).To(BeNil())
Expect(bytes.Equal(configFileAsBytes, emptyClusterAsBytes)).To(BeTrue(), "Failed to delete cluster from config")
})

It("removes current cluster from the kubeconfig if the kubeconfig file includes the cluster", func() {
existingClusterConfig := GetClusterConfig("cluster-one")
kubeconfig.MaybeDeleteConfig(existingClusterConfig.Metadata)

configFileAsBytes, err := ioutil.ReadFile(configFile.Name())
Expect(err).To(BeNil())
Expect(bytes.Equal(configFileAsBytes, oneClusterWithoutContextAsBytes)).To(BeTrue(), "Failed to delete cluster from config")
})

It("removes a secondary cluster from the kubeconfig if the kubeconfig file includes the cluster", func() {
existingClusterConfig := GetClusterConfig("cluster-two")
kubeconfig.MaybeDeleteConfig(existingClusterConfig.Metadata)

Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/kubeconfig/testdata/empty_cluster.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []
25 changes: 25 additions & 0 deletions pkg/utils/kubeconfig/testdata/one_cluster_without_context.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS9CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5PENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTLS0tLS9CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5PENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTLS0tLS9CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5PENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTLS0tLS9CRUdJTiBDRVJUSUZJQ0FURS0tLS0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S0tLQo=
server: https://21.sk1.us-west-2.eks.amazonaws.com
name: cluster-two.us-west-2.eksctl.io
contexts:
- context:
cluster: cluster-two.us-west-2.eksctl.io
user: admin@cluster-two.us-west-2.eksctl.io
name: admin@cluster-two.us-west-2.eksctl.io
current-context: ""
kind: Config
preferences: {}
users:
- name: admin@cluster-two.us-west-2.eksctl.io
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
args:
- token
- -i
- cluster-two
command: aws-iam-authenticator
env: null

0 comments on commit bb552c8

Please sign in to comment.