Repository: quarkusio/quarkus
Issue: OpenShift binary build targeting to another namespace failing

### Describe the bug

We have Jenkins running in OpenShift project/namespace (jenkins-namespace) and we have also another namespace(s) where Quarkus applications are deployed. Jenkins is running with Service Account "jenkins" which has access to another namespaces but implicitly "jenkins-namespace" is selected for it. When I try:
`mvn clean package -Dquarkus.openshift.namespace=target-namespace-name -Dquarkus.kubernetes.deploy=true`
then correct namespace is set in openshift.yaml, but OpenshiftProcessor is then trying to start build in user's current namespace (jenkins-namespace):

`[INFO] [io.quarkus.container.image.openshift.deployment.OpenshiftProcessor] Starting (in-cluster) container image build for jar using: BINARY on server: https://serverUrl/ in namespace:jenkins-namespace.`

This behaviour could be workarounded by calling `oc project target-namespace-name` beforehand.

### Expected behavior

Application is deployed in namespace which is defined by `quarkus.openshift.namespace` property

### Actual behavior

Build failure

### How to Reproduce?

``` bash
oc project jenkins-namespace
mvn clean package -Dquarkus.openshift.namespace=target-namespace-name -Dquarkus.kubernetes.deploy=true
```

### Output of `uname -a` or `ver`

_No response_

### Output of `java -version`

_No response_

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

_No response_

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

_No response_

### Additional information

_No response_

Changed files:
extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftUtils.java
extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftProcessor.java

diff --git a/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftProcessor.java b/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftProcessor.java
index 3b735804542..f6d78bd7e16 100644
--- a/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftProcessor.java
+++ b/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftProcessor.java
@@ -1,5 +1,6 @@
 package io.quarkus.container.image.openshift.deployment;
 
+import static io.quarkus.container.image.openshift.deployment.OpenshiftUtils.getNamespace;
 import static io.quarkus.container.image.openshift.deployment.OpenshiftUtils.mergeConfig;
 import static io.quarkus.container.util.PathsUtil.findMainSourcesRoot;
 import static io.quarkus.deployment.pkg.steps.JarResultBuildStep.DEFAULT_FAST_JAR_DIRECTORY_NAME;
@@ -264,7 +265,7 @@ public void openshiftBuildFromJar(OpenshiftConfig openshiftConfig,
             return;
         }
 
-        try (KubernetesClient kubernetesClient = kubernetesClientBuilder.buildClient()) {
+        try (KubernetesClient kubernetesClient = buildClient(kubernetesClientBuilder)) {
             String namespace = Optional.ofNullable(kubernetesClient.getNamespace()).orElse("default");
             LOG.info("Starting (in-cluster) container image build for jar using: " + config.buildStrategy + " on server: "
                     + kubernetesClient.getMasterUrl() + " in namespace:" + namespace + ".");
@@ -324,7 +325,7 @@ public void openshiftBuildFromNative(OpenshiftConfig openshiftConfig, S2iConfig
             return;
         }
 
-        try (KubernetesClient kubernetesClient = kubernetesClientBuilder.buildClient()) {
+        try (KubernetesClient kubernetesClient = buildClient(kubernetesClientBuilder)) {
             String namespace = Optional.ofNullable(kubernetesClient.getNamespace()).orElse("default");
 
             LOG.info("Starting (in-cluster) container image build for jar using: " + config.buildStrategy + " on server: "
@@ -543,6 +544,11 @@ private static KubernetesClientBuilder newClientBuilderWithoutHttp2(Config confi
         return new KubernetesClientBuilder().withConfig(configuration).withHttpClientFactory(httpClientFactory);
     }
 
+    private static KubernetesClient buildClient(KubernetesClientBuildItem kubernetesClientBuilder) {
+        getNamespace().ifPresent(kubernetesClientBuilder.getConfig()::setNamespace);
+        return kubernetesClientBuilder.buildClient();
+    }
+
     // visible for test
     static String concatUnixPaths(String... elements) {
         StringBuilder result = new StringBuilder();
diff --git a/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftUtils.java b/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftUtils.java
index 1a3a6c04eac..461c63097c1 100644
--- a/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftUtils.java
+++ b/extensions/container-image/container-image-openshift/deployment/src/main/java/io/quarkus/container/image/openshift/deployment/OpenshiftUtils.java
@@ -28,6 +28,9 @@
  */
 public class OpenshiftUtils {
 
+    private static final String OPENSHIFT_NAMESPACE = "quarkus.openshift.namespace";
+    private static final String KUBERNETES_NAMESPACE = "quarkus.kubernetes.namespace";
+
     /**
      * Wait for the references ImageStreamTags to become available.
      *
@@ -142,4 +145,12 @@ public static OpenshiftConfig mergeConfig(OpenshiftConfig openshiftConfig, S2iCo
 
         return result;
     }
+
+    /**
+     * @return the openshift namespace set in the OpenShift extension.
+     */
+    public static Optional<String> getNamespace() {
+        return ConfigProvider.getConfig().getOptionalValue(OPENSHIFT_NAMESPACE, String.class)
+                .or(() -> ConfigProvider.getConfig().getOptionalValue(KUBERNETES_NAMESPACE, String.class));
+    }
 }