Skip to content

Commit e07d955

Browse files
feat(appsets): add state upgrade logic for appsets in any namespace (#644)
* feat(appsets): add state upgrade logic for appsets in any namespace Signed-off-by: Nathanael Liechti <[email protected]> * Update argocd/schema_application_set.go Co-authored-by: Brian Fox <[email protected]> Signed-off-by: Nathanael Liechti <[email protected]> --------- Signed-off-by: Nathanael Liechti <[email protected]> Co-authored-by: Brian Fox <[email protected]>
1 parent c1f6c3d commit e07d955

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

argocd/resource_argocd_application_set.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ func resourceArgoCDApplicationSet() *schema.Resource {
2626
},
2727
Schema: map[string]*schema.Schema{
2828
"metadata": metadataSchema("applicationsets.argoproj.io"),
29-
"spec": applicationSetSpecSchemaV0(),
29+
"spec": applicationSetSpecSchemaV1(),
30+
},
31+
SchemaVersion: 1,
32+
StateUpgraders: []schema.StateUpgrader{
33+
{
34+
Type: resourceArgoCDApplicationV1().CoreConfigSchema().ImpliedType(),
35+
Upgrade: resourceArgoCDApplicationSetStateUpgradeV0,
36+
Version: 0,
37+
},
3038
},
3139
}
3240
}

argocd/resource_argocd_application_set_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package argocd
22

33
import (
44
"fmt"
5+
"reflect"
56
"regexp"
67
"testing"
78

@@ -1070,6 +1071,52 @@ func TestAccArgoCDApplicationSet_CustomNamespace(t *testing.T) {
10701071
})
10711072
}
10721073

1074+
func TestUpgradeSchemaApplicationSet_V0V1_Default_NoChange(t *testing.T) {
1075+
t.Parallel()
1076+
1077+
v0 := map[string]interface{}{
1078+
"metadata": []interface{}{
1079+
map[string]interface{}{
1080+
"name": "test",
1081+
"namespace": "argocd",
1082+
},
1083+
},
1084+
"spec": []interface{}{
1085+
map[string]interface{}{
1086+
"generator": []interface{}{
1087+
map[string]interface{}{
1088+
"clusters": []interface{}{map[string]interface{}{}},
1089+
},
1090+
},
1091+
"template": []interface{}{
1092+
map[string]interface{}{
1093+
"metadata": []interface{}{map[string]interface{}{
1094+
"name": "{{ name }}-clusters",
1095+
}},
1096+
"spec": []interface{}{map[string]interface{}{
1097+
"source": []interface{}{map[string]interface{}{
1098+
"repo_url": "https://github.com/argoproj/argocd-example-apps",
1099+
"target_revision": "HEAD",
1100+
"path": "guestbook",
1101+
}},
1102+
"destination": []interface{}{map[string]interface{}{
1103+
"server": "{{ server }}",
1104+
"namespace": "default",
1105+
}},
1106+
}},
1107+
},
1108+
},
1109+
},
1110+
},
1111+
}
1112+
1113+
actual, _ := resourceArgoCDApplicationStateUpgradeV0(t.Context(), v0, nil)
1114+
1115+
if !reflect.DeepEqual(v0, actual) {
1116+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", v0, actual)
1117+
}
1118+
}
1119+
10731120
func testAccArgoCDApplicationSet_clusters() string {
10741121
return `
10751122
resource "argocd_application_set" "clusters" {

argocd/schema_application_set.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package argocd
22

33
import (
4+
"context"
5+
"fmt"
6+
47
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
58
)
69

@@ -136,6 +139,15 @@ func applicationSetSpecSchemaV0() *schema.Schema {
136139
}
137140
}
138141

142+
func applicationSetSpecSchemaV1() *schema.Schema {
143+
// To support deploying applicationsets to non-default namespaces we need to
144+
// do a state migration to ensure that the Id on existing resources is
145+
// updated to include the namespace. For this to happen, we need to trigger
146+
// a schema version upgrade on the applicationset resource however, the
147+
// schema of the applicationset `spec` has not changed from `v0`.
148+
return applicationSetSpecSchemaV0()
149+
}
150+
139151
func applicationSetGeneratorSchemaV0() *schema.Schema {
140152
return &schema.Schema{
141153
Type: schema.TypeList,
@@ -146,6 +158,18 @@ func applicationSetGeneratorSchemaV0() *schema.Schema {
146158
}
147159
}
148160

161+
func resourceArgoCDApplicationSetStateUpgradeV0(_ context.Context, rawState map[string]interface{}, _ interface{}) (map[string]interface{}, error) {
162+
_metadata, ok := rawState["metadata"].([]interface{})
163+
if !ok || len(_metadata) == 0 {
164+
return nil, fmt.Errorf("failed to read metadata during state migration v0 to v1")
165+
}
166+
167+
metadata := _metadata[0].(map[string]interface{})
168+
rawState["id"] = fmt.Sprintf("%s:%s", metadata["name"].(string), metadata["namespace"].(string))
169+
170+
return rawState, nil
171+
}
172+
149173
func generatorResourceV0(level int) *schema.Resource {
150174
if level > 1 {
151175
return &schema.Resource{

0 commit comments

Comments
 (0)