rimusz / charts

Helm Charts for Kubernetes
MIT License
90 stars 70 forks source link

gcloud-sqlproxy - Duplicate Port Name if instanceShortName is not defined #143

Closed danyworks closed 1 year ago

danyworks commented 1 year ago

Bug in gcloud-sqlproxy chart

Used Tools: Google Kubernetes Engine Version: v1.27.3-gke.1700 Helm Version: v3.11.3 Helm Terraform Provider Version: 2.11.0

Description: This bug affects gcloud-sqlproxy chart, because of this I could neither install this chart as a new release nor upgrade an existing one. The values.yaml of the release, looked like this

cloudsql:
    instances:
      - instance: "long-instance-name-foo"
        project: "project"
        region: "region"
        port: 5432
      - instance: "long-instance-name-bar"
        project: "project"
        region: "region"
        port: 5433
# other values....

The helm release install/upgrade had failed with the error message " Duplicate Values for port name in deployment.yaml "

snippet from error message: * Service "sqlproxy" is invalid: [spec.ports[1].name: Duplicate value: "long-instance-n", spec.ports[2].name: Duplicate value: "long-instance-n"] * Deployment.apps "sqlproxy" is invalid: [spec.template.spec.containers[0].ports[1].name: Duplicate value: "long-instance-n", spec.template.spec.containers[0].ports[2].name: Duplicate value: "long-instance-n"]

My analysis on this issue:

if the instanceShortName is not explicitly set, the chart creates instanceShortName using the first 15 characters of instance and the created instanceShortName is no more unique if two instances have identitical prefix names of length 15 characters or longer. As in our case we had multiple cloudsql instances with the same prefix and the prefix was longer than 15 characters.

What you expected to happen: When the sqlproxy is deployed, it should have generated a random alphanumeric string of 9 or less digits and have appended it to instance name, so to create a unique instanceShortName

instanceShortName in helpers.tpl (gcloud-sqlproxy Chart Version 0.25.2):

{{/*
Create the short instance name
*/}}
{{- define "gcloud-sqlproxy.instanceShortName" -}}
{{ .instanceShortName |  default ((.instance |  trunc 15 | trimSuffix  "-" ) }}
{{- end -}} 

Suggested Fix (gcloud-sqlproxy Chart Version 0.25.3):

{{- define "gcloud-sqlproxy.instanceShortName" -}}
{{- $randomString := randAlphaNum 9 | lower -}}
{{ .instanceShortName | default (printf "%s-%s" (.instance | trunc 5 | trimSuffix "-") $randomString) }}
{{- end -}}

as a result, instanceShortName looks like this image

How to reproduce it:


Deploy the chart using helm (even without terraform helm provider), with the following values but the only condition is to have the prefix on more than one instances, and also the prefix should be more than 15 characters long

  cloudsql:
      instances:
        - instance: "long-instance-name-foo" # or any other name you prefer 
          project: "project"
          region: "region"
          port: 5432
        - instance: "long-instance-name-bar" # or any other name you prefer 
          project: "project"
          region: "region"
          port: 5433