This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcurrent.go
72 lines (59 loc) · 2.13 KB
/
current.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
package endpoint
import (
"context"
"github.com/giantswarm/microerror"
"github.com/giantswarm/operatorkit/controller/context/finalizerskeptcontext"
"github.com/giantswarm/operatorkit/controller/context/resourcecanceledcontext"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/kvm-operator/service/controller/v12/key"
)
func (r *Resource) GetCurrentState(ctx context.Context, obj interface{}) (interface{}, error) {
pod, err := toPod(obj)
if err != nil {
return nil, microerror.Mask(err)
}
r.logger.LogCtx(ctx, "level", "debug", "message", "looking for annotations on pod")
_, serviceName, err := getAnnotations(*pod, IPAnnotation, ServiceAnnotation)
if IsMissingAnnotationError(err) {
r.logger.LogCtx(ctx, "level", "debug", "message", "annotation is missing on pod")
resourcecanceledcontext.SetCanceled(ctx)
r.logger.LogCtx(ctx, "level", "debug", "message", "canceling resource for pod")
return nil, nil
} else if err != nil {
return nil, microerror.Mask(err)
}
if key.IsPodDeleted(pod) {
isDrained, err := key.IsPodDraind(pod)
if err != nil {
return nil, microerror.Mask(err)
}
if !isDrained {
r.logger.LogCtx(ctx, "level", "debug", "message", "cannot finish deletion of pod due to undrained status")
resourcecanceledcontext.SetCanceled(ctx)
finalizerskeptcontext.SetKept(ctx)
r.logger.LogCtx(ctx, "level", "debug", "message", "canceling resource for custom object")
return nil, nil
}
}
currentEndpoint := Endpoint{
IPs: []string{},
ServiceName: serviceName,
ServiceNamespace: pod.GetNamespace(),
}
k8sEndpoints, err := r.k8sClient.CoreV1().Endpoints(pod.GetNamespace()).Get(serviceName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return nil, nil
} else if err != nil {
return nil, microerror.Mask(err)
}
for _, endpointSubset := range k8sEndpoints.Subsets {
for _, endpointAddress := range endpointSubset.Addresses {
foundIP := endpointAddress.IP
if !containsIP(currentEndpoint.IPs, foundIP) {
currentEndpoint.IPs = append(currentEndpoint.IPs, foundIP)
}
}
}
return ¤tEndpoint, nil
}