Skip to content

Commit

Permalink
feat: bootstrap service monitor (#5)
Browse files Browse the repository at this point in the history
* feat: added template configuration for service monitors

* docs: added note that ServiceMonitor resources are created as well

* fix: align service monitor endpoints value source with property key

* chore: align property key with template config
  • Loading branch information
leonsteinhaeuser authored Jan 9, 2024
1 parent 50fe021 commit 99edfbd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# helm-plugin-bootstrap

A plugin for [Helm](https://helm.sh/) to add additional files to your Helm chart. This plugin adds resources such as `PodDisruptionBudget`, `NetworkPoliciy` in the same way as the other files are created during helm create.
A plugin for [Helm](https://helm.sh/) to add additional files to your Helm chart. This plugin adds resources such as `PodDisruptionBudget`, `NetworkPoliciy`, `ServiceMonitor` in the same way as the other files are created during helm create.

## Installation

Expand Down
23 changes: 15 additions & 8 deletions internal/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,30 @@ func Bootstrap(chartLocation string, force bool) error {

files := []struct {
path string
content []byte
content string
propertyKey string
values []byte
values string
}{
{
// pdb.yaml
path: filepath.Join(templateFolderLocation, PodDisruptionBudgetFileName),
content: []byte(fmt.Sprintf(pdbTemplate, metadata.Name)),
content: pdbTemplate,
propertyKey: "pdb",
values: []byte(pdbValuesYaml),
values: pdbValuesYaml,
},
{
// networkpolicy.yaml
path: filepath.Join(templateFolderLocation, NetworkPolicyFileName),
content: []byte(fmt.Sprintf(networkPolicy, metadata.Name)),
content: networkPolicyTemplate,
propertyKey: "networkPolicy",
values: []byte(networkPolicyValuesYaml),
values: networkPolicyValuesYaml,
},
{
// servicemonitor.yaml
path: filepath.Join(templateFolderLocation, ServiceMonitorFileName),
content: serviceMonitorTemplate,
propertyKey: "metrics",
values: serviceMonitorValuesYaml,
},
}
// write files
Expand All @@ -131,12 +138,12 @@ func Bootstrap(chartLocation string, force bool) error {
// There is no handle to a preferred output stream here.
fmt.Fprintf(os.Stderr, "WARNING: File %q already exists. Overwriting.\n", file.path)
}
if err := os.WriteFile(file.path, file.content, 0644); err != nil {
if err := os.WriteFile(file.path, []byte(fmt.Sprintf(file.content, metadata.Name, file.propertyKey)), 0644); err != nil {
return err
}
// write values.yaml if there is content
if len(file.values) > 0 && (!bytes.Contains(valuesData, []byte(file.propertyKey)) || force) {
valuesData = append(valuesData, []byte(file.values)...)
valuesData = append(valuesData, []byte(fmt.Sprintf(file.values, file.propertyKey))...)
}
}
// write values.yaml
Expand Down
66 changes: 53 additions & 13 deletions internal/bootstrap/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,36 @@ const (
PodDisruptionBudgetFileName = "pdb.yaml"
// NetworkPolicyFileName is the name of the file that will be created in the templates folder
NetworkPolicyFileName = "networkpolicy.yaml"
// ServiceMonitorFileName is the name of the file that will be created in the templates folder
ServiceMonitorFileName = "servicemonitor.yaml"
)

const pdbTemplate = `{{- if .Values.pdb.enabled }}
// manifest templates

const pdbTemplate = `{{- if .Values.%[2]s.enabled }}
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: {{ include "%[1]s.fullname" . }}
labels:
{{- include "%[1]s.labels" . | nindent 4 }}
{{- with .Values.pdb.annotations }}
{{- with .Values.%[2]s.annotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
{{- with .Values.pdb.maxUnavailable }}
{{- with .Values.%[2]s.maxUnavailable }}
maxUnavailable: {{ . }}
{{- end }}
{{- with .Values.pdb.minAvailable }}
{{- with .Values.%[2]s.minAvailable }}
minAvailable: {{ . }}
{{- end }}
selector:
matchLabels:
{{- include "%[1]s.selectorLabels" . | nindent 6 }}
{{- include "%[1]s.selectorLabels" . | nindent 6 }}
{{- end }}
`

const networkPolicy = `{{- if .Values.networkPolicy.enabled }}
const networkPolicyTemplate = `{{- if .Values.%[2]s.enabled }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
Expand All @@ -43,32 +46,59 @@ spec:
matchLabels:
{{- include "%[1]s.selectorLabels" . | nindent 6 }}
policyTypes:
{{- if .Values.networkPolicy.ingress }}
{{- if .Values.%[2]s.ingress }}
- Ingress
{{- end }}
{{- if .Values.networkPolicy.egress }}
{{- if .Values.%[2]s.egress }}
- Egress
{{- end }}
{{- with .Values.networkPolicy.ingress }}
{{- with .Values.%[2]s.ingress }}
ingress:
{{- toYaml . | nindent 4 }}
{{- end }}
{{- with .Values.networkPolicy.egress }}
{{- with .Values.%[2]s.egress }}
egress:
{{- toYaml . | nindent 4 }}
{{- end -}}
{{- end }}
`
const serviceMonitorTemplate = `{{- if and .Values.%[1]s.enabled .Values.%[1]s.serviceMonitor.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ template "%[1]s.fullname" . }}
{{- if .Values.%[2]s.serviceMonitor.namespace }}
namespace: {{ .Values.%[2]s.serviceMonitor.namespace }}
{{- end }}
labels:
{{- include "%[1]s.labels" . | nindent 4 }}
spec:
endpoints:
- port: {{ .Values.service.port }}
path: {{ .Values.%[2]s.serviceMonitor.metricsPath }}
{{- with .Values.%[2]s.serviceMonitor.interval }}
interval: {{ . }}
{{- end }}
{{- with .Values.%[2]s.serviceMonitor.scrapeTimeout }}
scrapeTimeout: {{ . }}
{{- end }}
selector:
matchLabels:
{{- include "%[1]s.selectorLabels" . | nindent 6 }}
{{- end }}
`

// values.yaml configurations

const pdbValuesYaml = `
pdb:
%[1]s:
enabled: true
annotations: {}
minAvailable: 1
maxUnavailable: 0
`
const networkPolicyValuesYaml = `
networkPolicy:
%[1]s:
enabled: false
ingress: []
# - from:
Expand All @@ -93,3 +123,13 @@ networkPolicy:
# - protocol: UDP
# port: 53
`
const serviceMonitorValuesYaml = `
%[1]s:
enabled: false
serviceMonitor:
enabled: false
metricsPath: /metrics
namespace: ""
interval: ""
scrapeTimeout: ""
`

0 comments on commit 99edfbd

Please sign in to comment.