Skip to content

Commit 49a6c70

Browse files
committed
fixed #1 #2 and #3
Signed-off-by: Christian Hernandez <christian@chernand.io>
1 parent 372a248 commit 49a6c70

File tree

3 files changed

+103
-12
lines changed

3 files changed

+103
-12
lines changed

cmd/helmrelease.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,31 @@ with kubectl.`,
131131
// Do the migration automatically if that is set, if not print to stdout
132132
if confirmMigrate {
133133
log.Info("Migrating HelmRelease \"" + helmRelease.Name + "\" to Argo CD via an Application")
134-
// Suspend reconcilation
135-
helmRelease.Spec.Suspend = true
136-
k.Update(ctx, helmRelease)
134+
// Suspend helm reconciliation
135+
if err := utils.SuspendFluxObject(k, ctx, helmRelease); err != nil {
136+
log.Fatal(err)
137+
}
138+
139+
// suspend helm repo reconciliation
140+
if err := utils.SuspendFluxObject(k, ctx, helmRepo); err != nil {
141+
log.Fatal(err)
142+
}
137143

138144
// Finally, create the Argo CD Application
139145
if err := utils.CreateK8SObjects(k, ctx, helmArgoCdApp); err != nil {
140146
log.Fatal(err)
141147
}
148+
149+
// Delete the HelmRelease
150+
if err := utils.DeleteK8SObjects(k, ctx, helmRelease); err != nil {
151+
log.Fatal(err)
152+
}
153+
154+
// Delete the HelmRepo
155+
if err := utils.DeleteK8SObjects(k, ctx, helmRepo); err != nil {
156+
log.Fatal(err)
157+
}
158+
142159
} else {
143160
// Set the printer type to YAML
144161
printr := printers.NewTypeSetter(k.Scheme()).ToPrinter(&printers.YAMLPrinter{})

cmd/kustomization.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,31 @@ with kubectl.`,
151151

152152
// Do the migration automatically if that is set, if not print to stdout
153153
if confirmMigrate {
154-
// Suspend reconcilation
155-
kustomization.Spec.Suspend = true
156-
k.Update(ctx, kustomization)
154+
// Suspend kustomization reconcilation
155+
if err := utils.SuspendFluxObject(k, ctx, kustomization); err != nil {
156+
log.Fatal(err)
157+
}
158+
159+
// Suspend the GitRepo reconcilation
160+
if err := utils.SuspendFluxObject(k, ctx, gitSource); err != nil {
161+
log.Fatal(err)
162+
}
157163

158164
// Finally, create the ApplicationSet with the ApplicationSet Secret
159165
log.Info("Migrating Kustomization \"" + kustomization.Name + "\" to ArgoCD via an ApplicationSet")
160166
if err := utils.CreateK8SObjects(k, ctx, appsetSecret, appset); err != nil {
161167
log.Fatal(err)
162168
}
163169

170+
// If the migration is successful, delete the Kustomization and GitRepo
171+
if err := utils.DeleteK8SObjects(k, ctx, kustomization); err != nil {
172+
log.Fatal(err)
173+
}
174+
175+
if err := utils.DeleteK8SObjects(k, ctx, gitSource); err != nil {
176+
log.Fatal(err)
177+
}
178+
164179
} else {
165180
// Print the ApplicationSet and Secret to stdout
166181

pkg/utils/utils.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,32 @@ func MigrateKustomizationToApplicationSet(c client.Client, ctx context.Context,
7979
// Generate the ApplicationSet Secret and set the GVK
8080
appsetSecret := GenK8SSecret(applicationSet)
8181

82-
// Suspend reconcilation
83-
k.Spec.Suspend = true
84-
c.Update(ctx, &k)
82+
// Suspend Kustomization reconcilation
83+
if err := SuspendFluxObject(c, ctx, &k); err != nil {
84+
return err
85+
86+
}
87+
88+
// Suspend git repo reconcilation
89+
if err := SuspendFluxObject(c, ctx, gitSource); err != nil {
90+
return err
91+
}
8592

8693
// Finally, create the Argo CD Application
8794
if err := CreateK8SObjects(c, ctx, appsetSecret, appset); err != nil {
8895
return err
8996
}
9097

98+
// Delete the Kustomization
99+
if err := DeleteK8SObjects(c, ctx, &k); err != nil {
100+
return err
101+
}
102+
103+
// Delete the GitRepository
104+
if err := DeleteK8SObjects(c, ctx, gitSource); err != nil {
105+
return err
106+
}
107+
91108
// If we're here, it should have gone okay...
92109
return nil
93110
}
@@ -125,15 +142,31 @@ func MigrateHelmReleaseToApplication(c client.Client, ctx context.Context, ans s
125142
return err
126143
}
127144

128-
// Suspend reconcilation
129-
h.Spec.Suspend = true
130-
c.Update(ctx, &h)
145+
// Suspend helm reconcilation
146+
if err := SuspendFluxObject(c, ctx, &h); err != nil {
147+
return err
148+
}
149+
150+
// Suspend helm repo reconcilation
151+
if err := SuspendFluxObject(c, ctx, helmRepo); err != nil {
152+
return err
153+
}
131154

132155
// Finally, create the Argo CD Application
133156
if err := CreateK8SObjects(c, ctx, helmArgoCdApp); err != nil {
134157
return err
135158
}
136159

160+
// Delete the HelmRelease
161+
if err := DeleteK8SObjects(c, ctx, &h); err != nil {
162+
return err
163+
}
164+
165+
// Delete the HelmRepository
166+
if err := DeleteK8SObjects(c, ctx, helmRepo); err != nil {
167+
return err
168+
}
169+
137170
// If we're here, it should have gone okay...
138171
return nil
139172
}
@@ -180,6 +213,19 @@ func FluxCleanUp(k client.Client, ctx context.Context, log log.Logger, ns string
180213
return nil
181214
}
182215

216+
// SuspendFluxObject suspends Flux specific objects based on the schema passed in the client.
217+
func SuspendFluxObject(c client.Client, ctx context.Context, obj ...client.Object) error {
218+
// suspend the objects
219+
for _, o := range obj {
220+
if err := c.Patch(ctx, o, client.RawPatch(types.MergePatchType, []byte(`{"spec":{"suspend":true}}`))); err != nil {
221+
return err
222+
}
223+
}
224+
225+
// If we're here, it should have gone okay...
226+
return nil
227+
}
228+
183229
// CreateK8SObjects Creates Kubernetes Objects on the Cluster based on the schema passed in the client.
184230
func CreateK8SObjects(c client.Client, ctx context.Context, obj ...client.Object) error {
185231
// Migrate the objects
@@ -193,6 +239,19 @@ func CreateK8SObjects(c client.Client, ctx context.Context, obj ...client.Object
193239
return nil
194240
}
195241

242+
// DeleteK8SObjects Deletes Kubernetes Objects on the Cluster based on the schema passed in the client.
243+
func DeleteK8SObjects(c client.Client, ctx context.Context, obj ...client.Object) error {
244+
// Migrate the objects
245+
for _, o := range obj {
246+
if err := c.Delete(ctx, o); err != nil {
247+
return err
248+
}
249+
}
250+
251+
// If we're here, it should have gone okay...
252+
return nil
253+
}
254+
196255
// GenK8SSecret generates a kubernetes secret using a clientset
197256
func GenK8SSecret(a argo.GitDirApplicationSet) *apiv1.Secret {
198257
// Some Defaults

0 commit comments

Comments
 (0)