|
1 | 1 | package util |
2 | 2 |
|
3 | | -import "k8s.io/apimachinery/pkg/util/sets" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 8 | + "k8s.io/apimachinery/pkg/util/sets" |
| 9 | + "k8s.io/client-go/kubernetes" |
| 10 | +) |
4 | 11 |
|
5 | 12 | // ManagedServiceNamespaces is the set of namespaces used by managed service platforms |
6 | 13 | // like ROSA, ARO, etc. These are typically exempt from the requirements we impose on |
@@ -51,3 +58,40 @@ var ManagedServiceNamespaces = sets.New[string]( |
51 | 58 | "openshift-validation-webhook", |
52 | 59 | "openshift-velero", |
53 | 60 | ) |
| 61 | + |
| 62 | +// IsAroHCP checks if the HyperShift operator deployment has MANAGED_SERVICE=ARO-HCP environment variable. |
| 63 | +func IsAroHCP(ctx context.Context, namespace string, kubeClient kubernetes.Interface) (bool, error) { |
| 64 | + // List deployments with the correct label that actually exists on the deployment |
| 65 | + deployments, err := kubeClient.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{ |
| 66 | + LabelSelector: "hypershift.openshift.io/managed-by=control-plane-operator", |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + logrus.Infof("Failed to list deployments in namespace %s: %v", namespace, err) |
| 70 | + return false, nil // Not an error if we can't list deployments, just means it's not ARO HCP |
| 71 | + } |
| 72 | + |
| 73 | + if len(deployments.Items) == 0 { |
| 74 | + logrus.Infof("No control-plane-operator deployments found in namespace %s", namespace) |
| 75 | + return false, nil |
| 76 | + } |
| 77 | + |
| 78 | + logrus.Infof("Found %d control-plane-operator deployments in namespace %s", len(deployments.Items), namespace) |
| 79 | + |
| 80 | + // Look through all matching deployments |
| 81 | + for _, deployment := range deployments.Items { |
| 82 | + |
| 83 | + // Look for the control-plane-operator container directly in the deployment spec |
| 84 | + for _, container := range deployment.Spec.Template.Spec.Containers { |
| 85 | + if container.Name == "control-plane-operator" { |
| 86 | + logrus.Infof("Found container 'control-plane-operator' in deployment %s", deployment.Name) |
| 87 | + |
| 88 | + result := HasEnvVar(&container, "MANAGED_SERVICE", "ARO-HCP") |
| 89 | + logrus.Infof("hasEnvVar result for MANAGED_SERVICE=ARO-HCP: %v", result) |
| 90 | + return result, nil |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + logrus.Infof("No deployment found with control-plane-operator container in namespace %s", namespace) |
| 96 | + return false, nil |
| 97 | +} |
0 commit comments