feat: allow to filter compose policies based on policy id (#8246) · updatecli/updatecli@2a69a93 · GitHub
Skip to content

Commit 2a69a93

Browse files
authored
feat: allow to filter compose policies based on policy id (#8246)
* feat: allow to filter compose policies based on policy id Signed-off-by: Olblak <me@olblak.com> * fix: missing edge case Signed-off-by: Olblak <me@olblak.com> --------- Signed-off-by: Olblak <me@olblak.com>
1 parent 410444c commit 2a69a93

8 files changed

Lines changed: 172 additions & 6 deletions

File tree

‎cmd/compose_apply.go‎

Lines changed: 9 additions & 1 deletion

‎cmd/compose_diff.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
)
1111

1212
var (
13+
composeDiffOnlyPolicyIDs []string
14+
composeDiffIgnoredPolicyIDs []string
15+
1316
composeDiffCmd = &cobra.Command{
1417
Use: "diff",
1518
Short: "diff show changes defined by the compose file",
@@ -24,7 +27,11 @@ var (
2427
manifests := []manifest.Manifest{}
2528
for i := range composeFiles {
2629
c := composeFiles[i]
27-
policies, err := c.GetPolicies(disableTLS)
30+
policies, err := c.GetPolicies(
31+
disableTLS,
32+
parseParametersList(composeDiffOnlyPolicyIDs),
33+
parseParametersList(composeDiffIgnoredPolicyIDs),
34+
)
2835
if err != nil {
2936
logrus.Errorf("command failed: %s", err)
3037
os.Exit(1)
@@ -56,6 +63,7 @@ func init() {
5663
composeDiffCmd.Flags().BoolVar(&disableTLS, "disable-tls", false, "Disable TLS verification like '--disable-tls=true'")
5764
composeDiffCmd.Flags().StringArrayVar(&pipelineIds, "pipeline-ids", []string{}, "Filter pipelines to apply by their IDs, accepted a comma separated list")
5865
composeDiffCmd.Flags().StringArrayVar(&labels, "labels", []string{}, "Filter pipelines to apply by their labels, accepted as a comma separated list (key:value)")
59-
66+
composeDiffCmd.Flags().StringArrayVar(&composeDiffOnlyPolicyIDs, "only-policy-ids", []string{}, "Filter policies to apply by their policy IDs, accepted as a comma separated list")
67+
composeDiffCmd.Flags().StringArrayVar(&composeDiffIgnoredPolicyIDs, "ignored-policy-ids", []string{}, "Filter policies to ignore by their policy IDs, accepted as a comma separated list")
6068
composeCmd.AddCommand(composeDiffCmd)
6169
}

‎cmd/compose_show.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
)
1212

1313
var (
14+
composeShowOnlyPolicyIDs []string
15+
composeShowIgnoredPolicyIDs []string
16+
1417
composeShowCmd = &cobra.Command{
1518
Use: "show",
1619
Short: "show manifest(s) defined by the compose file that should be executed",
@@ -25,7 +28,11 @@ var (
2528
manifests := []manifest.Manifest{}
2629
for i := range composeFiles {
2730
c := composeFiles[i]
28-
policies, err := c.GetPolicies(disableTLS)
31+
policies, err := c.GetPolicies(
32+
disableTLS,
33+
parseParametersList(composeShowOnlyPolicyIDs),
34+
parseParametersList(composeShowIgnoredPolicyIDs),
35+
)
2936
if err != nil {
3037
logrus.Errorf("command failed: %s", err)
3138
os.Exit(1)
@@ -59,6 +66,8 @@ func init() {
5966
composeShowCmd.Flags().BoolVar(&disableTLS, "disable-tls", false, "Disable TLS verification like '--disable-tls=true'")
6067
composeShowCmd.Flags().StringArrayVar(&pipelineIds, "pipeline-ids", []string{}, "Filter pipelines to apply by their pipeline IDs, accepted a comma separated list")
6168
composeShowCmd.Flags().StringArrayVar(&labels, "labels", []string{}, "Filter pipelines to apply by their labels, accepted as a comma separated list (key:value)")
69+
composeShowCmd.Flags().StringArrayVar(&composeShowOnlyPolicyIDs, "only-policy-ids", []string{}, "Filter policies to apply by their policy IDs, accepted as a comma separated list")
70+
composeShowCmd.Flags().StringArrayVar(&composeShowIgnoredPolicyIDs, "ignored-policy-ids", []string{}, "Filter policies to ignore by their policy IDs, accepted as a comma separated list")
6271

6372
composeCmd.AddCommand(composeShowCmd)
6473
}

‎cmd/utils.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// parseParametersList is a helper function to parse a list of parameters that can be passed as a comma separated list or as multiple values
8+
func parseParametersList(input []string) []string {
9+
result := []string{}
10+
11+
for i := range input {
12+
for j := range strings.Split(input[i], ",") {
13+
result = append(result, strings.TrimSpace(strings.Split(input[i], ",")[j]))
14+
}
15+
}
16+
17+
return result
18+
}

‎pkg/core/compose/main.go‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package compose
33
import (
44
"errors"
55
"fmt"
6+
"slices"
67

78
"github.com/sirupsen/logrus"
89
"github.com/updatecli/updatecli/pkg/core/engine/manifest"
@@ -80,7 +81,7 @@ func New(filename string, visitedComposeFiles map[string]bool) ([]Compose, error
8081
}
8182

8283
// GetPolicies returns a list of policies defined in the compose file
83-
func (c *Compose) GetPolicies(disableTLS bool) ([]manifest.Manifest, error) {
84+
func (c *Compose) GetPolicies(disableTLS bool, onlyPolicyIDs, ignoredPolicyIDs []string) ([]manifest.Manifest, error) {
8485

8586
manifests := []manifest.Manifest{}
8687
var errs []error
@@ -102,12 +103,42 @@ func (c *Compose) GetPolicies(disableTLS bool) ([]manifest.Manifest, error) {
102103
}
103104
}
104105

106+
var globalValues []string
107+
if len(c.spec.Values) > 0 {
108+
globalValues = relativePathToFile(c.filename, c.spec.Values)
109+
}
110+
111+
var secretFiles []string
112+
if len(c.spec.Secrets) > 0 {
113+
secretFiles = relativePathToFile(c.filename, c.spec.Secrets)
114+
}
115+
105116
// Fail fast if there is an error with the compose file before processing policies
106117
if len(errs) > 0 {
107118
return nil, fmt.Errorf("compose file %q errors: %s", c.name, errors.Join(errs...))
108119
}
109120

110121
for i := range c.spec.Policies {
122+
if len(ignoredPolicyIDs) > 0 && c.spec.Policies[i].ID != "" {
123+
if slices.Contains(ignoredPolicyIDs, c.spec.Policies[i].ID) {
124+
logrus.Debugf("Policy %q is ignored, skipping", c.spec.Policies[i].Name)
125+
continue
126+
}
127+
}
128+
129+
if len(onlyPolicyIDs) > 0 {
130+
131+
if c.spec.Policies[i].ID == "" {
132+
logrus.Debugf("Policy %q does not have an ID, skipping because only policies with IDs can be executed when the list of policies to execute is not empty", c.spec.Policies[i].Name)
133+
continue
134+
}
135+
136+
if !slices.Contains(onlyPolicyIDs, c.spec.Policies[i].ID) {
137+
logrus.Debugf("Policy %q is not in the list of policies to execute, skipping", c.spec.Policies[i].Name)
138+
continue
139+
}
140+
}
141+
111142
if c.spec.Policies[i].IsZero() {
112143
continue
113144
}
@@ -126,8 +157,17 @@ func (c *Compose) GetPolicies(disableTLS bool) ([]manifest.Manifest, error) {
126157
}
127158

128159
policyManifest = append(policyManifest, c.spec.Policies[i].Config...)
160+
161+
if len(globalValues) > 0 {
162+
policyValues = append(policyValues, globalValues...)
163+
}
129164
policyValues = append(policyValues, c.spec.Policies[i].Values...)
165+
166+
if len(secretFiles) > 0 {
167+
policySecrets = append(policySecrets, secretFiles...)
168+
}
130169
policySecrets = append(policySecrets, c.spec.Policies[i].Secrets...)
170+
131171
showDetectedFiles := func(files []string, fileType string) {
132172
switch len(files) {
133173
case 0:

‎pkg/core/compose/main_test.go‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,64 @@ func TestGetPolicies(t *testing.T) {
1717
file string
1818
expectedManifests [][]manifest.Manifest
1919
expectedEnv map[string]string
20+
ignorePolicyIDs []string
21+
onlyPolicyIDs []string
2022
}{
23+
{
24+
name: "Test getPolicies with only rules",
25+
file: "testdata/policies/updatecli-compose.yaml",
26+
onlyPolicyIDs: []string{
27+
"scm_enabled",
28+
},
29+
expectedManifests: [][]manifest.Manifest{
30+
{
31+
{
32+
Manifests: []string{
33+
filepath.Join(os.TempDir(), "updatecli", "store", "7aaff2727eef42f7d0add2d5ed3fd83f74a125420682bec7e4bc8835bb28e833", "updatecli.d", "default.tpl"),
34+
},
35+
Values: []string{
36+
filepath.Join(os.TempDir(), "updatecli", "store", "7aaff2727eef42f7d0add2d5ed3fd83f74a125420682bec7e4bc8835bb28e833", "values.yaml"),
37+
},
38+
ValuesInline: []string{
39+
"scm:\n enabled: true\n",
40+
},
41+
},
42+
},
43+
},
44+
},
45+
{
46+
name: "Test getPolicies with ignore rules",
47+
file: "testdata/policies/updatecli-compose.yaml",
48+
ignorePolicyIDs: []string{
49+
"scm_enabled",
50+
},
51+
expectedManifests: [][]manifest.Manifest{
52+
{
53+
{
54+
Manifests: []string{
55+
filepath.Join(os.TempDir(), "updatecli", "store", "7aaff2727eef42f7d0add2d5ed3fd83f74a125420682bec7e4bc8835bb28e833", "updatecli.d", "default.tpl"),
56+
},
57+
Values: []string{
58+
filepath.Join(os.TempDir(), "updatecli", "store", "7aaff2727eef42f7d0add2d5ed3fd83f74a125420682bec7e4bc8835bb28e833", "values.yaml"),
59+
},
60+
ValuesInline: []string{
61+
"scm:\n enabled: false\n",
62+
},
63+
},
64+
{
65+
Manifests: []string{
66+
filepath.Join(os.TempDir(), "updatecli", "store", "7aaff2727eef42f7d0add2d5ed3fd83f74a125420682bec7e4bc8835bb28e833", "updatecli.d", "default.tpl"),
67+
},
68+
Values: []string{
69+
filepath.Join(os.TempDir(), "updatecli", "store", "7aaff2727eef42f7d0add2d5ed3fd83f74a125420682bec7e4bc8835bb28e833", "values.yaml"),
70+
},
71+
ValuesInline: []string{
72+
"scm:\n enabled: 0\n",
73+
},
74+
},
75+
},
76+
},
77+
},
2178
{
2279
name: "Test getPolicies with environment variables",
2380
file: "testdata/simple/updatecli-compose.yaml",
@@ -99,7 +156,7 @@ func TestGetPolicies(t *testing.T) {
99156

100157
for i := range updatecliComposes {
101158
c := updatecliComposes[i]
102-
gotManifests, err := c.GetPolicies(false)
159+
gotManifests, err := c.GetPolicies(false, data.onlyPolicyIDs, data.ignorePolicyIDs)
103160
require.NoError(t, err)
104161

105162
assert.Equal(t, data.expectedManifests[i], gotManifests)

‎pkg/core/compose/spec.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ type Spec struct {
2525
// key1: value1
2626
// key2: value2
2727
ValuesInline *map[string]any `yaml:",omitempty"`
28+
// Values contains a list of Updatecli config file path
29+
// This is the default values file that will be applied to all policies if not overridden by the policy values file.
30+
Values []string `yaml:",omitempty"`
2831
// Include contains a list of compose files to include
2932
Include []string `yaml:",omitempty"`
33+
// Secrets contains a list of Updatecli secret file path
34+
// This is the default secret file that will be applied to all policies if not overridden by the policy secret file.
35+
Secrets []string `yaml:",omitempty"`
3036
}
3137

3238
type Policy struct {
@@ -47,6 +53,8 @@ type Policy struct {
4753
ValuesInline *map[string]any `yaml:",omitempty"`
4854
// Secrets contains a list of Updatecli secret file path
4955
Secrets []string `yaml:",omitempty"`
56+
// ID contains the policy ID, it can be used to filter policies to execute
57+
ID string `yaml:",omitempty"`
5058
}
5159

5260
func (p Policy) IsZero() bool {
Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)