/
plugin.go
117 lines (96 loc) · 2.77 KB
/
plugin.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
109
110
111
112
113
114
115
116
117
package plugin
import (
"fmt"
"strings"
"github.com/borisputerka/updatecontext/pkg/logger"
"github.com/borisputerka/updatecontext/pkg/utils"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd/api"
)
type ConfigFlags struct {
Config *utils.Config
}
func (o *ConfigFlags) Complete() (err error) {
o.Config, err = utils.NewConfig()
if err != nil {
return fmt.Errorf("failed to get config: %v", err)
}
return nil
}
// RunPlugin function generated contexts
func (o *ConfigFlags) RunPlugin() error {
logger := logger.NewLogger()
namespaces, err := o.listNamespaces()
if err != nil {
return errors.Wrap(err, "failed to list namespaces")
}
currentContexts, cluster := o.Config.ListContexts()
var createdContexts []string
for _, namespace := range namespaces {
contextName := o.Config.ContextName(cluster, namespace)
if _, ok := currentContexts[contextName]; !ok {
o.Config.AddContext(cluster, namespace)
createdContexts = append(createdContexts, contextName)
}
delete(currentContexts, contextName) // Non-existent context will be in allContexts after all iterations
}
err = o.Config.Update()
if err != nil {
return errors.Wrap(err, "failed to modify configAccess")
}
if len(createdContexts) > 0 {
logger.Info("Contexts created: \n")
logger.Info(strings.Join(createdContexts, "\n"))
}
if len(createdContexts) == 0 {
logger.Info("Nothing to create \n")
}
if len(currentContexts) > 0 {
err := o.deleteContexts(currentContexts)
if err != nil {
return errors.Wrap(err, "could not delete contexts")
}
}
return nil
}
func (o *ConfigFlags) listNamespaces() ([]string, error) {
client, err := o.Config.GetKubernetesClient()
if err != nil {
return nil, fmt.Errorf("could get kubernetes client: %v", err)
}
namespaces, err := client.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("could not get list of namespaces: %v", err)
}
var namespaceList []string
excludedNsList := []string{"kube-public", "kube-node-lease"}
for _, ns := range namespaces.Items {
if !utils.StringInSlice(ns.Name, excludedNsList) {
namespaceList = append(namespaceList, ns.Name)
}
}
return namespaceList, nil
}
func (o *ConfigFlags) deleteContexts(contexts map[string]*api.Context) error {
logger := logger.NewLogger()
contextNames := ""
for name := range contexts {
contextNames += name + "\n"
}
logger.Info("Following contexts are not used anymore:")
logger.Info(contextNames)
logger.Info("Do you want to delete them? [Y/n]: ")
confirmed, err := utils.AskForConfirmation()
if err != nil {
return err
}
if confirmed {
err := o.Config.DeleteContexts(contexts)
if err != nil {
return err
}
logger.Info("Contexts deleted successfully")
}
return nil
}