Skip to content

Commit 8aaf884

Browse files
Merge pull request #30484 from rioliu-rh/fix-debugnode-guest-kubeconfig
NO-JIRA: Fix compat_otp.DebugNode() to support guest kubeconfig for HyperShift
2 parents b7d2a64 + 534f0e5 commit 8aaf884

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

test/extended/util/compat_otp/client.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,22 @@ func NewCLIForKubeOpenShift(basename string) *exutil.CLI {
3838
return exutil.NewCLI(basename)
3939
}
4040

41+
// determineExecCLI returns the appropriate CLI object based on guest kubeconfig availability.
42+
// If guest kubeconfig is set, returns CLI with guest config; otherwise returns CLI with admin config.
43+
// This ensures operations target the correct cluster (management vs. guest) automatically.
44+
func determineExecCLI(oc *exutil.CLI) *exutil.CLI {
45+
if oc.GetGuestKubeconf() != "" {
46+
return oc.AsGuestKubeconf()
47+
}
48+
return oc.AsAdmin()
49+
}
50+
4151
// IsNamespacePrivileged checks if a namespace has privileged SCC
4252
func IsNamespacePrivileged(oc *exutil.CLI, namespace string) (bool, error) {
4353
// Check for the Kubernetes Pod Security Admission 'enforce: privileged' label.
4454
// This is the direct confirmation that the namespace's admission controller
4555
// will allow an unrestricted pod (like the one created by 'oc debug node').
46-
stdout, err := oc.AsAdmin().Run("get").Args("ns", namespace, "-o", `jsonpath={.metadata.labels.pod-security\.kubernetes\.io/enforce}`).Output()
56+
stdout, err := determineExecCLI(oc).Run("get").Args("ns", namespace, "-o", `jsonpath={.metadata.labels.pod-security\.kubernetes\.io/enforce}`).Output()
4757

4858
if err != nil {
4959
return false, err
@@ -59,7 +69,7 @@ func IsNamespacePrivileged(oc *exutil.CLI, namespace string) (bool, error) {
5969

6070
// SetNamespacePrivileged sets a namespace to use privileged SCC
6171
func SetNamespacePrivileged(oc *exutil.CLI, namespace string) error {
62-
err := oc.AsAdmin().Run("label").Args("ns", namespace, "pod-security.kubernetes.io/enforce=privileged", "pod-security.kubernetes.io/audit=privileged", "pod-security.kubernetes.io/warn=privileged", "security.openshift.io/scc.podSecurityLabelSync=false", "--overwrite").Execute()
72+
err := determineExecCLI(oc).Run("label").Args("ns", namespace, "pod-security.kubernetes.io/enforce=privileged", "pod-security.kubernetes.io/audit=privileged", "pod-security.kubernetes.io/warn=privileged", "security.openshift.io/scc.podSecurityLabelSync=false", "--overwrite").Execute()
6373
if err != nil {
6474
return fmt.Errorf("failed to set namespace %s privileged: %v", namespace, err)
6575
}
@@ -68,7 +78,7 @@ func SetNamespacePrivileged(oc *exutil.CLI, namespace string) error {
6878

6979
// RecoverNamespaceRestricted recovers a namespace to restricted mode
7080
func RecoverNamespaceRestricted(oc *exutil.CLI, namespace string) error {
71-
err := oc.AsAdmin().Run("label").Args("ns", namespace, "pod-security.kubernetes.io/enforce-", "pod-security.kubernetes.io/audit-", "pod-security.kubernetes.io/warn-", "security.openshift.io/scc.podSecurityLabelSync-").Execute()
81+
err := determineExecCLI(oc).Run("label").Args("ns", namespace, "pod-security.kubernetes.io/enforce-", "pod-security.kubernetes.io/audit-", "pod-security.kubernetes.io/warn-", "security.openshift.io/scc.podSecurityLabelSync-").Execute()
7282
if err != nil {
7383
return fmt.Errorf("failed to recover namespace %s to restricted: %v", namespace, err)
7484
}

test/extended/util/compat_otp/nodes.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func debugNode(oc *exutil.CLI, nodeName string, cmdOptions []string, needChroot
174174
cargs = append(cargs, "--")
175175
}
176176
cargs = append(cargs, cmd...)
177-
return oc.AsAdmin().WithoutNamespace().Run("debug").Args(cargs...).Outputs()
177+
return determineExecCLI(oc).WithoutNamespace().Run("debug").Args(cargs...).Outputs()
178178
}
179179

180180
// DeleteLabelFromNode delete the custom label from the node
@@ -390,15 +390,15 @@ func GetNodeArchByName(oc *exutil.CLI, nodeName string) string {
390390

391391
// GetNodeListByLabel gets the node list by label
392392
func GetNodeListByLabel(oc *exutil.CLI, labelKey string) []string {
393-
output, err := oc.AsAdmin().WithoutNamespace().Run("get").Args("node", "-l", labelKey, "-o=jsonpath={.items[*].metadata.name}").Output()
393+
output, err := determineExecCLI(oc).WithoutNamespace().Run("get").Args("node", "-l", labelKey, "-o=jsonpath={.items[*].metadata.name}").Output()
394394
o.Expect(err).NotTo(o.HaveOccurred(), "Fail to get node with label %v, got error: %v\n", labelKey, err)
395395
nodeNameList := strings.Fields(output)
396396
return nodeNameList
397397
}
398398

399399
// IsDefaultNodeSelectorEnabled judges whether the test cluster enabled the defaultNodeSelector
400400
func IsDefaultNodeSelectorEnabled(oc *exutil.CLI) bool {
401-
defaultNodeSelector, getNodeSelectorErr := oc.AsAdmin().WithoutNamespace().Run("get").Args("scheduler", "cluster", "-o=jsonpath={.spec.defaultNodeSelector}").Output()
401+
defaultNodeSelector, getNodeSelectorErr := determineExecCLI(oc).WithoutNamespace().Run("get").Args("scheduler", "cluster", "-o=jsonpath={.spec.defaultNodeSelector}").Output()
402402
if getNodeSelectorErr != nil && strings.Contains(defaultNodeSelector, `the server doesn't have a resource type`) {
403403
e2e.Logf("WARNING: The scheduler API is not supported on the test cluster")
404404
return false

test/extended/util/compat_otp/resource_op.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func AddAnnotationsToSpecificResource(oc *exutil.CLI, resourceKindAndName, resou
5656
cargs = append(cargs, resourceKindAndName)
5757
cargs = append(cargs, annotations...)
5858
cargs = append(cargs, "--overwrite")
59-
return oc.AsAdmin().WithoutNamespace().Run("annotate").Args(cargs...).Output()
59+
return determineExecCLI(oc).WithoutNamespace().Run("annotate").Args(cargs...).Output()
6060
}
6161

6262
// RemoveAnnotationFromSpecificResource removes the specified annotation from the resource
@@ -67,7 +67,7 @@ func RemoveAnnotationFromSpecificResource(oc *exutil.CLI, resourceKindAndName, r
6767
}
6868
cargs = append(cargs, resourceKindAndName)
6969
cargs = append(cargs, annotationName+"-")
70-
return oc.AsAdmin().WithoutNamespace().Run("annotate").Args(cargs...).Output()
70+
return determineExecCLI(oc).WithoutNamespace().Run("annotate").Args(cargs...).Output()
7171
}
7272

7373
// GetAnnotationsFromSpecificResource gets the annotations from the specific resource
@@ -77,7 +77,7 @@ func GetAnnotationsFromSpecificResource(oc *exutil.CLI, resourceKindAndName, res
7777
cargs = append(cargs, "-n", resourceNamespace)
7878
}
7979
cargs = append(cargs, resourceKindAndName, "--list")
80-
annotationsStr, getAnnotationsErr := oc.AsAdmin().WithoutNamespace().Run("annotate").Args(cargs...).Output()
80+
annotationsStr, getAnnotationsErr := determineExecCLI(oc).WithoutNamespace().Run("annotate").Args(cargs...).Output()
8181
if getAnnotationsErr != nil {
8282
e2e.Logf(`Failed to get annotations from /%s in namespace %s: "%v"`, resourceKindAndName, resourceNamespace, getAnnotationsErr)
8383
}

0 commit comments

Comments
 (0)