|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +{{/* |
| 17 | +Validation to prevent operator conflicts |
| 18 | +Prevents all conflict scenarios: |
| 19 | +1. Multiple cluster-wide operators (multiple cluster managers) |
| 20 | +2. Namespace-restricted operator when cluster-wide exists (both would manage same resources) |
| 21 | +3. Cluster-wide operator when namespace-restricted exist (both would manage same resources) |
| 22 | +*/}} |
| 23 | +{{- define "dynamo-operator.validateClusterWideInstallation" -}} |
| 24 | +{{- $currentReleaseName := .Release.Name -}} |
| 25 | + |
| 26 | +{{/* Check for existing namespace-restricted operators (only when installing cluster-wide) */}} |
| 27 | +{{- if not .Values.namespaceRestriction.enabled -}} |
| 28 | + {{- $allRoles := lookup "rbac.authorization.k8s.io/v1" "Role" "" "" -}} |
| 29 | + {{- $namespaceRestrictedOperators := list -}} |
| 30 | + |
| 31 | + {{- if $allRoles -}} |
| 32 | + {{- range $role := $allRoles.items -}} |
| 33 | + {{- if and (contains "-dynamo-operator-" $role.metadata.name) (hasSuffix "-manager-role" $role.metadata.name) -}} |
| 34 | + {{- $namespaceRestrictedOperators = append $namespaceRestrictedOperators $role.metadata.namespace -}} |
| 35 | + {{- end -}} |
| 36 | + {{- end -}} |
| 37 | + {{- end -}} |
| 38 | + |
| 39 | + {{- if $namespaceRestrictedOperators -}} |
| 40 | + {{- fail (printf "VALIDATION ERROR: Cannot install cluster-wide Dynamo operator. Found existing namespace-restricted Dynamo operators in namespaces: %s. This would create resource conflicts as both the cluster-wide operator and namespace-restricted operators would manage the same DGDs/DCDs. Either:\n1. Use one of the existing namespace-restricted operators for your specific namespace, or\n2. Uninstall all existing namespace-restricted operators first, or\n3. Install this operator in namespace-restricted mode: --set namespaceRestriction.enabled=true" (join ", " ($namespaceRestrictedOperators | uniq))) -}} |
| 41 | + {{- end -}} |
| 42 | +{{- end -}} |
| 43 | + |
| 44 | +{{/* Check for existing ClusterRoles that would indicate other cluster-wide installations */}} |
| 45 | +{{- $existingClusterRoles := lookup "rbac.authorization.k8s.io/v1" "ClusterRole" "" "" -}} |
| 46 | +{{- $foundExistingClusterWideOperator := false -}} |
| 47 | +{{- $existingOperatorRelease := "" -}} |
| 48 | +{{- $existingOperatorRoleName := "" -}} |
| 49 | +{{- $existingOperatorNamespace := "" -}} |
| 50 | + |
| 51 | +{{- if $existingClusterRoles -}} |
| 52 | + {{- range $cr := $existingClusterRoles.items -}} |
| 53 | + {{- if and (contains "-dynamo-operator-" $cr.metadata.name) (hasSuffix "-manager-role" $cr.metadata.name) -}} |
| 54 | + {{- $currentRoleName := printf "%s-dynamo-operator-manager-role" $currentReleaseName -}} |
| 55 | + {{- if ne $cr.metadata.name $currentRoleName -}} |
| 56 | + {{- $foundExistingClusterWideOperator = true -}} |
| 57 | + {{- $existingOperatorRoleName = $cr.metadata.name -}} |
| 58 | + {{- if $cr.metadata.labels -}} |
| 59 | + {{- if $cr.metadata.labels.release -}} |
| 60 | + {{- $existingOperatorRelease = $cr.metadata.labels.release -}} |
| 61 | + {{- else if index $cr.metadata.labels "app.kubernetes.io/instance" -}} |
| 62 | + {{- $existingOperatorRelease = index $cr.metadata.labels "app.kubernetes.io/instance" -}} |
| 63 | + {{- end -}} |
| 64 | + {{- end -}} |
| 65 | + |
| 66 | + {{/* Find the namespace by looking at ClusterRoleBinding subjects */}} |
| 67 | + {{- $clusterRoleBindings := lookup "rbac.authorization.k8s.io/v1" "ClusterRoleBinding" "" "" -}} |
| 68 | + {{- if $clusterRoleBindings -}} |
| 69 | + {{- range $crb := $clusterRoleBindings.items -}} |
| 70 | + {{- if eq $crb.roleRef.name $cr.metadata.name -}} |
| 71 | + {{- range $subject := $crb.subjects -}} |
| 72 | + {{- if and (eq $subject.kind "ServiceAccount") $subject.namespace -}} |
| 73 | + {{- $existingOperatorNamespace = $subject.namespace -}} |
| 74 | + {{- end -}} |
| 75 | + {{- end -}} |
| 76 | + {{- end -}} |
| 77 | + {{- end -}} |
| 78 | + {{- end -}} |
| 79 | + {{- end -}} |
| 80 | + {{- end -}} |
| 81 | + {{- end -}} |
| 82 | +{{- end -}} |
| 83 | + |
| 84 | +{{- if $foundExistingClusterWideOperator -}} |
| 85 | + {{- $uninstallCmd := printf "helm uninstall %s" $existingOperatorRelease -}} |
| 86 | + {{- if $existingOperatorNamespace -}} |
| 87 | + {{- $uninstallCmd = printf "helm uninstall %s -n %s" $existingOperatorRelease $existingOperatorNamespace -}} |
| 88 | + {{- end -}} |
| 89 | + |
| 90 | + {{- if .Values.namespaceRestriction.enabled -}} |
| 91 | + {{- if $existingOperatorNamespace -}} |
| 92 | + {{- fail (printf "VALIDATION ERROR: Found existing cluster-wide Dynamo operator from release '%s' in namespace '%s' (ClusterRole: %s). Cannot install namespace-restricted operator because the cluster-wide operator already manages resources in all namespaces, including the target namespace. This would create resource conflicts. Either:\n1. Use the existing cluster-wide operator, or\n2. Uninstall the existing cluster-wide operator first: %s" $existingOperatorRelease $existingOperatorNamespace $existingOperatorRoleName $uninstallCmd) -}} |
| 93 | + {{- else -}} |
| 94 | + {{- fail (printf "VALIDATION ERROR: Found existing cluster-wide Dynamo operator from release '%s' (ClusterRole: %s). Cannot install namespace-restricted operator because the cluster-wide operator already manages resources in all namespaces, including the target namespace. This would create resource conflicts. Either:\n1. Use the existing cluster-wide operator, or\n2. Uninstall the existing cluster-wide operator first: %s" $existingOperatorRelease $existingOperatorRoleName $uninstallCmd) -}} |
| 95 | + {{- end -}} |
| 96 | + {{- else -}} |
| 97 | + {{- if $existingOperatorNamespace -}} |
| 98 | + {{- fail (printf "VALIDATION ERROR: Found existing cluster-wide Dynamo operator from release '%s' in namespace '%s' (ClusterRole: %s). Only one cluster-wide Dynamo operator should be deployed per cluster. Either:\n1. Use the existing cluster-wide operator (no need to install another), or\n2. Uninstall the existing cluster-wide operator first: %s" $existingOperatorRelease $existingOperatorNamespace $existingOperatorRoleName $uninstallCmd) -}} |
| 99 | + {{- else -}} |
| 100 | + {{- fail (printf "VALIDATION ERROR: Found existing cluster-wide Dynamo operator from release '%s' (ClusterRole: %s). Only one cluster-wide Dynamo operator should be deployed per cluster. Either:\n1. Use the existing cluster-wide operator (no need to install another), or\n2. Uninstall the existing cluster-wide operator first: %s" $existingOperatorRelease $existingOperatorRoleName $uninstallCmd) -}} |
| 101 | + {{- end -}} |
| 102 | + {{- end -}} |
| 103 | +{{- end -}} |
| 104 | + |
| 105 | +{{/* Additional validation for cluster-wide mode */}} |
| 106 | +{{- if not .Values.namespaceRestriction.enabled -}} |
| 107 | + {{/* Warn if using different leader election IDs */}} |
| 108 | + {{- $leaderElectionId := default "dynamo.nvidia.com" .Values.controllerManager.leaderElection.id -}} |
| 109 | + {{- if ne $leaderElectionId "dynamo.nvidia.com" -}} |
| 110 | + {{- fail (printf "VALIDATION WARNING: Using custom leader election ID '%s' in cluster-wide mode. For proper coordination, all cluster-wide Dynamo operators should use the SAME leader election ID. Different IDs will allow multiple leaders simultaneously (split-brain scenario)." $leaderElectionId) -}} |
| 111 | + {{- end -}} |
| 112 | +{{- end -}} |
| 113 | +{{- end -}} |
| 114 | + |
| 115 | +{{/* |
| 116 | +Validation for configuration consistency |
| 117 | +*/}} |
| 118 | +{{- define "dynamo-operator.validateConfiguration" -}} |
| 119 | +{{/* Validate leader election namespace setting */}} |
| 120 | +{{- if and (not .Values.namespaceRestriction.enabled) .Values.controllerManager.leaderElection.namespace -}} |
| 121 | + {{- if eq .Values.controllerManager.leaderElection.namespace .Release.Namespace -}} |
| 122 | + {{- printf "\nWARNING: Leader election namespace is set to the same as release namespace (%s) in cluster-wide mode. This may prevent proper coordination between multiple releases. Consider using 'kube-system' or leaving empty for default.\n" .Release.Namespace | fail -}} |
| 123 | + {{- end -}} |
| 124 | +{{- end -}} |
| 125 | +{{- end -}} |
0 commit comments