1 // Copyright 2022 Google LLC 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 registration 16 17 import ( 18 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/crd/crdgeneration" 19 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 "sigs.k8s.io/controller-runtime/pkg/event" 22 "sigs.k8s.io/controller-runtime/pkg/predicate" 23 ) 24 25 // This predicate will react only to Create requests from CRDs that KCC manages. 26 type ManagedByKCCPredicate struct { 27 predicate.Funcs 28 } 29 30 // Create returns true if the given resource has the KCC management label. 31 func (ManagedByKCCPredicate) Create(e event.CreateEvent) bool { 32 return isManagedByKCC(e.Object) 33 } 34 35 // Update returns true if the given resource has the KCC management label. 36 // When CRD is changed, the controller should reload its jsonSchema from the 37 // newly updated CRD. 38 func (ManagedByKCCPredicate) Update(e event.UpdateEvent) bool { 39 return isManagedByKCC(e.ObjectNew) 40 } 41 42 // Delete always returns false, as currently there is no support for removing controllers 43 // on CRD deletion. 44 func (ManagedByKCCPredicate) Delete(_ event.DeleteEvent) bool { 45 return false 46 } 47 48 func isManagedByKCC(o metav1.Object) bool { 49 val, ok := o.GetLabels()[crdgeneration.ManagedByKCCLabel] 50 return ok && val == "true" 51 } 52