Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcosborges/f06716df4009ce6496baa00d2516ff89 to your computer and use it in GitHub Desktop.
Save marcosborges/f06716df4009ce6496baa00d2516ff89 to your computer and use it in GitHub Desktop.
def name = "NOME_DA_NAMESPACE"
def solution = "NOME_DA_SOLUÇÃO"
def existsK8sNamespace = sh (script: """kubectl get namespace --field-selector="metadata.name==${name}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sService = sh (script: """kubectl get service -n ${name} --field-selector="metadata.name==${solution}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sIngress = sh (script: """kubectl get ingress -n ${name} --field-selector="metadata.name==${name}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sDeployment = sh (script: """kubectl get deployment -n ${name} --field-selector="metadata.name==${solution}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def existsK8sHPA = sh (script: """kubectl get hpa -n ${name} --field-selector="metadata.name==${solution}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
def k8sExists(type, name, solution) {
if (type == "namespace") {
return sh (script: """kubectl get ${type} --field-selector="metadata.name==${name}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
} else {
return sh (script: """kubectl get ${type} -n ${name} --field-selector="metadata.name==${solution}" -o jsonpath="{.items[*].metadata.name}" """ , returnStdout: true).trim() == "" ? false : true
}
}
def name = "NOME_DA_NAMESPACE"
def solution = "NOME_DA_SOLUÇÃO"
def existsK8sNamespace = k8sExists("namespace", name, "");
def existsK8sService = k8sExists("service", name, solution);
def existsK8sIngress = k8sExists("ingress", name, solution);
def existsK8sDeployment = k8sExists("deployment", name, solution);
def existsK8sHPA = k8sExists("hpa", name, solution);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment