github.com/banzaicloud/operator-tools@v0.28.10/pkg/resources/objectmodifiers.go (about) 1 // Copyright © 2020 Banzai Cloud 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 resources 16 17 import ( 18 admissionregistrationv1 "k8s.io/api/admissionregistration/v1" 19 admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1" 20 appsv1 "k8s.io/api/apps/v1" 21 batchv1 "k8s.io/api/batch/v1" 22 corev1 "k8s.io/api/core/v1" 23 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 24 25 policyv1beta1 "k8s.io/api/policy/v1beta1" 26 apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" 27 "k8s.io/apimachinery/pkg/runtime" 28 ) 29 30 type ObjectModifierFunc func(o runtime.Object) (runtime.Object, error) 31 type ObjectModifierWithParentFunc func(o, p runtime.Object) (runtime.Object, error) 32 33 var DefaultModifiers = []ObjectModifierFunc{ 34 ClearCRDStatusModifier, 35 ClusterScopeNamespaceFixModifier, 36 MutatingWebhookConfigurationModifier, 37 ValidatingWebhookConfigurationModifier, 38 } 39 40 func WorkloadImagePullSecretsModifier(imagePullSecrets ...[]corev1.LocalObjectReference) ObjectModifierFunc { 41 merge := func(existing []corev1.LocalObjectReference, additions ...[]corev1.LocalObjectReference) []corev1.LocalObjectReference { 42 var ips []corev1.LocalObjectReference 43 ips = append(ips, existing...) 44 for _, addition := range additions { 45 ips = append(ips, addition...) 46 } 47 return ips 48 } 49 50 return func(obj runtime.Object) (runtime.Object, error) { 51 switch o := obj.(type) { 52 case *appsv1.DaemonSet: 53 o.Spec.Template.Spec.ImagePullSecrets = merge(o.Spec.Template.Spec.ImagePullSecrets, imagePullSecrets...) 54 case *appsv1.Deployment: 55 o.Spec.Template.Spec.ImagePullSecrets = merge(o.Spec.Template.Spec.ImagePullSecrets, imagePullSecrets...) 56 case *appsv1.ReplicaSet: 57 o.Spec.Template.Spec.ImagePullSecrets = merge(o.Spec.Template.Spec.ImagePullSecrets, imagePullSecrets...) 58 case *appsv1.StatefulSet: 59 o.Spec.Template.Spec.ImagePullSecrets = merge(o.Spec.Template.Spec.ImagePullSecrets, imagePullSecrets...) 60 case *corev1.ReplicationController: 61 o.Spec.Template.Spec.ImagePullSecrets = merge(o.Spec.Template.Spec.ImagePullSecrets, imagePullSecrets...) 62 case *batchv1.Job: 63 o.Spec.Template.Spec.ImagePullSecrets = merge(o.Spec.Template.Spec.ImagePullSecrets, imagePullSecrets...) 64 } 65 66 return obj, nil 67 } 68 } 69 70 func ClearCRDStatusModifier(o runtime.Object) (runtime.Object, error) { 71 if crd, ok := o.(*apiextensionsv1beta1.CustomResourceDefinition); ok { 72 crd.Status = apiextensionsv1beta1.CustomResourceDefinitionStatus{} 73 } 74 75 if crd, ok := o.(*apiextensionsv1.CustomResourceDefinition); ok { 76 crd.Status = apiextensionsv1.CustomResourceDefinitionStatus{} 77 } 78 79 return o, nil 80 } 81 82 func ClusterScopeNamespaceFixModifier(o runtime.Object) (runtime.Object, error) { 83 if obj, ok := o.(*policyv1beta1.PodSecurityPolicy); ok { 84 obj.Namespace = "" 85 } 86 87 return o, nil 88 } 89 90 func MutatingWebhookConfigurationModifier(o runtime.Object) (runtime.Object, error) { 91 if obj, ok := o.(*admissionregistrationv1beta1.MutatingWebhookConfiguration); ok { 92 allScope := admissionregistrationv1beta1.AllScopes 93 for i, wh := range obj.Webhooks { 94 for l, r := range wh.Rules { 95 if r.Scope == nil { 96 r.Scope = &allScope 97 wh.Rules[l] = r 98 } 99 } 100 obj.Webhooks[i] = wh 101 } 102 } 103 if obj, ok := o.(*admissionregistrationv1.MutatingWebhookConfiguration); ok { 104 allScope := admissionregistrationv1.AllScopes 105 for i, wh := range obj.Webhooks { 106 for l, r := range wh.Rules { 107 if r.Scope == nil { 108 r.Scope = &allScope 109 wh.Rules[l] = r 110 } 111 } 112 obj.Webhooks[i] = wh 113 } 114 } 115 116 return o, nil 117 } 118 119 func ValidatingWebhookConfigurationModifier(o runtime.Object) (runtime.Object, error) { 120 if obj, ok := o.(*admissionregistrationv1beta1.ValidatingWebhookConfiguration); ok { 121 allScope := admissionregistrationv1beta1.AllScopes 122 for i, wh := range obj.Webhooks { 123 for l, r := range wh.Rules { 124 if r.Scope == nil { 125 r.Scope = &allScope 126 wh.Rules[l] = r 127 } 128 } 129 obj.Webhooks[i] = wh 130 } 131 } 132 if obj, ok := o.(*admissionregistrationv1.ValidatingWebhookConfiguration); ok { 133 allScope := admissionregistrationv1.AllScopes 134 for i, wh := range obj.Webhooks { 135 for l, r := range wh.Rules { 136 if r.Scope == nil { 137 r.Scope = &allScope 138 wh.Rules[l] = r 139 } 140 } 141 obj.Webhooks[i] = wh 142 } 143 } 144 145 return o, nil 146 }