From fc5357be3f737fa28f1435bb0e194329c6a5b3ce Mon Sep 17 00:00:00 2001 From: Andriy Knysh Date: Fri, 3 Nov 2023 00:20:41 -0400 Subject: [PATCH] Update `depends_on` and `atmos describe dependents` (#471) * Add `overrides` section to Atmos stack manifests * Update `atmos describe dependents` * Update `atmos describe dependents` * Update `atmos describe dependents` --- .github/workflows/auto-format.yml | 89 ------------------------ README.md | 51 ++++---------- internal/exec/describe_dependents.go | 58 +++++++++------ pkg/describe/describe_dependents_test.go | 20 +++++- pkg/stack/stack_processor.go | 2 +- 5 files changed, 68 insertions(+), 152 deletions(-) delete mode 100644 .github/workflows/auto-format.yml diff --git a/.github/workflows/auto-format.yml b/.github/workflows/auto-format.yml deleted file mode 100644 index 39ef7e95b..000000000 --- a/.github/workflows/auto-format.yml +++ /dev/null @@ -1,89 +0,0 @@ -name: Auto Format -on: - pull_request_target: - types: [opened, synchronize] - -jobs: - auto-format: - runs-on: ubuntu-latest - container: cloudposse/build-harness:latest - steps: - # Checkout the pull request branch - # "An action in a workflow run can’t trigger a new workflow run. For example, if an action pushes code using - # the repository’s GITHUB_TOKEN, a new workflow will not run even when the repository contains - # a workflow configured to run when push events occur." - # However, using a personal access token will cause events to be triggered. - # We need that to ensure a status gets posted after the auto-format commit. - # We also want to trigger tests if the auto-format made no changes. - - uses: actions/checkout@v4 - if: github.event.pull_request.state == 'open' - name: Privileged Checkout - with: - token: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }} - repository: ${{ github.event.pull_request.head.repo.full_name }} - # Check out the PR commit, not the merge commit - # Use `ref` instead of `sha` to enable pushing back to `ref` - ref: ${{ github.event.pull_request.head.ref }} - - # Do all the formatting stuff - - name: Auto Format - if: ${{ (github.event.pull_request.state == 'open') && (github.actor != 'dependabot[bot]') }} - shell: bash - env: - GITHUB_TOKEN: "${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }}" - run: make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true -f /build-harness/templates/Makefile.build-harness pr/auto-format/host - - # Commit changes (if any) to the PR branch - - name: Commit changes to the PR branch - if: ${{ (github.event.pull_request.state == 'open') && (github.actor != 'dependabot[bot]') }} - shell: bash - id: commit - env: - SENDER: ${{ github.event.sender.login }} - run: | - set -x - output=$(git diff --name-only) - - if [ -n "$output" ]; then - echo "Changes detected. Pushing to the PR branch" - git config --global user.name 'cloudpossebot' - git config --global user.email '11232728+cloudpossebot@users.noreply.github.com' - git add -A - git commit -m "Auto Format" - # Prevent looping by not pushing changes in response to changes from cloudpossebot - [[ $SENDER == "cloudpossebot" ]] || git push - # Set status to fail, because the push should trigger another status check, - # and we use success to indicate the checks are finished. - printf "::set-output name=%s::%s\n" "changed" "true" - exit 1 - else - printf "::set-output name=%s::%s\n" "changed" "false" - echo "No changes detected" - fi - - - name: Auto Test - uses: cloudposse/actions/github/repository-dispatch@0.33.0 - # match users by ID because logins (user names) are inconsistent, - # for example in the REST API Renovate Bot is `renovate[bot]` but - # in GraphQL it is just `renovate`, plus there is a non-bot - # user `renovate` with ID 1832810. - # Mergify bot: 37929162 - # Renovate bot: 29139614 - # Cloudpossebot: 11232728 - # Need to use space separators to prevent "21" from matching "112144" - if: > - contains(' 37929162 29139614 11232728 ', format(' {0} ', github.event.pull_request.user.id)) - && steps.commit.outputs.changed == 'false' && github.event.pull_request.state == 'open' - && github.actor != 'dependabot[bot]' - with: - token: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }} - repository: cloudposse/actions - event-type: test-command - client-payload: |- - { "slash_command":{"args": {"unnamed": {"all": "all", "arg1": "all"}}}, - "pull_request": ${{ toJSON(github.event.pull_request) }}, - "github":{"payload":{"repository": ${{ toJSON(github.event.repository) }}, - "comment": {"id": ""} - } - } - } diff --git a/README.md b/README.md index 587c79bfa..e587b1f3e 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,6 @@ --- This project is part of our comprehensive ["SweetOps"](https://cpco.io/sweetops) approach towards DevOps. -[][share_email] -[][share_googleplus] -[][share_facebook] -[][share_reddit] -[][share_linkedin] -[][share_twitter] @@ -149,10 +143,6 @@ We deliver 10x the value for a fraction of the cost of a full-time engineer. Our Join our [Open Source Community][slack] on Slack. It's **FREE** for everyone! Our "SweetOps" community is where you get to talk with others who share a similar vision for how to rollout and manage infrastructure. This is the best place to talk shop, ask questions, solicit feedback, and work together as a community to build totally *sweet* infrastructure. -## Discourse Forums - -Participate in our [Discourse Forums][discourse]. Here you'll find answers to commonly asked questions. Most questions will be related to the enormous number of projects we support on our GitHub. Come here to collaborate on answers, find solutions, and get ideas about the products and services we value. It only takes a minute to get started! Just sign in with SSO using your GitHub account. - ## Newsletter Sign up for [our newsletter][newsletter] that covers everything on our technology radar. Receive updates on what we're up to on GitHub as well as awesome new projects we discover. @@ -163,7 +153,18 @@ Sign up for [our newsletter][newsletter] that covers everything on our technolog [![zoom](https://img.cloudposse.com/fit-in/200x200/https://cloudposse.com/wp-content/uploads/2019/08/Powered-by-Zoom.png")][office_hours] -## Contributing +## ✨ Contributing + + + +This project is under active development, and we encourage contributions from our community. +Many thanks to our outstanding contributors: + + + + + + ### Bug Reports & Feature Requests @@ -237,25 +238,7 @@ We're a [DevOps Professional Services][hire] company based in Los Angeles, CA. W We offer [paid support][commercial_support] on all of our projects. -Check out [our other projects][github], [follow us on twitter][twitter], [apply for a job][jobs], or [hire us][hire] to help with your cloud strategy and implementation. - - - -### Contributors - - -| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]
[Andriy Knysh][aknysh_homepage] | [![RB][nitrocode_avatar]][nitrocode_homepage]
[RB][nitrocode_homepage] | -|---|---|---| - - - [osterman_homepage]: https://github.com/osterman - [osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png - [aknysh_homepage]: https://github.com/aknysh - [aknysh_avatar]: https://img.cloudposse.com/150x150/https://github.com/aknysh.png - [nitrocode_homepage]: https://github.com/nitrocode - [nitrocode_avatar]: https://img.cloudposse.com/150x150/https://github.com/nitrocode.png - -[![README Footer][readme_footer_img]][readme_footer_link] +Check out [our other projects][github], [follow us on twitter][twitter], [apply for a job][jobs], or [hire us][hire] to help with your cloud strategy and implementation.[![README Footer][readme_footer_img]][readme_footer_link] [![Beacon][beacon]][website] [logo]: https://cloudposse.com/logo-300x69.svg @@ -265,12 +248,10 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply [jobs]: https://cpco.io/jobs?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=jobs [hire]: https://cpco.io/hire?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=hire [slack]: https://cpco.io/slack?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=slack - [linkedin]: https://cpco.io/linkedin?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=linkedin [twitter]: https://cpco.io/twitter?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=twitter [testimonial]: https://cpco.io/leave-testimonial?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=testimonial [office_hours]: https://cloudposse.com/office-hours?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=office_hours [newsletter]: https://cpco.io/newsletter?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=newsletter - [discourse]: https://ask.sweetops.com/?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=discourse [email]: https://cpco.io/email?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=email [commercial_support]: https://cpco.io/commercial-support?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=commercial_support [we_love_open_source]: https://cpco.io/we-love-open-source?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=we_love_open_source @@ -281,11 +262,5 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply [readme_footer_link]: https://cloudposse.com/readme/footer/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=readme_footer_link [readme_commercial_support_img]: https://cloudposse.com/readme/commercial-support/img [readme_commercial_support_link]: https://cloudposse.com/readme/commercial-support/link?utm_source=github&utm_medium=readme&utm_campaign=cloudposse/atmos&utm_content=readme_commercial_support_link - [share_twitter]: https://twitter.com/intent/tweet/?text=atmos&url=https://github.com/cloudposse/atmos - [share_linkedin]: https://www.linkedin.com/shareArticle?mini=true&title=atmos&url=https://github.com/cloudposse/atmos - [share_reddit]: https://reddit.com/submit/?url=https://github.com/cloudposse/atmos - [share_facebook]: https://facebook.com/sharer/sharer.php?u=https://github.com/cloudposse/atmos - [share_googleplus]: https://plus.google.com/share?url=https://github.com/cloudposse/atmos - [share_email]: mailto:?subject=atmos&body=https://github.com/cloudposse/atmos [beacon]: https://ga-beacon.cloudposse.com/UA-76589703-4/cloudposse/atmos?pixel&cs=github&cm=readme&an=atmos diff --git a/internal/exec/describe_dependents.go b/internal/exec/describe_dependents.go index cdeebb8b9..197ba6e44 100644 --- a/internal/exec/describe_dependents.go +++ b/internal/exec/describe_dependents.go @@ -77,20 +77,20 @@ func ExecuteDescribeDependents( return nil, err } - currentComponentSection, err := ExecuteDescribeComponent(component, stack) + providedComponentSection, err := ExecuteDescribeComponent(component, stack) if err != nil { return nil, err } - // Get the current component `vars` - var currentComponentVarsSection map[any]any - if currentComponentVarsSection, ok = currentComponentSection["vars"].(map[any]any); !ok { + // Get the provided component `vars` + var providedComponentVarsSection map[any]any + if providedComponentVarsSection, ok = providedComponentSection["vars"].(map[any]any); !ok { return dependents, nil } - // Convert the current component `vars` section to the `Context` structure - var currentComponentVars schema.Context - err = mapstructure.Decode(currentComponentVarsSection, ¤tComponentVars) + // Convert the provided component `vars` section to the `Context` structure + var providedComponentVars schema.Context + err = mapstructure.Decode(providedComponentVarsSection, &providedComponentVars) if err != nil { return nil, err } @@ -120,7 +120,7 @@ func ExecuteDescribeDependents( continue } - // Skip the stack component if it's the same as the current component + // Skip the stack component if it's the same as the provided component if stackComponentName == component { continue } @@ -166,41 +166,53 @@ func ExecuteDescribeDependents( continue } - // Check if the stack component is a dependent of the current component - for _, stackComponentSettingsContext := range stackComponentSettings.DependsOn { - if stackComponentSettingsContext.Component != component { + // Check if the stack component is a dependent of the provided component + for _, dependsOn := range stackComponentSettings.DependsOn { + if dependsOn.Component != component { continue } - if stackComponentSettingsContext.Namespace != "" { - if stackComponentSettingsContext.Namespace != stackComponentVars.Namespace { + // Include the component from the stack if any of the following is true: + // - `namespace` is specified in `depends_on` and the provided component's namespace is equal to the namespace in `depends_on` + // - `namespace` is not specified in `depends_on` and the provided component is from the same namespace as the component in `depends_on` + if dependsOn.Namespace != "" { + if providedComponentVars.Namespace != dependsOn.Namespace { continue } - } else if currentComponentVars.Namespace != stackComponentVars.Namespace { + } else if providedComponentVars.Namespace != stackComponentVars.Namespace { continue } - if stackComponentSettingsContext.Tenant != "" { - if stackComponentSettingsContext.Tenant != stackComponentVars.Tenant { + // Include the component from the stack if any of the following is true: + // - `tenant` is specified in `depends_on` and the provided component's tenant is equal to the tenant in `depends_on` + // - `tenant` is not specified in `depends_on` and the provided component is from the same tenant as the component in `depends_on` + if dependsOn.Tenant != "" { + if providedComponentVars.Tenant != dependsOn.Tenant { continue } - } else if currentComponentVars.Tenant != stackComponentVars.Tenant { + } else if providedComponentVars.Tenant != stackComponentVars.Tenant { continue } - if stackComponentSettingsContext.Environment != "" { - if stackComponentSettingsContext.Environment != stackComponentVars.Environment { + // Include the component from the stack if any of the following is true: + // - `environment` is specified in `depends_on` and the component's environment is equal to the environment in `depends_on` + // - `environment` is not specified in `depends_on` and the provided component is from the same environment as the component in `depends_on` + if dependsOn.Environment != "" { + if providedComponentVars.Environment != dependsOn.Environment { continue } - } else if currentComponentVars.Environment != stackComponentVars.Environment { + } else if providedComponentVars.Environment != stackComponentVars.Environment { continue } - if stackComponentSettingsContext.Stage != "" { - if stackComponentSettingsContext.Stage != stackComponentVars.Stage { + // Include the component from the stack if any of the following is true: + // - `stage` is specified in `depends_on` and the provided component's stage is equal to the stage in `depends_on` + // - `stage` is not specified in `depends_on` and the provided component is from the same stage as the component in `depends_on` + if dependsOn.Stage != "" { + if providedComponentVars.Stage != dependsOn.Stage { continue } - } else if currentComponentVars.Stage != stackComponentVars.Stage { + } else if providedComponentVars.Stage != stackComponentVars.Stage { continue } diff --git a/pkg/describe/describe_dependents_test.go b/pkg/describe/describe_dependents_test.go index b313beeb8..f55f6809b 100644 --- a/pkg/describe/describe_dependents_test.go +++ b/pkg/describe/describe_dependents_test.go @@ -22,7 +22,25 @@ func TestDescribeDependents(t *testing.T) { dependents, err := e.ExecuteDescribeDependents(cliConfig, component, stack) assert.Nil(t, err) - assert.Equal(t, 2, len(dependents)) + assert.Equal(t, 1, len(dependents)) + + dependentsYaml, err := yaml.Marshal(dependents) + assert.Nil(t, err) + t.Log(string(dependentsYaml)) +} + +func TestDescribeDependents2(t *testing.T) { + configAndStacksInfo := schema.ConfigAndStacksInfo{} + + cliConfig, err := cfg.InitCliConfig(configAndStacksInfo, true) + assert.Nil(t, err) + + component := "test/test-component" + stack := "tenant1-ue2-dev" + + dependents, err := e.ExecuteDescribeDependents(cliConfig, component, stack) + assert.Nil(t, err) + assert.Equal(t, 4, len(dependents)) dependentsYaml, err := yaml.Marshal(dependents) assert.Nil(t, err) diff --git a/pkg/stack/stack_processor.go b/pkg/stack/stack_processor.go index 440b86ece..86cb8369c 100644 --- a/pkg/stack/stack_processor.go +++ b/pkg/stack/stack_processor.go @@ -184,7 +184,7 @@ func ProcessYAMLConfigFile( } // Process `Go` templates in the stack manifest using the provided context - if !skipTemplatesProcessingInImports { + if !skipTemplatesProcessingInImports && len(context) > 0 { stackYamlConfig, err = u.ProcessTmpl(relativeFilePath, stackYamlConfig, context, ignoreMissingTemplateValues) if err != nil { return nil, nil, nil, err