Skip to content

Commit 8e4cbf4

Browse files
Move K8s cloud setup to init script only; remove ensureK8sCloud from Matrix
- Remove ensureK8sCloud() and its call sites from Matrix.groovy so the pipeline never modifies Jenkins cloud config (bad on shared Jenkins). - Enhance jenkins-configure-k8s-and-labels.groovy: support JENKINS_K8S_NAMESPACE, set serverUrl only when JENKINS_K8S_API_URL is provided (GHA passes it; avoid default on shared Jenkins). - Pass JENKINS_K8S_NAMESPACE from local_gha_ci.sh into Jenkins container. - Document in USERGUIDE that Kubernetes clouds must be pre-configured. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 3209ddd commit 8e4cbf4

File tree

4 files changed

+11
-61
lines changed

4 files changed

+11
-61
lines changed

.github/scripts/jenkins-configure-k8s-and-labels.groovy

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// - JENKINS_AGENT_EXECUTORS (integer)
55
// - JENKINS_K8S_CLOUDS (comma-separated cloud names)
66
// - JENKINS_K8S_API_URL (Kubernetes API URL)
7+
// - JENKINS_K8S_NAMESPACE (optional; default "default")
78
// - JENKINS_K8S_TOKEN (service account bearer token)
89

910
import jenkins.model.Jenkins
@@ -18,7 +19,9 @@ def labelTokens = (env.get("JENKINS_AGENT_LABELS") ?: "")
1819
.split(",")
1920
.collect { it.trim() }
2021
.findAll { it }
21-
def apiUrl = env.get("JENKINS_K8S_API_URL") ?: "https://kind-control-plane:6443"
22+
def apiUrlRaw = (env.get("JENKINS_K8S_API_URL") ?: "").trim()
23+
def apiUrl = apiUrlRaw ?: "https://kind-control-plane:6443"
24+
def namespace = (env.get("JENKINS_K8S_NAMESPACE") ?: "default").trim() ?: "default"
2225
def k8sToken = env.get("JENKINS_K8S_TOKEN") ?: ""
2326
def jenkinsUrl = env.get("JENKINS_K8S_JENKINS_URL") ?: "http://jenkins:8080"
2427
def jenkinsTunnel = env.get("JENKINS_K8S_JENKINS_TUNNEL") ?: "jenkins:50000"
@@ -105,9 +108,11 @@ cloudNames.each { name ->
105108
cloud = new KubernetesCloud(name)
106109
j.clouds.add(cloud)
107110
}
108-
cloud.serverUrl = apiUrl
111+
if (apiUrlRaw) {
112+
cloud.serverUrl = apiUrl
113+
}
109114
cloud.skipTlsVerify = true
110-
cloud.namespace = "default"
115+
cloud.namespace = namespace
111116
cloud.jenkinsUrl = jenkinsUrl
112117
cloud.jenkinsTunnel = jenkinsTunnel
113118
if (tokenCredReady) {

USERGUIDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ Source of truth: `schema_validator/ci_demo_schema.yaml`.
5858

5959
### `kubernetes_conf`
6060

61+
When using Kubernetes, the Jenkins server must have the Kubernetes cloud(s) referenced by `cloud` pre-configured (e.g. via init scripts at startup or by administrators). The pipeline does not create or update Jenkins cloud configuration.
62+
6163
| Key | Type |
6264
|---|---|
6365
| `cloud` | `str` |

scripts/local_gha_ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ docker run -d --name "${JENKINS_NAME}" \
249249
-e JENKINS_K8S_TOKEN="${K8S_TOKEN}" \
250250
-e JENKINS_K8S_JENKINS_URL="http://${JENKINS_K8S_DNS_NAME}:8080" \
251251
-e JENKINS_K8S_JENKINS_TUNNEL="${JENKINS_K8S_DNS_NAME}:50000" \
252+
-e JENKINS_K8S_NAMESPACE="${JENKINS_K8S_NAMESPACE:-default}" \
252253
-e JAVA_OPTS="-Djenkins.install.runSetupWizard=false -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true -Dpermissive-script-security.enabled=no_security -Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true" \
253254
"${JENKINS_IMAGE}" \
254255
/usr/local/bin/jenkins.sh >/dev/null

src/com/mellanox/cicd/Matrix.groovy

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -853,62 +853,6 @@ def parseImagePullSecrets(secretsInput) {
853853
reportFail('config', "imagePullSecrets must be a List or String, got: ${secretsInput.getClass().getName()}")
854854
}
855855

856-
@NonCPS
857-
def ensureK8sCloud(cloudName, namespace = "default") {
858-
if (!cloudName) {
859-
return false
860-
}
861-
def j = Jenkins.instance
862-
def cl = j.pluginManager.uberClassLoader
863-
def k8sCloudClass = cl.loadClass("org.csanchez.jenkins.plugins.kubernetes.KubernetesCloud")
864-
def cloud = j.clouds.getByName(cloudName)
865-
def cloudCreated = false
866-
867-
if (cloud != null && !k8sCloudClass.isInstance(cloud)) {
868-
j.clouds.remove(cloud)
869-
cloud = null
870-
}
871-
872-
if (cloud == null) {
873-
cloud = k8sCloudClass.getConstructor(String).newInstance(cloudName)
874-
j.clouds.add(cloud)
875-
cloudCreated = true
876-
}
877-
878-
// Only set serverUrl when JENKINS_K8S_API_URL is set; otherwise preserve existing (e.g. from
879-
// startup script). Do not default to "https://k3s:6443" here—that hostname only resolves in
880-
// local Docker; on shared Jenkins it causes UnknownHostException. Local runs must pass
881-
// JENKINS_K8S_API_URL (e.g. scripts/local_gha_ci.sh does).
882-
def apiUrl = System.getenv("JENKINS_K8S_API_URL")?.trim()
883-
if (apiUrl) {
884-
cloud.serverUrl = apiUrl
885-
}
886-
cloud.skipTlsVerify = true
887-
// Do not overwrite namespace on existing shared clouds unless explicitly requested.
888-
def namespaceEnv = System.getenv("JENKINS_K8S_NAMESPACE")?.trim()
889-
def desiredNamespace = namespaceEnv ?: namespace ?: "default"
890-
if (namespaceEnv || cloudCreated || !cloud.namespace) {
891-
cloud.namespace = desiredNamespace
892-
}
893-
def jenkinsUrlEnv = System.getenv("JENKINS_URL")?.trim()
894-
if (jenkinsUrlEnv) {
895-
cloud.jenkinsUrl = jenkinsUrlEnv
896-
} else if (!cloud.jenkinsUrl) {
897-
cloud.jenkinsUrl = "http://jenkins:8080"
898-
}
899-
def tunnelEnv = System.getenv("JENKINS_TUNNEL")?.trim()
900-
if (tunnelEnv) {
901-
cloud.jenkinsTunnel = tunnelEnv
902-
} else if (!cloud.jenkinsTunnel) {
903-
cloud.jenkinsTunnel = "jenkins:50000"
904-
}
905-
if ((System.getenv("JENKINS_K8S_TOKEN") ?: "").trim()) {
906-
cloud.credentialsId = "k8s-sa-token"
907-
}
908-
j.save()
909-
return true
910-
}
911-
912856
def runK8(image, branchName, config, axis, steps=config.steps) {
913857

914858
def cloudName = image.cloud ?: getConfigVal(config, ['kubernetes', 'cloud'], null)
@@ -956,7 +900,6 @@ def runK8(image, branchName, config, axis, steps=config.steps) {
956900
def namespace = image.namespace ?: getConfigVal(config, ['kubernetes', 'namespace'], "default")
957901
def tolerations = image.tolerations ?: getConfigVal(config, ['kubernetes', 'tolerations'], "[]")
958902
def imagePullSecrets = parseImagePullSecrets(getConfigVal(config, ['kubernetes', 'imagePullSecrets'], "[]"))
959-
ensureK8sCloud(cloudName, namespace)
960903
def yaml = """
961904
spec:
962905
containers:
@@ -1481,7 +1424,6 @@ def build_docker_on_k8(image, config) {
14811424
def namespace = image.namespace ?: getConfigVal(config, ['kubernetes', 'namespace'], "default")
14821425
def tolerations = image.tolerations ?: getConfigVal(config, ['kubernetes', 'tolerations'], "[]")
14831426
def imagePullSecrets = parseImagePullSecrets(getConfigVal(config, ['kubernetes', 'imagePullSecrets'], "[]"))
1484-
ensureK8sCloud(cloudName, namespace)
14851427
def yaml = """
14861428
spec:
14871429
containers:

0 commit comments

Comments
 (0)