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

Generate azure pipelines #337

Merged
merged 6 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
138 changes: 138 additions & 0 deletions pkg/azurePipelines/azurePipelines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package azurePipelines

import (
"embed"
"fmt"
"io/fs"
"path"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/embedutils"
"github.com/Azure/draft/pkg/osutil"
"github.com/Azure/draft/pkg/templatewriter"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

const (
pipelineParentDirName = "azurePipelines"
configFileName = "draft.yaml"
pipelineNameVar = "PIPELINENAME"
)

type AzurePipelines struct {
pipelines map[string]fs.DirEntry
configs map[string]*config.DraftConfig
dest string
pipelineTemplates embed.FS
}

func CreatePipelinesFromEmbedFS(pipelineTemplates embed.FS, dest string) (*AzurePipelines, error) {
pipelineMap, err := embedutils.EmbedFStoMap(pipelineTemplates, "azurePipelines")
if err != nil {
return nil, fmt.Errorf("error creating map from embedded FS: %w", err)
}

p := &AzurePipelines{
pipelines: pipelineMap,
dest: dest,
configs: make(map[string]*config.DraftConfig),
pipelineTemplates: pipelineTemplates,
}
p.populateConfigs()

return p, nil

}

func (p *AzurePipelines) populateConfigs() {
for _, val := range p.pipelines {
draftConfig, err := p.loadConfig(val.Name())
if err != nil {
log.Debugf("error loading draftConfig for pipeline of deploy type %s: %v", val.Name(), err)
draftConfig = &config.DraftConfig{}
}
p.configs[val.Name()] = draftConfig
}
}

func (p *AzurePipelines) GetConfig(deployType string) (*config.DraftConfig, error) {
val, ok := p.configs[deployType]
if !ok {
return nil, fmt.Errorf("deploy type %s unsupported", deployType)
}
return val, nil
}

func (p *AzurePipelines) loadConfig(deployType string) (*config.DraftConfig, error) {
val, ok := p.pipelines[deployType]
if !ok {
return nil, fmt.Errorf("deploy type %s unsupported", deployType)
}

configPath := path.Join(pipelineParentDirName, val.Name(), configFileName)
configBytes, err := fs.ReadFile(p.pipelineTemplates, configPath)
if err != nil {
return nil, fmt.Errorf("error reading config file: %w", err)
}

var draftConfig config.DraftConfig
if err = yaml.Unmarshal(configBytes, &draftConfig); err != nil {
return nil, fmt.Errorf("error unmarshalling config file: %w", err)
}

return &draftConfig, nil
}

func (p *AzurePipelines) overrideFilename(draftConfig *config.DraftConfig, srcDir string) error {
if draftConfig.FileNameOverrideMap == nil {
draftConfig.FileNameOverrideMap = make(map[string]string)
}
pipelineVar, err := draftConfig.GetVariable(pipelineNameVar)
if err != nil {
return fmt.Errorf("error getting pipeline name variable: %w", err)
}

files, err := fs.ReadDir(p.pipelineTemplates, srcDir)
if err != nil {
return fmt.Errorf("error reading source directory: %w", err)
}

for _, f := range files {
if f.Name() == configFileName {
continue
}
if f.IsDir() {
davidgamero marked this conversation as resolved.
Show resolved Hide resolved
if err := p.overrideFilename(draftConfig, path.Join(srcDir, f.Name())); err != nil {
return fmt.Errorf("error overriding filename: %w", err)
}
continue
}

draftConfig.FileNameOverrideMap[f.Name()] = pipelineVar.Value + ".yaml"
davidgamero marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

func (p *AzurePipelines) CreatePipelineFiles(deployType string, draftConfig *config.DraftConfig, templateWriter templatewriter.TemplateWriter) error {
val, ok := p.pipelines[deployType]
if !ok {
return fmt.Errorf("deploy type %s currently unsupported for azure pipeline", deployType)
}
srcDir := path.Join(pipelineParentDirName, val.Name())
log.Debugf("source directory of pipeline template: %s", srcDir)

if err := p.overrideFilename(draftConfig, srcDir); err != nil {
return fmt.Errorf("error overriding filename: %w", err)
}

if err := draftConfig.ApplyDefaultVariables(); err != nil {
return fmt.Errorf("error applying default variables: %w", err)
}

if err := osutil.CopyDir(p.pipelineTemplates, srcDir, p.dest, draftConfig, templateWriter); err != nil {
return fmt.Errorf("error copying pipeline files: %w", err)
}

return nil
}
167 changes: 167 additions & 0 deletions pkg/azurePipelines/azurePipelines_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package azurePipelines

import (
"fmt"
"os"
"testing"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/templatewriter/writers"
"github.com/Azure/draft/template"
"github.com/stretchr/testify/assert"
)

func TestCreatePipelines(t *testing.T) {
var pipelineFilePath string
templateWriter := &writers.LocalFSWriter{}

tests := []struct {
name string
deployType string
shouldError bool
setConfig func(dc *config.DraftConfig)
defaultPipelineFileName string
cleanUp func(tempDir string)
}{
{
name: "kustomize_default_path",
deployType: "kustomize",
shouldError: false,
defaultPipelineFileName: "azure-kubernetes-service-kustomize.yaml",
},
{
name: "kustomize_given_path",
deployType: "kustomize",
shouldError: false,
setConfig: func(dc *config.DraftConfig) {
dc.SetVariable("KUSTOMIZEPATH", "test/kustomize/overlays/production")
},
defaultPipelineFileName: "azure-kubernetes-service-kustomize.yaml",
},
{
name: "manifests_default_path",
deployType: "manifests",
shouldError: false,
setConfig: func(dc *config.DraftConfig) {
dc.SetVariable("PIPELINENAME", "some-other-name")
},
defaultPipelineFileName: "azure-kubernetes-service.yaml",
},
{
name: "manifests_custom_path",
deployType: "manifests",
shouldError: false,
setConfig: func(dc *config.DraftConfig) {
dc.SetVariable("MANIFESTPATH", "test/manifests")
},
defaultPipelineFileName: "azure-kubernetes-service.yaml",
},
{
name: "invalid",
deployType: "invalid",
shouldError: true,
defaultPipelineFileName: "azure-kubernetes-service.yaml",
},
{
name: "missing_config",
deployType: "kustomize",
shouldError: true,
setConfig: func(dc *config.DraftConfig) {
// removing the last variable from draftConfig
dc.Variables = dc.Variables[:len(dc.Variables)-1]
},
defaultPipelineFileName: "azure-kubernetes-service-kustomize.yaml",
},
}

for _, tt := range tests {
draftConfig := newDraftConfig()

tempDir, err := os.MkdirTemp(".", "testTempDir")
assert.Nil(t, err)

if tt.setConfig != nil {
tt.setConfig(draftConfig)
}

pipelines, err := CreatePipelinesFromEmbedFS(template.AzurePipelines, tempDir)
assert.Nil(t, err)

err = pipelines.CreatePipelineFiles(tt.deployType, draftConfig, templateWriter)

pipelineFilePath = fmt.Sprintf("%s/.pipelines/%s", tempDir, tt.defaultPipelineFileName)
if val, ok := draftConfig.FileNameOverrideMap[tt.defaultPipelineFileName]; ok {
pipelineFilePath = fmt.Sprintf("%s/.pipelines/%s", tempDir, val)
}

if tt.shouldError {
assert.NotNil(t, err)
_, err = os.Stat(pipelineFilePath)
assert.Equal(t, os.IsNotExist(err), true)
} else {
assert.Nil(t, err)
_, err = os.Stat(pipelineFilePath)
assert.Nil(t, err)
}

err = os.RemoveAll(tempDir)
assert.Nil(t, err)
}
}

func newDraftConfig() *config.DraftConfig {
return &config.DraftConfig{
Variables: []*config.BuilderVar{
{
Name: "PIPELINENAME",
Value: "testPipeline",
},
{
Name: "BRANCHNAME",
Default: config.BuilderVarDefault{
Value: "main",
},
},
{
Name: "ARMSERVICECONNECTION",
Value: "testServiceConnection",
},
{
Name: "AZURECONTAINERREGISTRY",
Value: "testACR",
},
{
Name: "CONTAINERNAME",
Value: "testContainer",
},
{
Name: "CLUSTERRESOURCEGROUP",
Value: "testRG",
},
{
Name: "ACRRESOURCEGROUP",
Value: "testACRRG",
},
{
Name: "CLUSTERNAME",
Value: "testCluster",
},
{
Name: "KUSTOMIZEPATH",
Default: config.BuilderVarDefault{
Value: "kustomize/overlays/production",
},
},
{
Name: "MANIFESTPATH",
Default: config.BuilderVarDefault{
Value: "manifests",
},
},
{
Name: "NAMESPACE",
Value: "testNamespace",
},
},
}
}
7 changes: 4 additions & 3 deletions pkg/config/draftconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (

// TODO: remove Name Overrides since we don't need them anymore
type DraftConfig struct {
DisplayName string `yaml:"displayName"`
NameOverrides []FileNameOverride `yaml:"nameOverrides"`
Variables []*BuilderVar `yaml:"variables"`
DisplayName string `yaml:"displayName"`
NameOverrides []FileNameOverride `yaml:"nameOverrides"`
Variables []*BuilderVar `yaml:"variables"`
FileNameOverrideMap map[string]string `yaml:"filenameOverrideMap"`
bosesuneha marked this conversation as resolved.
Show resolved Hide resolved

nameOverrideMap map[string]string
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/osutil/osutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ func CopyDir(
continue
}

fileName := f.Name()
if overrideName, ok := draftConfig.FileNameOverrideMap[f.Name()]; ok {
fileName = overrideName
}
srcPath := path.Join(src, f.Name())
destPath := path.Join(dest, f.Name())
destPath := path.Join(dest, fileName)
log.Debugf("Source path: %s Dest path: %s", srcPath, destPath)

if f.IsDir() {
Expand Down
Loading