[helm] 샘플 차트 만들기

zzery·2022년 10월 30일

일지(2022~2024)

목록 보기
22/25

argocd에서 applications 여러 개를 쉽게 생성하는 용도로 applicationSet이 있다.
아직 설정 override 측면에서 기능이 덜 제공돼서 (syncPolicy 설정 바꾸는게 제한적)
아예 application을 여러 개 배포할 수 있는 헬름 차트를 만들어보려고 한다.

공개된 application 차트가 있다.

이 차트는 리소스 형태를 그대로 list로 받아와서 만들었다.

❯ helm create common_app

❯ cd common_app
❯ tree .
.
├── Chart.yaml
├── README.md
├── charts
├── templates
│   └── applications.yaml
└── values.yaml

# test
❯ helm lint .
==> Linting .
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed

# test template
❯ helm template .
---
# Source: common_app/templates/applications.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  labels:
    app: default-app-0
  name: app-0
  namespace: argocd
spec:
  project: default
  source:
    repoURL: ...
    targetRevision: main
    path: default/app-0
  destination:
    server: https://kubernetes.default.svc
    namespace: default
---
# Source: common_app/templates/applications.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  labels:
    app: default-app-3
  name: app-3
  namespace: argocd
spec:
  project: default
  source:
    repoURL: ...
    targetRevision: main
    path: default/app-3
  destination:
    server: https://kubernetes.default.svc
    namespace: default
  syncPolicy:
    automated:
      prune: false
      selfHeal: false

❯ helm package .
❯ helm repo index .

❯ tree  
.
├── 0.1.0
│   ├── common_app-0.1.0.tgz
│   └── index.yaml
...

최종 결과는 이렇게 관리되도록 바꿨다.

commonSettings:
  namespace: default
  project: default
  repoURL: ...
  targetRevision: main
  # server: https://kubernetes.default.svc
  finalizers: {}

customSettings:
  - name: app-0
    path: "default/app-0"
    syncPolicy: {}
    # additionalAnnotations: {}
    # additionalLabels: {}
  - name: app-3
    path: "default/app-3"
    syncPolicy:
      automated:
        prune: false
        selfHeal: false
    # additionalAnnotations: {}
    # additionalLabels: {}
      
  # ...

템플릿

{{- $namespace := .Values.commonSettings.namespace -}}
{{- $project := .Values.commonSettings.project -}}
{{- $repoURL := .Values.commonSettings.repoURL -}}
{{- $targetRevision := .Values.commonSettings.targetRevision -}}
{{- $server := .Values.commonSettings.server -}}
{{- $finalizers := .Values.commonSettings.finalizers -}}
{{- range .Values.customSettings }}
---
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  {{- with .additionalAnnotations }}
  annotations:
    {{- range $key, $value := . }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
  {{- end }}
  labels:
    app: {{ $namespace }}-{{ .name }}
    {{- with .additionalLabels }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
  name: {{ .name }}
  namespace: argocd
  {{- with $finalizers }}
  finalizers:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  project: {{ tpl $project $ }}
  source:
    repoURL: {{ $repoURL }}
    targetRevision: {{ $targetRevision }}
    path: {{ .path }}
  destination:
    server: {{ default "https://kubernetes.default.svc" $server }}
    namespace: {{ $namespace }}
  {{- with .syncPolicy }}
  syncPolicy:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .ignoreDifferences }}
  ignoreDifferences:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  {{- with .info }}
  info:
    {{- toYaml . | nindent 4 }}
  {{- end }}
{{- end }}

여기까지 결과

profile
이 블로그의 모든 글은 수제로 짜여져 있습니다...

0개의 댓글