/
custom_kubeconfig_uninstall.go
108 lines (93 loc) · 3.04 KB
/
custom_kubeconfig_uninstall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package k8s
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"github.com/kubernetes-client/go-base/config/api"
"github.com/scaleway/scaleway-cli/internal/core"
)
type k8sKubeconfigUninstallRequest struct {
ClusterID string
}
func k8sKubeconfigUninstallCommand() *core.Command {
return &core.Command{
Short: `Uninstall a kubeconfig`,
Long: `Remove specified cluster from kubeconfig file specified by the KUBECONFIG env, if empty it will default to $HOME/.kube/config.
If the current context points to this cluster, it will be set to an empty context.`,
Namespace: "k8s",
Verb: "uninstall",
Resource: "kubeconfig",
ArgsType: reflect.TypeOf(k8sKubeconfigUninstallRequest{}),
ArgSpecs: core.ArgSpecs{
{
Name: "cluster-id",
Short: "Cluster ID from which to uninstall the kubeconfig",
Required: true,
Positional: true,
},
},
Run: k8sKubeconfigUninstallRun,
Examples: []*core.Example{
{
Short: "Uninstall the kubeconfig for a given cluster",
ArgsJSON: `{"cluster_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
SeeAlsos: []*core.SeeAlso{
{
Command: "scw k8s kubeconfig install",
Short: "Install a kubeconfig",
},
},
}
}
// k8sKubeconfigUninstallRun use the specified cluster ID to remove it from the wanted kubeconfig file
// it removes all the users, contexts and clusters that contains this ID from the file
func k8sKubeconfigUninstallRun(ctx context.Context, argsI interface{}) (i interface{}, e error) {
request := argsI.(*k8sKubeconfigUninstallRequest)
kubeconfigPath, err := getKubeconfigPath(ctx)
if err != nil {
return nil, err
}
// if the file does not exist, the cluster is not there
if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) {
return fmt.Sprintf("File %s does not exist.", kubeconfigPath), nil
}
existingKubeconfig, err := openAndUnmarshalKubeconfig(kubeconfigPath)
if err != nil {
return nil, err
}
newClusters := []api.NamedCluster{}
for _, cluster := range existingKubeconfig.Clusters {
if !strings.HasSuffix(cluster.Name, request.ClusterID) {
newClusters = append(newClusters, cluster)
}
}
newContexts := []api.NamedContext{}
for _, kubeconfigContext := range existingKubeconfig.Contexts {
if !strings.HasSuffix(kubeconfigContext.Name, request.ClusterID) {
newContexts = append(newContexts, kubeconfigContext)
}
}
newUsers := []api.NamedAuthInfo{}
for _, user := range existingKubeconfig.AuthInfos {
if !strings.HasSuffix(user.Name, request.ClusterID) {
newUsers = append(newUsers, user)
}
}
// reset the current context
if strings.HasSuffix(existingKubeconfig.CurrentContext, request.ClusterID) {
existingKubeconfig.CurrentContext = ""
}
// write the modification
existingKubeconfig.Clusters = newClusters
existingKubeconfig.Contexts = newContexts
existingKubeconfig.AuthInfos = newUsers
err = marshalAndWriteKubeconfig(existingKubeconfig, kubeconfigPath)
if err != nil {
return nil, err
}
return fmt.Sprintf("Cluster %s successfully deleted from %s", request.ClusterID, kubeconfigPath), nil
}