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

Migrating the Azpipeline templates to Go templates #373

Merged
merged 3 commits into from
Aug 27, 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
2 changes: 1 addition & 1 deletion pkg/azurePipelines/azurePipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (p *AzurePipelines) CreatePipelineFiles(deployType string, draftConfig *con
return fmt.Errorf("error applying default variables: %w", err)
}

if err := osutil.CopyDir(p.pipelineTemplates, srcDir, p.dest, draftConfig, templateWriter); err != nil {
if err := osutil.CopyDirWithTemplates(p.pipelineTemplates, srcDir, p.dest, draftConfig, templateWriter); err != nil {
Vidya2606 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("error copying pipeline files: %w", err)
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/azurePipelines/azurePipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package azurePipelines

import (
"fmt"
"github.com/Azure/draft/pkg/fixtures"
"os"
"testing"

Expand Down Expand Up @@ -32,15 +33,15 @@ func TestCreatePipelines(t *testing.T) {
deployType: "kustomize",
shouldError: false,
setConfig: func(dc *config.DraftConfig) {
dc.SetVariable("KUSTOMIZEPATH", "test/kustomize/overlays/production")
dc.SetVariable("KUSTOMIZEPATH", "kustomize/overlays/production")
},
},
{
name: "manifests_default_path",
deployType: "manifests",
shouldError: false,
setConfig: func(dc *config.DraftConfig) {
dc.SetVariable("PIPELINENAME", "some-other-name")
dc.SetVariable("PIPELINENAME", "testPipeline")
},
},
{
Expand Down Expand Up @@ -95,6 +96,19 @@ func TestCreatePipelines(t *testing.T) {
assert.Nil(t, err)
_, err = os.Stat(pipelineFilePath)
assert.Nil(t, err)

// Read the generated content
generatedContent, err := os.ReadFile(pipelineFilePath)
assert.Nil(t, err)

// Validate against the fixture file
fixturePath := fmt.Sprintf("../fixtures/pipelines/%s.yaml", tt.deployType)
if _, err := os.Stat(fixturePath); os.IsNotExist(err) {
t.Fatalf("Fixture file does not exist at path: %s", fixturePath)
}

err = fixtures.ValidateContentAgainstFixture(generatedContent, fixturePath)
assert.Nil(t, err)
}

err = os.RemoveAll(tempDir)
Expand Down Expand Up @@ -148,7 +162,7 @@ func newDraftConfig() *config.DraftConfig {
{
Name: "MANIFESTPATH",
Default: config.BuilderVarDefault{
Value: "manifests",
Value: "test/manifests",
Vidya2606 marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
Expand Down
68 changes: 68 additions & 0 deletions pkg/fixtures/pipelines/kustomize.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Azure Kubernetes Service (AKS) pipeline with Kustomize
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service cluster

variables:
armServiceConnection: testServiceConnection
azureContainerRegistry: testACR
containerName: testContainer
acrRg: testACRRG
clusterRg: testRG
clusterName: testCluster
kustomizePath: kustomize/overlays/production
namespace: testNamespace
tag: "$(Build.BuildId)"
vmImageName: "ubuntu-latest"

trigger:
- main

name: testPipeline

stages:
- stage: BuildAndPush
displayName: Build stage
jobs:
- job: BuildAndPush
displayName: Build and push image
pool:
vmImage: $(vmImageName)
steps:
- task: AzureCLI@2
displayName: Build and push image to Azure Container Registry
inputs:
azureSubscription: $(armServiceConnection)
scriptType: "bash"
scriptLocation: "inlineScript"
inlineScript: |
az acr build --image $1.azurecr.io/$2:$3 --registry $1 -g $4 .
arguments: "$(azureContainerRegistry) $(containerName) $(tag) $(acrRg)"

- stage: Deploy
displayName: Deploy stage
dependsOn: BuildAndPush
jobs:
- job: Deploy
displayName: Deploy to AKS using Kustomize
pool:
vmImage: $(vmImageName)
steps:
- task: KubernetesManifest@1
displayName: Bake Kustomize manifests
inputs:
action: 'bake'
kustomizationPath: $(kustomizePath)
renderType: 'kustomize'
name: 'bake'

- task: KubernetesManifest@1
displayName: Deploy baked manifests to Kubernetes cluster
inputs:
action: 'deploy'
connectionType: 'azureResourceManager'
azureSubscriptionConnection: $(armServiceConnection)
azureResourceGroup: $(clusterRg)
kubernetesCluster: $(clusterName)
namespace: $(namespace)
manifests: $(bake.manifestsBundle)
containers: |
$(azureContainerRegistry).azurecr.io/$(containerName):$(tag)
60 changes: 60 additions & 0 deletions pkg/fixtures/pipelines/manifests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Azure Kubernetes Service pipeline
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service cluster

variables:
armServiceConnection: testServiceConnection
azureContainerRegistry: testACR
containerName: testContainer
clusterRg: testRG
acrRg: testACRRG
clusterName: testCluster
manifestPath: test/manifests
namespace: testNamespace
tag: "$(Build.BuildId)"
vmImageName: "ubuntu-latest"

name: testPipeline

trigger:
- main

stages:
- stage: BuildAndPush
displayName: Build stage
jobs:
- job: BuildAndPush
displayName: Build and push image
pool:
vmImage: $(vmImageName)
steps:
- task: AzureCLI@2
displayName: Build and push image to Azure Container Registry
inputs:
azureSubscription: $(armServiceConnection)
scriptType: "bash"
scriptLocation: "inlineScript"
inlineScript: |
az acr build --image $1.azurecr.io/$2:$3 --registry $1 -g $4 .
arguments: "$(azureContainerRegistry) $(containerName) $(tag) $(acrRg)"

- stage: Deploy
displayName: Deploy stage
dependsOn: BuildAndPush
jobs:
- job: Deploy
displayName: Deploy to AKS
pool:
vmImage: $(vmImageName)
steps:
- task: KubernetesManifest@1
displayName: Deploy to Kubernetes cluster
inputs:
action: "deploy"
connectionType: "azureResourceManager"
azureSubscriptionConnection: $(armServiceConnection)
azureResourceGroup: $(clusterRg)
kubernetesCluster: $(clusterName)
manifests: $(manifestPath)
namespace: $(namespace)
containers: |
$(azureContainerRegistry).azurecr.io/$(containerName):$(tag)
37 changes: 37 additions & 0 deletions pkg/fixtures/validatetemplate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package fixtures

import (
"embed"
"errors"
"fmt"
"os"
"regexp"
"strings"
)

//go:embed pipelines/*
var pipelines embed.FS

func ValidateContentAgainstFixture(generatedContent []byte, fixturePath string) error {
fullFixturePath := fmt.Sprintf("%s", fixturePath)

// Read the fixture content
fixtureContent, err := os.ReadFile(fullFixturePath)
if err != nil {
return fmt.Errorf("failed to read fixture: %w", err)
}

if normalizeWhitespace(fixtureContent) != normalizeWhitespace(generatedContent) {
return errors.New("generated content does not match fixture")
}

return nil
}

func normalizeWhitespace(content []byte) string {
s := string(content)
re := regexp.MustCompile(`\r?\n`)
s = re.ReplaceAllString(s, "\n")
re = regexp.MustCompile(`\s+`)
return strings.TrimSpace(re.ReplaceAllString(s, " "))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service cluster

variables:
armServiceConnection: {{ARMSERVICECONNECTION}}
azureContainerRegistry: {{AZURECONTAINERREGISTRY}}
containerName: {{CONTAINERNAME}}
acrRg: {{ACRRESOURCEGROUP}}
clusterRg: {{CLUSTERRESOURCEGROUP}}
clusterName: {{CLUSTERNAME}}
kustomizePath: {{KUSTOMIZEPATH}}
namespace: {{NAMESPACE}}
armServiceConnection: {{.ARMSERVICECONNECTION}}
azureContainerRegistry: {{.AZURECONTAINERREGISTRY}}
containerName: {{.CONTAINERNAME}}
acrRg: {{.ACRRESOURCEGROUP}}
clusterRg: {{.CLUSTERRESOURCEGROUP}}
clusterName: {{.CLUSTERNAME}}
kustomizePath: {{.KUSTOMIZEPATH}}
namespace: {{.NAMESPACE}}
tag: "$(Build.BuildId)"
vmImageName: "ubuntu-latest"

trigger:
- {{BRANCHNAME}}

name: {{PIPELINENAME}}
- {{.BRANCHNAME}}

name: {{.PIPELINENAME}}
{{`
stages:
- stage: BuildAndPush
displayName: Build stage
Expand Down Expand Up @@ -65,4 +65,5 @@ stages:
namespace: $(namespace)
manifests: $(bake.manifestsBundle)
containers: |
$(azureContainerRegistry).azurecr.io/$(containerName):$(tag)
$(azureContainerRegistry).azurecr.io/$(containerName):$(tag)
`}}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service cluster

variables:
armServiceConnection: {{ARMSERVICECONNECTION}}
azureContainerRegistry: {{AZURECONTAINERREGISTRY}}
containerName: {{CONTAINERNAME}}
clusterRg: {{CLUSTERRESOURCEGROUP}}
acrRg: {{ACRRESOURCEGROUP}}
clusterName: {{CLUSTERNAME}}
manifestPath: {{MANIFESTPATH}}
namespace: {{NAMESPACE}}
armServiceConnection: {{.ARMSERVICECONNECTION}}
azureContainerRegistry: {{.AZURECONTAINERREGISTRY}}
containerName: {{.CONTAINERNAME}}
clusterRg: {{.CLUSTERRESOURCEGROUP}}
acrRg: {{.ACRRESOURCEGROUP}}
clusterName: {{.CLUSTERNAME}}
manifestPath: {{.MANIFESTPATH}}
namespace: {{.NAMESPACE}}
tag: "$(Build.BuildId)"
vmImageName: "ubuntu-latest"

name: {{PIPELINENAME}}
name: {{.PIPELINENAME}}

trigger:
- {{BRANCHNAME}}

- {{.BRANCHNAME}}
{{`
stages:
- stage: BuildAndPush
displayName: Build stage
Expand Down Expand Up @@ -58,3 +58,4 @@ stages:
namespace: $(namespace)
containers: |
$(azureContainerRegistry).azurecr.io/$(containerName):$(tag)
`}}
Loading