Skip to content

Commit e8881ac

Browse files
mgencurclaude
andcommitted
Refactor HCP test client initialization to use dynamic kubeconfig retrieval
- Remove HC_KUBECONFIG flag and related global variables from test suite - Remove hardcoded crClientForHC global client initialization - Add GetHostedClusterKubeconfig() method to dynamically retrieve kubeconfig from HostedCluster status - Update pre/post backup verification to create client on-demand using retrieved kubeconfig - Clean up Makefile to remove HC_KUBECONFIG parameter handling - Simplify HCHandler by removing ClientGuest field This change improves test reliability by ensuring the guest cluster client is always created with the current kubeconfig rather than relying on potentially stale configuration passed via flags. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 7eec2f2 commit e8881ac

File tree

7 files changed

+41
-32
lines changed

7 files changed

+41
-32
lines changed

Makefile

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ ENVTEST_K8S_VERSION = 1.32 # Kubernetes version from OpenShift 4.19.x https://op
6767
# HC_NAME is the name of the HostedCluster to use for HCP tests when
6868
# hc_backup_restore_mode is set to external. Otherwise, HC_NAME is ignored.
6969
HC_NAME ?= ""
70-
# HC_KUBECONFIG is the path to the kubeconfig file for the HostedCluster
71-
# to use for HCP tests when hc_backup_restore_mode is set to external.
72-
# Otherwise, HC_KUBECONFIG is ignored.
73-
HC_KUBECONFIG ?= ""
7470

7571
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
7672
ifeq (,$(shell go env GOBIN))
@@ -764,7 +760,7 @@ else
764760
endif
765761
ifeq ($(TEST_HCP_EXTERNAL),true)
766762
TEST_FILTER += && (hcp_external)
767-
HCP_EXTERNAL_ARGS = -hc_backup_restore_mode=external -hc_name=$(HC_NAME) -hc_kubeconfig=$(HC_KUBECONFIG)
763+
HCP_EXTERNAL_ARGS = -hc_backup_restore_mode=external -hc_name=$(HC_NAME)
768764
else
769765
TEST_FILTER += && (! hcp_external)
770766
endif

docs/developer/testing/TESTING.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ Set common env variables as mentioned above, then run:
107107
```bash
108108
TEST_HCP_EXTERNAL=true \
109109
HC_NAME=hc1 \
110-
HC_KUBECONFIG=/path/to/kubeconfig/for/hosted/cluster \
111110
make test-e2e
112111
```
113112

tests/e2e/e2e_suite_test.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"k8s.io/client-go/dynamic"
1515
"k8s.io/client-go/kubernetes"
1616
"k8s.io/client-go/rest"
17-
"k8s.io/client-go/tools/clientcmd"
1817
ctrl "sigs.k8s.io/controller-runtime"
1918
"sigs.k8s.io/controller-runtime/pkg/client"
2019
"sigs.k8s.io/controller-runtime/pkg/client/config"
@@ -25,11 +24,10 @@ import (
2524

2625
var (
2726
// Common vars obtained from flags passed in ginkgo.
28-
bslCredFile, namespace, instanceName, provider, vslCredFile, settings, artifact_dir, hcKubeconfig string
29-
flakeAttempts int64
27+
bslCredFile, namespace, instanceName, provider, vslCredFile, settings, artifact_dir string
28+
flakeAttempts int64
3029

3130
kubernetesClientForSuiteRun *kubernetes.Clientset
32-
crClientForHC client.Client
3331
runTimeClientForSuiteRun client.Client
3432
dynamicClientForSuiteRun dynamic.Interface
3533

@@ -39,7 +37,6 @@ var (
3937
vslSecretName string
4038

4139
kubeConfig *rest.Config
42-
kubeConfigForHC *rest.Config
4340
knownFlake bool
4441
accumulatedTestLogs []string
4542

@@ -66,7 +63,6 @@ func init() {
6663
flag.BoolVar(&skipMustGather, "skipMustGather", false, "avoid errors with local execution and cluster architecture")
6764
flag.StringVar(&hcBackupRestoreMode, "hc_backup_restore_mode", string(HCModeCreate), "Type of HC test to run")
6865
flag.StringVar(&hcName, "hc_name", "", "Name of the HostedCluster to use for HCP tests")
69-
flag.StringVar(&hcKubeconfig, "hc_kubeconfig", "", "Path to kubeconfig file for HostedCluster")
7066

7167
// helps with launching debug sessions from IDE
7268
if os.Getenv("E2E_USE_ENV_FLAGS") == "true" {
@@ -131,11 +127,7 @@ func init() {
131127
if os.Getenv("HC_NAME") != "" {
132128
hcName = os.Getenv("HC_NAME")
133129
}
134-
if os.Getenv("HC_KUBECONFIG") != "" {
135-
hcKubeconfig = os.Getenv("HC_KUBECONFIG")
136-
}
137130
}
138-
139131
}
140132

141133
func TestOADPE2E(t *testing.T) {
@@ -152,18 +144,6 @@ func TestOADPE2E(t *testing.T) {
152144
kubernetesClientForSuiteRun, err = kubernetes.NewForConfig(kubeConfig)
153145
gomega.Expect(err).NotTo(gomega.HaveOccurred())
154146

155-
// Set up kubeConfigForHC if kubeconfig_hc flag is provided
156-
if hcKubeconfig != "" {
157-
kubeConfigForHC, err = clientcmd.BuildConfigFromFlags("", hcKubeconfig)
158-
gomega.Expect(err).NotTo(gomega.HaveOccurred())
159-
160-
kubeConfigForHC.QPS = kubeConfig.QPS
161-
kubeConfigForHC.Burst = kubeConfig.Burst
162-
163-
crClientForHC, err = client.New(kubeConfigForHC, client.Options{Scheme: lib.Scheme})
164-
gomega.Expect(err).NotTo(gomega.HaveOccurred())
165-
}
166-
167147
runTimeClientForSuiteRun, err = client.New(kubeConfig, client.Options{Scheme: lib.Scheme})
168148
gomega.Expect(err).NotTo(gomega.HaveOccurred())
169149

tests/e2e/hcp_backup_restore_suite_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ func runHCPBackupAndRestore(
6969
// Pre-backup verification for guest cluster
7070
if brCase.PreBackupVerifyGuest != nil {
7171
log.Printf("Validating guest cluster pre-backup")
72-
err := brCase.PreBackupVerifyGuest(crClientForHC, "" /*unused*/)
72+
hcKubeconfig, err := h.GetHostedClusterKubeconfig(h.HostedCluster)
73+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
74+
crClientForHC, err := client.New(hcKubeconfig, client.Options{Scheme: lib.Scheme})
75+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
76+
err = brCase.PreBackupVerifyGuest(crClientForHC, "" /*unused*/)
7377
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to run pre-backup verification for guest cluster: %v", err)
7478
}
7579
}
@@ -102,7 +106,11 @@ func runHCPBackupAndRestore(
102106
// Post-restore verification for guest cluster
103107
if brCase.PostRestoreVerifyGuest != nil {
104108
log.Printf("Validating guest cluster post-restore")
105-
err := brCase.PostRestoreVerifyGuest(crClientForHC, "" /*unused*/)
109+
hcKubeconfig, err := h.GetHostedClusterKubeconfig(h.HostedCluster)
110+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
111+
crClientForHC, err := client.New(hcKubeconfig, client.Options{Scheme: lib.Scheme})
112+
gomega.Expect(err).ToNot(gomega.HaveOccurred())
113+
err = brCase.PostRestoreVerifyGuest(crClientForHC, "" /*unused*/)
106114
gomega.Expect(err).ToNot(gomega.HaveOccurred(), "failed to run post-restore verification for guest cluster: %v", err)
107115
}
108116
}

tests/e2e/hcp_external_cluster_backup_restore_suite_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ var _ = ginkgo.Describe("HCP external cluster Backup and Restore tests", ginkgo.
3939
h = &libhcp.HCHandler{
4040
Ctx: context.Background(),
4141
Client: runTimeClientForSuiteRun,
42-
ClientGuest: crClientForHC,
4342
HCOCPTestImage: libhcp.HCOCPTestImage,
4443
}
4544
})

tests/e2e/lib/hcp/hcp.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"k8s.io/apimachinery/pkg/runtime/schema"
1717
"k8s.io/apimachinery/pkg/types"
1818
"k8s.io/apimachinery/pkg/util/wait"
19+
"k8s.io/client-go/rest"
20+
"k8s.io/client-go/tools/clientcmd"
1921
"sigs.k8s.io/controller-runtime/pkg/client"
2022
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2123

@@ -685,3 +687,29 @@ func RestartHCPPods(HCPNamespace string, c client.Client) error {
685687
}
686688
return nil
687689
}
690+
691+
func buildConfigFromBytes(kubeconfigData []byte) (*rest.Config, error) {
692+
clientConfig, err := clientcmd.NewClientConfigFromBytes(kubeconfigData)
693+
if err != nil {
694+
return nil, err
695+
}
696+
config, err := clientConfig.ClientConfig()
697+
if err != nil {
698+
return nil, err
699+
}
700+
return config, nil
701+
}
702+
703+
func (h *HCHandler) GetHostedClusterKubeconfig(hc *hypershiftv1.HostedCluster) (*rest.Config, error) {
704+
kubeconfigSecret := &corev1.Secret{}
705+
err := h.Client.Get(h.Ctx,
706+
types.NamespacedName{
707+
Namespace: hc.Namespace,
708+
Name: hc.Status.KubeConfig.Name},
709+
kubeconfigSecret)
710+
if err != nil {
711+
return nil, err
712+
}
713+
kubeconfigData := kubeconfigSecret.Data["kubeconfig"]
714+
return buildConfigFromBytes(kubeconfigData)
715+
}

tests/e2e/lib/hcp/types.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ var (
145145
type HCHandler struct {
146146
Ctx context.Context
147147
Client client.Client
148-
ClientGuest client.Client
149148
HCOCPTestImage string
150149
HCPNamespace string
151150
HostedCluster *hypershiftv1.HostedCluster

0 commit comments

Comments
 (0)