-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubeapi.go
61 lines (54 loc) · 1.49 KB
/
kubeapi.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
package main
import (
"context"
"path/filepath"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func (rootElement *Element) UpdateData() {
kubeLog.Info("Updating Data for all contexts")
// Mark all for pending deletion
rootElement.ElementTraversalMarkNonUpdated()
// Update contexts
for _, ctx := range existingContext {
rootElement.UpdateContextData(ctx)
}
// Delete missing elements after updates
rootElement.ElementTraversalDisposeNonUpdated()
}
func GetNamespaces(path string) []v1.Namespace {
config, err := clientcmd.BuildConfigFromFlags("", path)
if err != nil {
kubeLog.Warning(err)
return []v1.Namespace{}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
kubeLog.Warning(err)
return []v1.Namespace{}
}
namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
if err != nil {
kubeLog.Warning(err)
return []v1.Namespace{}
}
return namespaces.Items
}
func (rootElement *Element) UpdateContextData(ctx string) {
ctxElement, ok := rootElement.Children[ctx]
if !ok {
ctxElement = rootElement.UpsertContext(ctx)
} else {
ctxElement.Updated = true
}
ctxElement.UpdateNamespaceData()
}
func (ctxElement *Element) UpdateNamespaceData() {
matches, _ := filepath.Glob(filepath.Join(contextDirectory, ctxElement.Title, "*"))
for _, match := range matches {
ns := filepath.Base(match)
ctxElement.UpsertNamespace(ns)
}
}