Skip to content

Commit

Permalink
#29 - change root-domain configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
apanasiuk-el committed Oct 1, 2024
1 parent 12b1761 commit f1fd76b
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 195 deletions.
45 changes: 15 additions & 30 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,6 @@ func flagsConfig() []cli.Flag {
Value: util.AWSClusterProvider,
},
),
// TODO: will be transfer to cluster category for AWS provider
altsrc.NewStringFlag(
&cli.StringFlag{
Name: "root-domain",
Usage: "domain name for external access to app services via ingress controller",
Aliases: []string{"rd"},
EnvVars: []string{"RMK_ROOT_DOMAIN"},
},
),
altsrc.NewBoolFlag(
&cli.BoolFlag{
Name: "progress-bar",
Expand Down Expand Up @@ -144,27 +135,6 @@ func flagsClusterK3DImport() []cli.Flag {
)
}

//func flagsClusterPlan() []cli.Flag {
// return append(flagsHidden(),
// &cli.BoolFlag{
// Name: "plan",
// Usage: "creates an execution Terraform plan",
// Aliases: []string{"p"},
// },
// )
//}

//func flagsClusterStateDelete() []cli.Flag {
// return append(flagsHidden(),
// &cli.StringFlag{
// Name: "resource-address",
// Usage: "resource address for delete from Terraform state",
// Aliases: []string{"ra"},
// EnvVars: []string{"RMK_CLUSTER_STATE_RESOURCE_ADDRESS"},
// },
// )
//}

func flagsClusterSwitch() []cli.Flag {
return append(flagsHidden(),
&cli.BoolFlag{
Expand All @@ -182,6 +152,21 @@ func flagsProjectGenerate() []cli.Flag {
Usage: "create SOPS age keys for generated project structure",
Aliases: []string{"c"},
},
&cli.StringSliceFlag{
Name: "environments",
Usage: "list of project environments. Root domain can take form of <environment>.root-domain=<domain-name>",
Aliases: []string{"e"},
},
&cli.StringSliceFlag{
Name: "owners",
Usage: "list of project owners",
Aliases: []string{"o"},
},
&cli.StringSliceFlag{
Name: "scopes",
Usage: "list of project scopes",
Aliases: []string{"s"},
},
)
}

Expand Down
95 changes: 65 additions & 30 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,61 @@ func newProjectCommand(conf *config.Config, ctx *cli.Context, workDir string) *P
}
}

func (p *ProjectCommands) createProjectFile() error {
var buf bytes.Buffer

if !p.Ctx.IsSet("environments") || !p.Ctx.IsSet("scopes") {
return fmt.Errorf("%s file not found or values not set for flags: %s, %s",
util.GetPwdPath(util.TenantProjectFile), "environments", "scopes")
}

if p.Ctx.IsSet("environments") {
p.projectFile.Spec.Environments = make(map[string]*config.ProjectRootDomain)
for _, val := range p.Ctx.StringSlice("environments") {
if len(val) > 0 {
matchRootDomain := regexp.MustCompile(`^.+\.root-domain=.+$`).MatchString(val)
splitRootDomain := strings.SplitN(val, ".", 2)

if !matchRootDomain && len(splitRootDomain) == 2 {
return fmt.Errorf("option %s for environment %s set not correctly",
splitRootDomain[1], splitRootDomain[0])
}

if matchRootDomain && len(splitRootDomain) == 2 {
p.projectFile.Spec.Environments[splitRootDomain[0]] = &config.ProjectRootDomain{
RootDomain: strings.TrimPrefix(splitRootDomain[1], "root-domain="),
}
}

if !matchRootDomain || len(splitRootDomain) == 1 {
p.projectFile.Spec.Environments[splitRootDomain[0]] = &config.ProjectRootDomain{}
}
}
}
}

if p.Ctx.IsSet("owners") {
p.projectFile.Spec.Owners = p.Ctx.StringSlice("owners")
}

if p.Ctx.IsSet("scopes") {
p.projectFile.Spec.Scopes = p.Ctx.StringSlice("scopes")
}

encoder := yaml.NewEncoder(&buf)
encoder.SetIndent(2)
if err := encoder.Encode(&p.projectFile); err != nil {
return err
}

return os.WriteFile(util.GetPwdPath(util.TenantProjectFile), buf.Bytes(), 0644)
}

func (p *ProjectCommands) readProjectFile() error {
if !util.IsExists(util.GetPwdPath(util.TenantProjectFile), true) {
return fmt.Errorf("%s file not found", util.GetPwdPath(util.TenantProjectFile))
if err := p.createProjectFile(); err != nil {
return err
}
}

data, err := os.ReadFile(util.GetPwdPath(util.TenantProjectFile))
Expand Down Expand Up @@ -196,9 +248,15 @@ func (p *ProjectCommands) generateReadme(gitSpec *git_handler.GitSpec) error {
}

func (p *ProjectCommands) generateHelmfile() error {
sort.Strings(p.projectFile.Spec.Environments)
var environmentKeys = make([]string, 0, len(p.projectFile.Spec.Environments))

for key := range p.projectFile.Spec.Environments {
environmentKeys = append(environmentKeys, key)
}

sort.Strings(environmentKeys)

for key, name := range p.projectFile.Spec.Environments {
for key, name := range environmentKeys {
p.EnvironmentName = name
hEnvironments, err := p.Conf.ParseTemplate(template.New("Helmfile"), &p.parseContent, helmfileEnvironments)
if err != nil {
Expand Down Expand Up @@ -246,14 +304,6 @@ func (p *ProjectCommands) generateProjectFiles(gitSpec *git_handler.GitSpec) err
for _, sc := range p.scopes {
for _, env := range sc.environments {
switch sc.name {
case "clusters":
if err := p.writeProjectFiles(filepath.Join(env.valuesPath, util.TerraformVarsFile), clusterVariables); err != nil {
return err
}

if err := p.writeProjectFiles(filepath.Join(env.valuesPath, util.TerraformWGFile), clusterWorkerGroups); err != nil {
return err
}
case p.TenantName:
tGlobals, err := p.Conf.ParseTemplate(template.New("TenantGlobals"), &p.parseContent, tenantGlobals)
if err != nil {
Expand Down Expand Up @@ -299,10 +349,8 @@ func (p *ProjectCommands) generateProjectFiles(gitSpec *git_handler.GitSpec) err
}
}

if sc.name != "clusters" {
if err := p.writeProjectFiles(filepath.Join(env.secretsPath, util.SopsConfigFile), sopsConfigFile); err != nil {
return err
}
if err := p.writeProjectFiles(filepath.Join(env.secretsPath, util.SopsConfigFile), sopsConfigFile); err != nil {
return err
}
}
}
Expand Down Expand Up @@ -357,16 +405,7 @@ func (p *ProjectCommands) generateProject(gitSpec *git_handler.GitSpec) error {
for sKey, sc := range p.projectFile.Spec.Scopes {
p.Scopes = append(p.Scopes, sc)
p.scopes = append(p.scopes, scope{name: sc, environments: make(map[string]*environment)})
for _, env := range p.projectFile.Spec.Environments {
if sc == "clusters" {
p.scopes[sKey].environments[env] = &environment{
secretsPath: util.GetPwdPath(util.TenantValuesDIR, sc, p.Conf.ClusterProvider, env, "secrets"),
valuesPath: util.GetPwdPath(util.TenantValuesDIR, sc, p.Conf.ClusterProvider, env, "values"),
}

continue
}

for env := range p.projectFile.Spec.Environments {
p.scopes[sKey].environments[env] = &environment{
globalsPath: util.GetPwdPath(util.TenantValuesDIR, sc, env, util.GlobalsFileName),
releasesPath: util.GetPwdPath(util.TenantValuesDIR, sc, env, util.ReleasesFileName),
Expand Down Expand Up @@ -415,11 +454,7 @@ func projectGenerateAction(conf *config.Config, gitSpec *git_handler.GitSpec) cl
return err
}

if err := newProjectCommand(conf, c, util.GetPwdPath()).generateProject(gitSpec); err != nil {
return err
}

return resolveDependencies(conf.InitConfig(), c, false)
return newProjectCommand(conf, c, util.GetPwdPath()).generateProject(gitSpec)
}
}

Expand Down
50 changes: 0 additions & 50 deletions cmd/project_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const (
var (
TenantPrDependenciesDir = filepath.Join(util.TenantProjectDIR, "dependencies")
TenantPrInventoryDir = filepath.Join(util.TenantProjectDIR, "inventory")
TenantPrInvClustersDir = filepath.Join(TenantPrInventoryDir, "clusters")
TenantPrInvHooksDir = filepath.Join(TenantPrInventoryDir, "hooks")
)

Expand Down Expand Up @@ -206,11 +205,6 @@ func uniqueHooksMapping(hooks []config.HookMapping) []config.HookMapping {
}

func (is *InventoryState) saveState(inv config.Inventory) {
is.clustersState = make(map[string]struct{})
for key := range inv.Clusters {
is.clustersState[key] = struct{}{}
}

is.helmPluginsState = make(map[string]struct{})
for key := range inv.HelmPlugins {
is.helmPluginsState[key] = struct{}{}
Expand All @@ -222,26 +216,6 @@ func (is *InventoryState) saveState(inv config.Inventory) {
}
}

func (is *InventoryState) resolveClusters(invPkg map[string]*config.Package, conf *config.Config) (map[string]*config.Package, error) {
if len(conf.Clusters) == 0 {
conf.Clusters = make(map[string]*config.Package)
}

for key, pkg := range invPkg {
vPkg, _ := semver.NewVersion(pkg.Version)
if _, ok := conf.Clusters[key]; !ok {
conf.Clusters[key] = pkg
} else if _, found := is.clustersState[key]; !found {
vP, _ := semver.NewVersion(conf.Clusters[key].Version)
if vPkg.GreaterThan(vP) {
conf.Clusters[key] = pkg
}
}
}

return conf.Clusters, nil
}

func (is *InventoryState) resolveHelmPlugins(invPkg map[string]*config.Package, conf *config.Config) (map[string]*config.Package, error) {
if len(conf.HelmPlugins) == 0 {
conf.HelmPlugins = make(map[string]*config.Package)
Expand Down Expand Up @@ -308,11 +282,6 @@ func resolveDependencies(conf *config.Config, ctx *cli.Context, silent bool) err
return err
}

// Resolve and recursively download repositories containing clusters
if conf.Clusters, invErr = invState.resolveClusters(projectFile.Clusters, conf); invErr != nil {
return invErr
}

// Resolve and recursively download repositories containing helm plugins
if conf.HelmPlugins, invErr = invState.resolveHelmPlugins(projectFile.HelmPlugins, conf); invErr != nil {
return invErr
Expand Down Expand Up @@ -366,10 +335,6 @@ func resolveDependencies(conf *config.Config, ctx *cli.Context, silent bool) err
return err
}

if err := updateClusters(conf, ctx, silent); err != nil {
return err
}

// Finding unique versions of hooks in HooksMapping
conf.HooksMapping = uniqueHooksMapping(conf.HooksMapping)

Expand Down Expand Up @@ -464,21 +429,6 @@ func updateDependencies(conf *config.Config, ctx *cli.Context, silent bool) erro
return nil
}

func updateClusters(conf *config.Config, ctx *cli.Context, silent bool) error {
pwd := util.GetPwdPath(TenantPrInvClustersDir)

for key, val := range conf.Clusters {
spec := &SpecDownload{Conf: conf, Ctx: ctx, PkgDst: pwd, rmOldDir: true}
if err := spec.batchUpdate(pwd, *val, silent); err != nil {
return err
}

conf.Clusters[key].DstPath = spec.PkgDst
}

return nil
}

func updateHooks(conf *config.Config, ctx *cli.Context, silent bool) error {
pwd := util.GetPwdPath(TenantPrInvHooksDir)

Expand Down
Loading

0 comments on commit f1fd76b

Please sign in to comment.