|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package deployment |
| 24 | + |
| 25 | +import ( |
| 26 | + "k8s.io/api/core/v1" |
| 27 | + "k8s.io/apimachinery/pkg/fields" |
| 28 | + "k8s.io/client-go/tools/cache" |
| 29 | +) |
| 30 | + |
| 31 | +// listenForPodEvents keep listening for changes in pod until the given channel is closed. |
| 32 | +func (d *Deployment) listenForPodEvents(stopCh <-chan struct{}) { |
| 33 | + source := cache.NewListWatchFromClient( |
| 34 | + d.deps.KubeCli.CoreV1().RESTClient(), |
| 35 | + "pods", |
| 36 | + d.apiObject.GetNamespace(), |
| 37 | + fields.Everything()) |
| 38 | + |
| 39 | + getPod := func(obj interface{}) (*v1.Pod, bool) { |
| 40 | + pod, ok := obj.(*v1.Pod) |
| 41 | + if !ok { |
| 42 | + tombstone, ok := obj.(cache.DeletedFinalStateUnknown) |
| 43 | + if !ok { |
| 44 | + return nil, false |
| 45 | + } |
| 46 | + pod, ok = tombstone.Obj.(*v1.Pod) |
| 47 | + return pod, ok |
| 48 | + } |
| 49 | + return pod, true |
| 50 | + } |
| 51 | + |
| 52 | + _, informer := cache.NewIndexerInformer(source, &v1.Pod{}, 0, cache.ResourceEventHandlerFuncs{ |
| 53 | + AddFunc: func(obj interface{}) { |
| 54 | + if p, ok := getPod(obj); ok && d.isOwnerOf(p) { |
| 55 | + d.triggerInspection() |
| 56 | + } |
| 57 | + }, |
| 58 | + UpdateFunc: func(oldObj, newObj interface{}) { |
| 59 | + if p, ok := getPod(newObj); ok && d.isOwnerOf(p) { |
| 60 | + d.triggerInspection() |
| 61 | + } |
| 62 | + }, |
| 63 | + DeleteFunc: func(obj interface{}) { |
| 64 | + if p, ok := getPod(obj); ok && d.isOwnerOf(p) { |
| 65 | + d.triggerInspection() |
| 66 | + } |
| 67 | + }, |
| 68 | + }, cache.Indexers{}) |
| 69 | + |
| 70 | + informer.Run(stopCh) |
| 71 | +} |
| 72 | + |
| 73 | +// listenForPVCEvents keep listening for changes in PVC's until the given channel is closed. |
| 74 | +func (d *Deployment) listenForPVCEvents(stopCh <-chan struct{}) { |
| 75 | + source := cache.NewListWatchFromClient( |
| 76 | + d.deps.KubeCli.CoreV1().RESTClient(), |
| 77 | + "persistentvolumeclaims", |
| 78 | + d.apiObject.GetNamespace(), |
| 79 | + fields.Everything()) |
| 80 | + |
| 81 | + getPVC := func(obj interface{}) (*v1.PersistentVolumeClaim, bool) { |
| 82 | + pvc, ok := obj.(*v1.PersistentVolumeClaim) |
| 83 | + if !ok { |
| 84 | + tombstone, ok := obj.(cache.DeletedFinalStateUnknown) |
| 85 | + if !ok { |
| 86 | + return nil, false |
| 87 | + } |
| 88 | + pvc, ok = tombstone.Obj.(*v1.PersistentVolumeClaim) |
| 89 | + return pvc, ok |
| 90 | + } |
| 91 | + return pvc, true |
| 92 | + } |
| 93 | + |
| 94 | + _, informer := cache.NewIndexerInformer(source, &v1.PersistentVolumeClaim{}, 0, cache.ResourceEventHandlerFuncs{ |
| 95 | + AddFunc: func(obj interface{}) { |
| 96 | + if p, ok := getPVC(obj); ok && d.isOwnerOf(p) { |
| 97 | + d.triggerInspection() |
| 98 | + } |
| 99 | + }, |
| 100 | + UpdateFunc: func(oldObj, newObj interface{}) { |
| 101 | + if p, ok := getPVC(newObj); ok && d.isOwnerOf(p) { |
| 102 | + d.triggerInspection() |
| 103 | + } |
| 104 | + }, |
| 105 | + DeleteFunc: func(obj interface{}) { |
| 106 | + if p, ok := getPVC(obj); ok && d.isOwnerOf(p) { |
| 107 | + d.triggerInspection() |
| 108 | + } |
| 109 | + }, |
| 110 | + }, cache.Indexers{}) |
| 111 | + |
| 112 | + informer.Run(stopCh) |
| 113 | +} |
| 114 | + |
| 115 | +// listenForServiceEvents keep listening for changes in Service's until the given channel is closed. |
| 116 | +func (d *Deployment) listenForServiceEvents(stopCh <-chan struct{}) { |
| 117 | + source := cache.NewListWatchFromClient( |
| 118 | + d.deps.KubeCli.CoreV1().RESTClient(), |
| 119 | + "services", |
| 120 | + d.apiObject.GetNamespace(), |
| 121 | + fields.Everything()) |
| 122 | + |
| 123 | + getService := func(obj interface{}) (*v1.Service, bool) { |
| 124 | + service, ok := obj.(*v1.Service) |
| 125 | + if !ok { |
| 126 | + tombstone, ok := obj.(cache.DeletedFinalStateUnknown) |
| 127 | + if !ok { |
| 128 | + return nil, false |
| 129 | + } |
| 130 | + service, ok = tombstone.Obj.(*v1.Service) |
| 131 | + return service, ok |
| 132 | + } |
| 133 | + return service, true |
| 134 | + } |
| 135 | + |
| 136 | + _, informer := cache.NewIndexerInformer(source, &v1.Service{}, 0, cache.ResourceEventHandlerFuncs{ |
| 137 | + AddFunc: func(obj interface{}) { |
| 138 | + if s, ok := getService(obj); ok && d.isOwnerOf(s) { |
| 139 | + d.triggerInspection() |
| 140 | + } |
| 141 | + }, |
| 142 | + UpdateFunc: func(oldObj, newObj interface{}) { |
| 143 | + if s, ok := getService(newObj); ok && d.isOwnerOf(s) { |
| 144 | + d.triggerInspection() |
| 145 | + } |
| 146 | + }, |
| 147 | + DeleteFunc: func(obj interface{}) { |
| 148 | + if s, ok := getService(obj); ok && d.isOwnerOf(s) { |
| 149 | + d.triggerInspection() |
| 150 | + } |
| 151 | + }, |
| 152 | + }, cache.Indexers{}) |
| 153 | + |
| 154 | + informer.Run(stopCh) |
| 155 | +} |
0 commit comments