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

Adding Configmap handling to deployments #419

Merged
merged 12 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/integration-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,8 @@ jobs:
npm install -g [email protected]
ajv validate -s test/update_dry_run_schema.json -d test/temp/update_dry_run.json
- run: ./draft -v update -d ./langtest/ -a webapp_routing --variable ingress-tls-cert-keyvault-uri=test.cert.keyvault.uri --variable ingress-use-osm-mtls=true --variable ingress-host=host1
- name: print manifests
run: cat ./langtest/manifests/*
- name: start minikube
id: minikube
uses: medyagh/setup-minikube@master
Expand Down Expand Up @@ -2238,7 +2240,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- name: Execute Dry Run with config file
run: |
Expand Down Expand Up @@ -2273,7 +2275,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- run: rm -rf ./langtest/manifests && rm -f ./langtest/Dockerfile ./langtest/.dockerignore
- run: ./draft -v create -c ./test/integration/ruby/helm.yaml -d ./langtest/
Expand Down Expand Up @@ -2379,7 +2381,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- name: Execute Dry Run with config file
run: |
Expand Down Expand Up @@ -2414,7 +2416,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- run: rm -rf ./langtest/manifests && rm -f ./langtest/Dockerfile ./langtest/.dockerignore
- run: ./draft -v create -c ./test/integration/ruby/kustomize.yaml -d ./langtest/
Expand Down Expand Up @@ -2511,7 +2513,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- name: Execute Dry Run with config file
run: |
Expand Down Expand Up @@ -2546,7 +2548,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- run: rm -rf ./langtest/manifests && rm -f ./langtest/Dockerfile ./langtest/.dockerignore
- run: ./draft -v create -c ./test/integration/ruby/manifest.yaml -d ./langtest/
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/integration-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- run: Remove-Item ./langtest/manifests -Recurse -Force -ErrorAction Ignore
- run: Remove-Item ./langtest/Dockerfile -ErrorAction Ignore
Expand Down Expand Up @@ -601,7 +601,7 @@ jobs:
- run: mkdir ./langtest
- uses: actions/checkout@v3
with:
repository: OliverMKing/ruby-hello-world
repository: davidgamero/sinatra-hello-world
path: ./langtest
- run: Remove-Item ./langtest/manifests -Recurse -Force -ErrorAction Ignore
- run: Remove-Item ./langtest/Dockerfile -ErrorAction Ignore
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmdhelpers/workflow_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func setDeploymentContainerImage(filePath, productionImage string) error {

printer := printers.YAMLPrinter{}

out, err := os.OpenFile(filePath, os.O_RDWR, 0755)
out, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/draftconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
const draftConfigFile = "draft.yaml"

type VariableValidator func(string) error
type VariableTransformer func(string) (string, error)
type VariableTransformer func(string) (any, error)

type DraftConfig struct {
TemplateName string `yaml:"templateName"`
Expand Down Expand Up @@ -99,7 +99,7 @@ func (d *DraftConfig) GetVariable(name string) (*BuilderVar, error) {
return nil, fmt.Errorf("variable %s not found", name)
}

func (d *DraftConfig) GetVariableValue(name string) (string, error) {
func (d *DraftConfig) GetVariableValue(name string) (any, error) {
for _, variable := range d.Variables {
if variable.Name == name {
if variable.Value == "" {
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/draftconfig_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ var validVariableKinds = map[string]bool{
"containerImageVersion": true,
"dirPath": true,
"dockerFileName": true,
"envVarMap": true,
"filePath": true,
"flag": true,
"helmChartOverrides": true,
"ingressHostName": true,
"kubernetesNamespace": true,
"kubernetesProbePeriod": true,
"kubernetesProbeTimeout": true,
"kubernetesProbeThreshold": true,
"kubernetesProbeDelay": true,
"kubernetesResourceLimit": true,
"kubernetesResourceName": true,
"kubernetesResourceRequest": true,
"label": true,
"port": true,
"repositoryBranch": true,
Expand Down
19 changes: 17 additions & 2 deletions pkg/config/transformers/transformers.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package transformers

func GetTransformer(variableKind string) func(string) (string, error) {
import (
"encoding/json"
"fmt"
)

func GetTransformer(variableKind string) func(string) (any, error) {
switch variableKind {
case "envVarMap":
return EnvironmentVariableMapTransformer
default:
return DefaultTransformer
}
}

func DefaultTransformer(inputVar string) (string, error) {
func EnvironmentVariableMapTransformer(inputVar string) (any, error) {
var inputVarMap map[string]string
if err := json.Unmarshal([]byte(inputVar), &inputVarMap); err != nil {
return "", fmt.Errorf("failed to unmarshal variable as map[string]string: %s", err)
}
return inputVarMap, nil
}

func DefaultTransformer(inputVar string) (any, error) {
return inputVar, nil
}
14 changes: 14 additions & 0 deletions pkg/config/validators/validators.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package validators

import (
"encoding/json"
"fmt"
)

func GetValidator(variableKind string) func(string) error {
switch variableKind {
case "envVarMap":
return KeyValueMapValidator
default:
return DefaultValidator
}
}

func KeyValueMapValidator(input string) error {
if err := json.Unmarshal([]byte(input), &map[string]string{}); err != nil {
return fmt.Errorf("failed to unmarshal variable as map[string]string: %s", err)
}
return nil
}

func DefaultValidator(input string) error {
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
{{- define "testapp.labels" -}}
helm.sh/chart: {{ include "testapp.chart" . }}
{{ include "testapp.selectorLabels" . }}
kubernetes.azure.com/generator: {{ .Values.generatorLabel }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
Expand Down
11 changes: 11 additions & 0 deletions pkg/fixtures/deployments/helm/charts/templates/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "testapp.fullname" . }}-config
labels:
{{- include "testapp.labels" . | nindent 4 }}
namespace: {{ .Values.namespace }}
data:
{{- range $key, $value := .Values.envVars }}
{{ $key }}: {{ $value }}
{{- end }}
16 changes: 9 additions & 7 deletions pkg/fixtures/deployments/helm/charts/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ metadata:
name: {{ include "testapp.fullname" . }}
labels:
{{- include "testapp.labels" . | nindent 4 }}
kubernetes.azure.com/generator: {{ .Values.generatorLabel }}
namespace: {{ .Values.namespace }}
spec:
{{- if not .Values.autoscaling.enabled }}
Expand Down Expand Up @@ -40,15 +39,14 @@ spec:
containerPort: {{ .Values.containerPort }}
protocol: TCP
livenessProbe:
httpGet:
path: /
port: http
{{- toYaml .Values.livenessProbe | nindent 12 }}
readinessProbe:
httpGet:
path: /
port: http
{{- toYaml .Values.readinessProbe | nindent 12 }}
resources:
{{- toYaml .Values.resources | nindent 12 }}
envFrom:
- configMapRef:
name: {{ include "testapp.fullname" . }}-config
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand All @@ -57,6 +55,10 @@ spec:
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.topologySpreadConstraints }}
topologySpreadConstraints:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ metadata:
name: {{ include "testapp.fullname" . }}
labels:
{{- include "testapp.labels" . | nindent 4 }}
kubernetes.azure.com/generator: {{.Values.generatorLabel}}
annotations:
{{ toYaml .Values.service.annotations | nindent 4 }}
namespace: {{ .Values.namespace }}
Expand Down
78 changes: 60 additions & 18 deletions pkg/fixtures/deployments/helm/charts/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,30 @@ image:
tag: latest
pullPolicy: Always


imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

podAnnotations: {}

podSecurityContext: {}
# fsGroup: 2000

securityContext: {}
# capabilities:
# drop:
# - ALL
# readOnlyRootFilesystem: true
# runAsNonRoot: true
# runAsUser: 1000

service:
annotations: {}
type: LoadBalancer
port: 80

resources: {}
resources:
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi
limits:
cpu: "2"
memory: "1Gi"
requests:
cpu: "1"
memory: "512Mi"

autoscaling:
enabled: false
Expand All @@ -54,10 +44,62 @@ autoscaling:
targetCPUUtilizationPercentage: 80
# targetMemoryUtilizationPercentage: 80

livenessProbe:
tcpSocket:
port: 80
readinessProbe:
tcpSocket:
port: 80
periodSeconds: 5
timeoutSeconds: 5
failureThreshold: 1
successThreshold: 1
initialDelaySeconds: 3

nodeSelector: {}

tolerations: []

affinity: {}
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app.kubernetes.io/name: testapp

affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
topologyKey: kubernetes.io/hostname
labelSelector:
matchLabels:
app.kubernetes.io/name: testapp

securityContext:
seccompProfile:
type: RuntimeDefault
capabilities:
drop:
- ALL
add:
bfoley13 marked this conversation as resolved.
Show resolved Hide resolved
- SETPCAP
- MKNOD
- AUDIT_WRITE
- CHOWN
- DAC_OVERRIDE
- FOWNER
- FSETID
- KILL
- SETGID
- SETUID
- NET_BIND_SERVICE
- SYS_CHROOT
- SETFCAP
- SYS_PTRACE

envVars:

generatorLabel: draft
9 changes: 9 additions & 0 deletions pkg/fixtures/deployments/kustomize/base/configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: testapp-config
namespace: default
labels:
app.kubernetes.io/name: testapp
kubernetes.azure.com/generator: draft
data:
Loading
Loading