Skip to content

Commit

Permalink
standardize namespace creation behavior (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgamero authored Jan 16, 2025
1 parent 92508c0 commit aa0f872
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 43 deletions.
23 changes: 9 additions & 14 deletions pkg/fixtures/validatetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,25 @@ import (
"os"
"regexp"
"strings"

"github.com/google/go-cmp/cmp"
)

func ValidateContentAgainstFixture(generatedContent []byte, fixturePath string) error {
got := generatedContent
// Read the fixture content
fixtureContent, err := os.ReadFile(fixturePath)
want, err := os.ReadFile(fixturePath)
if err != nil {
return fmt.Errorf("failed to read fixture: %w", err)
}

if normalizeWhitespace(fixtureContent) != normalizeWhitespace(generatedContent) {
genWords := strings.Split(normalizeWhitespace(generatedContent), " ")
fixtureWords := strings.Split(normalizeWhitespace(fixtureContent), " ")
differingWords := []string{}
for i, word := range genWords {
if i < len(fixtureWords) && word != fixtureWords[i] {
differingWords = append(differingWords, fmt.Sprintf("'%s' != '%s'", word, fixtureWords[i]))
if len(differingWords) == 1 {
fmt.Println("Generated Word | Fixture Word")
}
fmt.Printf("'%s' != '%s'\n", word, fixtureWords[i])
}
if normalizeWhitespace(want) != normalizeWhitespace(got) {
if diff := cmp.Diff(string(want), string(got)); diff != "" {
fmt.Println("Diff for file ", fixturePath, " (-want +got)")
fmt.Printf(diff)
return fmt.Errorf("generated content does not match fixture for file %s, check above for rich diff", fixturePath)
}

return fmt.Errorf("generated content does not match fixture for file %s: %s", fixturePath, strings.Join(differingWords, ", "))
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ env:
CHART_OVERRIDE_PATH: testOverridePath
CHART_OVERRIDES: replicas:2
NAMESPACE: default
ENABLENAMESPACECREATION: false

jobs:
buildImage:
Expand Down Expand Up @@ -112,6 +113,17 @@ jobs:
admin: "false"
use-kubelogin: "true"

# Create Namespace
- name: Create Namespace
if: ${{ env.ENABLENAMESPACECREATION == 'true' }}
run: |
kubectl get namespace ${{ env.NAMESPACE }} || kubectl create namespace ${{ env.NAMESPACE }}
# Validate Namespace exists
- name: Validate Namespace Exists
run: |
kubectl get namespace ${{ env.NAMESPACE }}
# Checks if the AKS cluster is private
- name: Is private cluster
id: isPrivate
Expand All @@ -122,9 +134,9 @@ jobs:
# Deploys application
- name: Deploy application on private cluster
if: steps.isPrivate.outputs.PRIVATE_CLUSTER == 'true'
if: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER == 'true' }}
run: |
command_id=$(az aks command invoke --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command "helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }} --create-namespace --timeout 240s" --file . --query id -o tsv)
command_id=$(az aks command invoke --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command "helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }} --timeout 240s" --file . --query id -o tsv)
result=$(az aks command result --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command-id $command_id)
echo "Helm upgrade result: $result"
exitCode=$(az aks command result --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command-id $command_id --query exitCode -o tsv)
Expand All @@ -134,11 +146,6 @@ jobs:
fi
- name: Deploy application on public cluster
if: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER != 'true' && env.CLUSTER_RESOURCE_TYPE != 'Microsoft.ContainerService/fleets' }}
run: |
helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }} --create-namespace
- name: Deploy application on fleet hub cluster
if: ${{ env.CLUSTER_RESOURCE_TYPE == 'Microsoft.ContainerService/fleets' }}
if: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER != 'true' }}
run: |
helm upgrade -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }}
helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ env:
DOCKER_FILE: ./Dockerfile
BUILD_CONTEXT_PATH: test
NAMESPACE: default
ENABLENAMESPACECREATION: false

jobs:
buildImage:
Expand Down Expand Up @@ -111,6 +112,17 @@ jobs:
use-kubelogin: 'true'
resource-type: ${{ env.CLUSTER_RESOURCE_TYPE }}

# Create Namespace
- name: Create Namespace
if: ${{ env.ENABLENAMESPACECREATION == 'true' }}
run: |
kubectl get namespace ${{ env.NAMESPACE }} || kubectl create namespace ${{ env.NAMESPACE }}
# Validate Namespace exists
- name: Validate Namespace Exists
run: |
kubectl get namespace ${{ env.NAMESPACE }}
# Runs Kustomize to create manifest files
- name: Bake deployment
uses: azure/k8s-bake@v2
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetTemplate(name, version, dest string, templateWriter templatewriter.Templ
func (t *Template) Generate() error {
if err := t.validate(); err != nil {
log.Printf("template validation failed: %s", err.Error())
return err
return fmt.Errorf("generating template: %w", err)
}

if err := t.Config.ApplyDefaultVariablesForVersion(t.version); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion template/deployments/helm/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@ variables:
disablePrompt: true
value: "secret-ref"
description: "the name of the kubernetes secret reference"
versions: ">=0.0.1"
versions: ">=0.0.1"
6 changes: 0 additions & 6 deletions template/deployments/kustomize/base/namespace.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion template/deployments/kustomize/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@ variables:
value: "true"
condition: "equals"
description: "the name of the service account to use with workload identity"
versions: ">=0.0.1"
versions: ">=0.0.1"
2 changes: 1 addition & 1 deletion template/deployments/manifests/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@ variables:
disablePrompt: true
value: "secret-ref"
description: "the name of the kubernetes secret reference"
versions: ">=0.0.1"
versions: ">=0.0.1"
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ env:
CHART_OVERRIDE_PATH: {{ .Config.GetVariableValue "CHARTOVERRIDEPATH" }}
CHART_OVERRIDES: {{ .Config.GetVariableValue "CHARTOVERRIDES" }}
NAMESPACE: {{ .Config.GetVariableValue "NAMESPACE" }}
ENABLENAMESPACECREATION: {{ .Config.GetVariableValue "ENABLENAMESPACECREATION" }}
{{`
jobs:
buildImage:
Expand Down Expand Up @@ -112,6 +113,17 @@ jobs:
admin: "false"
use-kubelogin: "true"

# Create Namespace
- name: Create Namespace
if: ${{ env.ENABLENAMESPACECREATION == 'true' }}
run: |
kubectl get namespace ${{ env.NAMESPACE }} || kubectl create namespace ${{ env.NAMESPACE }}
# Validate Namespace exists
- name: Validate Namespace Exists
run: |
kubectl get namespace ${{ env.NAMESPACE }}
# Checks if the AKS cluster is private
- name: Is private cluster
id: isPrivate
Expand All @@ -122,9 +134,9 @@ jobs:
# Deploys application
- name: Deploy application on private cluster
if: steps.isPrivate.outputs.PRIVATE_CLUSTER == 'true'
if: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER == 'true' }}
run: |
command_id=$(az aks command invoke --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command "helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }} --create-namespace --timeout 240s" --file . --query id -o tsv)
command_id=$(az aks command invoke --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command "helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }} --timeout 240s" --file . --query id -o tsv)
result=$(az aks command result --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command-id $command_id)
echo "Helm upgrade result: $result"
exitCode=$(az aks command result --resource-group ${{ env.CLUSTER_RESOURCE_GROUP }} --name ${{ env.CLUSTER_NAME }} --command-id $command_id --query exitCode -o tsv)
Expand All @@ -134,12 +146,7 @@ jobs:
fi
- name: Deploy application on public cluster
if: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER != 'true' && env.CLUSTER_RESOURCE_TYPE != 'Microsoft.ContainerService/fleets' }}
run: |
helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }} --create-namespace
- name: Deploy application on fleet hub cluster
if: ${{ env.CLUSTER_RESOURCE_TYPE == 'Microsoft.ContainerService/fleets' }}
if: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER != 'true' }}
run: |
helm upgrade -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }}
helm upgrade --wait -i -f ${{ env.CHART_OVERRIDE_PATH }} --set ${{ env.CHART_OVERRIDES }} --set image.tag=${{ github.sha }} automated-deployment ${{ env.CHART_PATH }} --namespace ${{ env.NAMESPACE }}
`}}
8 changes: 8 additions & 0 deletions template/workflows/helm/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ variables:
value: "default"
description: "the Kubernetes namespace"
versions: ">=0.0.1"
- name: "ENABLENAMESPACECREATION"
type: "bool"
kind: "flag"
default:
disablePrompt: true
value: "false"
description: "enable creation of target namespace if it does not exist"
versions: ">=0.0.1"
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ env:
DOCKER_FILE: {{ .Config.GetVariableValue "DOCKERFILE" }}
BUILD_CONTEXT_PATH: {{ .Config.GetVariableValue "BUILDCONTEXTPATH" }}
NAMESPACE: {{ .Config.GetVariableValue "NAMESPACE" }}
ENABLENAMESPACECREATION: {{ .Config.GetVariableValue "ENABLENAMESPACECREATION" }}
{{`
jobs:
buildImage:
Expand Down Expand Up @@ -111,6 +112,17 @@ jobs:
use-kubelogin: 'true'
resource-type: ${{ env.CLUSTER_RESOURCE_TYPE }}

# Create Namespace
- name: Create Namespace
if: ${{ env.ENABLENAMESPACECREATION == 'true' }}
run: |
kubectl get namespace ${{ env.NAMESPACE }} || kubectl create namespace ${{ env.NAMESPACE }}
# Validate Namespace exists
- name: Validate Namespace Exists
run: |
kubectl get namespace ${{ env.NAMESPACE }}
# Runs Kustomize to create manifest files
- name: Bake deployment
uses: azure/k8s-bake@v2
Expand Down
8 changes: 8 additions & 0 deletions template/workflows/kustomize/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ variables:
value: "default"
description: "the Kubernetes namespace"
versions: ">=0.0.1"
- name: "ENABLENAMESPACECREATION"
type: "bool"
kind: "flag"
default:
disablePrompt: true
value: "false"
description: "enable creation of target namespace if it does not exist"
versions: ">=0.0.1"
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ env:
DOCKER_FILE: {{ .Config.GetVariableValue "DOCKERFILE" }}
BUILD_CONTEXT_PATH: {{ .Config.GetVariableValue "BUILDCONTEXTPATH" }}
NAMESPACE: {{ .Config.GetVariableValue "NAMESPACE" }}
ENABLENAMESPACECREATION: {{ .Config.GetVariableValue "ENABLENAMESPACECREATION" }}
{{`
jobs:
buildImage:
Expand Down Expand Up @@ -106,6 +107,17 @@ jobs:
use-kubelogin: 'true'
resource-type: ${{ env.CLUSTER_RESOURCE_TYPE }}

# Create Namespace
- name: Create Namespace
if: ${{ env.ENABLENAMESPACECREATION == 'true' }}
run: |
kubectl get namespace ${{ env.NAMESPACE }} || kubectl create namespace ${{ env.NAMESPACE }}
# Validate Namespace exists
- name: Validate Namespace Exists
run: |
kubectl get namespace ${{ env.NAMESPACE }}
# Checks if the AKS cluster is private
- name: Is private cluster
if: ${{ env.CLUSTER_RESOURCE_TYPE != 'Microsoft.ContainerService/fleets' }}
Expand All @@ -127,4 +139,4 @@ jobs:
private-cluster: ${{ steps.isPrivate.outputs.PRIVATE_CLUSTER == 'true' }}
namespace: ${{ env.NAMESPACE }}
resource-type: ${{ env.CLUSTER_RESOURCE_TYPE }}
`}}
`}}
8 changes: 8 additions & 0 deletions template/workflows/manifests/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ variables:
value: "default"
description: "the Kubernetes namespace"
versions: ">=0.0.1"
- name: "ENABLENAMESPACECREATION"
type: "bool"
kind: "flag"
default:
disablePrompt: true
value: "false"
description: "enable creation of target namespace if it does not exist"
versions: ">=0.0.1"

0 comments on commit aa0f872

Please sign in to comment.