-
Notifications
You must be signed in to change notification settings - Fork 8
/
labelwatch.go
76 lines (63 loc) · 2.06 KB
/
labelwatch.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 validation
import (
"fmt"
"github.com/go-logr/logr"
appsv1beta1 "k8s.io/api/apps/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"sigs.k8s.io/controller-runtime/pkg/client"
kanaryv1alpha1 "github.com/amadeusitgroup/kanary/pkg/apis/kanary/v1alpha1"
)
// NewLabelWatch returns new validation.LabelWatch instance
func NewLabelWatch(list *kanaryv1alpha1.KanaryDeploymentSpecValidationList, s *kanaryv1alpha1.KanaryDeploymentSpecValidation) Interface {
return &labelWatchImpl{
dryRun: list.NoUpdate,
config: s.LabelWatch,
}
}
type labelWatchImpl struct {
dryRun bool
config *kanaryv1alpha1.KanaryDeploymentSpecValidationLabelWatch
}
func (l *labelWatchImpl) Validation(kclient client.Client, reqLogger logr.Logger, kd *kanaryv1alpha1.KanaryDeployment, dep, canaryDep *appsv1beta1.Deployment) (*Result, error) {
var err error
result := &Result{}
// By default a Deployement is valid until a Label is discovered on pod or deployment.
isSucceed := true
if l.config.DeploymentInvalidationLabels != nil {
var selector labels.Selector
selector, err = metav1.LabelSelectorAsSelector(l.config.DeploymentInvalidationLabels)
if err != nil {
// TODO improve error handling
return result, err
}
if selector.Matches(labels.Set(canaryDep.Labels)) {
isSucceed = false
}
}
// watch pods label
if l.config.PodInvalidationLabels != nil {
var selector labels.Selector
selector, err = metav1.LabelSelectorAsSelector(l.config.PodInvalidationLabels)
if err != nil {
return result, fmt.Errorf("unable to create the label selector from PodInvalidationLabels: %v", err)
}
var pods []corev1.Pod
pods, err = getPods(kclient, reqLogger, kd.Name, kd.Namespace)
if err != nil {
return result, fmt.Errorf("unable to list pods: %v", err)
}
for _, pod := range pods {
if selector.Matches(labels.Set(pod.Labels)) {
isSucceed = false
break
}
}
}
if !isSucceed {
result.IsFailed = true
result.Comment = "labelWatch has detected invalidation labels"
}
return result, err
}