This repository has been archived by the owner on Nov 6, 2019. It is now read-only.
/
disruption.go
83 lines (70 loc) · 2.21 KB
/
disruption.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
package vsvc
import (
"errors"
"github.com/manifoldco/heighliner/apis/v1alpha1"
"github.com/manifoldco/heighliner/internal/meta"
"github.com/jelmersnoeck/kubekit"
"k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
)
var (
// ErrMinMaxAvailabilitySet is used when the Availability Configuration has
// both MinAvailabe and MaxUnavailable set.
ErrMinMaxAvailabilitySet = errors.New("Can't have both MinAvailable and MaxUnavailable configured")
)
func getPodDisruptionBudget(crd *v1alpha1.VersionedMicroservice) (runtime.Object, error) {
budget := defaultDisruptionBudget.DeepCopy()
labels := meta.Labels(crd.Labels, crd)
annotations := meta.Annotations(crd.Annotations, v1alpha1.Version, crd)
budget.ObjectMeta = metav1.ObjectMeta{
Name: crd.Name,
Namespace: crd.Namespace,
Labels: labels,
Annotations: annotations,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(
crd,
v1alpha1.SchemeGroupVersion.WithKind(kubekit.TypeName(crd)),
),
},
}
budget.Spec.Selector.MatchLabels[meta.LabelServiceKey] = crd.Name
if crd.Spec.Availability != nil {
av := crd.Spec.Availability
if av.MinAvailable != nil && av.MaxUnavailable != nil {
return nil, ErrMinMaxAvailabilitySet
}
if av.MinAvailable != nil && av.MaxUnavailable == nil {
budget.Spec.MinAvailable = av.MinAvailable
budget.Spec.MaxUnavailable = nil
}
if av.MaxUnavailable != nil && av.MinAvailable == nil {
budget.Spec.MaxUnavailable = av.MaxUnavailable
budget.Spec.MinAvailable = nil
}
}
return budget, nil
}
var defaultDisruptionBudget = &v1beta1.PodDisruptionBudget{
TypeMeta: metav1.TypeMeta{
Kind: "PodDisruptionBudget",
APIVersion: "policy/v1beta1",
},
Spec: v1beta1.PodDisruptionBudgetSpec{
MinAvailable: ptrIntOrStringFromInt(1),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{},
},
},
Status: v1beta1.PodDisruptionBudgetStatus{
DisruptedPods: map[string]metav1.Time{},
},
}
func ptrIntOrStringFromInt(i int) *intstr.IntOrString {
return ptrIntOrString(intstr.FromInt(i))
}
func ptrIntOrString(i intstr.IntOrString) *intstr.IntOrString {
return &i
}