-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[v2.9] allow overriding flux object spec via annotations
Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com>
- Loading branch information
Showing
9 changed files
with
198 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
patches-argo-cd-v2.9/23-allow-overriding-flux-object.patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
allow overriding flux object spec | ||
|
||
From: Chanwit Kaewkasi <chanwit@gmail.com> | ||
|
||
Signed-off-by: Chanwit Kaewkasi <chanwit@gmail.com> | ||
--- | ||
controller/sync_fsa.go | 42 +++++++++++++++++++++++++++++++++++++-- | ||
controller/sync_fsa_override.go | 38 +++++++++++++++++++++++++++++++++++ | ||
controller/sync_fsa_test.go | 41 +++++++++++++++++++++++++++++++++++--- | ||
3 files changed, 116 insertions(+), 5 deletions(-) | ||
create mode 100644 controller/sync_fsa_override.go | ||
|
||
diff --git a/controller/sync_fsa.go b/controller/sync_fsa.go | ||
index b4b71f98d..b2372d5bd 100644 | ||
--- a/controller/sync_fsa.go | ||
+++ b/controller/sync_fsa.go | ||
@@ -30,6 +30,22 @@ func mergeHelmParamsAndValues(app appv1.Application) (map[string]interface{}, er | ||
} | ||
|
||
func generateFluxHelmAndSource(app *appv1.Application) (*unstructured.Unstructured, *unstructured.Unstructured, error) { | ||
+ obj, src, err := generateFluxHelmAndHelmRepository(app) | ||
+ if err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ | ||
+ if err := overrideSpec(app, FlamingoHelmReleaseOverride, obj); err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ if err := overrideSpec(app, FlamingoHelmRepositoryOverride, src); err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ | ||
+ return obj, src, nil | ||
+} | ||
+ | ||
+func generateFluxHelmAndHelmRepository(app *appv1.Application) (*unstructured.Unstructured, *unstructured.Unstructured, error) { | ||
mergedValues, err := mergeHelmParamsAndValues(*app) | ||
if err != nil { | ||
return nil, nil, err | ||
@@ -135,9 +151,31 @@ spec: | ||
|
||
func generateFluxKustomizationAndSource(app *appv1.Application) (*unstructured.Unstructured, *unstructured.Unstructured, error) { | ||
if strings.HasPrefix(app.Spec.Source.RepoURL, "oci://") { | ||
- return generateFluxKustomizationAndOCISource(app) | ||
+ obj, src, err := generateFluxKustomizationAndOCISource(app) | ||
+ if err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ if err := overrideSpec(app, FlamingoKustomizeOverride, obj); err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ if err := overrideSpec(app, FlamingoOCIRepositoryOverride, src); err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ | ||
+ return obj, src, nil | ||
} else { | ||
- return generateFluxKustomizationAndGitSource(app) | ||
+ obj, src, err := generateFluxKustomizationAndGitSource(app) | ||
+ if err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ if err := overrideSpec(app, FlamingoKustomizeOverride, obj); err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ if err := overrideSpec(app, FlamingoGitRepositoryOverride, src); err != nil { | ||
+ return obj, src, err | ||
+ } | ||
+ | ||
+ return obj, src, nil | ||
} | ||
} | ||
|
||
diff --git a/controller/sync_fsa_override.go b/controller/sync_fsa_override.go | ||
new file mode 100644 | ||
index 000000000..0c124ebb4 | ||
--- /dev/null | ||
+++ b/controller/sync_fsa_override.go | ||
@@ -0,0 +1,38 @@ | ||
+package controller | ||
+ | ||
+import ( | ||
+ "github.com/imdario/mergo" | ||
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
+ "sigs.k8s.io/yaml" | ||
+ | ||
+ appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
+) | ||
+ | ||
+const ( | ||
+ FlamingoKustomizeOverride = "flamingo/kustomize-override" | ||
+ FlamingoHelmReleaseOverride = "flamingo/helmrelease-override" | ||
+ FlamingoHelmRepositoryOverride = "flamingo/helmrepository-override" | ||
+ FlamingoGitRepositoryOverride = "flamingo/gitrepository-override" | ||
+ FlamingoOCIRepositoryOverride = "flamingo/ocirepository-override" | ||
+) | ||
+ | ||
+func overrideSpec(app *appv1.Application, overrideKey string, u *unstructured.Unstructured) error { | ||
+ if app.Annotations == nil { | ||
+ return nil | ||
+ } | ||
+ | ||
+ object := u.Object | ||
+ if overrideSpec, ok := app.Annotations[overrideKey]; ok { | ||
+ spec := map[string]interface{}{} | ||
+ if err := yaml.Unmarshal([]byte(overrideSpec), &spec); err == nil { | ||
+ if err := mergo.Merge(&object, spec, mergo.WithOverride); err != nil { | ||
+ return err | ||
+ } | ||
+ } else { | ||
+ return err | ||
+ } | ||
+ } | ||
+ | ||
+ u.Object = object | ||
+ return nil | ||
+} | ||
diff --git a/controller/sync_fsa_test.go b/controller/sync_fsa_test.go | ||
index 9ed03c712..7bd72cabe 100644 | ||
--- a/controller/sync_fsa_test.go | ||
+++ b/controller/sync_fsa_test.go | ||
@@ -1,6 +1,7 @@ | ||
package controller | ||
|
||
import ( | ||
+ v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"testing" | ||
|
||
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
@@ -10,7 +11,7 @@ import ( | ||
func TestMergeValues(t *testing.T) { | ||
app := appv1.Application{ | ||
Spec: appv1.ApplicationSpec{ | ||
- Source: appv1.ApplicationSource{ | ||
+ Source: &appv1.ApplicationSource{ | ||
Helm: &appv1.ApplicationSourceHelm{ | ||
Parameters: []appv1.HelmParameter{ | ||
{ | ||
@@ -36,11 +37,45 @@ ui: | ||
assert.Equal(t, expected, actual, "should equal") | ||
} | ||
|
||
-/* | ||
func TestGenerateFluxHelmAndSource(t *testing.T) { | ||
- generateFluxHelmAndSource(app) | ||
+ app := &appv1.Application{ | ||
+ ObjectMeta: v1.ObjectMeta{ | ||
+ Name: "name", | ||
+ Annotations: map[string]string{ | ||
+ FlamingoKustomizeOverride: `spec: | ||
+ interval: 5m0s | ||
+ wait: false | ||
+`, | ||
+ }, | ||
+ }, | ||
+ Spec: appv1.ApplicationSpec{ | ||
+ Source: &appv1.ApplicationSource{ | ||
+ Kustomize: &appv1.ApplicationSourceKustomize{}, | ||
+ }, | ||
+ }, | ||
+ } | ||
+ | ||
+ ks, src, err := generateFluxKustomizationAndGitSource(app) | ||
+ assert.NoError(t, err, "should not have error") | ||
+ assert.NotNil(t, ks, "should not be nil") | ||
+ assert.NotNil(t, src, "should not be nil") | ||
+ assert.Equal(t, "Kustomization", ks.GetKind(), "should equal") | ||
+ assert.Equal(t, "name", ks.GetName(), "should equal") | ||
+ assert.Equal(t, "10m", ks.Object["spec"].(map[string]interface{})["interval"], "should equal") | ||
+ assert.Equal(t, true, ks.Object["spec"].(map[string]interface{})["wait"], "should equal") | ||
+ | ||
+ err = overrideSpec(app, FlamingoKustomizeOverride, ks) | ||
+ assert.NoError(t, err, "should not have error") | ||
+ assert.NotNil(t, ks, "should not be nil") | ||
+ assert.Equal(t, "Kustomization", ks.GetKind(), "should equal") | ||
+ assert.Equal(t, "name", ks.GetName(), "should equal") | ||
+ assert.Equal(t, "5m0s", ks.Object["spec"].(map[string]interface{})["interval"], "should equal") | ||
+ assert.Equal(t, false, ks.Object["spec"].(map[string]interface{})["wait"], "should equal") | ||
+ | ||
+ assert.Equal(t, "GitRepository", src.GetKind(), "should equal") | ||
} | ||
|
||
+/* | ||
func TestGenerateFluxKustomizationAndSource(t *testing.T) { | ||
generateFluxKustomizationAndSource(app) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters