// 配置configMap Environment // - SERVICE: service配置 // - K3S: k3s配置 // - configMapPath: 指定configMapPath,默认使用"projdir/configmap-env" def applyConfigMapEnv(Object SERVICE, Object K3S, String configMapPath='projdir/configmap-env.ini', String defaultNS='default') { println "applying k3s configmap environment from ${configMapPath}" configmap_env_name = "${SERVICE.name}${SERVICE.version}-env" configmap_env = "${SERVICE.name}${SERVICE.version}-env.yaml" sh "/k3s/kubectl create configmap ${configmap_env_name} --dry-run=client --from-env-file=${configMapPath} -o yaml > ${configmap_env}" sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${configmap_env} -n ${defaultNS}" return configmap_env_name } // 配置configMap // - SERVICE: service配置 // - K3S: k3s配置 // - configMapPath: 指定configMapPath,默认使用./configmap/ def applyConfigMapConfig(Object SERVICE, Object K3S, String configMapPath = './configmap/', String defaultNS='default') { println "applying k3s configmap config from ${configMapPath}" configmap_name = "${SERVICE.name}${SERVICE.version}-config" configmap_file = "${SERVICE.name}-configmap.yaml" sh "/k3s/kubectl create configmap ${configmap_name} --dry-run=client --from-file=${configMapPath} -o yaml > ${configmap_file}" sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${configmap_file} -n ${defaultNS}" return configmap_name } // 生成deployment.yaml // - template: deployment(template).yaml模板文件路径 // - kvs: key/value替换deployment(template) def generateDeployment(Map kvs, String template="deployment.yaml") { if (!fileExists(template)) { println "skip generating deployment.yaml, file ${template} not exists" return } println "generating k8s deployment... replace Key/Value in file: ${template}." kvs.each { def key, val -> print(val) value = val .replace('/', '\\/') .replace('"', '\\"') sh "sed -i \"s/${key}/${value}/g\" ${template}" } } // 生成service.yaml // - template: service(template).yaml模板文件路径 // - kvs: key/value替换service(template).yaml def generateService(Map kvs, String template="service.yaml") { if (!fileExists(template)) { println "skip generating service.yaml, file ${template} not exists" return } println "generating k8s service.yaml... replace Key/Value in file: ${template}." kvs.each { def key, val -> print(val) value = val .replace('/', '\\/') .replace('"', '\\"') sh "sed -i \"s/${key}/${value}/g\" ${template}" } } // 配置deployment.yaml def applyDeployment(Object K3S, String deployment="deployment.yaml") { if (!fileExists(deployment)) { println "ignore, file ${deployment} not exists" return } println "applying k8s deployment file: ${deployment} ..." sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${deployment}" } // 配置service.yaml def applyService(K3S, String service="service.yaml") { if (!fileExists(service)) { println "ignore, file ${service} not exists" return } println "applying k8s deployment file: ${service} ..." sh "/k3s/kubectl --kubeconfig ${env.WORKSPACE}/${K3S.kubeconfig} apply -f ${service}" } return this