|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2026 The Kubernetes Authors. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +set -o errexit |
| 18 | +set -o nounset |
| 19 | +set -o pipefail |
| 20 | + |
| 21 | +KOPS_ROOT=$(git rev-parse --show-toplevel) |
| 22 | +cd "${KOPS_ROOT}" |
| 23 | + |
| 24 | +errors=0 |
| 25 | + |
| 26 | +# Find all .go files, excluding vendor, .build, and the file where roles are defined. |
| 27 | +# We also exclude pkg/apis/kops/instancegroup.go |
| 28 | +files=$(find . -name "*.go" -not -path "./vendor/*" -not -path "./.build/*" -not -path "./pkg/apis/kops/instancegroup.go") |
| 29 | + |
| 30 | +# Regex to match == or != with InstanceGroupRole constants |
| 31 | +# We check for constants with or without package prefixes (e.g. kops., api., unversioned.) |
| 32 | +# We match both: |
| 33 | +# variable == constant |
| 34 | +# constant == variable |
| 35 | +# variable != constant |
| 36 | +# constant != variable |
| 37 | +REGEX='==[[:space:]]*([a-zA-Z0-9_]+\.)?InstanceGroupRole(ControlPlane|Node|Bastion|APIServer)\b|\b([a-zA-Z0-9_]+\.)?InstanceGroupRole(ControlPlane|Node|Bastion|APIServer)[[:space:]]*==|!=[[:space:]]*([a-zA-Z0-9_]+\.)?InstanceGroupRole(ControlPlane|Node|Bastion|APIServer)\b|\b([a-zA-Z0-9_]+\.)?InstanceGroupRole(ControlPlane|Node|Bastion|APIServer)[[:space:]]*!=' |
| 38 | + |
| 39 | +for file in $files; do |
| 40 | + if grep -E "${REGEX}" "${file}" > /dev/null; then |
| 41 | + echo "Verification failed in ${file}: direct comparison with InstanceGroupRole constant found:" |
| 42 | + grep -n -E "${REGEX}" "${file}" |
| 43 | + errors=$((errors + 1)) |
| 44 | + fi |
| 45 | +done |
| 46 | + |
| 47 | +if [ "${errors}" -ne 0 ]; then |
| 48 | + echo "Error: Found ${errors} files with direct InstanceGroupRole comparisons (== or !=). Use HasControlPlane(), HasNode(), HasBastion(), or HasAPIServer() instead." |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +echo "InstanceGroupRole comparison verification passed." |
| 53 | +exit 0 |
0 commit comments