-
Notifications
You must be signed in to change notification settings - Fork 0
/
workloads.go
98 lines (79 loc) · 2.79 KB
/
workloads.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
package k8s
import (
"context"
"fmt"
"github.com/oam-dev/kubevela-core-api/apis/core.oam.dev/v1beta1"
k8sV1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"strings"
)
func GetDeploymentsByLabels(clientset *kubernetes.Clientset, namespace string, labels map[string]string) ([]k8sV1.Deployment, error) {
deploymentsClient := clientset.AppsV1().Deployments(namespace)
var labelSelector []string
for key, value := range labels {
labelSelector = append(labelSelector, fmt.Sprintf("%s=%s", key, value))
}
listOptions := metav1.ListOptions{
LabelSelector: strings.Join(labelSelector, ","),
}
deployments, err := deploymentsClient.List(context.TODO(), listOptions)
if err != nil {
return nil, err
}
if len(deployments.Items) == 0 {
return nil, fmt.Errorf("no deployment found with label selector: %s", labelSelector)
}
return deployments.Items, nil
}
func GetStatefulSetByLabels(clientset *kubernetes.Clientset, namespace string, labels map[string]string) ([]k8sV1.StatefulSet, error) {
statefulSetsClient := clientset.AppsV1().StatefulSets(namespace)
var labelSelector []string
for key, value := range labels {
labelSelector = append(labelSelector, fmt.Sprintf("%s=%s", key, value))
}
listOptions := metav1.ListOptions{
LabelSelector: strings.Join(labelSelector, ","),
}
statefulsets, err := statefulSetsClient.List(context.TODO(), listOptions)
if err != nil {
return nil, err
}
if len(statefulsets.Items) == 0 {
return nil, fmt.Errorf("no statefulset found with label selector: %s", labelSelector)
}
return statefulsets.Items, nil
}
func GetServicesByLabels(clientset *GenericClientset, namespace string, labels map[string]string) ([]corev1.Service, error) {
servicesClient := clientset.K8s.CoreV1().Services(namespace)
var labelSelector []string
for key, value := range labels {
labelSelector = append(labelSelector, fmt.Sprintf("%s=%s", key, value))
}
listOptions := metav1.ListOptions{
LabelSelector: strings.Join(labelSelector, ","),
}
services, err := servicesClient.List(context.TODO(), listOptions)
if err != nil {
return nil, err
}
return services.Items, nil
}
func GetApplicationByLabels(clientset *GenericClientset, namespace string, labels map[string]string) (*v1beta1.Application, error) {
var labelSelector []string
for key, value := range labels {
labelSelector = append(labelSelector, fmt.Sprintf("%s=%s", key, value))
}
listOptions := metav1.ListOptions{
LabelSelector: strings.Join(labelSelector, ","),
}
applications, err := clientset.Vela.CoreV1beta1().Applications(namespace).List(context.Background(), listOptions)
if err != nil {
return nil, err
}
if len(applications.Items) == 0 {
return nil, ErrApplicationNotFound
}
return &applications.Items[0], nil
}