Skip to content

Commit

Permalink
Fix delete removes user even if another context is using it (#837)
Browse files Browse the repository at this point in the history
I added a check to make sure that we don't delete a user or a cluster that is in use by another context than the one getting deleted.

I also added a unit test that deletes a context that references a user that is referenced within another context.

Signed-off-by: Yarden Shoham <git@yardenshoham.com>
  • Loading branch information
yardenshoham authored Jan 8, 2024
1 parent 1cb4471 commit 9f08246
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
27 changes: 25 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ func deleteContext(ctxs []string, config *clientcmdapi.Config) error {
for _, ctx := range ctxs {
if _, ok := config.Contexts[ctx]; ok {
delContext := config.Contexts[ctx]
delete(config.AuthInfos, delContext.AuthInfo)
delete(config.Clusters, delContext.Cluster)
isClusterNameExist, isUserNameExist := checkClusterAndUserNameExceptContextToDelete(config, config.Contexts[ctx])
if !isUserNameExist {
delete(config.AuthInfos, delContext.AuthInfo)
}
if !isClusterNameExist {
delete(config.Clusters, delContext.Cluster)
}
delete(config.Contexts, ctx)
fmt.Printf("Context Delete:「%s」\n", ctx)
} else {
Expand All @@ -80,6 +85,24 @@ func deleteContext(ctxs []string, config *clientcmdapi.Config) error {
return nil
}

func checkClusterAndUserNameExceptContextToDelete(oldConfig *clientcmdapi.Config, contextToDelete *clientcmdapi.Context) (bool, bool) {
var (
isClusterNameExist bool
isUserNameExist bool
)

for _, ctx := range oldConfig.Contexts {
if ctx.Cluster == contextToDelete.Cluster && ctx != contextToDelete {
isClusterNameExist = true
}
if ctx.AuthInfo == contextToDelete.AuthInfo && ctx != contextToDelete {
isUserNameExist = true
}
}

return isClusterNameExist, isUserNameExist
}

func selectDeleteContext(config *clientcmdapi.Config) (string, string, error) {
var kubeItems []Needle
for key, obj := range config.Contexts {
Expand Down
27 changes: 27 additions & 0 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ var (
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"}},
}
delButKeepUserConfigBefore = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"},
},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"},
"cow-cluster": {Server: "http://cow.org:8080"},
},
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"},
"federal-context": {AuthInfo: "black-user", Cluster: "cow-cluster", Namespace: "hammer-ns"},
},
}
delButKeepUserConfigAfter = clientcmdapi.Config{
AuthInfos: map[string]*clientcmdapi.AuthInfo{
"black-user": {Token: "black-token"},
},
Clusters: map[string]*clientcmdapi.Cluster{
"pig-cluster": {Server: "http://pig.org:8080"},
},
Contexts: map[string]*clientcmdapi.Context{
"root-context": {AuthInfo: "black-user", Cluster: "pig-cluster", Namespace: "saw-ns"},
},
}
)

func Test_deleteContext(t *testing.T) {
Expand All @@ -46,6 +70,7 @@ func Test_deleteContext(t *testing.T) {
{"delete", args{[]string{"federal-context"}, &delMergeConfig}, false},
{"delete-not-exist", args{[]string{"a"}, &delMergeConfig}, true},
{"multiple-delete", args{[]string{"federal-context", "root-context"}, &delMergeConfig}, false},
{"delete-but-keep-user", args{[]string{"federal-context"}, &delButKeepUserConfigBefore}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -57,6 +82,8 @@ func Test_deleteContext(t *testing.T) {
checkConfig(&delRootConfigConflictAlfa, tt.args.config, t)
case "multiple-delete":
checkConfig(clientcmdapi.NewConfig(), tt.args.config, t)
case "delete-but-keep-user":
checkConfig(&delButKeepUserConfigAfter, tt.args.config, t)
}
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 9f08246

Please sign in to comment.