Jenkins Kubernetes Slave agent setup

82 views Asked by At

I'm trying to construct a shared library for kuberenetes slave agents. I'm following the jenkins shared library structure. Anyone please help me in setting up this library.

My src/com/k8s/PodTemplatesLib.groovy is as follows:

package com.k8s

class PodTemplatesLib implements Serializable {
  def steps
  def PodTemplatesLib(steps) {this.steps = steps}

public void mavenTemplate(body) {
  
  podTemplate(
    yaml: libraryResource('KubernetesPod.yaml'),
    cloud: 'K8Cluster',
    label: 'eks-dev-slaves'
    ) {
    body.call()
}
}
}

My corresponding Jenkinsfile is under vars\K8sAgentLib.groovy

@Library('k8sagent') _
import com.k8s.PodTemplatesLib

def call(body){

  def k8s_helper = new PodTemplatesLib(this)

  def pipelineParams= [:]
  body.resolveStrategy = Closure.DELEGATE_FIRST
  body.delegate = pipelineParams
  body()
  my_node="eks-dev-slaves"

  k8s_helper.mavenTemplate {
    node(my_node) {
      container('k8client') {
        sh '''
        echo K8S_CONTAINER_ENV_VAR = ${CONTAINER_ENV_VAR}
        kubectl version --short
        kubectl get ns --kubeconfig=/app/config
        '''
      }
     }
  }
}

With above configuration, the kubernetes slave is not creating and its not invoking. Any experts here can help me in writing the library structure to invoke the agents.

0

There are 0 answers