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