Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new standard labels and allow custom labels #45

Merged
merged 1 commit into from
Sep 28, 2023

Conversation

alex-bezek
Copy link
Contributor

Resolves #44

Why

This chart appears to have the standard labels used with helm 2. This adds in the new helm 3 standard labels and also allows the user to pass in additional labels that will get applied to all resources.

How

  • Create a template helper that creates metadata labels that can be reused by multiple resource templates
  • Include the existing labels in this helper for passivity
  • default to use the fullName value for the name labels but allow an optional appSuffix to be passed in for passivity for the jaeger and storage usecases
  • include this new helper in some resources that were missing the common labels
  • add a helm value to pass in an object of custom labels which is a common helm convention
  • enable jaeger and pass custom labels in CI to exercise more code paths

Validation

Run helm template charts/athens-proxy --set extraLabels.athensIs=awesome --set jaeger.enabled=true against main and this branch and diff them

Diff
7a8
>
9c10
<     chart: "athens-proxy-0.7.0"
---
>     chart: athens-proxy-0.7.0
11a13,18
>     app.kubernetes.io/name: release-name-athens-proxy
>     helm.sh/chart: athens-proxy-0.7.0
>     app.kubernetes.io/managed-by: "Helm"
>     app.kubernetes.io/instance: "release-name"
>     app.kubernetes.io/version: "v0.12.0"
>     athensIs: awesome
17a25,36
>   labels:
>
>     app: release-name-athens-proxy
>     chart: athens-proxy-0.7.0
>     release: "release-name"
>     heritage: "Helm"
>     app.kubernetes.io/name: release-name-athens-proxy
>     helm.sh/chart: athens-proxy-0.7.0
>     app.kubernetes.io/managed-by: "Helm"
>     app.kubernetes.io/instance: "release-name"
>     app.kubernetes.io/version: "v0.12.0"
>     athensIs: awesome
26a46
>
28c48
<     chart: "athens-proxy-0.7.0"
---
>     chart: athens-proxy-0.7.0
30a51,56
>     app.kubernetes.io/name: release-name-athens-proxy-jaeger
>     helm.sh/chart: athens-proxy-0.7.0
>     app.kubernetes.io/managed-by: "Helm"
>     app.kubernetes.io/instance: "release-name"
>     app.kubernetes.io/version: "v0.12.0"
>     athensIs: awesome
67a94
>
69c96
<     chart: "athens-proxy-0.7.0"
---
>     chart: athens-proxy-0.7.0
71a99,104
>     app.kubernetes.io/name: release-name-athens-proxy
>     helm.sh/chart: athens-proxy-0.7.0
>     app.kubernetes.io/managed-by: "Helm"
>     app.kubernetes.io/instance: "release-name"
>     app.kubernetes.io/version: "v0.12.0"
>     athensIs: awesome
88a122
>
90c124
<     chart: "athens-proxy-0.7.0"
---
>     chart: athens-proxy-0.7.0
92a127,132
>     app.kubernetes.io/name: release-name-athens-proxy
>     helm.sh/chart: athens-proxy-0.7.0
>     app.kubernetes.io/managed-by: "Helm"
>     app.kubernetes.io/instance: "release-name"
>     app.kubernetes.io/version: "v0.12.0"
>     athensIs: awesome
101a142
>
103c144
<         chart: "athens-proxy-0.7.0"
---
>         chart: athens-proxy-0.7.0
104a146,152
>         heritage: "Helm"
>         app.kubernetes.io/name: release-name-athens-proxy
>         helm.sh/chart: athens-proxy-0.7.0
>         app.kubernetes.io/managed-by: "Helm"
>         app.kubernetes.io/instance: "release-name"
>         app.kubernetes.io/version: "v0.12.0"
>         athensIs: awesome
152a201
>
154c203
<     chart: "athens-proxy-0.7.0"
---
>     chart: athens-proxy-0.7.0
156a206,211
>     app.kubernetes.io/name: release-name-athens-proxy-jaeger
>     helm.sh/chart: athens-proxy-0.7.0
>     app.kubernetes.io/managed-by: "Helm"
>     app.kubernetes.io/instance: "release-name"
>     app.kubernetes.io/version: "v0.12.0"
>     athensIs: awesome
Example Output from template
---
# Source: athens-proxy/templates/service-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: release-name-athens-proxy
  labels:
    
    app: release-name-athens-proxy
    chart: athens-proxy-0.7.0
    release: "release-name"
    heritage: "Helm"
    app.kubernetes.io/name: release-name-athens-proxy
    helm.sh/chart: athens-proxy-0.7.0
    app.kubernetes.io/managed-by: "Helm"
    app.kubernetes.io/instance: "release-name"
    app.kubernetes.io/version: "v0.12.0"
    athensIs: awesome
---
# Source: athens-proxy/templates/secret.yaml
kind: Secret
apiVersion: v1
metadata:
  name: release-name-athens-proxy-secret
  labels:
    
    app: release-name-athens-proxy
    chart: athens-proxy-0.7.0
    release: "release-name"
    heritage: "Helm"
    app.kubernetes.io/name: release-name-athens-proxy
    helm.sh/chart: athens-proxy-0.7.0
    app.kubernetes.io/managed-by: "Helm"
    app.kubernetes.io/instance: "release-name"
    app.kubernetes.io/version: "v0.12.0"
    athensIs: awesome
type: Opaque
data:
---
# Source: athens-proxy/templates/jaeger-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: release-name-athens-proxy-jaeger
  labels:
    
    app: release-name-athens-proxy-jaeger
    chart: athens-proxy-0.7.0
    release: "release-name"
    heritage: "Helm"
    app.kubernetes.io/name: release-name-athens-proxy-jaeger
    helm.sh/chart: athens-proxy-0.7.0
    app.kubernetes.io/managed-by: "Helm"
    app.kubernetes.io/instance: "release-name"
    app.kubernetes.io/version: "v0.12.0"
    athensIs: awesome
spec:
  type: ClusterIP
  ports:
    - name: jaeger-collector-http
      port: 14268
      protocol: TCP
      targetPort: 14268
    - name: jaeger-zipkin-thrift
      port: 5775
      protocol: UDP
      targetPort: 5775
    - name: jaeger-compact
      port: 6831
      protocol: UDP
      targetPort: 6831
    - name: jaeger-binary
      port: 6832
      protocol: UDP
      targetPort: 6832
    - name: jaeger-configs
      port: 5778
      protocol: TCP
      targetPort: 5778
    - name: jaeger-query-http
      port: 16686
      protocol: TCP
      targetPort: 16686
  selector:
    app: release-name-athens-proxy-jaeger
    release: "release-name"
---
# Source: athens-proxy/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: release-name-athens-proxy
  labels:
    
    app: release-name-athens-proxy
    chart: athens-proxy-0.7.0
    release: "release-name"
    heritage: "Helm"
    app.kubernetes.io/name: release-name-athens-proxy
    helm.sh/chart: athens-proxy-0.7.0
    app.kubernetes.io/managed-by: "Helm"
    app.kubernetes.io/instance: "release-name"
    app.kubernetes.io/version: "v0.12.0"
    athensIs: awesome
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 80
    targetPort: 3000
    protocol: TCP
  selector:
    app: release-name-athens-proxy
    release: "release-name"
---
# Source: athens-proxy/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: release-name-athens-proxy
  labels:
    
    app: release-name-athens-proxy
    chart: athens-proxy-0.7.0
    release: "release-name"
    heritage: "Helm"
    app.kubernetes.io/name: release-name-athens-proxy
    helm.sh/chart: athens-proxy-0.7.0
    app.kubernetes.io/managed-by: "Helm"
    app.kubernetes.io/instance: "release-name"
    app.kubernetes.io/version: "v0.12.0"
    athensIs: awesome
spec:
  replicas: 1
  selector:
    matchLabels:
      app: release-name-athens-proxy
      release: "release-name"
  template:
    metadata:
      labels:
        
        app: release-name-athens-proxy
        chart: athens-proxy-0.7.0
        release: "release-name"
        heritage: "Helm"
        app.kubernetes.io/name: release-name-athens-proxy
        helm.sh/chart: athens-proxy-0.7.0
        app.kubernetes.io/managed-by: "Helm"
        app.kubernetes.io/instance: "release-name"
        app.kubernetes.io/version: "v0.12.0"
        athensIs: awesome
      annotations:
        checksum/upstream: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
        checksum/ssh-config: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
        checksum/ssh-secret: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    spec:
      serviceAccount: release-name-athens-proxy
      containers:
      - name: release-name-athens-proxy
        image: "docker.io/gomods/athens:v0.12.0"
        imagePullPolicy: "IfNotPresent"
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: "/healthz"
            port: 3000
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        readinessProbe:
          httpGet:
            path: "/readyz"
            port: 3000
        env:
        - name: ATHENS_GOGET_WORKERS
          value: "3"
        - name: ATHENS_STORAGE_TYPE
          value: "disk"
        - name: ATHENS_DISK_STORAGE_ROOT
          value: "/var/lib/athens"
        - name: ATHENS_TRACE_EXPORTER_URL
          value: "SET THIS ON THE COMMAND LINE"
        - name: ATHENS_TRACE_EXPORTER
          value: "jaeger"
        ports:
        - containerPort: 3000
        volumeMounts:
        - name: storage-volume
          mountPath: "/var/lib/athens"
      volumes:
      - name: storage-volume
        emptyDir: {}
---
# Source: athens-proxy/templates/jaeger-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: release-name-athens-proxy-jaeger
  labels:
    
    app: release-name-athens-proxy-jaeger
    chart: athens-proxy-0.7.0
    release: "release-name"
    heritage: "Helm"
    app.kubernetes.io/name: release-name-athens-proxy-jaeger
    helm.sh/chart: athens-proxy-0.7.0
    app.kubernetes.io/managed-by: "Helm"
    app.kubernetes.io/instance: "release-name"
    app.kubernetes.io/version: "v0.12.0"
    athensIs: awesome
spec:
  replicas: 1
  selector:
    matchLabels:
      app: release-name-athens-proxy-jaeger
      release: "release-name"
  template:
    metadata:
      labels:
        app: release-name-athens-proxy-jaeger
        chart: "athens-proxy-0.7.0"
        release: "release-name"
    spec:
      containers:
        - env:
            - name: COLLECTOR_ZIPKIN_HTTP_PORT
              value: "9441"
          image: "jaegertracing/all-in-one:latest"
          name: release-name-athens-proxy-jaeger
          ports:
            - containerPort: 14268
              protocol: TCP
            - containerPort: 5775
              protocol: UDP
            - containerPort: 6831
              protocol: UDP
            - containerPort: 6832
              protocol: UDP
            - containerPort: 5778
              protocol: TCP
            - containerPort: 16686
              protocol: TCP
            - containerPort: 9411
              protocol: TCP
---
# Source: athens-proxy/templates/test/test-connection.yaml
apiVersion: v1
kind: Pod
metadata:
  name: "release-name-athens-proxy-test-connection"
  labels:
    app: release-name-athens-proxy
  annotations:
    "helm.sh/hook": test
spec:
  containers:
    - name: wget
      image: busybox
      command: ['wget']
      args: ['release-name-athens-proxy:80']
  restartPolicy: Never

@alex-bezek alex-bezek requested a review from a team as a code owner September 27, 2023 05:36
{{- /* Allow an app suffix name to be passed in to append to the fullname */}}
{{- $defaultAppName := include "fullname" . }}
{{- $appName := printf "%s%s" $defaultAppName (default "" .appSuffix) }}
{{- /* Existing Legacy labels for passivity */}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these comments with the intention to document why they were here, however this top one does create an extra empty line the in the helm template

apiVersion: apps/v1
kind: Deployment
metadata:
  name: release-name-athens-proxy-jaeger
  labels:
    
    app: release-name-athens-proxy-jaeger
    chart: athens-proxy-0.7.0
    release: "release-name"
    heritage: "Helm"
    app.kubernetes.io/name: release-name-athens-proxy-jaeger
    helm.sh/chart: athens-proxy-0.7.0
    app.kubernetes.io/managed-by: "Helm"
    app.kubernetes.io/instance: "release-name"
    app.kubernetes.io/version: "v0.12.0"
    athensIs: awesome
spec:

This output seems generally accepted by the helm docs https://helm.sh/docs/chart_best_practices/templates/#whitespace-in-generated-templates but i'm happy to remove these comments if the tradeoff isn't worth it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As long as the indentation is correct, it's fine. I've seen many helm charts with some empty lines. The comment should generally not be necessary when the code is readable, but it may help newcomers and does not hurt.

chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
release: "{{ .Release.Name }}"
heritage: "{{ .Release.Service }}"
{{- include "athens.metaLabels" (dict "appSuffix" "-jaeger" "Values" .Values "Release" .Release "Chart" .Chart) | nindent 4 }}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit gross since it has to be aware of the values being used by the underlying helper, but I wanted to find a way to re-use the helper for these couple of unique cases that have a different name specified currently.

FWIW, these differences seem a bit strange, I'd normally expect all of the resources created by the helm chart to have this same app name. However, I tried to keep things as passive as possible.

I did consider creating a separate helper function that was metaLabelsWithoutAppName that would be re-used by the new helper and could be used here and have the app and app.kubernetes.io/name be set here explicity. I favored this approach but am happy to change things up

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, not the nicest include I've seen, but it helps keeping the labels consistent and may not be worth to add an extra helper.

Copy link
Member

@DrPsychick DrPsychick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@DrPsychick DrPsychick merged commit d4a06ec into gomods:main Sep 28, 2023
1 check passed
@DrPsychick
Copy link
Member

Thank you for your contribution and effort put in @alex-bezek, unfortunately I overlooked that this PR did not bump the chart version to trigger a release, so I just merged #46 to take care of that.

@alex-bezek
Copy link
Contributor Author

thanks @DrPsychick and @michalpristas !

@alex-bezek alex-bezek deleted the standard-labels branch September 29, 2023 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Recommended K8s Labels
3 participants