-
Notifications
You must be signed in to change notification settings - Fork 48
/
predicates.go
86 lines (74 loc) · 2.48 KB
/
predicates.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
//
// Copyright (c) 2019-2022 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package controllers
import (
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/pkg/constants"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
// predicates filters incoming events to avoid unnecessary reconciles to failed workspaces.
// If a workspace failed and its spec is changed, we trigger reconciles to allow for fixing
// issues in the workspace spec.
var predicates = predicate.Funcs{
CreateFunc: func(_ event.CreateEvent) bool { return true },
DeleteFunc: func(_ event.DeleteEvent) bool { return true },
UpdateFunc: func(ev event.UpdateEvent) bool {
newObj, ok := ev.ObjectNew.(*dw.DevWorkspace)
if !ok {
return true
}
if newObj.Status.Phase != dw.DevWorkspaceStatusFailed {
return true
}
oldObj, ok := ev.ObjectOld.(*dw.DevWorkspace)
if !ok {
// Should never happen
return true
}
// always reconcile if resource is deleted
if newObj.GetDeletionTimestamp() != nil {
return true
}
// Trigger a reconcile on failed workspaces if spec is updated.
return !equality.Semantic.DeepEqual(oldObj.Spec, newObj.Spec)
},
GenericFunc: func(_ event.GenericEvent) bool { return true },
}
var podPredicates = predicate.Funcs{
UpdateFunc: func(ev event.UpdateEvent) bool {
newObj, ok := ev.ObjectNew.(*corev1.Pod)
if !ok {
//that's no Pod. Let other predicates decide
return true
}
oldObj, ok := ev.ObjectOld.(*corev1.Pod)
if !ok {
// Should never happen
return true
}
// If the devworkspace label does not exist, do no reconcile
if _, ok = newObj.Labels[constants.DevWorkspaceNameLabel]; !ok {
return false
}
// Trigger a devworkspace reconcile if related pod status is updated.
if equality.Semantic.DeepEqual(oldObj.Status, newObj.Status) {
return false
}
return true
},
}