我正在尝试在我的应用程序负载均衡器中检索主机名,我将其配置为入口。
目前的场景是:我正在使用地形部署一个舵图,并配置了一个ALB作为入口。ALB和Helm图表已正常部署,并且正在工作,但是,我需要检索此ALB的主机名,以创建指向该ALB的Route53记录。当我尝试检索这些信息时,它会返回null值。
根据terraform自己的文档,正确的方法如下:
data "kubernetes_ingress" "example" {
metadata {
name = "terraform-example"
}
}
resource "aws_route53_record" "example" {
zone_id = data.aws_route53_zone.k8.zone_id
name = "example"
type = "CNAME"
ttl = "300"
records = [data.kubernetes_ingress.example.status.0.load_balancer.0.ingress.0.hostname]
}
我完全按照文档中的操作(即使是提供商版本也是最新的(,以下是我的代码摘录:
# Helm release resource
resource "helm_release" "argocd" {
name = "argocd"
repository = "https://argoproj.github.io/argo-helm"
chart = "argo-cd"
namespace = "argocd"
version = "4.9.7"
create_namespace = true
values = [
templatefile("${path.module}/settings/helm/argocd/values.yaml", {
certificate_arn = module.acm_certificate.arn
})
]
}
# Kubernetes Ingress data to retrieve de ingress hostname from helm deployment (ALB Hostname)
data "kubernetes_ingress" "argocd" {
metadata {
name = "argocd-server"
namespace = helm_release.argocd.namespace
}
depends_on = [
helm_release.argocd
]
}
# Route53 record creation
resource "aws_route53_record" "argocd" {
name = "argocd"
type = "CNAME"
ttl = 600
zone_id = aws_route53_zone.r53_zone.id
records = [data.kubernetes_ingress.argocd.status.0.load_balancer.0.ingress.0.hostname]
}
当我运行terraform apply
时,我得到以下错误:
╷
│ Error: Attempt to index null value
│
│ on route53.tf line 67, in resource "aws_route53_record" "argocd":
│ 67: records = [data.kubernetes_ingress.argocd.status.0.load_balancer.0.ingress.0.hostname]
│ ├────────────────
│ │ data.kubernetes_ingress.argocd.status is null
│
│ This value is null, so it does not have any indices.
我的入口配置(由Helm Release部署(:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: argocd-server
namespace: argocd
uid: 646f6ea0-7991-4a13-91d0-da236164ac3e
resourceVersion: '4491'
generation: 1
creationTimestamp: '2022-08-08T13:29:16Z'
labels:
app.kubernetes.io/component: server
app.kubernetes.io/instance: argocd
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: argocd-server
app.kubernetes.io/part-of: argocd
helm.sh/chart: argo-cd-4.9.7
annotations:
alb.ingress.kubernetes.io/backend-protocol: HTTPS
alb.ingress.kubernetes.io/certificate-arn: >-
arn:aws:acm:us-east-1:124416843011:certificate/7b79fa2c-d446-423d-b893-c8ff3d92a5e1
alb.ingress.kubernetes.io/group.name: altb-devops-eks-support-alb
alb.ingress.kubernetes.io/listen-ports: '[{"HTTPS":443}]'
alb.ingress.kubernetes.io/load-balancer-name: altb-devops-eks-support-alb
alb.ingress.kubernetes.io/scheme: internal
alb.ingress.kubernetes.io/tags: >-
Name=altb-devops-eks-support-alb,Stage=Support,CostCenter=Infrastructure,Project=Shared
Infrastructure,Team=DevOps
alb.ingress.kubernetes.io/target-type: ip
kubernetes.io/ingress.class: alb
meta.helm.sh/release-name: argocd
meta.helm.sh/release-namespace: argocd
finalizers:
- group.ingress.k8s.aws/altb-devops-eks-support-alb
managedFields:
- manager: controller
operation: Update
apiVersion: networking.k8s.io/v1
time: '2022-08-08T13:29:16Z'
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:finalizers:
.: {}
v:"group.ingress.k8s.aws/altb-devops-eks-support-alb": {}
- manager: terraform-provider-helm_v2.6.0_x5
operation: Update
apiVersion: networking.k8s.io/v1
time: '2022-08-08T13:29:16Z'
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:alb.ingress.kubernetes.io/backend-protocol: {}
f:alb.ingress.kubernetes.io/certificate-arn: {}
f:alb.ingress.kubernetes.io/group.name: {}
f:alb.ingress.kubernetes.io/listen-ports: {}
f:alb.ingress.kubernetes.io/load-balancer-name: {}
f:alb.ingress.kubernetes.io/scheme: {}
f:alb.ingress.kubernetes.io/tags: {}
f:alb.ingress.kubernetes.io/target-type: {}
f:kubernetes.io/ingress.class: {}
f:meta.helm.sh/release-name: {}
f:meta.helm.sh/release-namespace: {}
f:labels:
.: {}
f:app.kubernetes.io/component: {}
f:app.kubernetes.io/instance: {}
f:app.kubernetes.io/managed-by: {}
f:app.kubernetes.io/name: {}
f:app.kubernetes.io/part-of: {}
f:helm.sh/chart: {}
f:spec:
f:rules: {}
- manager: controller
operation: Update
apiVersion: networking.k8s.io/v1
time: '2022-08-08T13:29:20Z'
fieldsType: FieldsV1
fieldsV1:
f:status:
f:loadBalancer:
f:ingress: {}
subresource: status
selfLink: /apis/networking.k8s.io/v1/namespaces/argocd/ingresses/argocd-server
status:
loadBalancer:
ingress:
- hostname: >-
internal-altb-devops-eks122-support-alb-1845221539.us-east-1.elb.amazonaws.com
spec:
rules:
- host: argocd.altb.co
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: argocd-server
port:
number: 80
Ingress的地形数据源是:kubernetes_igress_v1。https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/ingress_v1
data "kubernetes_ingress_v1" "argocd" {
metadata {
name = "argocd-server"
namespace = helm_release.argocd.namespace
}
depends_on = [
helm_release.argocd
]
}
这应该行得通。