From 6e670b4f20d5ea08b26ece6cd4a6ec03a15fa6df Mon Sep 17 00:00:00 2001 From: Brandon Foley Date: Tue, 29 Oct 2024 14:35:57 -0400 Subject: [PATCH] updating templates --- pkg/config/draftconfig_template_test.go | 8 +++++ pkg/fixtures/manifests/hpa/hpa.yaml | 22 ++++++++++++ pkg/fixtures/manifests/pdb/pdb.yaml | 13 +++++++ pkg/fixtures/manifests/service/service.yaml | 16 +++++++++ pkg/handlers/template_test.go | 36 +++++++++++++++++++ .../manifests}/draft.yaml | 11 +++++- .../manifests/hpa.yaml | 22 ++++++++++++ .../manifest/hpa.yaml | 22 ------------ .../PodDisruptionBudget/manifest/pdb.yaml | 13 ------- .../{manifest => manifests}/draft.yaml | 8 ++++- .../PodDisruptionBudget/manifests/pdb.yaml | 13 +++++++ .../Service/{ => manifests}/draft.yaml | 9 ++++- .../manifests/Service/manifests/service.yaml | 16 +++++++++ template/manifests/Service/service.yaml | 16 --------- 14 files changed, 171 insertions(+), 54 deletions(-) create mode 100644 pkg/fixtures/manifests/hpa/hpa.yaml create mode 100644 pkg/fixtures/manifests/pdb/pdb.yaml create mode 100644 pkg/fixtures/manifests/service/service.yaml rename template/manifests/{HorizontalPodAutoscaling/manifest => HorizontalPodAutoscaler/manifests}/draft.yaml (83%) create mode 100644 template/manifests/HorizontalPodAutoscaler/manifests/hpa.yaml delete mode 100644 template/manifests/HorizontalPodAutoscaling/manifest/hpa.yaml delete mode 100644 template/manifests/PodDisruptionBudget/manifest/pdb.yaml rename template/manifests/PodDisruptionBudget/{manifest => manifests}/draft.yaml (80%) create mode 100644 template/manifests/PodDisruptionBudget/manifests/pdb.yaml rename template/manifests/Service/{ => manifests}/draft.yaml (81%) create mode 100644 template/manifests/Service/manifests/service.yaml delete mode 100644 template/manifests/Service/service.yaml diff --git a/pkg/config/draftconfig_template_test.go b/pkg/config/draftconfig_template_test.go index 83bc06b5..ec661142 100644 --- a/pkg/config/draftconfig_template_test.go +++ b/pkg/config/draftconfig_template_test.go @@ -3,6 +3,7 @@ package config import ( "fmt" "io/fs" + "regexp" "strings" "testing" @@ -10,6 +11,8 @@ import ( "github.com/stretchr/testify/assert" ) +const alphaNumUnderscoreHyphen = "^[A-Za-z][A-Za-z0-9-_]{1,62}[A-Za-z0-9]$" + var allTemplates = map[string]*DraftConfig{} var validTemplateTypes = map[string]bool{ @@ -67,6 +70,7 @@ func TestTempalteValidation(t *testing.T) { } func loadTemplatesWithValidation() error { + regexp := regexp.MustCompile(alphaNumUnderscoreHyphen) return fs.WalkDir(template.Templates, ".", func(path string, d fs.DirEntry, err error) error { if err != nil { return err @@ -93,6 +97,10 @@ func loadTemplatesWithValidation() error { return fmt.Errorf("template %s has no template name", path) } + if !regexp.MatchString(currTemplate.TemplateName) { + return fmt.Errorf("template %s name must match the alpha-numeric-underscore-hyphen regex: %s", path, currTemplate.TemplateName) + } + if _, ok := allTemplates[strings.ToLower(currTemplate.TemplateName)]; ok { return fmt.Errorf("template %s has a duplicate template name", path) } diff --git a/pkg/fixtures/manifests/hpa/hpa.yaml b/pkg/fixtures/manifests/hpa/hpa.yaml new file mode 100644 index 00000000..dd13b9cd --- /dev/null +++ b/pkg/fixtures/manifests/hpa/hpa.yaml @@ -0,0 +1,22 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: test-app + labels: + app.kubernetes.io/name: test-app + app.kubernetes.io/part-of: test-app-project + kubernetes.azure.com/generator: draft +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: test-app + minReplicas: 2 + maxReplicas: 5 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 \ No newline at end of file diff --git a/pkg/fixtures/manifests/pdb/pdb.yaml b/pkg/fixtures/manifests/pdb/pdb.yaml new file mode 100644 index 00000000..40e9063f --- /dev/null +++ b/pkg/fixtures/manifests/pdb/pdb.yaml @@ -0,0 +1,13 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: test-app + labels: + app.kubernetes.io/name: test-app + app.kubernetes.io/part-of: test-app-project + kubernetes.azure.com/generator: draft +spec: + maxUnavailable: 1 + selector: + matchLabels: + app: test-app \ No newline at end of file diff --git a/pkg/fixtures/manifests/service/service.yaml b/pkg/fixtures/manifests/service/service.yaml new file mode 100644 index 00000000..802987ce --- /dev/null +++ b/pkg/fixtures/manifests/service/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: test-app + labels: + app.kubernetes.io/name: test-app + app.kubernetes.io/part-of: test-app-project + kubernetes.azure.com/generator: draft +spec: + type: ClusterIP + selector: + app: test-app + ports: + - protocol: TCP + port: 80 + targetPort: 80 \ No newline at end of file diff --git a/pkg/handlers/template_test.go b/pkg/handlers/template_test.go index 02d3946d..29d3bb15 100644 --- a/pkg/handlers/template_test.go +++ b/pkg/handlers/template_test.go @@ -422,6 +422,42 @@ func TestTemplateHandlerValidation(t *testing.T) { }, expectedErr: fmt.Errorf("invalid label: *myTestApp"), }, + { + name: "valid hpa manifest", + templateName: "horizontalPodAutoscaler-manifests", + fixturesBaseDir: "../fixtures/manifests/hpa", + version: "0.0.1", + dest: ".", + templateWriter: &writers.FileMapWriter{}, + varMap: map[string]string{ + "APPNAME": "test-app", + "PARTOF": "test-app-project", + }, + }, + { + name: "valid pdb manifest", + templateName: "podDisruptionBudget-manifests", + fixturesBaseDir: "../fixtures/manifests/pdb", + version: "0.0.1", + dest: ".", + templateWriter: &writers.FileMapWriter{}, + varMap: map[string]string{ + "APPNAME": "test-app", + "PARTOF": "test-app-project", + }, + }, + { + name: "valid service manifest", + templateName: "service-manifests", + fixturesBaseDir: "../fixtures/manifests/service", + version: "0.0.1", + dest: ".", + templateWriter: &writers.FileMapWriter{}, + varMap: map[string]string{ + "APPNAME": "test-app", + "PARTOF": "test-app-project", + }, + }, } for _, tt := range tests { diff --git a/template/manifests/HorizontalPodAutoscaling/manifest/draft.yaml b/template/manifests/HorizontalPodAutoscaler/manifests/draft.yaml similarity index 83% rename from template/manifests/HorizontalPodAutoscaling/manifest/draft.yaml rename to template/manifests/HorizontalPodAutoscaler/manifests/draft.yaml index 85aa7fb0..c2dcc98e 100644 --- a/template/manifests/HorizontalPodAutoscaling/manifest/draft.yaml +++ b/template/manifests/HorizontalPodAutoscaler/manifests/draft.yaml @@ -1,42 +1,51 @@ -templateName: "horizontalPodAutoscaling-manifest" +templateName: "horizontalPodAutoscaler-manifests" description: "This template is used to create a horizontalPodAutoscaling for an application" +versions: "0.0.1" +defaultVersion: "0.0.1" type: "manifest" variables: - name: "APPNAME" type: "string" kind: "kubernetesResourceName" description: "the name of the application" + versions: ">=0.0.1" - name: "PARTOF" type: "string" kind: "label" description: "the label to identify which project the resource belong to" + versions: ">=0.0.1" - name: "GENERATORLABEL" type: "string" kind: "label" description: "the label to identify who generated the resource" + versions: ">=0.0.1" default: value: "draft" - name: "MINIMUMREPLICAS" type: "int" kind: "replicaCount" description: "specifies the minimum number of pod replicas that the deployment should have" + versions: ">=0.0.1" default: value: 2 - name: "MAXIMUMREPLICAS" type: "int" kind: "replicaCount" description: "defines the maximum number of pod replicas the deployment can scale to" + versions: ">=0.0.1" default: value: 5 - name: "RESOURCETYPE" type: "string" kind: "scalingResourceType" description: "specifies the resource type (e.g., cpu or memory) to be monitored for scaling" + versions: ">=0.0.1" default: value: "cpu" - name: "AVGUTILIZATION" type: "int" kind: "scalingResourceUtilization" description: "specifies the average utilization for the monitored resource, triggering scaling when exceeded" + versions: ">=0.0.1" default: value: 80 \ No newline at end of file diff --git a/template/manifests/HorizontalPodAutoscaler/manifests/hpa.yaml b/template/manifests/HorizontalPodAutoscaler/manifests/hpa.yaml new file mode 100644 index 00000000..be8bb34a --- /dev/null +++ b/template/manifests/HorizontalPodAutoscaler/manifests/hpa.yaml @@ -0,0 +1,22 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: {{ .Config.GetVariableValue "APPNAME" }} + labels: + app.kubernetes.io/name: {{ .Config.GetVariableValue "APPNAME" }} + app.kubernetes.io/part-of: {{ .Config.GetVariableValue "PARTOF" }} + kubernetes.azure.com/generator: {{ .Config.GetVariableValue "GENERATORLABEL" }} +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: {{ .Config.GetVariableValue "APPNAME" }} + minReplicas: {{ .Config.GetVariableValue "MINIMUMREPLICAS" }} + maxReplicas: {{ .Config.GetVariableValue "MAXIMUMREPLICAS" }} + metrics: + - type: Resource + resource: + name: {{ .Config.GetVariableValue "RESOURCETYPE" }} + target: + type: Utilization + averageUtilization: {{ .Config.GetVariableValue "AVGUTILIZATION"}} \ No newline at end of file diff --git a/template/manifests/HorizontalPodAutoscaling/manifest/hpa.yaml b/template/manifests/HorizontalPodAutoscaling/manifest/hpa.yaml deleted file mode 100644 index 3250dd6e..00000000 --- a/template/manifests/HorizontalPodAutoscaling/manifest/hpa.yaml +++ /dev/null @@ -1,22 +0,0 @@ -apiVersion: autoscaling/v2 -kind: HorizontalPodAutoscaler -metadata: - name: {{.APPNAME}} - labels: - app.kubernetes.io/name: {{.APPNAME}} - app.kubernetes.io/part-of: {{.PARTOF}} - kubernetes.azure.com/generator: {{.GENERATORLABEL}} -spec: - scaleTargetRef: - apiVersion: apps/v1 - kind: Deployment - name: {{.APPNAME}} - minReplicas: {{.MINIMUMREPLICAS}} - maxReplicas: {{.MAXIMUMREPLICAS}} - metrics: - - type: Resource - resource: - name: {{.RESOURCETYPE}} - target: - type: Utilization - averageUtilization: {{.AVGUTILIZATION}} \ No newline at end of file diff --git a/template/manifests/PodDisruptionBudget/manifest/pdb.yaml b/template/manifests/PodDisruptionBudget/manifest/pdb.yaml deleted file mode 100644 index 71f25c83..00000000 --- a/template/manifests/PodDisruptionBudget/manifest/pdb.yaml +++ /dev/null @@ -1,13 +0,0 @@ -apiVersion: policy/v1 -kind: PodDisruptionBudget -metadata: - name: {{.APPNAME}} - labels: - app.kubernetes.io/name: {{.APPNAME}} - app.kubernetes.io/part-of: {{.PARTOF}} - kubernetes.azure.com/generator: {{.GENERATORLABEL}} -spec: - maxUnavailable: {{.MAXUNAVAILABLE}} - selector: - matchLabels: - app: {{.APPNAME}} \ No newline at end of file diff --git a/template/manifests/PodDisruptionBudget/manifest/draft.yaml b/template/manifests/PodDisruptionBudget/manifests/draft.yaml similarity index 80% rename from template/manifests/PodDisruptionBudget/manifest/draft.yaml rename to template/manifests/PodDisruptionBudget/manifests/draft.yaml index 58bc20fa..7602d2d4 100644 --- a/template/manifests/PodDisruptionBudget/manifest/draft.yaml +++ b/template/manifests/PodDisruptionBudget/manifests/draft.yaml @@ -1,24 +1,30 @@ -templateName: "podDisruptionBudget-manifest" +templateName: "podDisruptionBudget-manifests" description: "This template is used to create a PodDisruptionBudget for an application" +versions: "0.0.1" +defaultVersions: "0.0.1" type: "manifest" variables: - name: "APPNAME" type: "string" kind: "kubernetesResourceName" description: "the name of the application" + versions: ">=0.0.1" - name: "PARTOF" type: "string" kind: "label" description: "the label to identify which project the resource belong to" + versions: ">=0.0.1" - name: "GENERATORLABEL" type: "string" kind: "label" description: "the label to identify who generated the resource" + versions: ">=0.0.1" default: value: "draft" - name: "MAXUNAVAILABLE" type: "int" kind: "resourceLimit" description: "specifies the maximum number of pods that can be unavailable during a disruption, such as a pod eviction" + versions: ">=0.0.1" default: value: 1 \ No newline at end of file diff --git a/template/manifests/PodDisruptionBudget/manifests/pdb.yaml b/template/manifests/PodDisruptionBudget/manifests/pdb.yaml new file mode 100644 index 00000000..a9ea9bb2 --- /dev/null +++ b/template/manifests/PodDisruptionBudget/manifests/pdb.yaml @@ -0,0 +1,13 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{.Config.GetVariableValue "APPNAME" }} + labels: + app.kubernetes.io/name: {{ .Config.GetVariableValue "APPNAME"}} + app.kubernetes.io/part-of: {{ .Config.GetVariableValue "PARTOF" }} + kubernetes.azure.com/generator: {{ .Config.GetVariableValue "GENERATORLABEL"}} +spec: + maxUnavailable: {{ .Config.GetVariableValue "MAXUNAVAILABLE" }} + selector: + matchLabels: + app: {{ .Config.GetVariableValue "APPNAME" }} \ No newline at end of file diff --git a/template/manifests/Service/draft.yaml b/template/manifests/Service/manifests/draft.yaml similarity index 81% rename from template/manifests/Service/draft.yaml rename to template/manifests/Service/manifests/draft.yaml index b66fc0c1..3a884919 100644 --- a/template/manifests/Service/draft.yaml +++ b/template/manifests/Service/manifests/draft.yaml @@ -1,30 +1,37 @@ -templateName: "Service" +templateName: "service-manifests" description: "This template is used to create a generic Service for an application" +versions: "0.0.1" +defaultVersion: "0.0.1" type: "manifest" variables: - name: "PORT" type: "int" kind: "port" description: "the port the service uses to make the application accessible from outside the cluster" + versions: ">=0.0.1" default: value: 80 - name: "APPNAME" type: "string" kind: "kubernetesResourceName" description: "the name of the application" + versions: ">=0.0.1" - name: "PARTOF" type: "string" kind: "label" description: "the label to identify which project the resource belong to" + versions: ">=0.0.1" - name: "GENERATORLABEL" type: "string" kind: "label" description: "the label to identify who generated the resource" + versions: ">=0.0.1" default: value: "draft" - name: "TARGETPORT" type: "int" kind: "port" description: "the port exposed in the application" + versions: ">=0.0.1" default: referenceVar: "PORT" \ No newline at end of file diff --git a/template/manifests/Service/manifests/service.yaml b/template/manifests/Service/manifests/service.yaml new file mode 100644 index 00000000..4cac4368 --- /dev/null +++ b/template/manifests/Service/manifests/service.yaml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ .Config.GetVariableValue "APPNAME" }} + labels: + app.kubernetes.io/name: {{ .Config.GetVariableValue "APPNAME" }} + app.kubernetes.io/part-of: {{ .Config.GetVariableValue "PARTOF" }} + kubernetes.azure.com/generator: {{ .Config.GetVariableValue "GENERATORLABEL" }} +spec: + type: ClusterIP + selector: + app: {{ .Config.GetVariableValue "APPNAME" }} + ports: + - protocol: TCP + port: {{ .Config.GetVariableValue "PORT" }} + targetPort: {{ .Config.GetVariableValue "TARGETPORT" }} \ No newline at end of file diff --git a/template/manifests/Service/service.yaml b/template/manifests/Service/service.yaml deleted file mode 100644 index 2ab33e33..00000000 --- a/template/manifests/Service/service.yaml +++ /dev/null @@ -1,16 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: {{.APPNAME}} - labels: - app.kubernetes.io/name: {{.APPNAME}} - app.kubernetes.io/part-of: {{.PARTOF}} - kubernetes.azure.com/generator: {{.GENERATORLABEL}} -spec: - type: ClusterIP - selector: - app: {{.APPNAME}} - ports: - - protocol: TCP - port: {{.PORT}} - targetPort: {{.TARGETPORT}} \ No newline at end of file