-
Notifications
You must be signed in to change notification settings - Fork 0
/
pods.go
78 lines (69 loc) · 1.91 KB
/
pods.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
package main
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"os"
"path/filepath"
)
func getDefaultKubeconfigPath() string {
if os.Getenv("KUBECONFIG") != "" {
return os.Getenv("KUBECONFIG")
}
if home := homedir.HomeDir(); home != "" {
path := filepath.Join(home, ".kube", "config")
if _, err := os.Stat(path); err == nil {
return path
}
}
return ""
}
func getClientset(kubeconfig, context string) (*kubernetes.Clientset, error) {
var config *rest.Config
var err error
if kubeconfig == "" {
config, err = rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("error creating in-cluster config: %v", err)
}
} else {
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfig},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
if err != nil {
return nil, fmt.Errorf("error building config: %w", err)
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("error building clientset: %w", err)
}
return clientset, nil
}
func getImagesFromAllPods() ([]string, error) {
clientset, err := getClientset(getDefaultKubeconfigPath(), "")
if err != nil {
return nil, fmt.Errorf("error getting clientset: %v", err)
}
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, fmt.Errorf("error listing pods: %v", err)
}
var images []string
imageSet := make(map[string]struct{})
for _, pod := range pods.Items {
for _, container := range pod.Spec.Containers {
if _, exists := imageSet[container.Image]; !exists {
images = append(images, container.Image)
imageSet[container.Image] = struct{}{}
}
}
}
return images, nil
}