Jenkins master unable to spin up slaves on k8s due to malformed label

1/17/2020

I have deployed a helm - baed Jenkins instance on k8s.

I have also installed (by passing them as list in the values.yaml file about 300 plugins (if that matters).

The thing started going wrong when I tried to spin up an agent.

This failed with the following error:

.default/api/v1/namespaces/jenkins/pods?labelSelector=jenkins%2Fjenkins-myenv-jenkins-slave%3Dtrue%2Cjenkins%2Flabel%3Djenkins-myenv-jenkins-slave_. Message: unable to parse requirement: invalid label value: "jenkins-myenv-jenkins-slave_": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?'). Received status: Status(apiVersion=v1, code=400, details=null, kind=Status, message=unable to parse requirement: invalid label value: "jenkins-myenv-jenkins-slave_": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue',  or 'my_value',  or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?'), metadata=ListMeta(_continue=null, remainingItemCount=null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=BadRequest, status=Failure, additionalProperties={}).

I really wonder how it ended up requesting for a pod with the label

jenkins-myenv-jenkins-slave_

since nowhere in my values.yaml file there is anything ending in _.

Here are the first lines of my agent configuration in values.yaml

agent:
  enabled: true
  image: "jenkins/jnlp-slave"
  tag: "3.27-1"
  customJenkinsLabels: []
  # name of the secret to be used for image pulling
  imagePullSecretName:
  componentName: "jenkins-slave"
  privileged: false

What is more, the relevant field in the configuration section does not seem to have an underscore in the end

enter image description here

-- pkaramol
jenkins
kubernetes
kubernetes-helm

2 Answers

1/17/2020

I would recommend not to define agents manually in jenkins, you should only configure kubernetes host address

With a declarative pipeline you can define a kubernetes agent like this:

   agent {
       kubernetes {
            label "${kubernetesUUID}"
            defaultContainer 'docker'
            yaml readTrusted('kube.yaml')
            idleMinutes idleminutes
        }
    }

the file kube.yml would be versionned in the same folder as the Jenkinsfile

apiVersion: v1
kind: Pod
spec:
    containers:
        - name: jnlp
          image: jenkinsci/jnlp-slave:latest
          args:
              - ${computer.jnlpmac} ${computer.name}
          tty: true
        - name: docker
          image: myimage:version
          tty: true
          command:
              - cat
          volumeMounts:
            - mountPath: /dev/random
              name: host-urandom
    volumes:
    - name: host-urandom
      hostPath:
        path: /dev/urandom

you can add as many container as you want in the spec, however jnlp must remain. also you should fix the version and not let latest

you can also pass a string with your spec directly to the yaml step instead of using an external file.

you can use the step idleMinutes if you want to let your agent up after several minutes and not spawn a new agent each time you build

-- fredericrous
Source: StackOverflow

1/21/2020

Check if you mistakenly have " " (space) char at the and of your label

-- ido
Source: StackOverflow