Skip to content

Commit

Permalink
updating templates
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoley13 committed Oct 29, 2024
1 parent cdd3f0e commit 6e670b4
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 54 deletions.
8 changes: 8 additions & 0 deletions pkg/config/draftconfig_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package config
import (
"fmt"
"io/fs"
"regexp"
"strings"
"testing"

"github.com/Azure/draft/template"
"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{
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/fixtures/manifests/hpa/hpa.yaml
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions pkg/fixtures/manifests/pdb/pdb.yaml
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions pkg/fixtures/manifests/service/service.yaml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions pkg/handlers/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions template/manifests/HorizontalPodAutoscaler/manifests/hpa.yaml
Original file line number Diff line number Diff line change
@@ -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"}}
22 changes: 0 additions & 22 deletions template/manifests/HorizontalPodAutoscaling/manifest/hpa.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions template/manifests/PodDisruptionBudget/manifest/pdb.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions template/manifests/PodDisruptionBudget/manifests/pdb.yaml
Original file line number Diff line number Diff line change
@@ -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" }}
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 16 additions & 0 deletions template/manifests/Service/manifests/service.yaml
Original file line number Diff line number Diff line change
@@ -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" }}
16 changes: 0 additions & 16 deletions template/manifests/Service/service.yaml

This file was deleted.

0 comments on commit 6e670b4

Please sign in to comment.