Skip to content

Commit

Permalink
fixed #1 #2 and #3
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hernandez <[email protected]>
  • Loading branch information
christianh814 committed Feb 24, 2023
1 parent 372a248 commit 49a6c70
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 12 deletions.
23 changes: 20 additions & 3 deletions cmd/helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,31 @@ with kubectl.`,
// Do the migration automatically if that is set, if not print to stdout
if confirmMigrate {
log.Info("Migrating HelmRelease \"" + helmRelease.Name + "\" to Argo CD via an Application")
// Suspend reconcilation
helmRelease.Spec.Suspend = true
k.Update(ctx, helmRelease)
// Suspend helm reconciliation
if err := utils.SuspendFluxObject(k, ctx, helmRelease); err != nil {
log.Fatal(err)
}

// suspend helm repo reconciliation
if err := utils.SuspendFluxObject(k, ctx, helmRepo); err != nil {
log.Fatal(err)
}

// Finally, create the Argo CD Application
if err := utils.CreateK8SObjects(k, ctx, helmArgoCdApp); err != nil {
log.Fatal(err)
}

// Delete the HelmRelease
if err := utils.DeleteK8SObjects(k, ctx, helmRelease); err != nil {
log.Fatal(err)
}

// Delete the HelmRepo
if err := utils.DeleteK8SObjects(k, ctx, helmRepo); err != nil {
log.Fatal(err)
}

} else {
// Set the printer type to YAML
printr := printers.NewTypeSetter(k.Scheme()).ToPrinter(&printers.YAMLPrinter{})
Expand Down
21 changes: 18 additions & 3 deletions cmd/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,31 @@ with kubectl.`,

// Do the migration automatically if that is set, if not print to stdout
if confirmMigrate {
// Suspend reconcilation
kustomization.Spec.Suspend = true
k.Update(ctx, kustomization)
// Suspend kustomization reconcilation
if err := utils.SuspendFluxObject(k, ctx, kustomization); err != nil {
log.Fatal(err)
}

// Suspend the GitRepo reconcilation
if err := utils.SuspendFluxObject(k, ctx, gitSource); err != nil {
log.Fatal(err)
}

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

// If the migration is successful, delete the Kustomization and GitRepo
if err := utils.DeleteK8SObjects(k, ctx, kustomization); err != nil {
log.Fatal(err)
}

if err := utils.DeleteK8SObjects(k, ctx, gitSource); err != nil {
log.Fatal(err)
}

} else {
// Print the ApplicationSet and Secret to stdout

Expand Down
71 changes: 65 additions & 6 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,32 @@ func MigrateKustomizationToApplicationSet(c client.Client, ctx context.Context,
// Generate the ApplicationSet Secret and set the GVK
appsetSecret := GenK8SSecret(applicationSet)

// Suspend reconcilation
k.Spec.Suspend = true
c.Update(ctx, &k)
// Suspend Kustomization reconcilation
if err := SuspendFluxObject(c, ctx, &k); err != nil {
return err

}

// Suspend git repo reconcilation
if err := SuspendFluxObject(c, ctx, gitSource); err != nil {
return err
}

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

// Delete the Kustomization
if err := DeleteK8SObjects(c, ctx, &k); err != nil {
return err
}

// Delete the GitRepository
if err := DeleteK8SObjects(c, ctx, gitSource); err != nil {
return err
}

// If we're here, it should have gone okay...
return nil
}
Expand Down Expand Up @@ -125,15 +142,31 @@ func MigrateHelmReleaseToApplication(c client.Client, ctx context.Context, ans s
return err
}

// Suspend reconcilation
h.Spec.Suspend = true
c.Update(ctx, &h)
// Suspend helm reconcilation
if err := SuspendFluxObject(c, ctx, &h); err != nil {
return err
}

// Suspend helm repo reconcilation
if err := SuspendFluxObject(c, ctx, helmRepo); err != nil {
return err
}

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

// Delete the HelmRelease
if err := DeleteK8SObjects(c, ctx, &h); err != nil {
return err
}

// Delete the HelmRepository
if err := DeleteK8SObjects(c, ctx, helmRepo); err != nil {
return err
}

// If we're here, it should have gone okay...
return nil
}
Expand Down Expand Up @@ -180,6 +213,19 @@ func FluxCleanUp(k client.Client, ctx context.Context, log log.Logger, ns string
return nil
}

// SuspendFluxObject suspends Flux specific objects based on the schema passed in the client.
func SuspendFluxObject(c client.Client, ctx context.Context, obj ...client.Object) error {
// suspend the objects
for _, o := range obj {
if err := c.Patch(ctx, o, client.RawPatch(types.MergePatchType, []byte(`{"spec":{"suspend":true}}`))); err != nil {
return err
}
}

// If we're here, it should have gone okay...
return nil
}

// CreateK8SObjects Creates Kubernetes Objects on the Cluster based on the schema passed in the client.
func CreateK8SObjects(c client.Client, ctx context.Context, obj ...client.Object) error {
// Migrate the objects
Expand All @@ -193,6 +239,19 @@ func CreateK8SObjects(c client.Client, ctx context.Context, obj ...client.Object
return nil
}

// DeleteK8SObjects Deletes Kubernetes Objects on the Cluster based on the schema passed in the client.
func DeleteK8SObjects(c client.Client, ctx context.Context, obj ...client.Object) error {
// Migrate the objects
for _, o := range obj {
if err := c.Delete(ctx, o); err != nil {
return err
}
}

// If we're here, it should have gone okay...
return nil
}

// GenK8SSecret generates a kubernetes secret using a clientset
func GenK8SSecret(a argo.GitDirApplicationSet) *apiv1.Secret {
// Some Defaults
Expand Down

0 comments on commit 49a6c70

Please sign in to comment.