|
| 1 | +/* |
| 2 | +** Copyright (c) 2022 Oracle and/or its affiliates. |
| 3 | +** |
| 4 | +** The Universal Permissive License (UPL), Version 1.0 |
| 5 | +** |
| 6 | +** Subject to the condition set forth below, permission is hereby granted to any |
| 7 | +** person obtaining a copy of this software, associated documentation and/or data |
| 8 | +** (collectively the "Software"), free of charge and under any and all copyright |
| 9 | +** rights in the Software, and any and all patent rights owned or freely |
| 10 | +** licensable by each licensor hereunder covering either (i) the unmodified |
| 11 | +** Software as contributed to or provided by such licensor, or (ii) the Larger |
| 12 | +** Works (as defined below), to deal in both |
| 13 | +** |
| 14 | +** (a) the Software, and |
| 15 | +** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 16 | +** one is included with the Software (each a "Larger Work" to which the Software |
| 17 | +** is contributed by such licensors), |
| 18 | +** |
| 19 | +** without restriction, including without limitation the rights to copy, create |
| 20 | +** derivative works of, display, perform, and distribute the Software and make, |
| 21 | +** use, sell, offer for sale, import, export, have made, and have sold the |
| 22 | +** Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 23 | +** either these or other terms. |
| 24 | +** |
| 25 | +** This license is subject to the following condition: |
| 26 | +** The above copyright notice and either this complete permission notice or at |
| 27 | +** a minimum a reference to the UPL must be included in all copies or |
| 28 | +** substantial portions of the Software. |
| 29 | +** |
| 30 | +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | +** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | +** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | +** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | +** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | +** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | +** SOFTWARE. |
| 37 | + */ |
| 38 | + |
| 39 | +package v1alpha1 |
| 40 | + |
| 41 | +import ( |
| 42 | + "reflect" |
| 43 | + "strings" |
| 44 | + |
| 45 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 46 | + "k8s.io/apimachinery/pkg/runtime" |
| 47 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 48 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 49 | + ctrl "sigs.k8s.io/controller-runtime" |
| 50 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 51 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 52 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 53 | +) |
| 54 | + |
| 55 | +// log is for logging in this package. |
| 56 | +var cdblog = logf.Log.WithName("cdb-webhook") |
| 57 | + |
| 58 | +func (r *CDB) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 59 | + return ctrl.NewWebhookManagedBy(mgr). |
| 60 | + For(r). |
| 61 | + Complete() |
| 62 | +} |
| 63 | + |
| 64 | +//+kubebuilder:webhook:path=/mutate-database-oracle-com-v4-cdb,mutating=true,failurePolicy=fail,sideEffects=None,groups=database.oracle.com,resources=cdbs,verbs=create;update,versions=v4,name=mcdb.kb.io,admissionReviewVersions={v1,v1beta1} |
| 65 | + |
| 66 | +var _ webhook.Defaulter = &CDB{} |
| 67 | + |
| 68 | +// Default implements webhook.Defaulter so a webhook will be registered for the type |
| 69 | +func (r *CDB) Default() { |
| 70 | + cdblog.Info("Setting default values in CDB spec for : " + r.Name) |
| 71 | + |
| 72 | + if r.Spec.ORDSPort == 0 { |
| 73 | + r.Spec.ORDSPort = 8888 |
| 74 | + } |
| 75 | + |
| 76 | + if r.Spec.Replicas == 0 { |
| 77 | + r.Spec.Replicas = 1 |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. |
| 82 | +//+kubebuilder:webhook:path=/validate-database-oracle-com-v4-cdb,mutating=false,failurePolicy=fail,sideEffects=None,groups=database.oracle.com,resources=cdbs,verbs=create;update,versions=v4,name=vcdb.kb.io,admissionReviewVersions={v1,v1beta1} |
| 83 | + |
| 84 | +var _ webhook.Validator = &CDB{} |
| 85 | + |
| 86 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type |
| 87 | +func (r *CDB) ValidateCreate() (admission.Warnings, error) { |
| 88 | + cdblog.Info("ValidateCreate", "name", r.Name) |
| 89 | + |
| 90 | + var allErrs field.ErrorList |
| 91 | + |
| 92 | + if r.Spec.ServiceName == "" && r.Spec.DBServer != "" { |
| 93 | + allErrs = append(allErrs, |
| 94 | + field.Required(field.NewPath("spec").Child("serviceName"), "Please specify CDB Service name")) |
| 95 | + } |
| 96 | + |
| 97 | + if reflect.ValueOf(r.Spec.CDBTlsKey).IsZero() { |
| 98 | + allErrs = append(allErrs, |
| 99 | + field.Required(field.NewPath("spec").Child("cdbTlsKey"), "Please specify CDB Tls key(secret)")) |
| 100 | + } |
| 101 | + |
| 102 | + if reflect.ValueOf(r.Spec.CDBTlsCrt).IsZero() { |
| 103 | + allErrs = append(allErrs, |
| 104 | + field.Required(field.NewPath("spec").Child("cdbTlsCrt"), "Please specify CDB Tls Certificate(secret)")) |
| 105 | + } |
| 106 | + |
| 107 | + if reflect.ValueOf(r.Spec.CDBPriKey).IsZero() { |
| 108 | + allErrs = append(allErrs, |
| 109 | + field.Required(field.NewPath("spec").Child("CDBPriKey"), "Please specify CDB CDBPriKey(secret)")) |
| 110 | + } |
| 111 | + |
| 112 | + /*if r.Spec.SCANName == "" { |
| 113 | + allErrs = append(allErrs, |
| 114 | + field.Required(field.NewPath("spec").Child("scanName"), "Please specify SCAN Name for CDB")) |
| 115 | + }*/ |
| 116 | + |
| 117 | + if (r.Spec.DBServer == "" && r.Spec.DBTnsurl == "") || (r.Spec.DBServer != "" && r.Spec.DBTnsurl != "") { |
| 118 | + allErrs = append(allErrs, |
| 119 | + field.Required(field.NewPath("spec").Child("dbServer"), "Please specify Database Server Name/IP Address or tnsalias string")) |
| 120 | + } |
| 121 | + |
| 122 | + if r.Spec.DBTnsurl != "" && (r.Spec.DBServer != "" || r.Spec.DBPort != 0 || r.Spec.ServiceName != "") { |
| 123 | + allErrs = append(allErrs, |
| 124 | + field.Required(field.NewPath("spec").Child("dbServer"), "DBtnsurl is orthogonal to (DBServer,DBport,Services)")) |
| 125 | + } |
| 126 | + |
| 127 | + if r.Spec.DBPort == 0 && r.Spec.DBServer != "" { |
| 128 | + allErrs = append(allErrs, |
| 129 | + field.Required(field.NewPath("spec").Child("dbPort"), "Please specify DB Server Port")) |
| 130 | + } |
| 131 | + if r.Spec.DBPort < 0 && r.Spec.DBServer != "" { |
| 132 | + allErrs = append(allErrs, |
| 133 | + field.Required(field.NewPath("spec").Child("dbPort"), "Please specify a valid DB Server Port")) |
| 134 | + } |
| 135 | + if r.Spec.ORDSPort < 0 { |
| 136 | + allErrs = append(allErrs, |
| 137 | + field.Required(field.NewPath("spec").Child("ordsPort"), "Please specify a valid ORDS Port")) |
| 138 | + } |
| 139 | + if r.Spec.Replicas < 0 { |
| 140 | + allErrs = append(allErrs, |
| 141 | + field.Required(field.NewPath("spec").Child("replicas"), "Please specify a valid value for Replicas")) |
| 142 | + } |
| 143 | + if r.Spec.ORDSImage == "" { |
| 144 | + allErrs = append(allErrs, |
| 145 | + field.Required(field.NewPath("spec").Child("ordsImage"), "Please specify name of ORDS Image to be used")) |
| 146 | + } |
| 147 | + if reflect.ValueOf(r.Spec.CDBAdminUser).IsZero() { |
| 148 | + allErrs = append(allErrs, |
| 149 | + field.Required(field.NewPath("spec").Child("cdbAdminUser"), "Please specify user in the root container with sysdba priviledges to manage PDB lifecycle")) |
| 150 | + } |
| 151 | + if reflect.ValueOf(r.Spec.CDBAdminPwd).IsZero() { |
| 152 | + allErrs = append(allErrs, |
| 153 | + field.Required(field.NewPath("spec").Child("cdbAdminPwd"), "Please specify password for the CDB Administrator to manage PDB lifecycle")) |
| 154 | + } |
| 155 | + if reflect.ValueOf(r.Spec.ORDSPwd).IsZero() { |
| 156 | + allErrs = append(allErrs, |
| 157 | + field.Required(field.NewPath("spec").Child("ordsPwd"), "Please specify password for user ORDS_PUBLIC_USER")) |
| 158 | + } |
| 159 | + if reflect.ValueOf(r.Spec.WebServerUser).IsZero() { |
| 160 | + allErrs = append(allErrs, |
| 161 | + field.Required(field.NewPath("spec").Child("webServerUser"), "Please specify the Web Server User having SQL Administrator role")) |
| 162 | + } |
| 163 | + if reflect.ValueOf(r.Spec.WebServerPwd).IsZero() { |
| 164 | + allErrs = append(allErrs, |
| 165 | + field.Required(field.NewPath("spec").Child("webServerPwd"), "Please specify password for the Web Server User having SQL Administrator role")) |
| 166 | + } |
| 167 | + if len(allErrs) == 0 { |
| 168 | + return nil, nil |
| 169 | + } |
| 170 | + return nil, apierrors.NewInvalid( |
| 171 | + schema.GroupKind{Group: "database.oracle.com", Kind: "CDB"}, |
| 172 | + r.Name, allErrs) |
| 173 | +} |
| 174 | + |
| 175 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type |
| 176 | +func (r *CDB) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { |
| 177 | + cdblog.Info("validate update", "name", r.Name) |
| 178 | + |
| 179 | + isCDBMarkedToBeDeleted := r.GetDeletionTimestamp() != nil |
| 180 | + if isCDBMarkedToBeDeleted { |
| 181 | + return nil, nil |
| 182 | + } |
| 183 | + |
| 184 | + var allErrs field.ErrorList |
| 185 | + |
| 186 | + // Check for updation errors |
| 187 | + oldCDB, ok := old.(*CDB) |
| 188 | + if !ok { |
| 189 | + return nil, nil |
| 190 | + } |
| 191 | + |
| 192 | + if r.Spec.DBPort < 0 { |
| 193 | + allErrs = append(allErrs, |
| 194 | + field.Required(field.NewPath("spec").Child("dbPort"), "Please specify a valid DB Server Port")) |
| 195 | + } |
| 196 | + if r.Spec.ORDSPort < 0 { |
| 197 | + allErrs = append(allErrs, |
| 198 | + field.Required(field.NewPath("spec").Child("ordsPort"), "Please specify a valid ORDS Port")) |
| 199 | + } |
| 200 | + if r.Spec.Replicas < 0 { |
| 201 | + allErrs = append(allErrs, |
| 202 | + field.Required(field.NewPath("spec").Child("replicas"), "Please specify a valid value for Replicas")) |
| 203 | + } |
| 204 | + if !strings.EqualFold(oldCDB.Spec.ServiceName, r.Spec.ServiceName) { |
| 205 | + allErrs = append(allErrs, |
| 206 | + field.Forbidden(field.NewPath("spec").Child("replicas"), "cannot be changed")) |
| 207 | + } |
| 208 | + |
| 209 | + if len(allErrs) == 0 { |
| 210 | + return nil, nil |
| 211 | + } |
| 212 | + |
| 213 | + return nil, apierrors.NewInvalid( |
| 214 | + schema.GroupKind{Group: "database.oracle.com", Kind: "CDB"}, |
| 215 | + r.Name, allErrs) |
| 216 | +} |
| 217 | + |
| 218 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type |
| 219 | +func (r *CDB) ValidateDelete() (admission.Warnings, error) { |
| 220 | + cdblog.Info("validate delete", "name", r.Name) |
| 221 | + |
| 222 | + // TODO(user): fill in your validation logic upon object deletion. |
| 223 | + return nil, nil |
| 224 | +} |
0 commit comments