-
Notifications
You must be signed in to change notification settings - Fork 151
/
utils.go
139 lines (121 loc) · 3.97 KB
/
utils.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
package run
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/fluxcd/pkg/apis/meta"
"github.com/weaveworks/weave-gitops/pkg/kube"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func RequestReconciliation(ctx context.Context, kubeClient client.Client, namespacedName types.NamespacedName, gvk schema.GroupVersionKind) (string, error) {
requestAt := time.Now().Format(time.RFC3339Nano)
return requestAt, retry.RetryOnConflict(retry.DefaultBackoff, func() (err error) {
object := &metav1.PartialObjectMetadata{}
object.SetGroupVersionKind(gvk)
object.SetName(namespacedName.Name)
object.SetNamespace(namespacedName.Namespace)
if err := kubeClient.Get(ctx, namespacedName, object); err != nil {
return err
}
patch := client.MergeFrom(object.DeepCopy())
if ann := object.GetAnnotations(); ann == nil {
object.SetAnnotations(map[string]string{
meta.ReconcileRequestAnnotation: requestAt,
})
} else {
ann[meta.ReconcileRequestAnnotation] = requestAt
object.SetAnnotations(ann)
}
err = kubeClient.Patch(ctx, object, patch)
return err
})
}
// IsLocalCluster checks if it's a local cluster. See https://skaffold.dev/docs/environment/local-cluster/
func IsLocalCluster(kubeClient *kube.KubeHTTP) bool {
const (
// cluster context prefixes
kindPrefix = "kind-"
k3dPrefix = "k3d-"
// cluster context names
minikube = "minikube"
dockerForDesktop = "docker-for-desktop"
dockerDesktop = "docker-desktop"
)
if strings.HasPrefix(kubeClient.ClusterName, kindPrefix) ||
strings.HasPrefix(kubeClient.ClusterName, k3dPrefix) ||
kubeClient.ClusterName == minikube ||
kubeClient.ClusterName == dockerForDesktop ||
kubeClient.ClusterName == dockerDesktop {
return true
} else {
return false
}
}
func GetPodFromResourceDescription(ctx context.Context, namespacedName types.NamespacedName, kind string, kubeClient client.Client) (*corev1.Pod, error) {
switch kind {
case "pod":
pod := &corev1.Pod{}
if err := kubeClient.Get(ctx, namespacedName, pod); err != nil {
return nil, err
}
return pod, nil
case "service":
svc := &corev1.Service{}
if err := kubeClient.Get(ctx, namespacedName, svc); err != nil {
return nil, fmt.Errorf("error getting service: %s, namespaced Name: %v", err, namespacedName)
}
// list pods of the service "svc" by selector in a specific namespace using the controller-runtime client
podList := &corev1.PodList{}
if err := kubeClient.List(ctx, podList,
client.MatchingLabelsSelector{
Selector: labels.Set(svc.Spec.Selector).AsSelector(),
},
client.InNamespace(svc.Namespace),
); err != nil {
return nil, err
}
if len(podList.Items) == 0 {
return nil, ErrNoPodsForService
}
for _, pod := range podList.Items {
if pod.Status.Phase == corev1.PodRunning {
return &pod, nil
}
}
return nil, ErrNoRunningPodsForService
case "deployment":
deployment := &appsv1.Deployment{}
if err := kubeClient.Get(ctx, namespacedName, deployment); err != nil {
return nil, fmt.Errorf("error getting deployment: %s, namespaced Name: %v", err, namespacedName)
}
// list pods of the deployment "deployment" by selector in a specific namespace using the controller-runtime client
podList := &corev1.PodList{}
if err := kubeClient.List(ctx, podList,
client.MatchingLabelsSelector{
Selector: labels.Set(deployment.Spec.Selector.MatchLabels).AsSelector(),
},
client.InNamespace(deployment.Namespace),
); err != nil {
return nil, err
}
if len(podList.Items) == 0 {
return nil, ErrNoPodsForDeployment
}
for _, pod := range podList.Items {
if pod.Status.Phase == corev1.PodRunning {
return &pod, nil
}
}
return nil, ErrNoRunningPodsForDeployment
}
return nil, errors.New("unsupported spec kind")
}