Skip to content

Commit

Permalink
Merge branch 'master' into feat/docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny0826 committed Jan 8, 2024
2 parents 420133e + 42d206a commit cb3c55b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 8 deletions.
6 changes: 4 additions & 2 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
"avatar_url": "https://avatars.githubusercontent.com/u/20454870?v=4",
"profile": "https://github.com/yardenshoham",
"contributions": [
"code"
"code",
"test"
]
},
{
Expand Down Expand Up @@ -176,5 +177,6 @@
}
],
"contributorsPerLine": 7,
"linkToUsage": true
"linkToUsage": true,
"commitType": "docs"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/cnfatal"><img src="https://avatars.githubusercontent.com/u/15731850?v=4?s=100" width="100px;" alt="cnfatal"/><br /><sub><b>cnfatal</b></sub></a><br /><a href="https://github.com/sunny0826/kubecm/commits?author=cnfatal" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://se7enshare.netlify.app/"><img src="https://avatars.githubusercontent.com/u/40051120?v=4?s=100" width="100px;" alt="Se7en"/><br /><sub><b>Se7en</b></sub></a><br /><a href="https://github.com/sunny0826/kubecm/commits?author=cr7258" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/yardenshoham"><img src="https://avatars.githubusercontent.com/u/20454870?v=4?s=100" width="100px;" alt="Yarden Shoham"/><br /><sub><b>Yarden Shoham</b></sub></a><br /><a href="https://github.com/sunny0826/kubecm/commits?author=yardenshoham" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/yardenshoham"><img src="https://avatars.githubusercontent.com/u/20454870?v=4?s=100" width="100px;" alt="Yarden Shoham"/><br /><sub><b>Yarden Shoham</b></sub></a><br /><a href="https://github.com/sunny0826/kubecm/commits?author=yardenshoham" title="Code">💻</a> <a href="https://github.com/sunny0826/kubecm/commits?author=yardenshoham" title="Tests">⚠️</a></td>
<td align="center" valign="top" width="14.28%"><a href="http://vimsucks.com/"><img src="https://avatars.githubusercontent.com/u/21141423?v=4?s=100" width="100px;" alt="Vimsucks"/><br /><sub><b>Vimsucks</b></sub></a><br /><a href="https://github.com/sunny0826/kubecm/commits?author=vimsucks" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://suzuki-shunsuke.github.io/profile/"><img src="https://avatars.githubusercontent.com/u/13323303?v=4?s=100" width="100px;" alt="Shunsuke Suzuki"/><br /><sub><b>Shunsuke Suzuki</b></sub></a><br /><a href="https://github.com/sunny0826/kubecm/commits?author=suzuki-shunsuke" title="Code">💻</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://seanly.opsbox.cloud/"><img src="https://avatars.githubusercontent.com/u/232069?v=4?s=100" width="100px;" alt="YS Liu"/><br /><sub><b>YS Liu</b></sub></a><br /><a href="https://github.com/sunny0826/kubecm/commits?author=seanly" title="Code">💻</a></td>
Expand Down
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ require (
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
github.com/Azure/go-autorest/autorest v0.11.24
github.com/Azure/go-autorest/autorest/azure/auth v0.5.12
github.com/aws/aws-sdk-go v1.49.9
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/aws/aws-sdk-go v1.49.13
github.com/stretchr/testify v1.8.4
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5
github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:lB+ZfQJz7igIIfQNfa7Ml4HSf2uFQQRzpGGRXenZAgY=
github.com/atomicgo/cursor v0.0.1/go.mod h1:cBON2QmmrysudxNBFthvMtN32r3jxVRIvzkUiF/RuIk=
github.com/aws/aws-sdk-go v1.49.9 h1:4xoyi707rsifB1yMsd5vGbAH21aBzwpL3gNRMSmjIyc=
github.com/aws/aws-sdk-go v1.49.9/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.49.13 h1:f4mGztsgnx2dR9r8FQYa9YW/RsKb+N7bgef4UGrOW1Y=
github.com/aws/aws-sdk-go v1.49.13/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down

0 comments on commit cb3c55b

Please sign in to comment.