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

chore: refactor actions code #2276

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d4feaf2
feat: introduce actions conditionals
Racer159 Jan 31, 2024
858702a
add GitLab rules as an option
Racer159 Jan 31, 2024
68cc598
big bang refactor of actions and variables
Racer159 Feb 1, 2024
85371c3
Merge branch 'main' into 2273-storage-class-check-edge-cases
Racer159 Feb 1, 2024
a79e0b4
Update schema and linting issues
Racer159 Feb 1, 2024
f5210f4
Fix tests
Racer159 Feb 1, 2024
4557cfc
Actually save actions.go
Racer159 Feb 1, 2024
1058121
Fix a few bugs
Racer159 Feb 1, 2024
88d4efb
A bit more cleanup to ActionConfig
Racer159 Feb 1, 2024
8677c1e
address some feedback and reorganize more code
Racer159 Feb 1, 2024
2c29846
Merge branch 'main' into 2273-storage-class-check-edge-cases
Racer159 Feb 1, 2024
561c330
add more messaging around gitea updates and fix agent updates
Racer159 Feb 1, 2024
4bae16b
remove component knowledge from Helm
Racer159 Feb 1, 2024
37c66b9
Merge branch 'main' into 2273-storage-class-check-edge-cases
Racer159 Feb 12, 2024
2c91d96
Remove if for now since this should focus on the refactor
Racer159 Feb 12, 2024
58074bb
Merge branch 'main' into 2273-storage-class-check-edge-cases
Racer159 Feb 20, 2024
ea70d21
Merge branch 'main' into 2273-storage-class-check-edge-cases
Racer159 Mar 7, 2024
8eb97f6
remove ADR for conditional actions
Racer159 Mar 7, 2024
bc4b66f
move Zarf component actions back
Racer159 Mar 8, 2024
1281433
Move actions validate to actions pkg
Racer159 Mar 8, 2024
2854b50
move variable to anonymous inline struct
Racer159 Mar 8, 2024
f27f37b
Remove unused func
Racer159 Mar 8, 2024
d26e5a2
Merge branch 'main' into 2273-storage-class-check-edge-cases
Racer159 Mar 8, 2024
cfb6efd
Fix linting
Racer159 Mar 8, 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
78 changes: 78 additions & 0 deletions adr/0023-conditional-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 22. Introduce Conditional Actions

Date: 2024-01-31

## Status

Pending

## Context

There are many cases where actions need to be conditional in order to accomplish some objective. The normal pattern thus far has been to use shell conditionals to achieve this though that results in actions that are no longer cross-platform since POSIX and Windows `if` statements do not line up cleanly. Below are a few scenarios that this functionality has been requested for:

- Optionally running a command to check or create something (i.e. a `storageclass` if the deployment optionally wants to setup a `pvc`)
- Setting a Zarf variable to a certain value based on a conditional check that will be used in further templating (i.e. looking up values from a command if `USE_X` is set to true)

---

There are a few ways that we can go down for something that answers these needs:

#### 1. Expand Windows Powershell Compatibility to change POSIX `if`s to Windows ones

**Pros**

- This functionality is already in Zarf and would only need to be expanded
- People could use shell syntax that they may already be familiar with

**Cons**

- This is very complicated to actually do in practice
- This doesn't reduce verbosity of conditional checks
- For those not familiar with shell `if`s they can be obtuse

#### 2. Implement similar syntax to a GitHub action `if`

**Pros**

- Relatively familiar and terse syntax
- Reduces verbosity of conditionals from the current state of shell `if`s
- Relatively easy to implement if we used go templating as the expression engine

**Cons**

- We would need to determine how we were going to evaluate the expression (likely go templates instead of js)
- Not as flexible since you can only evaluate a single expression

#### 3. Implement similar syntax to a GitLab job `rule`

**Pros**

- Relatively familiar with the simpler rulesets
- Slightly reduces verbosity of conditionals from the current state of shell `if`s
- Relatively easy to implement if we used go templating as the expression engine

**Cons**

- We would need to determine how we were going to evaluate the expression (likely go templates instead of ruby)
- Adds complexity with how rules can interact that may lead to confusion
- We would need to address the complexity around `when` (or omit it) since we already have onFailure / onSuccess action sets.

#### 4. Implement similar syntax to the `test` command

**Pros**

- Relatively familiar and terse syntax
- We could embed a `zarf tools test` subcommand
- Reduces verbosity of conditionals from the current state of shell `if`s

**Cons**

- Not familiar to everyone and can be obtuse like shell `if`s at times
- No `test` expression libraries exist in go so we would need to write our own
- Not as flexible since you can only evaluate a single expression

## Decision



## Consequences
54 changes: 35 additions & 19 deletions docs/3-create-a-zarf-package/4-zarf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActions |
| **Defined in** | #/definitions/Actions |

<details open>
<summary>
Expand All @@ -1707,7 +1707,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionSet |
| **Defined in** | #/definitions/ActionSet |

<details open>
<summary>
Expand All @@ -1724,7 +1724,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionDefaults |
| **Defined in** | #/definitions/ActionDefaults |

<details>
<summary>
Expand Down Expand Up @@ -1832,7 +1832,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionShell |
| **Defined in** | #/definitions/ExecShell |

<details>
<summary>
Expand Down Expand Up @@ -1923,13 +1923,13 @@ Must be one of:
![Item unicity: False](https://img.shields.io/badge/Item%20unicity%3A%20False-gold)
![Additional items: N/A](https://img.shields.io/badge/Additional%20items%3A%20N/A-gold)

### <a name="autogenerated_heading_21"></a>ZarfComponentAction
### <a name="autogenerated_heading_21"></a>Action

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentAction |
| **Defined in** | #/definitions/Action |

<details>
<summary>
Expand Down Expand Up @@ -2022,6 +2022,22 @@ Must be one of:
</blockquote>
</details>

<details>
<summary>
<strong> <a name="components_items_actions_onCreate_before_items_if"></a>if</strong>
</summary>
&nbsp;
<blockquote>

**Description:** A go expression controlling whether this action will execute

| | |
| -------- | -------- |
| **Type** | `string` |

</blockquote>
</details>

<details>
<summary>
<strong> <a name="components_items_actions_onCreate_before_items_cmd"></a>cmd</strong>
Expand Down Expand Up @@ -2078,13 +2094,13 @@ Must be one of:
![Item unicity: False](https://img.shields.io/badge/Item%20unicity%3A%20False-gold)
![Additional items: N/A](https://img.shields.io/badge/Additional%20items%3A%20N/A-gold)

### <a name="autogenerated_heading_23"></a>ZarfComponentActionSetVariable
### <a name="autogenerated_heading_23"></a>ActionSetVariable

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionSetVariable |
| **Defined in** | #/definitions/ActionSetVariable |

<details>
<summary>
Expand Down Expand Up @@ -2115,7 +2131,7 @@ Must be one of:
&nbsp;
<blockquote>

**Description:** Whether to mark this variable as sensitive to not print it in the Zarf log
**Description:** Whether to mark this variable as sensitive to not print it in the log

| | |
| -------- | --------- |
Expand Down Expand Up @@ -2212,7 +2228,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionWait |
| **Defined in** | #/definitions/ActionWait |

<details open>
<summary>
Expand All @@ -2229,7 +2245,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionWaitCluster |
| **Defined in** | #/definitions/ActionWaitCluster |

<details>
<summary>
Expand Down Expand Up @@ -2332,7 +2348,7 @@ Must be one of:
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfComponentActionWaitNetwork |
| **Defined in** | #/definitions/ActionWaitNetwork |

<details>
<summary>
Expand Down Expand Up @@ -2432,7 +2448,7 @@ Must be one of:
![Item unicity: False](https://img.shields.io/badge/Item%20unicity%3A%20False-gold)
![Additional items: N/A](https://img.shields.io/badge/Additional%20items%3A%20N/A-gold)

### <a name="autogenerated_heading_24"></a>ZarfComponentAction
### <a name="autogenerated_heading_24"></a>Action

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -2463,7 +2479,7 @@ Must be one of:
![Item unicity: False](https://img.shields.io/badge/Item%20unicity%3A%20False-gold)
![Additional items: N/A](https://img.shields.io/badge/Additional%20items%3A%20N/A-gold)

### <a name="autogenerated_heading_25"></a>ZarfComponentAction
### <a name="autogenerated_heading_25"></a>Action

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -2494,7 +2510,7 @@ Must be one of:
![Item unicity: False](https://img.shields.io/badge/Item%20unicity%3A%20False-gold)
![Additional items: N/A](https://img.shields.io/badge/Additional%20items%3A%20N/A-gold)

### <a name="autogenerated_heading_26"></a>ZarfComponentAction
### <a name="autogenerated_heading_26"></a>Action

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
Expand Down Expand Up @@ -2574,13 +2590,13 @@ Must be one of:
![Item unicity: False](https://img.shields.io/badge/Item%20unicity%3A%20False-gold)
![Additional items: N/A](https://img.shields.io/badge/Additional%20items%3A%20N/A-gold)

### <a name="autogenerated_heading_27"></a>ZarfPackageConstant
### <a name="autogenerated_heading_27"></a>Constant

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfPackageConstant |
| **Defined in** | #/definitions/Constant |

<details>
<summary>
Expand Down Expand Up @@ -2693,13 +2709,13 @@ Must be one of:
![Item unicity: False](https://img.shields.io/badge/Item%20unicity%3A%20False-gold)
![Additional items: N/A](https://img.shields.io/badge/Additional%20items%3A%20N/A-gold)

### <a name="autogenerated_heading_28"></a>ZarfPackageVariable
### <a name="autogenerated_heading_28"></a>Variable

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ZarfPackageVariable |
| **Defined in** | #/definitions/Variable |

<details>
<summary>
Expand Down
7 changes: 4 additions & 3 deletions packages/zarf-registry/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ components:
before:
- description: Check that the cluster has the specified storage class
maxTotalSeconds: 3
# if: ${{ .Zarf.Var.REGISTRY_PVC_ENABLED }}
wait:
cluster:
kind: storageclass
name: "\"${ZARF_STORAGE_CLASS}\""
cluster:
kind: storageclass
name: "\"${ZARF_STORAGE_CLASS}\""

- name: zarf-registry
description: |
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/common/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
)

// LogLevelCLI holds the log level as input from a command
var LogLevelCLI string

// SetupCLI sets up the CLI logging, interrupt functions, and more
func SetupCLI() {
exec.ExitOnInterrupt()
ExitOnInterrupt()

match := map[string]message.LogLevel{
"warn": message.WarnLevel,
Expand Down
21 changes: 21 additions & 0 deletions src/cmd/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
package common

import (
"os"
"os/signal"
"syscall"

"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/types"
)

// SuppressGlobalInterrupt suppresses the global error on an interrupt
var SuppressGlobalInterrupt = false

// SetBaseDirectory sets base directory on package config when given in args
func SetBaseDirectory(args []string, pkgConfig *types.PackagerConfig) {
if len(args) > 0 {
Expand All @@ -16,3 +25,15 @@ func SetBaseDirectory(args []string, pkgConfig *types.PackagerConfig) {
pkgConfig.CreateOpts.BaseDir = "."
}
}

// ExitOnInterrupt catches an interrupt and exits with fatal error
func ExitOnInterrupt() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if !SuppressGlobalInterrupt {
message.Fatal(lang.ErrInterrupt, lang.ErrInterrupt.Error())
}
}()
}
3 changes: 2 additions & 1 deletion src/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os/signal"
"syscall"

"github.com/defenseunicorns/zarf/src/cmd/common"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/k8s"
Expand Down Expand Up @@ -72,7 +73,7 @@ var (
// Keep this open until an interrupt signal is received.
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
exec.SuppressGlobalInterrupt = true
common.SuppressGlobalInterrupt = true

// Wait for the interrupt signal or an error.
select {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/defenseunicorns/zarf/src/internal/packager/helm"
"github.com/defenseunicorns/zarf/src/pkg/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -52,7 +52,7 @@ var destroyCmd = &cobra.Command{

// Run all the scripts!
pattern := regexp.MustCompile(`(?mi)zarf-clean-.+\.sh$`)
scripts, _ := utils.RecursiveFileList(config.ZarfCleanupScriptsPath, pattern, true)
scripts, _ := helpers.RecursiveFileList(config.ZarfCleanupScriptsPath, pattern, true)
// Iterate over all matching zarf-clean scripts and exec them
for _, script := range scripts {
// Run the matched script
Expand Down
Loading
Loading