Kustomize — rise and shine

D. Heinrich
2 min readJan 22, 2021

--

I try to help you here with kustomize issues I had and how got along with them. You can follow for more updates in near future

by https://ordina-jworks.github.io/

Basic Preparations

$ brew install kustomize
[...]
$ mkdir -p myrepo/base myrepo/sonarqube/
$ cd myrepo
$ kustomize init

Case 1: Preparation

Replace your ingress hostname with kustomize configMapGenerator + vars

base/sonarqube/ingress.yaml

  • Define your variables like $(SONARQUBE_EXTERNAL_URL) which should be replaced later on by kustomize
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: sonarqube-ingress
labels:
app.kubernetes.io/name: sonarqube
app.kubernetes.io/instance: default
app.kubernetes.io/version: default
annotations: {}
namespace: default
spec:
# TLS -> can be ignored for now
# tls:
# - hosts:
# - $(SONARQUBE_EXTERNAL_URL)
# secretName: testsecret-tls

rules:
- host: $(SONARQUBE_EXTERNAL_URL)
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: $(SONARQUBE_SERVICE)
port:
number: 9000

base/sonarqube/ingress.yaml

apiVersion: v1
kind: Service
metadata:
name: sonarqube-svc
namespace: default
labels:
app.kubernetes.io/name: sonarqube
app.kubernetes.io/component: java
annotations: {}
spec:
selector:
app.kubernetes.io/name: sonarqube
ports:
- port: 9000
protocol: TCP
targetPort: 9000

kustomize.yaml

  • define the resources we’d like to import from base
  • describe vars you’d like to make usable in your kustomize yamls
    NOTE: kustomize searches for $(<MyVarName e.g.: SONARQUBE_SERVICE>)
  • The configMapGenerator will provide us with our literals as parameters which we then can reference from vars or other places
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- base/sonarqube/service.yaml
- base/sonarqube/ingress.yaml
#
# VARIABLES
#
vars:
- name: SONARQUBE_SERVICE
objref:
apiVersion: v1
kind: Service
name: sonarqube-svc
fieldref:
fieldpath: metadata.name
- name: SONARQUBE_EXTERNAL_URL
objref:
apiVersion: v1
kind: ConfigMap
name: test
fieldref:
fieldpath: data.SONARQUBE_EXTERNAL_URL
#
# CONFIGMAP-GENERATOR
#
generatorOptions:
disableNameSuffixHash: true
configMapGenerator:
- name: test
literals:
- SONARQUBE_EXTERNAL_URL=sonarqube.apps.example.com
- POSTGRES_PORT=5432
- SONARQUBE_PORT=9000
#
# ADDITIONAL-CONFIG
#
configurations:
- sonarqube/lookup.yaml

sonarqube/lookup.yaml

  • Here we store the “paths” where kustomize should look after variabels which should be replaced
varReference:
- kind: Ingress
path: spec/rules[]/http/paths[]/backend/service/name
- kind: Ingress
path: spec/rules[]/hosts
- kind: Ingress
path: spec/tls[]/hosts[]

Case 1: Rendering/Building

  • If you followed carefully then your result should equal the following
$ kustomize build --load_restrictor none
apiVersion: v1
data:
POSTGRES_PORT: "5432"
SONARQUBE_EXTERNAL_URL: sonarqube.apps.example.com
SONARQUBE_PORT: "9000"
kind: ConfigMap
metadata:
labels:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/part-of: sonarqube
name: test
namespace: default
---
apiVersion: v1
kind: Service
metadata:
labels:
app.kubernetes.io/component: java
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: sonarqube
app.kubernetes.io/part-of: sonarqube
name: sonarqube-svc
namespace: default
spec:
ports:
- port: 9000
protocol: TCP
targetPort: 9000
selector:
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: sonarqube
app.kubernetes.io/part-of: sonarqube
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
app.kubernetes.io/instance: default
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/name: sonarqube
app.kubernetes.io/part-of: sonarqube
app.kubernetes.io/version: default
name: sonarqube-ingress
namespace: default
spec:
rules:
- host: sonarqube.apps.example.com
http:
paths:
- backend:
service:
name: sonarqube-svc
port:
number: 9000
path: /
pathType: Prefix

Stay tuned for more!

Cheers!

Sources

--

--

D. Heinrich
D. Heinrich

Written by D. Heinrich

Working as a Head of Infrastructure at Flower Labs.

No responses yet