Jenkins ERROR: Labels must follow required specs

12/13/2019

I am trying to create a pipeline job for Angular code to deploy the application into k8 cluster. Below there is a code for pipeline container podTemplate, during the build I get the next error.

def label = "worker-${UUID.randomUUID().toString()}"

podTemplate(
    cloud: 'kubernetes',
    namespace: 'test',
    imagePullSecrets: ['regcred'],
    label: label,
    containers: [
        containerTemplate(name: 'nodejs', image: 'nodejscn/node:latest', ttyEnabled: true, command: 'cat'),
        containerTemplate(name: 'docker', image: 'nodejscn/node:latest', ttyEnabled: true, command: 'cat'),
        containerTemplate(name: 'kubectl', image: 'k8spoc1/kubctl:latest', ttyEnabled: true, command: 'cat')
    ],
    volumes: [
        hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock'),
        hostPathVolume(hostPath: '/root/.m2/repository', mountPath: '/root/.m2/repository')
    ]
) {

    node(label) {
      def scmInfo = checkout scm
      def image_tag
      def image_name
      sh 'pwd'
      def gitCommit = scmInfo.GIT_COMMIT
      def gitBranch = scmInfo.GIT_BRANCH
      def commitId
      commitId= scmInfo.GIT_COMMIT[0..7]
      image_tag = "${scmInfo.GIT_BRANCH}-${scmInfo.GIT_COMMIT[0..7]}"

      stage('NPM Install') {
           container ('nodejs') {
              withEnv(["NPM_CONFIG_LOGLEVEL=warn"]) {
                sh 'npm install'
            }
           }
      }
   }
}

Error from Jenkins:

[Pipeline] Start of Pipeline
[Pipeline] podTemplate
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: Labels must follow required specs - https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set:  Ubuntu-82f3782f-b5aa-4029-9c51-57610153747c
Finished: FAILURE

Do I need to mention a spec value of my Jenkins file?

-- tp.palanisamy thangavel
jenkins
kubernetes

2 Answers

12/23/2019
ERROR: Labels must follow required specs - https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set:  Ubuntu-82f3782f-b5aa-4029-9c51-57610153747c

Your label Ubuntu-82f3782f-b5aa-4029-9c51-57610153747c has a space before Ubuntu which is not valid

But that error message doesn't seem to match the pod definition you posted as there is no mention of Ubuntu anywhere. Maybe inherited

-- csanchez
Source: StackOverflow

12/16/2019

The error message you get:

ERROR: Labels must follow required specs - https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set:  Ubuntu-82f3782f-b5aa-4029-9c51-57610153747c

points out quite precisely what can be wrong with your Pod template. As you can see in link to kubernetes documentation given in the ERROR message, you need to follow certain rules when defining a Pod. labels element is a dictionary/map field that requires you to provide at least one key-value pair so you cannot just write label: label in your specification.

You can try to define your PodTemplate in yaml format (which is mostly used in kubernetes) like in this example:

podTemplate(yaml: """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: some-label-value
spec:
  containers:
  - name: busybox
    image: busybox
    command:
    - cat
    tty: true
"""
) {
    node(POD_LABEL) {
      container('busybox') {
        sh "hostname"
      }
    }
}

As you can read here:

label The label of the pod. Can be set to a unique value to avoid conflicts across builds, or omitted and POD_LABEL will be defined inside the step.

label field can be omitted at all so first you can try without it and you shouldn't get any error message.

-- mario
Source: StackOverflow