From 492e06f2afe0acda1cf83fd359c8a3ba0146f6ef Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Fri, 17 Nov 2023 20:21:23 -0300 Subject: [PATCH] Refactor --- .gitignore | 3 ++- README.md | 49 ++++++++++++++++++++++++++++++++++ helm/templates/deployment.yaml | 29 +++++++++++--------- helm/templates/ingress.yaml | 4 +-- helm/templates/service.yaml | 4 +-- helm/values.yaml | 29 +++++++------------- 6 files changed, 80 insertions(+), 38 deletions(-) create mode 100644 README.md diff --git a/.gitignore b/.gitignore index d94a444..5c9d101 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .venv old-helm -old \ No newline at end of file +old +consignas.txt \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a1f20b8 --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +Todas las variables que se repiten y que, en un principio, no tendría sentido cambiarlas para este chart se marcaron con default. + +Por ejemplo, busque los default de service.yaml. Verá que `type` es por defecto "ClusterIP" y que `targetPort` es por defecto `$port`. + +Esto tiene dos beneficios: + +1) Elimina redundancia en values.yaml +2) Da libertad para que en un futuro se pueda simplemente modificarlo en `values.yaml`. Por ejemplo, supongamos que queremos cambiar el `type` a "NodePort" en el servicio de `api`, basta con hacer el siguiente cambio: + +```yaml +services: + - api: + name: "api" + tier: "backend" + port: 5000 + type: "NodePort" +``` + +---------- + +En `ingress` se repite el `host` por dos razones: + +1) Si quisieran que la api esté en otro dominio se pueda hacer fácilmente mediante: + +```yaml +ingress: + annotations: + nginx.ingress.kubernetes.io/ssl-redirect: "true" + hosts: + - host: kube.slc.ar + paths: + - path: / + name: "client" + port: 8080 + pathType: "Prefix" + - host: api.kube.slc.ar + - path: / + name: "api" + port: 5000 + pathType: "Prefix" + tls: + - secretName: exam-crt + hosts: + - kube.slc.ar + - api.kube.slc.ar +``` +2) No se pueden reutilizar variables en YAML. Existe la posibilidad de usar YAML anchors pero en la [documentación](https://helm.sh/docs/chart_template_guide/yaml_techniques/#yaml-anchors) no lo recomiendan: "Because Helm and Kubernetes often read, modify, and then rewrite YAML files, the anchors will be lost." + +TODO: probar igual los anchors diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml index c4d32a6..0d436f9 100644 --- a/helm/templates/deployment.yaml +++ b/helm/templates/deployment.yaml @@ -8,7 +8,7 @@ metadata: app: {{ .name }} {{- include "exam.labels" $ | nindent 4 }} spec: - replicas: {{ .replicas }} + replicas: {{ default "1" .replicas }} selector: matchLabels: app: {{ .name }} @@ -32,19 +32,22 @@ spec: {{- if .env }} env: {{- with .env }} - {{- range $key, $val := .nonsecrets }} - {{- $envKey := $key | upper | replace "-" "_" }} - - name: {{ $envKey }} - value: {{ quote $val }} + {{- range $key, $val := . }} + {{- if ne "secrets" $key }} + {{- $envKey := $key | upper | replace "-" "_" }} + - name: {{ $envKey }} + value: {{ quote $val }} + {{- else }} + {{- range $key, $val := $val }} + {{- $envKey := $key | upper | replace "-" "_" }} + - name: {{ $envKey }} + valueFrom: + secretKeyRef: + # TODO + name: {{ include "exam.fullname" $ }}-secrets + key: {{ $val }} + {{- end }} {{- end }} - {{- range $key, $val := .secrets }} - {{- $envKey := $key | upper | replace "-" "_" }} - - name: {{ $envKey }} - valueFrom: - secretKeyRef: - # TODO - name: {{ include "exam.fullname" $ }}-secrets - key: {{ $val }} {{- end }} {{- end }} {{- end }} diff --git a/helm/templates/ingress.yaml b/helm/templates/ingress.yaml index cfe3064..25f376f 100644 --- a/helm/templates/ingress.yaml +++ b/helm/templates/ingress.yaml @@ -41,8 +41,8 @@ spec: paths: {{- range .paths }} - path: {{ .path }} - {{- if and .pathType (semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion) }} - pathType: {{ .pathType }} + {{- if semverCompare ">=1.18-0" $.Capabilities.KubeVersion.GitVersion }} + pathType: {{ default "ImplementationSpecific" .pathType }} {{- end }} backend: {{- if semverCompare ">=1.19-0" $.Capabilities.KubeVersion.GitVersion }} diff --git a/helm/templates/service.yaml b/helm/templates/service.yaml index a7ba04e..280da3d 100644 --- a/helm/templates/service.yaml +++ b/helm/templates/service.yaml @@ -8,10 +8,10 @@ metadata: app: {{ $service.name }} {{- include "exam.labels" $ | nindent 4 }} spec: - type: {{ $service.type }} + type: {{ default "ClusterIP" $service.type }} ports: - port: {{ $service.port }} - targetPort: {{ $service.target }} + targetPort: {{ default $service.port $service.target }} selector: app: {{ $service.name }} tier: {{ $service.tier }} diff --git a/helm/values.yaml b/helm/values.yaml index 7f7cd80..ebfa052 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -3,7 +3,6 @@ tls: host: kube.slc.ar ingress: - className: "" annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 nginx.ingress.kubernetes.io/ssl-redirect: "true" @@ -12,11 +11,9 @@ ingress: - host: kube.slc.ar paths: - path: /()(.*) - pathType: ImplementationSpecific name: "client" port: 8080 - path: /api(/|$)(.*) - pathType: ImplementationSpecific name: "api" port: 5000 tls: @@ -29,21 +26,17 @@ services: name: "api" tier: "backend" port: 5000 - target: 0 - type: ClusterIP - - postgres: - name: "db" - tier: "backend" - port: 5432 - target: 0 - type: ClusterIP - client: name: "client" tier: "frontend" port: 8080 target: 80 - type: ClusterIP + - postgres: + name: "db" + tier: "backend" + port: 5432 +# secrets.yaml secrets: username: "username" password: "password1234" @@ -58,16 +51,14 @@ deployments: name: "api" tier: "backend" env: - nonsecrets: - app-settings: src.config.ProductionConfig - port: "5000" + app-settings: src.config.ProductionConfig + port: "5000" secrets: database-url: database-url image: repository: api tag: prod port: 5000 - replicas: 1 - client: tier: "frontend" name: "client" @@ -75,13 +66,12 @@ deployments: repository: client tag: prod port: 8080 - replicas: 1 + replicas: 3 - db: tier: "backend" name: "db" env: - nonsecrets: - pgdata: /var/lib/postgresql/data/pgdata + pgdata: /var/lib/postgresql/data/pgdata secrets: postgres-password: password postgres-user: username @@ -89,6 +79,5 @@ deployments: repository: db tag: 13.3 port: 5432 - replicas: 1 mountPath: /var/lib/postgresql/data storage: postgres-pv-storage