Skip to content

Commit

Permalink
Merge pull request #44 from uselagoon/compose-go
Browse files Browse the repository at this point in the history
use compose-go for docker-compose validation
  • Loading branch information
shreddedbacon authored Jun 12, 2022
2 parents 7fc78bb + b9eaafc commit b7dd59d
Show file tree
Hide file tree
Showing 146 changed files with 2,549 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea
cmd/test-resources/template-routes/output
test-resources/output
38 changes: 27 additions & 11 deletions cmd/helpers_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"

composetypes "github.com/compose-spec/compose-go/types"
"github.com/uselagoon/build-deploy-tool/internal/helpers"
"github.com/uselagoon/build-deploy-tool/internal/lagoon"
"sigs.k8s.io/yaml"
Expand All @@ -27,6 +28,7 @@ func collectBuildValues(debug bool, activeEnv, standbyEnv *bool,
autogenRoutes *lagoon.RoutesV2,
mainRoutes *lagoon.RoutesV2,
activeStandbyRoutes *lagoon.RoutesV2,
ignoreNonStringKeyErrors bool,
) error {
var err error
// environment variables will override what is provided by flags
Expand Down Expand Up @@ -60,12 +62,6 @@ func collectBuildValues(debug bool, activeEnv, standbyEnv *bool,
_ = yaml.Unmarshal(s, &lYAML)
}

lCompose := lagoon.Compose{}
// unmarshal the docker-compose.yml file
if err = lagoon.UnmarshaDockerComposeYAML(lYAML.DockerComposeYAML, &lCompose); err != nil {
return err
}

lagoonValues.Project = projectName
lagoonValues.Environment = environmentName
lagoonValues.EnvironmentType = environmentType
Expand Down Expand Up @@ -121,17 +117,37 @@ func collectBuildValues(debug bool, activeEnv, standbyEnv *bool,
json.Unmarshal([]byte(projectVariables), &projectVars)
json.Unmarshal([]byte(environmentVariables), &envVars)
mergedVariables := lagoon.MergeVariables(projectVars, envVars)
// collect a bunch of the default LAGOON_X based build variables that are injected into `lagoon-env` and make them available
configVars := collectBuildVariables(*lagoonValues)
// add the calculated build runtime variables into the existing variable slice
// this will later be used to add `runtime|global` scope into the `lagoon-env` configmap
*lagoonEnvVars = lagoon.MergeVariables(mergedVariables, configVars)

composeVars := make(map[string]string)
for _, envvar := range *lagoonEnvVars {
// fmt.Println(envvar)
composeVars[envvar.Name] = envvar.Value
}

// create the services map
lagoonValues.Services = make(map[string]lagoon.ServiceValues)
lagoonServiceTypes, _ := lagoon.GetLagoonVariable("LAGOON_SERVICE_TYPES", []string{"build"}, mergedVariables)

// lCompose := composetypes.Project{}
// unmarshal the docker-compose.yml file
lCompose, err := lagoon.UnmarshaDockerComposeYAML(lYAML.DockerComposeYAML, ignoreNonStringKeyErrors, composeVars)
if err != nil {
return err
}
// fmt.Println(lCompose)

// convert docker-compose services to servicevalues
for csName, csValues := range lCompose.Services {
cService, err := composeToServiceValues(lYAML, lagoonValues, lagoonServiceTypes, csName, csValues)
for _, csValues := range lCompose.Services {
cService, err := composeToServiceValues(lYAML, lagoonValues, lagoonServiceTypes, csValues.Name, csValues)
if err != nil {
return err
}
lagoonValues.Services[csName] = cService
lagoonValues.Services[csValues.Name] = cService
}

// create all the routes for this environment and store the primary and secondary routes into values
Expand All @@ -152,14 +168,14 @@ func collectBuildValues(debug bool, activeEnv, standbyEnv *bool,
}

// collect a bunch of the default LAGOON_X based build variables that are injected into `lagoon-env` and make them available
configVars := collectBuildVariables(*lagoonValues)
configVars = collectBuildVariables(*lagoonValues)
// add the calculated build runtime variables into the existing variable slice
// this will later be used to add `runtime|global` scope into the `lagoon-env` configmap
*lagoonEnvVars = lagoon.MergeVariables(mergedVariables, configVars)
return nil
}

func composeToServiceValues(lYAML *lagoon.YAML, lagoonValues *lagoon.BuildValues, lagoonServiceTypes *lagoon.EnvironmentVariable, csName string, csValues lagoon.Service) (lagoon.ServiceValues, error) {
func composeToServiceValues(lYAML *lagoon.YAML, lagoonValues *lagoon.BuildValues, lagoonServiceTypes *lagoon.EnvironmentVariable, csName string, csValues composetypes.ServiceConfig) (lagoon.ServiceValues, error) {
lagoonType := lagoon.CheckServiceLagoonLabel(csValues.Labels, "lagoon.type")
autogenEnabled := true
// check if autogenerated routes are disabled
Expand Down
2 changes: 1 addition & 1 deletion cmd/identify_feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func IdentifyFeatureFlag(name string, debug bool) (string, error) {
autogenRoutes := &lagoon.RoutesV2{}
mainRoutes := &lagoon.RoutesV2{}
activeStanbyRoutes := &lagoon.RoutesV2{}
err := collectBuildValues(debug, &activeEnv, &standbyEnv, &lagoonEnvVars, &lagoonValues, &lYAML, autogenRoutes, mainRoutes, activeStanbyRoutes)
err := collectBuildValues(debug, &activeEnv, &standbyEnv, &lagoonEnvVars, &lagoonValues, &lYAML, autogenRoutes, mainRoutes, activeStanbyRoutes, ignoreNonStringKeyErrors)
if err != nil {
return "", err
}
Expand Down
24 changes: 12 additions & 12 deletions cmd/identify_feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestIdentifyFeatureFlag(t *testing.T) {
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "test-resources/template-ingress/single-lagoon.yml",
templatePath: "test-resources/template-ingress/output",
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
want: "enabled",
},
Expand All @@ -75,8 +75,8 @@ func TestIdentifyFeatureFlag(t *testing.T) {
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
secretPrefix: "fastly-api-",
lagoonYAML: "test-resources/template-ingress/single-lagoon.yml",
templatePath: "test-resources/template-ingress/output",
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
want: "enabled",
},
Expand All @@ -95,8 +95,8 @@ func TestIdentifyFeatureFlag(t *testing.T) {
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "test-resources/template-ingress/single-lagoon.yml",
templatePath: "test-resources/template-ingress/output",
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
vars: []struct {
name string
Expand Down Expand Up @@ -124,8 +124,8 @@ func TestIdentifyFeatureFlag(t *testing.T) {
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "test-resources/template-ingress/single-lagoon.yml",
templatePath: "test-resources/template-ingress/output",
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
vars: []struct {
name string
Expand Down Expand Up @@ -157,8 +157,8 @@ func TestIdentifyFeatureFlag(t *testing.T) {
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "test-resources/template-ingress/single-lagoon.yml",
templatePath: "test-resources/template-ingress/output",
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
vars: []struct {
name string
Expand Down Expand Up @@ -190,8 +190,8 @@ func TestIdentifyFeatureFlag(t *testing.T) {
projectVars: `[{"name":"LAGOON_SYSTEM_ROUTER_PATTERN","value":"${service}-${project}-${environment}.example.com","scope":"internal_system"},{"name":"LAGOON_FEATURE_FLAG_ROOTLESS_WORKLOAD","value":"enabled","scope":"build"}]`,
envVars: `[]`,
secretPrefix: "fastly-api-",
lagoonYAML: "test-resources/template-ingress/single-lagoon.yml",
templatePath: "test-resources/template-ingress/output",
lagoonYAML: "../test-resources/identify-feature/alltest/lagoon.yml",
templatePath: "../test-resources/output",
},
vars: []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion cmd/identify_ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func IdentifyPrimaryIngress(debug bool) (string, []string, []string, error) {
autogenRoutes := &lagoon.RoutesV2{}
mainRoutes := &lagoon.RoutesV2{}
activeStanbyRoutes := &lagoon.RoutesV2{}
err := collectBuildValues(debug, &activeEnv, &standbyEnv, &lagoonEnvVars, &lagoonValues, &lYAML, autogenRoutes, mainRoutes, activeStanbyRoutes)
err := collectBuildValues(debug, &activeEnv, &standbyEnv, &lagoonEnvVars, &lagoonValues, &lYAML, autogenRoutes, mainRoutes, activeStanbyRoutes, ignoreNonStringKeyErrors)
if err != nil {
return "", []string{}, []string{}, err
}
Expand Down
Loading

0 comments on commit b7dd59d

Please sign in to comment.