locals {
  kubeconfig = yamlencode({
    apiVersion = "v1"
    kind       = "Config"
    clusters = [{
      name = module.eks.cluster_name
      cluster = {
        server                     = module.eks.cluster_endpoint
        certificate-authority-data = module.eks.cluster_certificate_authority_data
      }
    }]
    contexts = [{
      name = module.eks.cluster_name
      context = {
        cluster = module.eks.cluster_name
        user    = module.eks.cluster_name
      }
    }]
    current-context = module.eks.cluster_name
    users = [{
      name = module.eks.cluster_name
      user = {
        exec = {
          apiVersion = "client.authentication.k8s.io/v1beta1"
          command    = "aws"
          args = [
            "eks",
            "get-token",
            "--region", var.region,
            "--cluster-name", module.eks.cluster_name,
            "--profile", var.account,
            "--output", "json"
          ]
        }
      }
    }]
  })
}

output "endpoiont" {
  description = "Endpoint for your Kubernetes API server"
  value       = module.eks.cluster_endpoint
}

output "public_ips" {
  description = "List of public Elastic IPs created for AWS NAT Gateway"
  value       = module.vpc.nat_public_ips
}

output "cluster_name" {
  description = "Name of the EKS cluster"
  value       = module.eks.cluster_name
}

output "cluster_oidc_issuer_url" {
  description = "The URL on the EKS cluster for the OpenID Connect identity provider"
  value       = module.eks.cluster_oidc_issuer_url
}

output "kubeconfig" {
  value     = local.kubeconfig
  sensitive = true
}

output "clinic_dns_entry" {
  description = "The DNS for private link of clinic"
  value = [
    for entry in aws_vpc_endpoint.clinic.dns_entry :
    entry.dns_name if !can(regex("^vpce-[0-9a-f]+-[a-z0-9]+-[a-z]{2}-[a-z]+-[1-9][a-z]\\.", entry.dns_name))
  ][0]
}
