Skip to content

Commit

Permalink
Support {attributes} token in the `components.helmfile.cluster_name…
Browse files Browse the repository at this point in the history
…_pattern` CLI config (cloudposse#150)

* updates

* Support `{attributes}'' token in the `components.helmfile.cluster_name_pattern` CLI config
  • Loading branch information
aknysh authored May 19, 2022
1 parent 6d53c9c commit eaebbd0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 10 deletions.
66 changes: 66 additions & 0 deletions pkg/aws/atmos.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# CLI config is loaded from the following locations (from lowest to highest priority):
# system dir (`/usr/local/etc/atmos` on Linux, `%LOCALAPPDATA%/atmos` on Windows)
# home dir (~/.atmos)
# current directory
# ENV vars
# Command-line arguments
#
# It supports POSIX-style Globs for file names/paths (double-star `**` is supported)
# https://en.wikipedia.org/wiki/Glob_(programming)

# Base path for components, stacks and workflows configurations.
# Can also be set using `ATMOS_BASE_PATH` ENV var, or `--base-path` command-line argument.
# Supports both absolute and relative paths.
# If not provided or is an empty string, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
# are independent settings (supporting both absolute and relative paths).
# If `base_path` is provided, `components.terraform.base_path`, `components.helmfile.base_path`, `stacks.base_path` and `workflows.base_path`
# are considered paths relative to `base_path`.
base_path: "../../examples/complete"

components:
terraform:
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_BASE_PATH` ENV var, or `--terraform-dir` command-line argument
# Supports both absolute and relative paths
base_path: "components/terraform"
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_APPLY_AUTO_APPROVE` ENV var
apply_auto_approve: false
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_DEPLOY_RUN_INIT` ENV var, or `--deploy-run-init` command-line argument
deploy_run_init: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_INIT_RUN_RECONFIGURE` ENV var, or `--init-run-reconfigure` command-line argument
init_run_reconfigure: true
# Can also be set using `ATMOS_COMPONENTS_TERRAFORM_AUTO_GENERATE_BACKEND_FILE` ENV var, or `--auto-generate-backend-file` command-line argument
auto_generate_backend_file: false
helmfile:
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_BASE_PATH` ENV var, or `--helmfile-dir` command-line argument
# Supports both absolute and relative paths
base_path: "components/helmfile"
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_KUBECONFIG_PATH` ENV var
kubeconfig_path: "/dev/shm"
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_HELM_AWS_PROFILE_PATTERN` ENV var
helm_aws_profile_pattern: "{namespace}-{tenant}-gbl-{stage}-helm"
# Can also be set using `ATMOS_COMPONENTS_HELMFILE_CLUSTER_NAME_PATTERN` ENV var
cluster_name_pattern: "{namespace}-{tenant}-{environment}-{stage}-{attributes}-eks-cluster"

stacks:
# Can also be set using `ATMOS_STACKS_BASE_PATH` ENV var, or `--config-dir` and `--stacks-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "stacks"
# Can also be set using `ATMOS_STACKS_INCLUDED_PATHS` ENV var (comma-separated values string)
included_paths:
- "**/*"
# Can also be set using `ATMOS_STACKS_EXCLUDED_PATHS` ENV var (comma-separated values string)
excluded_paths:
- "globals/**/*"
- "catalog/**/*"
- "**/*globals*"
# Can also be set using `ATMOS_STACKS_NAME_PATTERN` ENV var
name_pattern: "{tenant}-{environment}-{stage}"

workflows:
# Can also be set using `ATMOS_WORKFLOWS_BASE_PATH` ENV var, or `--workflows-dir` command-line arguments
# Supports both absolute and relative paths
base_path: "workflows"

logs:
verbose: false
colors: true
33 changes: 33 additions & 0 deletions pkg/aws/aws_eks_update_kubeconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package aws

import (
"fmt"
c "github.com/cloudposse/atmos/pkg/config"
u "github.com/cloudposse/atmos/pkg/utils"
"github.com/stretchr/testify/assert"
"testing"
)

func TestClusterNamePattern(t *testing.T) {
// InitConfig finds and processes `atmos.yaml` CLI config
err := c.InitConfig()
assert.Nil(t, err)

// Define variables for a component in a stack
componentVars := map[interface{}]interface{}{
"namespace": "eg",
"tenant": "plat",
"environment": "ue2",
"stage": "dev",
"attributes": []string{"blue"},
}

// Build `Context` from the variables
context := c.GetContextFromVars(componentVars)

// Build EKS cluster name using the `components.helmfile.cluster_name_pattern` config from `atmos.yaml`
// cluster_name_pattern: "{namespace}-{tenant}-{environment}-{stage}-{attributes}-eks-cluster"
clusterName := c.ReplaceContextTokens(context, c.Config.Components.Helmfile.ClusterNamePattern)
u.PrintInfo(fmt.Sprintf("Cluster name: %s", clusterName))
assert.Equal(t, "eg-plat-ue2-dev-blue-eks-cluster", clusterName)
}
1 change: 1 addition & 0 deletions pkg/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Context struct {
Region string
Component string
BaseComponent string
Attributes []string
}

type ArgsAndFlagsInfo struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ func GetContextFromVars(vars map[interface{}]interface{}) Context {
context.Region = region
}

if attributes, ok := vars["attributes"].([]string); ok {
context.Attributes = attributes
}

return context
}

Expand Down Expand Up @@ -437,6 +441,7 @@ func ReplaceContextTokens(context Context, pattern string) string {
"{environment}", context.Environment,
"{tenant}", context.Tenant,
"{stage}", context.Stage,
"{attributes}", strings.Join(context.Attributes, "-"),
)
return r.Replace(pattern)
}
Expand Down
18 changes: 9 additions & 9 deletions website/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"remark-html": "^13.0.1",
"remark-parse": "^8.0.3",
"sass": "^1.44.0",
"semver": "^7.3.5"
"semver": "^7.3.5",
"nth-check": "^2.0.1"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "0.0.0-4192",
Expand Down

0 comments on commit eaebbd0

Please sign in to comment.