Skip to content

Commit db5e78d

Browse files
committed
fix(disruption): Using correct internal LB of apiserver for monitor test on ARO and Baremetal Hypershift
1 parent 281bcd1 commit db5e78d

File tree

2 files changed

+148
-6
lines changed

2 files changed

+148
-6
lines changed

pkg/monitortests/kubeapiserver/disruptioninclusterapiserver/monitortest.go

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
utilerrors "k8s.io/apimachinery/pkg/util/errors"
1616

1717
"github.com/openshift/origin/pkg/monitortestlibrary/disruptionlibrary"
18+
"github.com/openshift/origin/pkg/test/extensions"
1819
"k8s.io/apimachinery/pkg/labels"
1920
"k8s.io/apimachinery/pkg/selection"
2021

@@ -76,6 +77,9 @@ type InvariantInClusterDisruption struct {
7677
replicas int32
7778
controlPlaneNodes int32
7879

80+
isHypershift bool
81+
isAROHCPCluster bool
82+
isBareMetalHypershift bool
7983
adminRESTConfig *rest.Config
8084
kubeClient kubernetes.Interface
8185
}
@@ -86,6 +90,68 @@ func NewInvariantInClusterDisruption(info monitortestframework.MonitorTestInitia
8690
}
8791
}
8892

93+
// parseAdminRESTConfigHost parses the adminRESTConfig.Host URL and returns hostname and port
94+
func (i *InvariantInClusterDisruption) parseAdminRESTConfigHost() (hostname, port string, err error) {
95+
parsedURL, err := url.Parse(i.adminRESTConfig.Host)
96+
if err != nil {
97+
return "", "", fmt.Errorf("failed to parse adminRESTConfig.Host %q: %v", i.adminRESTConfig.Host, err)
98+
}
99+
100+
hostname = parsedURL.Hostname()
101+
if hostname == "" {
102+
return "", "", fmt.Errorf("no hostname found in adminRESTConfig.Host %q", i.adminRESTConfig.Host)
103+
}
104+
105+
port = parsedURL.Port()
106+
if port == "" {
107+
port = "6443" // default port
108+
}
109+
110+
return hostname, port, nil
111+
}
112+
113+
// setKubernetesServiceEnvVars sets the KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT environment variables
114+
// based on the cluster type (ARO HCP, bare metal HyperShift, or standard)
115+
func (i *InvariantInClusterDisruption) setKubernetesServiceEnvVars(envVars []corev1.EnvVar, apiIntHost, apiIntPort string) []corev1.EnvVar {
116+
// Parse adminRESTConfig.Host once for bare metal HyperShift
117+
var bareMetalHost, bareMetalPort string
118+
var bareMetalErr error
119+
if i.isHypershift && i.isBareMetalHypershift {
120+
bareMetalHost, bareMetalPort, bareMetalErr = i.parseAdminRESTConfigHost()
121+
if bareMetalErr != nil {
122+
logrus.WithError(bareMetalErr).Errorf("Failed to parse adminRESTConfig.Host for bare metal HyperShift")
123+
}
124+
}
125+
126+
for j, env := range envVars {
127+
switch env.Name {
128+
case "KUBERNETES_SERVICE_HOST":
129+
if i.isHypershift && i.isBareMetalHypershift {
130+
if bareMetalErr != nil {
131+
envVars[j].Value = apiIntHost
132+
} else {
133+
envVars[j].Value = bareMetalHost
134+
}
135+
} else {
136+
envVars[j].Value = apiIntHost
137+
}
138+
case "KUBERNETES_SERVICE_PORT":
139+
if i.isHypershift && i.isAROHCPCluster {
140+
envVars[j].Value = "7443"
141+
} else if i.isHypershift && i.isBareMetalHypershift {
142+
if bareMetalErr != nil {
143+
envVars[j].Value = apiIntPort
144+
} else {
145+
envVars[j].Value = bareMetalPort
146+
}
147+
} else {
148+
envVars[j].Value = apiIntPort
149+
}
150+
}
151+
}
152+
return envVars
153+
}
154+
89155
func (i *InvariantInClusterDisruption) createDeploymentAndWaitToRollout(ctx context.Context, deploymentObj *appsv1.Deployment) error {
90156
deploymentID := uuid.New().String()
91157
deploymentObj = disruptionlibrary.UpdateDeploymentENVs(deploymentObj, deploymentID, "")
@@ -117,12 +183,14 @@ func (i *InvariantInClusterDisruption) createDeploymentAndWaitToRollout(ctx cont
117183
func (i *InvariantInClusterDisruption) createInternalLBDeployment(ctx context.Context, apiIntHost string) error {
118184
deploymentObj := resourceread.ReadDeploymentV1OrDie(internalLBDeploymentYaml)
119185
deploymentObj.SetNamespace(i.namespaceName)
120-
deploymentObj.Spec.Template.Spec.Containers[0].Env[0].Value = apiIntHost
121186
// set amount of deployment replicas to make sure it runs on all nodes
122187
deploymentObj.Spec.Replicas = &i.replicas
123188
// we need to use the openshift-tests image of the destination during an upgrade.
124189
deploymentObj.Spec.Template.Spec.Containers[0].Image = i.openshiftTestsImagePullSpec
125190

191+
// Set the correct host and port for internal API server based on cluster type
192+
deploymentObj.Spec.Template.Spec.Containers[0].Env = i.setKubernetesServiceEnvVars(
193+
deploymentObj.Spec.Template.Spec.Containers[0].Env, apiIntHost, apiIntPort)
126194
err := i.createDeploymentAndWaitToRollout(ctx, deploymentObj)
127195
if err != nil {
128196
return err
@@ -332,8 +400,30 @@ func (i *InvariantInClusterDisruption) StartCollection(ctx context.Context, admi
332400
managementOC := exutil.NewHypershiftManagementCLI(hcpNamespace)
333401
if isAROHCPcluster, err = exutil.IsAroHCP(ctx, hcpNamespace, managementOC.AdminKubeClient()); err != nil {
334402
logrus.WithError(err).Warning("Failed to check if ARO HCP, assuming it's not")
403+
i.isAROHCPCluster = false // Assume not ARO HCP on error
335404
} else if isAROHCPcluster {
336-
i.notSupportedReason = "platform Hypershift - ARO HCP not supported"
405+
i.isAROHCPCluster = true
406+
} else {
407+
i.isAROHCPCluster = false
408+
}
409+
410+
// Check if this is a bare metal HyperShift cluster
411+
i.isBareMetalHypershift, err = exutil.IsBareMetalHyperShiftCluster(ctx, managementOC)
412+
if err != nil {
413+
logrus.WithError(err).Warning("Failed to check if bare metal HyperShift, assuming it's not")
414+
i.isBareMetalHypershift = false // Assume not bare metal HyperShift on error
415+
}
416+
}
417+
418+
if len(i.payloadImagePullSpec) == 0 {
419+
i.payloadImagePullSpec, err = extensions.DetermineReleasePayloadImage()
420+
if err != nil {
421+
return err
422+
}
423+
424+
if len(i.payloadImagePullSpec) == 0 {
425+
log.Info("unable to determine payloadImagePullSpec")
426+
i.notSupportedReason = "no image pull spec specified."
337427
return nil
338428
}
339429
}
@@ -379,11 +469,25 @@ func (i *InvariantInClusterDisruption) StartCollection(ctx context.Context, admi
379469
return fmt.Errorf("error getting openshift infrastructure: %v", err)
380470
}
381471

382-
internalAPI, err := url.Parse(infra.Status.APIServerInternalURL)
383-
if err != nil {
384-
return fmt.Errorf("error parsing api int url: %v", err)
472+
var apiIntHost string
473+
var apiIntPort string
474+
if i.isHypershift {
475+
apiIntHost, apiIntPort, err = i.parseAdminRESTConfigHost()
476+
if err != nil {
477+
return fmt.Errorf("failed to parse adminRESTConfig.Host: %v", err)
478+
}
479+
} else {
480+
internalAPI, err := url.Parse(infra.Status.APIServerInternalURL)
481+
if err != nil {
482+
return fmt.Errorf("error parsing api int url: %v", err)
483+
}
484+
apiIntHost = internalAPI.Hostname()
485+
if internalAPI.Port() != "" {
486+
apiIntPort = internalAPI.Port()
487+
} else {
488+
apiIntPort = "6443" // default port
489+
}
385490
}
386-
apiIntHost := internalAPI.Hostname()
387491

388492
allNodes, err := i.kubeClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
389493
if err != nil {

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)