/
restart_factory.go
66 lines (55 loc) · 1.58 KB
/
restart_factory.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
package restart
import (
"context"
"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func restartActionFactory(ctx context.Context, c client.Client, pod v1.Pod) (restartAction, error) {
ownedBy, exists := getOwnerReferences(pod)
if !exists {
return newOwnerNotFoundAction(pod), nil
}
switch ownedBy.Kind {
case "Job":
return newOwnedByJobAction(pod), nil
case "ReplicaSet":
return getReplicaSetAction(ctx, c, pod, ownedBy)
case "ReplicationController":
return newDeleteAction(actionObjectFromPod(pod)), nil
default:
return newRolloutAction(actionObject{
Name: ownedBy.Name,
Namespace: pod.Namespace,
Kind: ownedBy.Kind,
}), nil
}
}
// getOwnerReferences returns the owner reference of the pod and a boolean to verify if the owner reference exists or not
func getOwnerReferences(pod v1.Pod) (*metav1.OwnerReference, bool) {
if len(pod.OwnerReferences) == 0 {
return &metav1.OwnerReference{}, false
}
return pod.OwnerReferences[0].DeepCopy(), true
}
type restartAction struct {
run func(context.Context, client.Client, actionObject, *logr.Logger) ([]RestartWarning, error)
object actionObject
}
type actionObject struct {
Name string
Namespace string
Kind string
}
// getKey returns a key that identifies this object
func (r actionObject) getKey() string {
return r.Name + r.Namespace + r.Kind
}
func actionObjectFromPod(pod v1.Pod) actionObject {
return actionObject{
Name: pod.Name,
Namespace: pod.Namespace,
Kind: pod.Kind,
}
}