Kubernetes terraform,使用EOF在null_resource内部应用

时间:2019-02-19 11:49:48

标签: kubernetes terraform

Terraform Kubernetes不允许使用apiVersion: v1以外的任何形式。但是,我想通过使用null_resource预配器来运行kubectl apply -f ...kubectl delete -f ...(使用when = "destroy")来解决此问题。

我已经尝试使用kubernetes应用EOF模式来使它工作,但尚未对其进行管理。

这是我去过的地方,但看来|是非法的字符。

也希望我可以将yaml字符串提取到多行变量中,这样我就不必在null资源中重复它。

resource "null_resource" "res_name" {
  provisioner {
    command = <<EOF | kubectl apply -f -
      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: some-ingress-name
        annotations:  
          kubernetes.io/ingress.class: nginx
      spec:
        rules:
        - host: subdomain.example.com
          http:
            paths:
            - backend:
                serviceName: some-cluster-ip-service
                servicePort: 80
  }

  provisioner {
    when = "destroy"

    command = <<EOF | kubectl delete -f -
      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: some-ingress-name
        annotations:  
          kubernetes.io/ingress.class: nginx
      spec:
        rules:
        - host: subdomain.example.com
          http:
            paths:
            - backend:
                serviceName: some-cluster-ip-service
                servicePort: 80
  }
}

1 个答案:

答案 0 :(得分:2)

您的代码中存在一些错误。

该代码对我有效:

resource "null_resource" "res_name" {
  provisioner "local-exec" {
    command = "kubectl apply -f - <<EOF\n${var.ingress_yaml}\nEOF"
  }

  provisioner "local-exec" {
    when = "destroy"
    command = "kubectl delete -f - <<EOF\n${var.ingress_yaml}\nEOF"
  }
}

variable "ingress_yaml" {
  default = <<EOF
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: some-ingress-name
      annotations:
        kubernetes.io/ingress.class: nginx
    spec:
      rules:
      - host: subdomain.example.com
        http:
          paths:
          - backend:
              serviceName: some-cluster-ip-service
              servicePort: 80
    EOF
}

我建议改为从文件中读取YAML配置。然后,您可以获得YAML语法突出显示和IDE中显示的错误。 要么使用