-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubernetes.go
152 lines (118 loc) · 3.76 KB
/
kubernetes.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"context"
"fmt"
argocd "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
crclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
var (
scheme = runtime.NewScheme()
)
func init() {
utilruntime.Must(argocd.AddToScheme(scheme))
}
type KubeClients struct {
kubeClient *kubernetes.Clientset
crClient crclient.Client
}
func NewKubeClient() (*KubeClients, error) {
cfg, err := config.GetConfig()
if err != nil {
return nil, err
}
kclient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, err
}
crClient, err := crclient.New(cfg, crclient.Options{
Scheme: scheme,
})
if err != nil {
return nil, err
}
return &KubeClients{
kubeClient: kclient,
crClient: crClient,
}, nil
}
func (k *KubeClients) Clientset() *kubernetes.Clientset {
return k.kubeClient
}
func (k *KubeClients) REST() crclient.Client {
return k.crClient
}
func (k *KubeClients) ListArgoCDApps() (*argocd.ApplicationList, error) {
//labels := map[string]string{"app.kubernetes.io/instance": "all-components-staging"}
list := &argocd.ApplicationList{}
err := k.REST().List(context.TODO(), list, &crclient.ListOptions{})
if err != nil {
return nil, err
}
return list, nil
}
func (k *KubeClients) ListArgoCDAppsByLabels(labelMap map[string]string) (*argocd.ApplicationList, error) {
list := &argocd.ApplicationList{}
err := k.crClient.List(context.TODO(), list, &crclient.ListOptions{LabelSelector: labels.SelectorFromSet(labelMap)})
if err != nil {
return nil, err
}
return list, nil
}
func (k *KubeClients) GetImagesFromArgoCDApp(app *argocd.Application) ([]string, error) {
images := []string{}
images = append(images, app.Status.Summary.Images...)
return images, nil
}
func (k *KubeClients) GetImagesFromArgoCDAppList(list *argocd.ApplicationList) ([]string, error) {
images := []string{}
for _, app := range list.Items {
images = append(images, app.Status.Summary.Images...)
}
return images, nil
}
func (k *KubeClients) ListDeploymentsByLabels(label string) (*appsv1.DeploymentList, error) {
deploymentList, err := k.kubeClient.AppsV1().Deployments("").List(context.Background(), metav1.ListOptions{LabelSelector: label})
if err != nil {
return nil, err
}
return deploymentList, nil
}
func (k *KubeClients) GetConfigMap(name string, namespace string) (*v1.ConfigMap, error) {
configMap, err := k.kubeClient.CoreV1().ConfigMaps(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return configMap, nil
}
func (k *KubeClients) IsDeploymentActiveSince(deployment *appsv1.Deployment) (bool, metav1.Time) {
for _, condition := range deployment.Status.Conditions {
if condition.Type == "Available" && condition.Status == "True" {
return true, condition.LastUpdateTime
}
}
return false, metav1.Time{}
}
func (k *KubeClients) GetDeploymentReplicaSetCreationTime(namespace string, owner string, image string) (metav1.Time, error) {
rsList, err := k.kubeClient.AppsV1().ReplicaSets(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return metav1.Time{}, err
}
for _, rs := range rsList.Items {
if rs.OwnerReferences[0].Name == owner && (rs.Status.AvailableReplicas == rs.Status.Replicas) {
for _, cont := range rs.Spec.Template.Spec.Containers {
if cont.Image == image {
return rs.ObjectMeta.CreationTimestamp, nil
}
}
}
}
return metav1.Time{}, fmt.Errorf("no replicaset found for %s or replicas are not available", image)
}