/
pdb.go
76 lines (69 loc) · 1.63 KB
/
pdb.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
package common
import (
"context"
zookeeperv1alpha1 "github.com/zncdata-labs/zookeeper-operator/api/v1alpha1"
policyv1 "k8s.io/api/policy/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type PDBReconciler[T client.Object] struct {
GeneralResourceStyleReconciler[T, any]
name string
labels map[string]string
pdb *zookeeperv1alpha1.PodDisruptionBudgetSpec
}
func NewReconcilePDB[T client.Object](
client client.Client,
schema *runtime.Scheme,
cr T,
labels map[string]string,
name string,
pdb *zookeeperv1alpha1.PodDisruptionBudgetSpec,
) *PDBReconciler[T] {
var cfg = &zookeeperv1alpha1.RoleGroupSpec{}
return &PDBReconciler[T]{
GeneralResourceStyleReconciler: *NewGeneraResourceStyleReconciler[T, any](
schema,
cr,
client,
"",
labels,
cfg,
),
name: name,
labels: labels,
pdb: pdb,
}
}
func (r *PDBReconciler[T]) Build(_ context.Context) (client.Object, error) {
if r.pdb == nil {
return nil, nil
}
obj := &policyv1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: r.name,
Namespace: r.Instance.GetNamespace(),
Labels: r.labels,
},
Spec: policyv1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: r.labels,
},
},
}
if r.pdb.MinAvailable > 0 {
obj.Spec.MinAvailable = &intstr.IntOrString{
Type: intstr.Int,
IntVal: r.pdb.MinAvailable,
}
}
if r.pdb.MaxUnavailable > 0 {
obj.Spec.MaxUnavailable = &intstr.IntOrString{
Type: intstr.Int,
IntVal: r.pdb.MaxUnavailable,
}
}
return obj, nil
}