digitalocean / digitalocean-cloud-controller-manager

Kubernetes cloud-controller-manager for DigitalOcean (beta)
Apache License 2.0
527 stars 149 forks source link

K8 annotations for load balancer name / id do not work as expected #512

Closed brizzbuzz closed 2 years ago

brizzbuzz commented 2 years ago

Hi, I am trying to customize my load balancer name or id so I can pull it from a terraform data block, but unfortunately, the annotations do not seem to be working.

This page https://docs.digitalocean.com/products/kubernetes/how-to/configure-load-balancers/ indicates that users should be able to set the load balancer id (it doesn't mention it, but feels like name should also be possible?)

I have a kubernetes service

resource "kubernetes_service" "git_server" {
  metadata {
    name = "soft-serve"
    annotations = {
      "kubernetes.digitalocean.com/load-balancer-name" = "soft-serve-${terraform.workspace}"
      "kubernetes.digitalocean.com/load-balancer-id" = "soft-serve-${terraform.workspace}"
    }
  }
  spec {
    type             = "LoadBalancer"
    session_affinity = "ClientIP"

    selector = {
      app = kubernetes_deployment.git_server.metadata.0.labels.app
    }

    port {
      port        = 22
      target_port = 23231
    }
  }
}

And the load balancer gets provisioned at least, but instead of the name being soft-serve-sandbox, it is the default name. thankfully, the documentation is pretty great, and mentions this

If you do not specify a custom name, the load balancer defaults to a name starting with the character a appended by the Service UID.

So I was able to cobble together a hack where I format the UID properly, slap an a before it and I can access the LB that way.

locals {
  lb_name = substr(replace("a${kubernetes_service.git_server.metadata.0.uid}", "-", ""), 0, 32)
}

But this really would be much nicer if I could use the name via annotations as expected.

timoreimann commented 2 years ago

Hi @unredundant đź‘‹

Your annotation name might be slightly off: it should be service.beta.kubernetes.io/do-loadbalancer-name. See also the official documentation at https://github.com/digitalocean/digitalocean-cloud-controller-manager/blob/master/docs/controllers/services/annotations.md#servicebetakubernetesiodo-loadbalancer-name

Setting the LB ID annotation should only be required if you want to start owning an LB that was previously created. By default, our CCM will set the LB ID annotation automatically.

Let me know if this helps.

brizzbuzz commented 2 years ago

hmmm... I can definitely give this a shot, thanks!

the doc i linked above https://docs.digitalocean.com/products/kubernetes/how-to/configure-load-balancers definitely specifies kubernetes.digitalocean.com/load-balancer-id as a valid annotation. Is the doc incorrect / out of date?

brizzbuzz commented 2 years ago

yep, looks like that did the trick, thanks again for the help!

timoreimann commented 2 years ago

Glad it worked for you!

To wrap up the discussion: I think the annotation for the LB ID was correct. It was only the one for the LB name that had to be adjusted.