-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial work on resource workload support
- Loading branch information
1 parent
24d525b
commit ae6847d
Showing
59 changed files
with
2,537 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}}) | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.