Skip to content

Commit 9a328e0

Browse files
committed
fix(disruption): Using correct internal LB of apiserver for monitor test on ARO and Baremetal Hypershift
1 parent 82d1922 commit 9a328e0

File tree

2 files changed

+115
-22
lines changed

2 files changed

+115
-22
lines changed

pkg/monitortests/kubeapiserver/disruptioninclusterapiserver/monitortest.go

Lines changed: 77 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
91154
func (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
119182
func (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)

test/extended/util/managed_services.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package util
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
57

68
"github.com/sirupsen/logrus"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -95,3 +97,39 @@ func IsAroHCP(ctx context.Context, namespace string, kubeClient kubernetes.Inter
9597
logrus.Infof("No deployment found with control-plane-operator container in namespace %s", namespace)
9698
return false, nil
9799
}
100+
101+
// IsBareMetalHyperShiftCluster checks if the HyperShift cluster is running on bare metal
102+
// by checking the platform type of the hosted cluster. It uses kubectl commands to query
103+
// the hosted cluster's platform type and returns true if it's "None" or "Agent".
104+
func IsBareMetalHyperShiftCluster(ctx context.Context, managementOC *CLI) (bool, error) {
105+
// Get the hosted cluster namespace
106+
_, hcpNamespace, err := GetHypershiftManagementClusterConfigAndNamespace()
107+
if err != nil {
108+
return false, fmt.Errorf("failed to get hypershift management cluster config and namespace: %v", err)
109+
}
110+
111+
// Get the first hosted cluster name
112+
clusterNames, err := managementOC.AsAdmin().WithoutNamespace().Run("get").Args(
113+
"-n", hcpNamespace, "hostedclusters", "-o=jsonpath={.items[*].metadata.name}").Output()
114+
if err != nil {
115+
return false, fmt.Errorf("failed to get hosted cluster names: %v", err)
116+
}
117+
118+
if len(clusterNames) == 0 {
119+
return false, fmt.Errorf("no hosted clusters found")
120+
}
121+
122+
// Get the first hosted cluster name
123+
clusterName := strings.Split(strings.TrimSpace(clusterNames), " ")[0]
124+
125+
// Get the platform type of the hosted cluster
126+
platformType, err := managementOC.AsAdmin().WithoutNamespace().Run("get").Args(
127+
"hostedcluster", clusterName, "-n", hcpNamespace, `-ojsonpath={.spec.platform.type}`).Output()
128+
if err != nil {
129+
return false, fmt.Errorf("failed to get hosted cluster platform type: %v", err)
130+
}
131+
132+
// Check if it's bare metal (None or Agent platform)
133+
platformTypeStr := strings.TrimSpace(platformType)
134+
return platformTypeStr == "None" || platformTypeStr == "Agent", nil
135+
}

0 commit comments

Comments
 (0)