Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-73383

Can't use yaml anchors/aliases in pod definition

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • kubernetes-plugin
    • jenkins: Version 2.421
      k8s: 1.25.0
      plugin version: 4029.v5712230ccb_f8

      When the fix for JENKINS-71956 was pushed, it changed the serializer to jackson, which has this long-standing bug:

      This means that pod yamls can no longer utilize anchors.

      Example Script:

      pipeline {
        agent {
          kubernetes {
            cloud "test"
            defaultContainer 'aws'
            yaml '''
              apiVersion: v1
              kind: Pod
              spec:
                containers:
                - name: aws
                  image: "amazon/aws-cli:latest"
                  command:
                  - cat
                  tty: true
                  securityContext: &securityContext
                    runAsUser: 1000
                    runAsGroup: 1000
                    allowPrivilegeEscalation: false
                - name: jnlp
                  securityContext: *securityContext
            '''
          }
        }
        stages {
          stage('Test') {
            steps{
              script {
                echo "Hello World"
              }
            }
          }
        }
      } 

       

      And the resulting error:

       

       hudson.remoting.ProxyException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `io.fabric8.kubernetes.api.model.SecurityContext` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('securityContext')
        at [Source: (ByteArrayInputStream); line: 16, column: 30] (through reference chain: io.fabric8.kubernetes.api.model.Pod["spec"]->io.fabric8.kubernetes.api.model.PodSpec["containers"]->java.util.ArrayList[1]->io.fabric8.kubernetes.api.model.Container["securityContext"])
       	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
       	at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1754)
       	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1379)
       	at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromString(StdDeserializer.java:311)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1588)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:197)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:187)
       	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
       	at io.fabric8.kubernetes.model.jackson.SettableBeanPropertyDelegate.deserializeAndSet(SettableBeanPropertyDelegate.java:134)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:310)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
       	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer._deserializeFromArray(CollectionDeserializer.java:361)
       	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:246)
       	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:30)
       	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
       	at io.fabric8.kubernetes.model.jackson.SettableBeanPropertyDelegate.deserializeAndSet(SettableBeanPropertyDelegate.java:134)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:310)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
       	at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
       	at io.fabric8.kubernetes.model.jackson.SettableBeanPropertyDelegate.deserializeAndSet(SettableBeanPropertyDelegate.java:134)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:310)
       	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
       	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
       	at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2125)
       	at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1501)
       	at org.csanchez.jenkins.plugins.kubernetes.Serialization2.unmarshal(Serialization2.java:39)
       Caused: hudson.remoting.ProxyException: java.io.IOException: Unable to parse InputStream.
       	at org.csanchez.jenkins.plugins.kubernetes.Serialization2.unmarshal(Serialization2.java:41)
       	at org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.parseFromYaml(PodTemplateUtils.java:696)
       	at org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.validateYamlContainerNames(PodTemplateUtils.java:725)
       	at org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.validateYamlContainerNames(PodTemplateUtils.java:715)
       	at org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution.start(PodTemplateStepExecution.java:155)
       	at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:323)
       	at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:196)
       	at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)
       	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
       	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
       	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
       	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
       	at org.jenkinsci.plugins.workflow.cps.LoggingInvoker.methodCall(LoggingInvoker.java:105)
       Also:   hudson.remoting.ProxyException: org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: c6c6a41a-3eeb-4468-bd44-6a70d53f0488
       Caused: hudson.remoting.ProxyException: java.lang.RuntimeException: Failed to parse yaml: "
               apiVersion: v1
               kind: Pod
               spec:
                 containers:
                 - name: aws
                   image: "amazon/aws-cli:latest"
                   command:
                   - cat
                   tty: true
                   securityContext: &securityContext
                     runAsUser: 1000
                     runAsGroup: 1000
                     allowPrivilegeEscalation: false
                 - name: jnlp
                   securityContext: *securityContext
             "
       	at org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.parseFromYaml(PodTemplateUtils.java:699)
       	at org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.validateYamlContainerNames(PodTemplateUtils.java:725)
       	at org.csanchez.jenkins.plugins.kubernetes.PodTemplateUtils.validateYamlContainerNames(PodTemplateUtils.java:715)
       	at org.csanchez.jenkins.plugins.kubernetes.pipeline.PodTemplateStepExecution.start(PodTemplateStepExecution.java:155)
       	at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:323)
       	at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:196)
       	at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:124)
       	at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:47)
       	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
       	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
       	at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
       	at org.jenkinsci.plugins.workflow.cps.LoggingInvoker.methodCall(LoggingInvoker.java:105)
       	at org.csanchez.jenkins.plugins.kubernetes.pipeline.KubernetesDeclarativeAgentScript.run(KubernetesDeclarativeAgentScript.groovy:51)
       	at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.inDeclarativeAgent(ModelInterpreter.groovy:601)
       	at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.call(ModelInterpreter.groovy:77)
       	at WorkflowScript.run(WorkflowScript:1)
       	at ___cps.transform___(Native Method)
       	at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:90)
       	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:116)
       	at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixArg(FunctionCallBlock.java:85)
       	at jdk.internal.reflect.GeneratedMethodAccessor238.invoke(Unknown Source)
       	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
       	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
       	at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
       	at com.cloudbees.groovy.cps.impl.ClosureBlock.eval(ClosureBlock.java:46)
       	at com.cloudbees.groovy.cps.Next.step(Next.java:83)
       	at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:147)
       	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:17)
       	at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:49)
       	at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:180)
       	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:423)
       	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:331)
       	at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:295)
       	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.lambda$wrap$4(CpsVmExecutorService.java:136)
       	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
       	at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
       	at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
       	at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
       	at jenkins.util.ErrorLoggingExecutorService.lambda$wrap$0(ErrorLoggingExecutorService.java:51)
       	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
       	at java.base/java.util.concurrent.FutureTask.run(Unknown Source)
       	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
       	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
       	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$1.call(CpsVmExecutorService.java:53)
       	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$1.call(CpsVmExecutorService.java:50)
       	at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:136)
       	at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:275)
       	at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService.lambda$categoryThreadFactory$0(CpsVmExecutorService.java:50)
       	at java.base/java.lang.Thread.run(Unknown Source)

       

       

            Unassigned Unassigned
            dionj Dion
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: