Skip to content

Commit

Permalink
feat: initial work on resource workload support
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jan 7, 2024
1 parent 24d525b commit ae6847d
Show file tree
Hide file tree
Showing 59 changed files with 2,537 additions and 79 deletions.
7 changes: 3 additions & 4 deletions cmd/template_autogen_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"io/fs"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -496,14 +495,14 @@ func TestAutogeneratedIngressGeneration(t *testing.T) {
t.Errorf("AutogeneratedIngressGeneration() error = %v, wantErr %v", err, tt.wantErr)
}

files, err := ioutil.ReadDir(savedTemplates)
files, err := os.ReadDir(savedTemplates)
if err != nil {
t.Errorf("couldn't read directory %v: %v", savedTemplates, err)
}
resultSize := 0
results := []fs.FileInfo{}
results := []fs.DirEntry{}
if !tt.emptyDir {
results, err = ioutil.ReadDir(tt.want)
results, err = os.ReadDir(tt.want)
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/template_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -213,11 +212,11 @@ func TestBackupTemplateGeneration(t *testing.T) {
if err := BackupTemplateGeneration(generator); (err != nil) != tt.wantErr {
t.Errorf("BackupTemplateGeneration() error = %v, wantErr %v", err, tt.wantErr)
}
files, err := ioutil.ReadDir(savedTemplates)
files, err := os.ReadDir(savedTemplates)
if err != nil {
t.Errorf("couldn't read directory %v: %v", savedTemplates, err)
}
results, err := ioutil.ReadDir(tt.want)
results, err := os.ReadDir(tt.want)
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/template_dbaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -58,11 +57,11 @@ func TestDBaaSTemplateGeneration(t *testing.T) {
if err := DBaaSTemplateGeneration(generator); (err != nil) != tt.wantErr {
t.Errorf("DBaaSTemplateGeneration() error = %v, wantErr %v", err, tt.wantErr)
}
files, err := ioutil.ReadDir(savedTemplates)
files, err := os.ReadDir(savedTemplates)
if err != nil {
t.Errorf("couldn't read directory %v: %v", savedTemplates, err)
}
results, err := ioutil.ReadDir(tt.want)
results, err := os.ReadDir(tt.want)
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/template_ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -400,11 +399,11 @@ func TestTemplateRoutes(t *testing.T) {
t.Errorf("%v", err)
}

files, err := ioutil.ReadDir(savedTemplates)
files, err := os.ReadDir(savedTemplates)
if err != nil {
t.Errorf("couldn't read directory %v: %v", savedTemplates, err)
}
results, err := ioutil.ReadDir(tt.want)
results, err := os.ReadDir(tt.want)
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
}
Expand Down
58 changes: 58 additions & 0 deletions cmd/template_resourceworkloads.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
generator "github.com/uselagoon/build-deploy-tool/internal/generator"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
hpatemplate "github.com/uselagoon/build-deploy-tool/internal/templating/resources/hpa"
pdbtemplate "github.com/uselagoon/build-deploy-tool/internal/templating/resources/pdb"
)

var resourceWorkloadGeneration = &cobra.Command{
Use: "resource-workloads",
Aliases: []string{"rw"},
Short: "Generate the resource workload templates for a Lagoon build",
RunE: func(cmd *cobra.Command, args []string) error {
generator, err := generator.GenerateInput(*rootCmd, true)
if err != nil {
return err
}
return ResourceWorkloadTemplateGeneration(generator)
},
}

// IngressTemplateGeneration .
func ResourceWorkloadTemplateGeneration(g generator.GeneratorInput) error {
lagoonBuild, err := generator.NewGenerator(
g,
)
if err != nil {
return err
}
savedTemplates := g.SavedTemplatesPath

// generate the templates
if g.Debug {
fmt.Println(fmt.Sprintf("Templating HPA manifests to %s", fmt.Sprintf("%s/%s.yaml", savedTemplates, "hpas")))
}
templateYAML, err := hpatemplate.GenerateHPATemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, "hpas"), templateYAML)
if g.Debug {
fmt.Println(fmt.Sprintf("Templating HPA manifests to %s", fmt.Sprintf("%s/%s.yaml", savedTemplates, "pdbs")))
}
templateYAML, err = pdbtemplate.GeneratePDBTemplate(*lagoonBuild.BuildValues)
if err != nil {
return fmt.Errorf("couldn't generate template: %v", err)
}
helpers.WriteTemplateFile(fmt.Sprintf("%s/%s.yaml", savedTemplates, "pdbs"), templateYAML)
return nil
}

func init() {
templateCmd.AddCommand(resourceWorkloadGeneration)
}
184 changes: 184 additions & 0 deletions cmd/template_resourceworkloads_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package cmd

import (
"fmt"
"os"
"reflect"
"testing"

"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/testdata"
)

func TestResourceWorkloadTemplateGeneration(t *testing.T) {
type args struct {
alertContact string
statusPageID string
projectName string
environmentName string
branch string
prNumber string
prHeadBranch string
prBaseBranch string
environmentType string
buildType string
activeEnvironment string
standbyEnvironment string
cacheNoCache string
serviceID string
secretPrefix string
ingressClass string
projectVars string
envVars string
lagoonVersion string
lagoonYAML string
valuesFilePath string
templatePath string
workloadJSONfile string
resourceWorkloadOverrides string
}
tests := []struct {
name string
// args args
args testdata.TestData
templatePath string
workloadJSONfile string
resourceWorkloadOverrides string
want string
}{
{
name: "test1 no resource workloads",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.yml",
}, true),
templatePath: "testdata/output",
want: "../internal/testdata/node/resource-templates/resource1",
},
{
name: "test2 node hpa",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/node/lagoon.resources1.yml",
}, true),
templatePath: "testdata/output",
workloadJSONfile: "../internal/testdata/node/workload.resources1.json",
want: "../internal/testdata/node/resource-templates/resource2",
},
{
name: "test3 nginx hpa and pdb",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.resource1.yml",
}, true),
templatePath: "testdata/output",
workloadJSONfile: "../internal/testdata/complex/workload.resources1.json",
want: "../internal/testdata/complex/resource-templates/resource1",
},
{
name: "test4 nginx hpa and pdb with resource override from feature flag",
args: testdata.GetSeedData(
testdata.TestData{
ProjectName: "example-project",
EnvironmentName: "main",
Branch: "main",
LagoonYAML: "../internal/testdata/complex/lagoon.yml",
}, true),
templatePath: "testdata/output",
workloadJSONfile: "../internal/testdata/complex/workload.resources2.json",
resourceWorkloadOverrides: "nginx-php:nginx-php-performance",
want: "../internal/testdata/complex/resource-templates/resource2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set the environment variables from args
err := os.Setenv("LAGOON_FEATURE_FLAG_DEFAULT_WORKLOAD_RESOURCES", helpers.ReadFileBase64Encode(tt.workloadJSONfile))
if err != nil {
t.Errorf("%v", err)
}
err = os.Setenv("LAGOON_FEATURE_FLAG_DEFAULT_WORKLOAD_RESOURCE_TYPES", tt.resourceWorkloadOverrides)
if err != nil {
t.Errorf("%v", err)
}
savedTemplates := tt.templatePath
generator, err := testdata.SetupEnvironment(*rootCmd, savedTemplates, tt.args)
if err != nil {
t.Errorf("%v", err)
}

err = os.MkdirAll(savedTemplates, 0755)
if err != nil {
t.Errorf("couldn't create directory %v: %v", savedTemplates, err)
}

defer os.RemoveAll(savedTemplates)

err = ResourceWorkloadTemplateGeneration(generator)
if err != nil {
t.Errorf("%v", err)
}

files, err := os.ReadDir(savedTemplates)
if err != nil {
t.Errorf("couldn't read directory %v: %v", savedTemplates, err)
}
results, err := os.ReadDir(tt.want)
if err != nil {
t.Errorf("couldn't read directory %v: %v", tt.want, err)
}
if len(files) != len(results) {
for _, f := range files {
f1, err := os.ReadFile(fmt.Sprintf("%s/%s", savedTemplates, f.Name()))
if err != nil {
t.Errorf("couldn't read file %v: %v", savedTemplates, err)
}
fmt.Println(string(f1))
}
t.Errorf("number of generated templates doesn't match results %v/%v: %v", len(files), len(results), err)
}
fCount := 0
for _, f := range files {
for _, r := range results {
if f.Name() == r.Name() {
fCount++
f1, err := os.ReadFile(fmt.Sprintf("%s/%s", savedTemplates, f.Name()))
if err != nil {
t.Errorf("couldn't read file %v: %v", savedTemplates, err)
}
r1, err := os.ReadFile(fmt.Sprintf("%s/%s", tt.want, f.Name()))
if err != nil {
t.Errorf("couldn't read file %v: %v", tt.want, err)
}
if !reflect.DeepEqual(f1, r1) {
fmt.Println(string(f1))
t.Errorf("resulting templates do not match")
}
}
}
}
if fCount != len(files) {
for _, f := range files {
f1, err := os.ReadFile(fmt.Sprintf("%s/%s", savedTemplates, f.Name()))
if err != nil {
t.Errorf("couldn't read file %v: %v", savedTemplates, err)
}
fmt.Println(string(f1))
}
t.Errorf("resulting templates do not match")
}
t.Cleanup(func() {
helpers.UnsetEnvVars([]helpers.EnvironmentVariable{{Name: "LAGOON_FEATURE_FLAG_DEFAULT_INGRESS_CLASS"}})
})
})
}
}
Loading

0 comments on commit ae6847d

Please sign in to comment.