@@ -75,8 +75,9 @@ type InvariantInClusterDisruption struct {
7575 replicas int32
7676 controlPlaneNodes int32
7777
78- isHypershift bool
79- isAROHCPCluster bool
78+ isHypershift bool
79+ isAROHCPCluster bool
80+ isBareMetalHypershift bool
8081
8182 adminRESTConfig * rest.Config
8283 kubeClient kubernetes.Interface
@@ -88,6 +89,68 @@ func NewInvariantInClusterDisruption(info monitortestframework.MonitorTestInitia
8889 }
8990}
9091
92+ // parseAdminRESTConfigHost parses the adminRESTConfig.Host URL and returns hostname and port
93+ func (i * InvariantInClusterDisruption ) parseAdminRESTConfigHost () (hostname , port string , err error ) {
94+ parsedURL , err := url .Parse (i .adminRESTConfig .Host )
95+ if err != nil {
96+ return "" , "" , fmt .Errorf ("failed to parse adminRESTConfig.Host %q: %v" , i .adminRESTConfig .Host , err )
97+ }
98+
99+ hostname = parsedURL .Hostname ()
100+ if hostname == "" {
101+ return "" , "" , fmt .Errorf ("no hostname found in adminRESTConfig.Host %q" , i .adminRESTConfig .Host )
102+ }
103+
104+ port = parsedURL .Port ()
105+ if port == "" {
106+ port = "6443" // default port
107+ }
108+
109+ return hostname , port , nil
110+ }
111+
112+ // setKubernetesServiceEnvVars sets the KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT environment variables
113+ // based on the cluster type (ARO HCP, bare metal HyperShift, or standard)
114+ func (i * InvariantInClusterDisruption ) setKubernetesServiceEnvVars (envVars []corev1.EnvVar , apiIntHost , apiIntPort string ) []corev1.EnvVar {
115+ // Parse adminRESTConfig.Host once for bare metal HyperShift
116+ var bareMetalHost , bareMetalPort string
117+ var bareMetalErr error
118+ if i .isHypershift && i .isBareMetalHypershift {
119+ bareMetalHost , bareMetalPort , bareMetalErr = i .parseAdminRESTConfigHost ()
120+ if bareMetalErr != nil {
121+ logrus .WithError (bareMetalErr ).Errorf ("Failed to parse adminRESTConfig.Host for bare metal HyperShift" )
122+ }
123+ }
124+
125+ for j , env := range envVars {
126+ switch env .Name {
127+ case "KUBERNETES_SERVICE_HOST" :
128+ if i .isHypershift && i .isBareMetalHypershift {
129+ if bareMetalErr != nil {
130+ envVars [j ].Value = apiIntHost
131+ } else {
132+ envVars [j ].Value = bareMetalHost
133+ }
134+ } else {
135+ envVars [j ].Value = apiIntHost
136+ }
137+ case "KUBERNETES_SERVICE_PORT" :
138+ if i .isHypershift && i .isAROHCPCluster {
139+ envVars [j ].Value = "7443"
140+ } else if i .isHypershift && i .isBareMetalHypershift {
141+ if bareMetalErr != nil {
142+ envVars [j ].Value = apiIntPort
143+ } else {
144+ envVars [j ].Value = bareMetalPort
145+ }
146+ } else {
147+ envVars [j ].Value = apiIntPort
148+ }
149+ }
150+ }
151+ return envVars
152+ }
153+
91154func (i * InvariantInClusterDisruption ) createDeploymentAndWaitToRollout (ctx context.Context , deploymentObj * appsv1.Deployment ) error {
92155 deploymentID := uuid .New ().String ()
93156 deploymentObj = disruptionlibrary .UpdateDeploymentENVs (deploymentObj , deploymentID , "" )
@@ -119,23 +182,14 @@ func (i *InvariantInClusterDisruption) createDeploymentAndWaitToRollout(ctx cont
119182func (i * InvariantInClusterDisruption ) createInternalLBDeployment (ctx context.Context , apiIntHost , apiIntPort string ) error {
120183 deploymentObj := resourceread .ReadDeploymentV1OrDie (internalLBDeploymentYaml )
121184 deploymentObj .SetNamespace (i .namespaceName )
122- deploymentObj .Spec .Template .Spec .Containers [0 ].Env [0 ].Value = apiIntHost
123185 // set amount of deployment replicas to make sure it runs on all nodes
124186 deploymentObj .Spec .Replicas = & i .replicas
125187 // we need to use the openshift-tests image of the destination during an upgrade.
126188 deploymentObj .Spec .Template .Spec .Containers [0 ].Image = i .openshiftTestsImagePullSpec
127189
128- // Set the correct port for internal API server
129- for j , env := range deploymentObj .Spec .Template .Spec .Containers [0 ].Env {
130- if env .Name == "KUBERNETES_SERVICE_PORT" {
131- if i .isHypershift && i .isAROHCPCluster {
132- deploymentObj .Spec .Template .Spec .Containers [0 ].Env [j ].Value = "7443"
133- } else {
134- deploymentObj .Spec .Template .Spec .Containers [0 ].Env [j ].Value = apiIntPort
135- }
136- break
137- }
138- }
190+ // Set the correct host and port for internal API server based on cluster type
191+ deploymentObj .Spec .Template .Spec .Containers [0 ].Env = i .setKubernetesServiceEnvVars (
192+ deploymentObj .Spec .Template .Spec .Containers [0 ].Env , apiIntHost , apiIntPort )
139193
140194 err := i .createDeploymentAndWaitToRollout (ctx , deploymentObj )
141195 if err != nil {
@@ -335,6 +389,13 @@ func (i *InvariantInClusterDisruption) StartCollection(ctx context.Context, admi
335389 logrus .WithError (err ).Warning ("Failed to check if ARO HCP, assuming it's not" )
336390 i .isAROHCPCluster = false // Assume not ARO HCP on error
337391 }
392+
393+ // Check if this is a bare metal HyperShift cluster
394+ i .isBareMetalHypershift , err = exutil .IsBareMetalHyperShiftCluster (ctx , managementOC )
395+ if err != nil {
396+ logrus .WithError (err ).Warning ("Failed to check if bare metal HyperShift, assuming it's not" )
397+ i .isBareMetalHypershift = false // Assume not bare metal HyperShift on error
398+ }
338399 }
339400
340401 if len (i .payloadImagePullSpec ) == 0 {
@@ -392,15 +453,9 @@ func (i *InvariantInClusterDisruption) StartCollection(ctx context.Context, admi
392453 var apiIntHost string
393454 var apiIntPort string
394455 if i .isHypershift {
395- parsedURL , err := url . Parse ( i . adminRESTConfig . Host )
456+ apiIntHost , apiIntPort , err = i . parseAdminRESTConfigHost ( )
396457 if err != nil {
397- return fmt .Errorf ("failed to parse adminRESTConfig.Host %q: %v" , i .adminRESTConfig .Host , err )
398- }
399- apiIntHost = parsedURL .Hostname ()
400- if parsedURL .Port () != "" {
401- apiIntPort = parsedURL .Port ()
402- } else {
403- apiIntPort = "6443" // default port
458+ return fmt .Errorf ("failed to parse adminRESTConfig.Host: %v" , err )
404459 }
405460 } else {
406461 internalAPI , err := url .Parse (infra .Status .APIServerInternalURL )
0 commit comments