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

Feature/rmk 29 refactor internal logic of rmk cluster provider initialization and selection #40

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
654e3b0
#29 - restructuring code files, refactoring
apanasiuk-el Aug 1, 2024
ba97fb5
#29 - fix command "config list" output for adjacent tenant names
apanasiuk-el Aug 5, 2024
b23a916
#29 - delete artifact-mode functionality
apanasiuk-el Aug 7, 2024
8d4acd5
#29 - draft refactoring 'rmk config init'
apanasiuk-el Aug 8, 2024
7da2c5f
#29 - deprecate Terraform commands, add commands for managing CAPI
apanasiuk-el Sep 25, 2024
5318f79
#29 - refactoring latest changes
apanasiuk-el Sep 26, 2024
12b1761
#29 - refactoring k3d list command
apanasiuk-el Sep 30, 2024
f1fd76b
#29 - change root-domain configuration
apanasiuk-el Oct 1, 2024
5ae37e4
#29 - refactoring
apanasiuk-el Oct 1, 2024
116bd15
#29 - refactoring
apanasiuk-el Oct 2, 2024
e7a2085
#29 - implement support Azure provider for Cluster API
apanasiuk-el Oct 23, 2024
0100612
#29 - implement support AWS provider for Cluster API
apanasiuk-el Oct 31, 2024
2204611
#29 - refactoring
apanasiuk-el Oct 31, 2024
8b518de
#29 - add creating SSH keys pair for AWS provider
apanasiuk-el Nov 6, 2024
1c0ca2f
#29 - refactoring
apanasiuk-el Nov 6, 2024
c4e02de
#29 - refactoring
apanasiuk-el Nov 6, 2024
016ac74
#29 - refactoring
apanasiuk-el Nov 6, 2024
c5fedc5
#29 - refactoring
apanasiuk-el Nov 7, 2024
8597c60
#29 - refactoring
apanasiuk-el Nov 8, 2024
57d810c
#29 - implement support GCP provider for Cluster API
apanasiuk-el Nov 21, 2024
1d7baf1
#29 - refactoring
apanasiuk-el Nov 21, 2024
a00f79d
#29 - refactoring
apanasiuk-el Nov 21, 2024
358d4e6
#29 - refactoring Google provider environment variables
apanasiuk-el Nov 25, 2024
57c742a
#29 - implement support Azure key vault for store SOPS Age keys
apanasiuk-el Dec 11, 2024
e37b7e3
#29 - refactoring
apanasiuk-el Dec 11, 2024
92ed29e
#29 - refactoring
apanasiuk-el Dec 11, 2024
81db188
#29 - implement support GCP secrets manager for store SOPS Age keys
apanasiuk-el Dec 16, 2024
50d34cf
#29 - refactoring
apanasiuk-el Dec 16, 2024
bf028e7
#29 - implement support AWS secrets manager for store SOPS Age keys
apanasiuk-el Dec 17, 2024
20e0ccf
#29 - refactoring
apanasiuk-el Dec 17, 2024
e1b43a7
#29 - add release notes
apanasiuk-el Dec 17, 2024
8b0d70f
#29 - refactoring release notes
apanasiuk-el Dec 17, 2024
6c9c60f
#29 - fix project generate behavior
apanasiuk-el Dec 18, 2024
cc25491
#29 - refactoring
apanasiuk-el Dec 18, 2024
ff3f732
#29 - resolved conflicts
apanasiuk-el Dec 19, 2024
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
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{
anovikov-el marked this conversation as resolved.
Show resolved Hide resolved
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",
anovikov-el marked this conversation as resolved.
Show resolved Hide resolved
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