Added Daemonset Status predicate
[demo.git] / vnfs / DAaaS / microservices / collectd-operator / pkg / controller / utils / predicate.go
1 // Copyright 2018 The Operator-SDK Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package utils
16
17 import (
18         appsv1 "k8s.io/api/apps/v1"
19
20         "sigs.k8s.io/controller-runtime/pkg/event"
21         "sigs.k8s.io/controller-runtime/pkg/predicate"
22         logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
23 )
24
25 var plog = logf.Log.WithName("predicate").WithName("eventFilters")
26
27 // DaemonSetStatusChangedPredicate implements a default update predicate function on status change for Daemonsets
28 // (adapted from sigs.k8s.io/controller-runtime/pkg/predicate/predicate.GenerationChangedPredicate)
29 type DaemonSetStatusChangedPredicate struct {
30         predicate.Funcs
31 }
32
33 // Update implements default UpdateEvent filter for validating generation change
34 func (DaemonSetStatusChangedPredicate) Update(e event.UpdateEvent) bool {
35         newDS := e.ObjectNew.DeepCopyObject().(*appsv1.DaemonSet)
36         oldDS := e.ObjectOld.DeepCopyObject().(*appsv1.DaemonSet)
37         plog.V(2).Info("newDS", "nUNS:=", newDS.Status.UpdatedNumberScheduled, "oUNS:=", oldDS.Status.UpdatedNumberScheduled, "nDNS:=", newDS.Status.DesiredNumberScheduled, "nNR:=", newDS.Status.NumberReady, "nNA:=", newDS.Status.NumberAvailable)
38         if newDS.Status.UpdatedNumberScheduled >= oldDS.Status.UpdatedNumberScheduled {
39                 if (newDS.Status.UpdatedNumberScheduled == newDS.Status.NumberReady) &&
40                         (newDS.Status.UpdatedNumberScheduled == newDS.Status.NumberAvailable) {
41                         return true
42                 }
43         }
44         if e.MetaOld == nil {
45                 plog.Error(nil, "Update event has no old metadata", "event", e)
46                 return false
47         }
48         if e.ObjectOld == nil {
49                 plog.Error(nil, "Update event has no old runtime object to update", "event", e)
50                 return false
51         }
52         if e.ObjectNew == nil {
53                 plog.Error(nil, "Update event has no new runtime object for update", "event", e)
54                 return false
55         }
56         if e.MetaNew == nil {
57                 plog.Error(nil, "Update event has no new metadata", "event", e)
58                 return false
59         }
60         if e.MetaNew.GetGeneration() == e.MetaOld.GetGeneration() {
61                 return false
62         }
63
64         return true
65 }