github.com/verrazzano/verrazzano@v1.7.0/platform-operator/thirdparty/charts/weblogic-operator/templates/_utils.tpl (about) 1 # Copyright (c) 2018, 2022, Oracle and/or its affiliates. 2 # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 {{/* 5 Start validation 6 */}} 7 {{- define "utils.startValidation" -}} 8 {{- $scope := . -}} 9 {{- $context := dict "scope" $scope "path" list -}} 10 {{- $stack := list $context -}} 11 {{- $ignore := set $scope "validationContextStack" $stack -}} 12 {{- $ignore := include "utils.setCurrentValidationContext" $scope -}} 13 {{- end -}} 14 15 {{/* 16 End validation 17 If there were any validation errors, report them and kill the helm chart installation. 18 */}} 19 {{- define "utils.endValidation" -}} 20 {{- $scope := . -}} 21 {{- if hasKey $scope "validationErrors" -}} 22 {{- fail $scope.validationErrors -}} 23 {{- end -}} 24 {{- end -}} 25 26 {{/* 27 Push a new validation context 28 */}} 29 {{- define "utils.pushValidationContext" -}} 30 {{- $scope := index . 0 }} 31 {{- $scopeName := index . 1 }} 32 {{- $newScope := index $scope.validationScope $scopeName -}} 33 {{- $newPath := append $scope.validationPath $scopeName -}} 34 {{- $newContext := dict "scope" $newScope "path" $newPath -}} 35 {{- $newStack := append $scope.validationContextStack $newContext -}} 36 {{- $ignore := set $scope "validationContextStack" $newStack -}} 37 {{- $ignore := include "utils.setCurrentValidationContext" $scope -}} 38 {{- end -}} 39 40 {{/* 41 Pop the validation context 42 */}} 43 {{- define "utils.popValidationContext" -}} 44 {{- $scope := . }} 45 {{- $stack := $scope.validationContextStack -}} 46 {{- $ignore := set $scope "validationContextStack" (initial $stack) -}} 47 {{- $ignore := include "utils.setCurrentValidationContext" $scope -}} 48 {{- end -}} 49 50 {{/* 51 Set the current validation context from the stack 52 */}} 53 {{- define "utils.setCurrentValidationContext" -}} 54 {{- $scope := . }} 55 {{- $context := $scope.validationContextStack | last -}} 56 {{- $ignore := set $scope "validationScope" (index $context "scope") -}} 57 {{- $ignore := set $scope "validationPath" (index $context "path") -}} 58 {{- end -}} 59 60 {{/* 61 Record a validation error (it will get reported later by utils.reportValidationErrors) 62 */}} 63 {{- define "utils.recordValidationError" -}} 64 {{- $scope := index . 0 -}} 65 {{- $errorMsg := index . 1 -}} 66 {{- $path := $scope.validationPath -}} 67 {{- $pathStr := $path | join "." | trim -}} 68 {{- $scopedErrorMsg := (list "\n" $pathStr $errorMsg) | compact | join " " -}} 69 {{- if hasKey $scope "validationErrors" -}} 70 {{- $newValidationErrors := cat $scope.validationErrors $scopedErrorMsg -}} 71 {{- $ignore := set $scope "validationErrors" $newValidationErrors -}} 72 {{- else -}} 73 {{- $newValidationErrors := $scopedErrorMsg -}} 74 {{- $ignore := set $scope "validationErrors" $newValidationErrors -}} 75 {{- end -}} 76 {{- end -}} 77 78 {{/* 79 Returns whether any errors have been reported 80 */}} 81 {{- define "utils.haveValidationErrors" -}} 82 {{- if hasKey . "validationErrors" -}} 83 true 84 {{- end -}} 85 {{- end -}} 86 87 {{/* 88 Determine whether a dictionary has a non-null value for a key 89 */}} 90 {{- define "utils.dictionaryHasNonNullValue" -}} 91 {{- $dict := index . 0 -}} 92 {{- $name := index . 1 -}} 93 {{- if and (hasKey $dict $name) (not ( eq (typeOf (index $dict $name)) "<nil>" )) -}} 94 true 95 {{- end -}} 96 {{- end -}} 97 98 {{/* 99 Verify that a value of a specific kind has been specified. 100 */}} 101 {{- define "utils.verifyValue" -}} 102 {{- $requiredKind := index . 0 -}} 103 {{- $scope := index . 1 -}} 104 {{- $name := index . 2 -}} 105 {{- $isRequired := index . 3 -}} 106 {{- if $scope.trace -}} 107 {{- $errorMsg := cat "TRACE" $name $requiredKind $isRequired -}} 108 {{- $ignore := include "utils.recordValidationError" (list $scope $errorMsg) -}} 109 {{- end -}} 110 {{- $parent := $scope.validationScope -}} 111 {{- if include "utils.dictionaryHasNonNullValue" (list $parent $name) -}} 112 {{- $value := index $parent $name -}} 113 {{- $actualKind := kindOf $value -}} 114 {{- if eq $requiredKind $actualKind -}} 115 true 116 {{- else -}} 117 {{- $errorMsg := cat $name "must be a" $requiredKind ":" $actualKind -}} 118 {{- include "utils.recordValidationError" (list $scope $errorMsg) -}} 119 {{- end -}} 120 {{- else -}} 121 {{- if $isRequired -}} 122 {{- $errorMsg := cat $requiredKind $name "must be specified" -}} 123 {{- include "utils.recordValidationError" (list $scope $errorMsg) -}} 124 {{- else -}} 125 true 126 {{- end -}} 127 {{- end -}} 128 {{- end -}} 129 130 {{/* 131 Verify that a list value has been specified 132 */}} 133 {{- define "utils.verifyListValue" -}} 134 {{- $requiredKind := index . 0 -}} 135 {{- $scope := index . 1 -}} 136 {{- $name := index . 2 -}} 137 {{- $isRequired := index . 3 -}} 138 {{- $parent := $scope.validationScope -}} 139 {{- $args := . -}} 140 {{- if include "utils.verifyValue" (list "slice" $scope $name $isRequired) -}} 141 {{- $status := dict -}} 142 {{- if hasKey $parent $name -}} 143 {{- $list := index $parent $name -}} 144 {{- range $value := $list -}} 145 {{- $actualKind := kindOf $value -}} 146 {{- if not (eq $requiredKind $actualKind) -}} 147 {{- $errorMsg := cat $name "must only contain" $requiredKind "elements:" $actualKind -}} 148 {{- include "utils.recordValidationError" (list $scope $errorMsg) -}} 149 {{- $ignore := set $status "error" true -}} 150 {{- end -}} 151 {{- end -}} 152 {{- end -}} 153 {{- if not (hasKey $status "error") -}} 154 true 155 {{- end -}} 156 {{- end -}} 157 {{- end -}} 158 159 {{/* 160 Verify a string value 161 */}} 162 {{- define "utils.baseVerifyString" -}} 163 {{- include "utils.verifyValue" (prepend . "string") -}} 164 {{- end -}} 165 166 {{/* 167 Verify a required string value 168 */}} 169 {{- define "utils.verifyString" -}} 170 {{- include "utils.baseVerifyString" (append . true) -}} 171 {{- end -}} 172 173 {{/* 174 Verify an optional string value 175 */}} 176 {{- define "utils.verifyOptionalString" -}} 177 {{- include "utils.baseVerifyString" (append . false) -}} 178 {{- end -}} 179 180 {{/* 181 Verify a boolean value 182 */}} 183 {{- define "utils.baseVerifyBoolean" -}} 184 {{- include "utils.verifyValue" (prepend . "bool") -}} 185 {{- end -}} 186 187 {{/* 188 Verify a required boolean value 189 */}} 190 {{- define "utils.verifyBoolean" -}} 191 {{- include "utils.baseVerifyBoolean" (append . true) -}} 192 {{- end -}} 193 194 {{/* 195 Verify an optional boolean value 196 */}} 197 {{- define "utils.verifyOptionalBoolean" -}} 198 {{- include "utils.baseVerifyBoolean" (append . false) -}} 199 {{- end -}} 200 201 {{/* 202 Verify an integer value 203 */}} 204 {{- define "utils.baseVerifyInteger" -}} 205 {{- include "utils.verifyValue" (prepend . "float64") -}} 206 {{- end -}} 207 208 {{/* 209 Verify a required integer value 210 */}} 211 {{- define "utils.verifyInteger" -}} 212 {{- include "utils.baseVerifyInteger" (append . true) -}} 213 {{- end -}} 214 215 {{/* 216 Verify an optional required integer value 217 */}} 218 {{- define "utils.verifyOptionalInteger" -}} 219 {{- include "utils.baseVerifyInteger" (append . false) -}} 220 {{- end -}} 221 222 {{/* 223 Verify a dictionary value 224 */}} 225 {{- define "utils.baseVerifyDictionary" -}} 226 {{- include "utils.verifyValue" (prepend . "map") -}} 227 {{- end -}} 228 229 {{/* 230 Verify a required dictionary value 231 */}} 232 {{- define "utils.verifyDictionary" -}} 233 {{- include "utils.baseVerifyDictionary" (append . true) -}} 234 {{- end -}} 235 236 {{/* 237 Verify an optional dictionary value 238 */}} 239 {{- define "utils.verifyOptionalDictionary" -}} 240 {{- include "utils.baseVerifyDictionary" (append . false) -}} 241 {{- end -}} 242 243 {{/* 244 Verify a enum string value 245 */}} 246 {{- define "utils.baseVerifyEnum" -}} 247 {{- $scope := index . 0 -}} 248 {{- $name := index . 1 -}} 249 {{- $legalValues := index . 2 -}} 250 {{- $isRequired := index . 3 -}} 251 {{- if include "utils.baseVerifyString" (list $scope $name $isRequired) -}} 252 {{- $parent := $scope.validationScope -}} 253 {{- if include "utils.dictionaryHasNonNullValue" (list $parent $name) -}} 254 {{- $value := index $parent $name -}} 255 {{- if has $value $legalValues -}} 256 true 257 {{- else -}} 258 {{ $errorMsg := cat $name "must be one of the following values" $legalValues ":" $value -}} 259 {{- include "utils.recordValidationError" (list $scope $errorMsg) -}} 260 {{- end -}} 261 {{- end -}} 262 {{- end -}} 263 {{- end -}} 264 265 {{/* 266 Verify a required enum string value 267 */}} 268 {{- define "utils.verifyEnum" -}} 269 {{- include "utils.baseVerifyEnum" (append . true) -}} 270 {{- end -}} 271 272 {{/* 273 Verify an optional enum string value 274 */}} 275 {{- define "utils.verifyOptionalEnum" -}} 276 {{- include "utils.baseVerifyEnum" (append . false) -}} 277 {{- end -}} 278 279 {{/* 280 Verify a kubernetes resource name string value 281 */}} 282 {{- define "utils.baseVerifyResourceName" -}} 283 {{/* https://kubernetes.io/docs/concepts/overview/working-with-objects/names */}} 284 {{/* names: only lower case, numbers, dot, dash, max 253 */}} 285 {{/* https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set */}} 286 {{/* labels/selectors - upper & lower case, numbers, dot, dash, underscore, max 63 */}} 287 {{- $scope := index . 0 -}} 288 {{- $name := index . 1 -}} 289 {{- $max := index . 2 -}} 290 {{- $isRequired := index . 3 -}} 291 {{- if include "utils.baseVerifyString" (list $scope $name $isRequired) -}} 292 {{- $parent := $scope.validationScope -}} 293 {{- if include "utils.dictionaryHasNonNullValue" (list $parent $name) -}} 294 {{- $value := index $parent $name -}} 295 {{- $len := len $value -}} 296 {{- if and (le $len $max) (regexMatch "^[a-z0-9.-]+$" $value) -}} 297 true 298 {{- else -}} 299 {{- $errorMsg := cat $name "must only contain lower case letters, numbers, dashes and dots, and must not contain more than" $max "characters: " $value -}} 300 {{- include "utils.recordValidationError" (list $scope $errorMsg) -}} 301 {{- end -}} 302 {{- end -}} 303 {{- else -}} 304 {{- end -}} 305 {{- end -}} 306 307 {{/* 308 Verify a required kubernetes resource name string value 309 */}} 310 {{- define "utils.verifyResourceName" -}} 311 {{- include "utils.baseVerifyResourceName" (append . true) -}} 312 {{- end -}} 313 314 {{/* 315 Verify an optional kubernetes resource name string value 316 */}} 317 {{- define "utils.verifyOptionalResourceName" -}} 318 {{- include "utils.baseVerifyResourceName" (append . false) -}} 319 {{- end -}} 320 321 {{/* 322 Verify external service name suffix string value 323 */}} 324 {{- define "utils.verifyExternalServiceNameSuffix" -}} 325 {{- include "utils.baseVerifyResourceName" (append . false) -}} 326 {{- end -}} 327 328 {{/* 329 Verify introspector job name suffix string value 330 */}} 331 {{- define "utils.verifyIntrospectorJobNameSuffix" -}} 332 {{- include "utils.baseVerifyResourceName" (append . false) -}} 333 {{- end -}} 334 335 {{/* 336 Verify a list of strings value 337 */}} 338 {{- define "utils.baseVerifyStringList" -}} 339 {{- include "utils.verifyListValue" (prepend . "string") -}} 340 {{- end -}} 341 342 {{/* 343 Verify a required list of strings value 344 */}} 345 {{- define "utils.verifyStringList" -}} 346 {{- include "utils.baseVerifyStringList" (append . true) -}} 347 {{- end -}} 348 349 {{/* 350 Verify an optional list of strings value 351 */}} 352 {{- define "utils.verifyOptionalStringList" -}} 353 {{- include "utils.baseVerifyStringList" (append . false) -}} 354 {{- end -}} 355 356 {{/* 357 Verify a list of dictionaries value 358 */}} 359 {{- define "utils.baseVerifyDictionaryList" -}} 360 {{- include "utils.verifyListValue" (prepend . "map") -}} 361 {{- end -}} 362 363 {{/* 364 Verify a required list of dictionaries value 365 */}} 366 {{- define "utils.verifyDictionaryList" -}} 367 {{- include "utils.baseVerifyDictionaryList" (append . true) -}} 368 {{- end -}} 369 370 {{/* 371 Verify an optional list of dictionaries value 372 */}} 373 {{- define "utils.verifyOptionalDictionaryList" -}} 374 {{- include "utils.baseVerifyDictionaryList" (append . false) -}} 375 {{- end -}} 376 377 {{/* 378 Merge a set of dictionaries into a single dictionary. 379 380 The scope must be a list of dictionaries, starting with the least specific 381 and ending with the most specific. 382 383 First it makes an empty destinaction dictionary, then iterates over the dictionaries, 384 overlaying their values on the destination dictionary. 385 386 If a value is null, then it removes that key from the destination dictionary. 387 388 If the value is already present in the destination dictionary, and the old and 389 new values are both dictionaries, it merges them into the destination. 390 */}} 391 {{- define "utils.mergeDictionaries" -}} 392 {{- $dest := dict -}} 393 {{- range $src := . -}} 394 {{- if not (empty $src) -}} 395 {{- range $key, $value := $src -}} 396 {{- $ignore := include "utils.mergeDictionaryValue" (list $dest $key $value) -}} 397 {{- end -}} 398 {{- end -}} 399 {{- end -}} 400 {{- toYaml $dest -}} 401 {{- end -}} 402 403 {{/* 404 Merge a value into a dictionary. 405 This is like helm's 'merge' function, except that it handles null entries too. 406 */}} 407 {{- define "utils.mergeDictionaryValue" -}} 408 {{- $dest := index . 0 -}} 409 {{- $key := index . 1 -}} 410 {{- $newValue := index . 2 -}} 411 {{- $newType := typeOf $newValue -}} 412 {{- if hasKey $dest $key -}} 413 {{- if eq $newType "<nil>" -}} 414 {{/* # if the value already existed, and the new value is null, remove the old value */}} 415 {{- $ignore := unset $dest $key -}} 416 {{- else -}} 417 {{- $oldValue := index $dest $key -}} 418 {{- $oldKind := kindOf $oldValue -}} 419 {{- $newKind := kindOf $newValue -}} 420 {{- if (and (eq $oldKind "map") (eq $newKind "map")) -}} 421 {{/* # if both values are maps, merge them */}} 422 {{- $merged := include "utils.mergeDictionaries" (list $oldValue $newValue) | fromYaml -}} 423 {{- $ignore := set $dest $key $merged -}} 424 {{- else -}} 425 {{/* # replace the old value with the new one */}} 426 {{- $ignore := set $dest $key $newValue -}} 427 {{- end -}} 428 {{- end -}} 429 {{- else -}} 430 {{- if not (eq $newType "<nil>") -}} 431 {{/* #if there was no old value, and the new value isn't null, use the new value */}} 432 {{- $ignore := set $dest $key $newValue -}} 433 {{- end -}} 434 {{- end -}} 435 {{- end -}} 436 437 {{/* 438 Make a writable copy of a dictionary. 439 TBD - does helm provide a clone method we can use instead? 440 */}} 441 {{- define "utils.cloneDictionary" -}} 442 {{- include "utils.mergeDictionaries" (list .) -}} 443 {{- end -}} 444 445 {{/* 446 Verify that a list of values (exclude) can not be defined if another value (key) is already defined 447 */}} 448 {{- define "utils.mutexValue" -}} 449 {{- $scope := index . 0 -}} 450 {{- $key := index . 1 -}} 451 {{- $exclude := index . 2 -}} 452 {{- $type := index . 3 -}} 453 {{- $parent := $scope.validationScope -}} 454 {{- $args := . -}} 455 {{- $status := dict -}} 456 {{- if hasKey $parent $key -}} 457 {{- range $value := $exclude -}} 458 {{- if hasKey $parent $value -}} 459 {{- $errorMsg := cat $value "can not be present when" $key "is defined" " " -}} 460 {{- include "utils.recordValidationError" (list $scope $errorMsg) -}} 461 {{- $ignore := set $status "error" true -}} 462 {{- end -}} 463 {{- end -}} 464 {{- end -}} 465 {{- if not (hasKey $status "error") -}} 466 true 467 {{- end -}} 468 {{- end -}} 469 470 {{/* 471 Verify that a list of strings can not be defined if another string is already defined 472 */}} 473 {{- define "utils.mutexString" -}} 474 {{- include "utils.mutexValue" (append . "string") -}} 475 {{- end -}} 476 477 {{/* 478 Verify that a Kubernetes resource exists in a given namespace 479 */}} 480 {{- define "utils.verifyK8SResource" -}} 481 {{- $scope := index . 0 -}} 482 {{- $name := index . 1 -}} 483 {{- $type := index . 2 -}} 484 {{- $namespace := index . 3 -}} 485 {{- $foundNS := (lookup "v1" "Namespace" "" $namespace) }} 486 {{- if $foundNS }} 487 {{- $foundResource := (lookup "v1" $type $namespace $name) }} 488 {{- if not $foundResource }} 489 {{- $errorMsg := cat $type $name " not found in namespace " $namespace -}} 490 {{- include "utils.recordValidationError" (list $scope $errorMsg) -}} 491 {{- end -}} 492 {{- end -}} 493 {{- end -}} 494 495 496 {{/* 497 Return true if there's an existing webhook deployment and chart version is less than or equal to the exsiting webhook version. 498 */}} 499 {{- define "utils.verifyExistingWebhookDeployment" -}} 500 {{- $chartVersion := index . 0 -}} 501 {{- $releaseNamespace := index . 1 -}} 502 {{- range $deployment := (lookup "apps/v1" "Deployment" "" "").items }} 503 {{- if not $deployment.metadata.labels }} 504 {{- $ignore := set $deployment.metadata "labels" (dict) }} 505 {{- end }} 506 {{- if and (eq $deployment.metadata.name "weblogic-operator-webhook") (hasKey $deployment.metadata.labels "weblogic.webhookVersion") }} 507 {{ $webhookVersion := get $deployment.metadata.labels "weblogic.webhookVersion" }} 508 {{- if (eq $deployment.metadata.namespace $releaseNamespace) }} 509 {{- print "false" }} 510 {{- end -}} 511 {{- if le $chartVersion $webhookVersion }} 512 {{- print "true" }} 513 {{- end -}} 514 {{- end -}} 515 {{- end -}} 516 {{- end -}}