@@ -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
4252func 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
6171func 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
7080func 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 }
0 commit comments