-
Notifications
You must be signed in to change notification settings - Fork 62
/
poddisruptionbudget_control.go
92 lines (78 loc) · 3.77 KB
/
poddisruptionbudget_control.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
package controller
import (
"fmt"
policyv1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
rapi "github.com/amadeusitgroup/redis-operator/pkg/api/redis/v1"
"github.com/amadeusitgroup/redis-operator/pkg/controller/pod"
)
// PodDisruptionBudgetsControlInterface inferface for the PodDisruptionBudgetsControl
type PodDisruptionBudgetsControlInterface interface {
// CreateRedisClusterPodDisruptionBudget used to create the Kubernetes PodDisruptionBudget needed to access the Redis Cluster
CreateRedisClusterPodDisruptionBudget(redisCluster *rapi.RedisCluster) (*policyv1.PodDisruptionBudget, error)
// DeleteRedisClusterPodDisruptionBudget used to delete the Kubernetes PodDisruptionBudget linked to the Redis Cluster
DeleteRedisClusterPodDisruptionBudget(redisCluster *rapi.RedisCluster) error
// GetRedisClusterPodDisruptionBudget used to retrieve the Kubernetes PodDisruptionBudget associated to the RedisCluster
GetRedisClusterPodDisruptionBudget(redisCluster *rapi.RedisCluster) (*policyv1.PodDisruptionBudget, error)
}
// PodDisruptionBudgetsControl contains all information for managing Kube PodDisruptionBudgets
type PodDisruptionBudgetsControl struct {
KubeClient clientset.Interface
Recorder record.EventRecorder
}
// NewPodDisruptionBudgetsControl builds and returns new PodDisruptionBudgetsControl instance
func NewPodDisruptionBudgetsControl(client clientset.Interface, rec record.EventRecorder) *PodDisruptionBudgetsControl {
ctrl := &PodDisruptionBudgetsControl{
KubeClient: client,
Recorder: rec,
}
return ctrl
}
// GetRedisClusterPodDisruptionBudget used to retrieve the Kubernetes PodDisruptionBudget associated to the RedisCluster
func (s *PodDisruptionBudgetsControl) GetRedisClusterPodDisruptionBudget(redisCluster *rapi.RedisCluster) (*policyv1.PodDisruptionBudget, error) {
PodDisruptionBudgetName := redisCluster.Name
pdb, err := s.KubeClient.PolicyV1beta1().PodDisruptionBudgets(redisCluster.Namespace).Get(PodDisruptionBudgetName, metav1.GetOptions{})
if err != nil {
return nil, err
}
if pdb.Name != PodDisruptionBudgetName {
return nil, fmt.Errorf("Couldn't find PodDisruptionBudget named %s", PodDisruptionBudgetName)
}
return pdb, nil
}
// DeleteRedisClusterPodDisruptionBudget used to delete the Kubernetes PodDisruptionBudget linked to the Redis Cluster
func (s *PodDisruptionBudgetsControl) DeleteRedisClusterPodDisruptionBudget(redisCluster *rapi.RedisCluster) error {
return s.KubeClient.PolicyV1beta1().PodDisruptionBudgets(redisCluster.Namespace).Delete(redisCluster.Name, nil)
}
// CreateRedisClusterPodDisruptionBudget used to create the Kubernetes PodDisruptionBudget needed to access the Redis Cluster
func (s *PodDisruptionBudgetsControl) CreateRedisClusterPodDisruptionBudget(redisCluster *rapi.RedisCluster) (*policyv1.PodDisruptionBudget, error) {
PodDisruptionBudgetName := redisCluster.Name
desiredlabels, err := pod.GetLabelsSet(redisCluster)
if err != nil {
return nil, err
}
desiredAnnotations, err := pod.GetAnnotationsSet(redisCluster)
if err != nil {
return nil, err
}
maxUnavailable := intstr.FromInt(1)
labelSelector := metav1.LabelSelector{
MatchLabels: desiredlabels,
}
newPodDisruptionBudget := &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Labels: desiredlabels,
Annotations: desiredAnnotations,
Name: PodDisruptionBudgetName,
OwnerReferences: []metav1.OwnerReference{pod.BuildOwnerReference(redisCluster)},
},
Spec: policyv1.PodDisruptionBudgetSpec{
MaxUnavailable: &maxUnavailable,
Selector: &labelSelector,
},
}
return s.KubeClient.PolicyV1beta1().PodDisruptionBudgets(redisCluster.Namespace).Create(newPodDisruptionBudget)
}