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 helm deployments to Go templates #360

Merged
merged 13 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 3 additions & 3 deletions pkg/deployments/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/Azure/draft/pkg/templatewriter"
)

var (
const (
parentDirName = "deployments"
configFileName = "draft.yaml"
)
Expand Down Expand Up @@ -47,7 +47,7 @@ func (d *Deployments) CopyDeploymentFiles(deployType string, deployConfig *confi
return fmt.Errorf("create deployment files for deployment type: %w", err)
}

if err := osutil.CopyDir(d.deploymentTemplates, srcDir, d.dest, deployConfig, templateWriter); err != nil {
if err := osutil.CopyDirWithTemplates(d.deploymentTemplates, srcDir, d.dest, deployConfig, templateWriter); err != nil {
return err
}

Expand Down Expand Up @@ -94,7 +94,7 @@ func (d *Deployments) PopulateConfigs() {
}

func CreateDeploymentsFromEmbedFS(deploymentTemplates embed.FS, dest string) *Deployments {
deployMap, err := embedutils.EmbedFStoMap(deploymentTemplates, "deployments")
deployMap, err := embedutils.EmbedFStoMap(deploymentTemplates, parentDirName)
if err != nil {
log.Fatal(err)
}
Expand Down
239 changes: 239 additions & 0 deletions pkg/deployments/deployments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package deployments

import (
"embed"
"fmt"
"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/embedutils"
"github.com/Azure/draft/pkg/templatewriter/writers"
"io"
"io/fs"
"os"
"testing"
"testing/fstest"

"github.com/Azure/draft/template"
"github.com/stretchr/testify/assert"
)

var testFS embed.FS

func TestCreateDeployments(t *testing.T) {
dest := "."
templateWriter := &writers.LocalFSWriter{}
draftConfig := &config.DraftConfig{
Variables: []*config.BuilderVar{
{Name: "APPNAME", Value: "testapp"},
{Name: "NAMESPACE", Value: "default"},
{Name: "PORT", Value: "80"},
{Name: "IMAGENAME", Value: "testimage"},
{Name: "IMAGETAG", Value: "latest"},
{Name: "GENERATORLABEL", Value: "draft"},
{Name: "SERVICEPORT", Value: "80"},
},
}

tests := []struct {
name string
deployType string
shouldError bool
tempDirPath string
tempFileName string
tempPath string
cleanUp func()
}{
{
name: "helm",
deployType: "helm",
shouldError: false,
tempDirPath: "charts/templates",
tempFileName: "charts/templates/deployment.yaml",
tempPath: "../../test/templates/helm/charts/templates/deployment.yaml",
cleanUp: func() {
os.Remove(".charts")
},
},
{
name: "unsupported",
deployType: "unsupported",
shouldError: true,
tempDirPath: "test/templates/unsupported",
tempFileName: "test/templates/unsupported/deployment.yaml",
tempPath: "test/templates/unsupported/deployment.yaml",
cleanUp: func() {
os.Remove("deployments")
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fmt.Println("Creating temp file:", tt.tempFileName)
err := createTempDeploymentFile(tt.tempDirPath, tt.tempFileName, tt.tempPath)
assert.Nil(t, err)

deployments := CreateDeploymentsFromEmbedFS(template.Deployments, dest)
err = deployments.CopyDeploymentFiles(tt.deployType, draftConfig, templateWriter)
if tt.shouldError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

tt.cleanUp()
})
}
}

func TestLoadConfig(t *testing.T) {
fakeFS, err := createMockDeploymentTemplatesFS()
assert.Nil(t, err)

d, err := createMockDeployments("deployments", fakeFS)
assert.Nil(t, err)

cases := []loadConfTestCase{
{"helm", true},
{"unsupported", false},
}

for _, c := range cases {
if c.isNil {
_, err = d.loadConfig(c.deployType)
assert.Nil(t, err)
} else {
_, err = d.loadConfig(c.deployType)
assert.NotNil(t, err)
}
}
}

func TestPopulateConfigs(t *testing.T) {
fakeFS, err := createMockDeploymentTemplatesFS()
assert.Nil(t, err)

d, err := createMockDeployments("deployments", fakeFS)
assert.Nil(t, err)

d.PopulateConfigs()
assert.Equal(t, 3, len(d.configs))

d, err = createTestDeploymentEmbed("deployments")
assert.Nil(t, err)

d.PopulateConfigs()
assert.Equal(t, 3, len(d.configs))
}

type loadConfTestCase struct {
deployType string
isNil bool
}

func createTempDeploymentFile(dirPath, fileName, path string) error {
err := os.MkdirAll(dirPath, 0755)
if err != nil {
return err
}
file, err := os.Create(fileName)
if err != nil {
return err
}
fmt.Printf("file %v\n", file)
defer file.Close()

var source *os.File
source, err = os.Open(path)
if err != nil {
return err
}
fmt.Printf("source %v\n", source)
defer source.Close()

_, err = io.Copy(file, source)
if err != nil {
return err
}
return nil
}

func createMockDeploymentTemplatesFS() (fs.FS, error) {
rootPath := "deplyments/"
embedFiles, err := embedutils.EmbedFStoMapWithFiles(template.Deployments, "deployments")
if err != nil {
return nil, fmt.Errorf("failed to readDir: %w in embeded files", err)
}

mockFS := fstest.MapFS{}

for path, file := range embedFiles {
if file.IsDir() {
mockFS[path] = &fstest.MapFile{Mode: fs.ModeDir}
} else {
bytes, err := template.Deployments.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failes to read file: %w", err)
}
mockFS[path] = &fstest.MapFile{Data: bytes}
}
}

mockFS[rootPath+"emptyDir"] = &fstest.MapFile{Mode: fs.ModeDir}
mockFS[rootPath+"corrupted"] = &fstest.MapFile{Mode: fs.ModeDir}
mockFS[rootPath+"corrupted/draft.yaml"] = &fstest.MapFile{Data: []byte("fake yaml data")}

return mockFS, nil
}

func createMockDeployments(dirPath string, mockDeployments fs.FS) (*Deployments, error) {
dest := "."

deployMap, err := fsToMap(mockDeployments, dirPath)
if err != nil {
return nil, fmt.Errorf("failed fsToMap: %w", err)
}

d := &Deployments{
deploys: deployMap,
dest: dest,
configs: make(map[string]*config.DraftConfig),
deploymentTemplates: mockDeployments,
}

return d, nil
}

func createTestDeploymentEmbed(dirPath string) (*Deployments, error) {
dest := "."

deployMap, err := embedutils.EmbedFStoMap(template.Deployments, "deployments")
if err != nil {
return nil, fmt.Errorf("failed to create deployMap: %w", err)
}

d := &Deployments{
deploys: deployMap,
dest: dest,
configs: make(map[string]*config.DraftConfig),
deploymentTemplates: template.Deployments,
}

return d, nil
}

func fsToMap(fsFS fs.FS, path string) (map[string]fs.DirEntry, error) {
files, err := fs.ReadDir(fsFS, path)
if err != nil {
return nil, fmt.Errorf("failed to ReadDir: %w", err)
}

mapping := make(map[string]fs.DirEntry)

for _, f := range files {
if f.IsDir() {
mapping[f.Name()] = f
}
}

return mapping, nil
}
3 changes: 1 addition & 2 deletions pkg/osutil/osutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package osutil
import (
"bytes"
"fmt"
log "github.com/sirupsen/logrus"
Vidya2606 marked this conversation as resolved.
Show resolved Hide resolved
"io/fs"
"os"
"path"
Expand All @@ -12,8 +13,6 @@ import (
"syscall"
"text/template"

log "github.com/sirupsen/logrus"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/templatewriter"
)
Expand Down
2 changes: 1 addition & 1 deletion template/deployments/helm/charts/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
name: {{APPNAME}}
name: {{.APPNAME}}
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
Expand Down
4 changes: 2 additions & 2 deletions template/deployments/helm/charts/production.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
image:
repository: "{{APPNAME}}"
repository: "{{.APPNAME}}"
pullPolicy: Always
tag: "latest"
service:
annotations: {}
type: LoadBalancer
port: "{{SERVICEPORT}}"
port: "{{.SERVICEPORT}}"
33 changes: 12 additions & 21 deletions template/deployments/helm/charts/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "{{APPNAME}}.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}
{{ printf "{{- define \"%s.name\" -}}" .APPNAME }}
{{ printf "{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix \"-\" }}\n{{- end }}" }}
Vidya2606 marked this conversation as resolved.
Show resolved Hide resolved

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "{{APPNAME}}.fullname" -}}
{{- if .Values.fullnameOverride }}
{{ printf "{{- define \"%s.fullname\" -}}" .APPNAME }}
{{` {{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
Expand All @@ -21,31 +20,23 @@ If release name contains chart name it will be used as a full name.
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}
{{- end }} `}}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "{{APPNAME}}.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}
{{ printf "{{- define \"%s.chart\" -}}" .APPNAME }}
{{` {{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }} `}}

{{/*
Common labels
*/}}
{{- define "{{APPNAME}}.labels" -}}
helm.sh/chart: {{ include "{{APPNAME}}.chart" . }}
{{ include "{{APPNAME}}.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}
{{ printf "{{- define \"%s.labels\" -}}" .APPNAME }}
{{ printf "helm.sh/chart: {{ include \"%s.chart\" . }}\n{{ include \"%s.selectorLabels\" . }}\n{{- if .Chart.AppVersion }}\napp.kubernetes.io/version: {{ .Chart.AppVersion | quote }}\n{{- end }}\napp.kubernetes.io/managed-by: {{ .Release.Service }}\n{{- end }}" .APPNAME .APPNAME }}

{{/*
Selector labels
*/}}
{{- define "{{APPNAME}}.selectorLabels" -}}
app.kubernetes.io/name: {{ include "{{APPNAME}}.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
{{ printf "{{- define \"%s.selectorLabels\" -}}" .APPNAME }}
{{ printf "app.kubernetes.io/name: {{ include \"%s.name\" . }}\napp.kubernetes.io/instance: {{ .Release.Name }}\n{{- end }}" .APPNAME }}
Loading
Loading