-
Notifications
You must be signed in to change notification settings - Fork 766
Add "cyctl helm migrate" command to batch-migrate Helm releases to Cyclops Modules #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
0XSIKIPON
wants to merge
7
commits into
cyclops-ui:main
Choose a base branch
from
0XSIKIPON:feat/batch-helm-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e356877
feat: fetch and validate templates existence passed in the cyctl cli …
0XSIKIPON 980d50d
feat: Implement the functionality of retrieving values from the given…
0XSIKIPON 5c6540c
Add list-release functionality within given namesapce
0XSIKIPON e0582ca
Directly directly constructs and submits a MOdule CR via Cyclops client
0XSIKIPON ebebf3b
Refactor: Reuse the k8s client from /cyclops-ctrl/pkg, add cleanup re…
0XSIKIPON 3e241c5
Fix: fix k8sclient call with required parameters
0XSIKIPON 39722be
Refactor: revert to go 1.22.0 and toolchain go1.22.2 and to helm v3
0XSIKIPON File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,21 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| helmExample = ` # Migrate Helm releases to Cyclops modules | ||
| cyctl helm migrate --namespace mynamespace --repo https://charts.bitnami.com/bitnami --path postgresql --version 12.5.6` | ||
| ) | ||
|
|
||
| var helmCmd = &cobra.Command{ | ||
| Use: "helm", | ||
| Short: "Helm release related operations", | ||
| Long: "Helm release related operations like migrating releases to Cyclops modules", | ||
| Example: helmExample, | ||
| } | ||
|
|
||
| func init() { | ||
| RootCmd.AddCommand(helmCmd) | ||
| } |
This file contains hidden or 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,144 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/spf13/cobra" | ||
| "helm.sh/helm/v3/pkg/action" | ||
| "helm.sh/helm/v3/pkg/cli" | ||
|
|
||
| "github.com/cyclops-ui/cyclops/cyclops-ctrl/api/v1alpha1" | ||
| "github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient" | ||
| "github.com/cyclops-ui/cycops-cyctl/utility" | ||
|
|
||
| apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
||
| var ( | ||
| namespace string | ||
| repo string | ||
| path string | ||
| version string | ||
| releases []string | ||
|
|
||
| migrateExample = ` # Migrate specific Helm releases to Cyclops Modules | ||
| cyctl helm migrate --namespace myns --releases app1,app2,app3 --repo https://charts.bitnami.com/bitnami --path postgresql --version 12.5.6` | ||
| ) | ||
|
|
||
| var migrateCmd = &cobra.Command{ | ||
| Use: "migrate", | ||
| Short: "Migrate specific Helm releases to Cyclops Modules", | ||
| Long: "Migrate specified Helm releases to Cyclops Module CRs", | ||
| Example: migrateExample, | ||
| Run: runMigrate, | ||
| } | ||
|
|
||
| func init() { | ||
| migrateCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "namespace containing the Helm releases") | ||
| migrateCmd.Flags().StringSliceVarP(&releases, "releases", "r", []string{}, "comma-separated list of release names to migrate") | ||
| migrateCmd.Flags().StringVar(&repo, "repo", "", "repository URL containing the template") | ||
| migrateCmd.Flags().StringVar(&path, "path", "", "path to the template in the repository") | ||
| migrateCmd.Flags().StringVar(&version, "version", "", "version of the template") | ||
|
|
||
| migrateCmd.MarkFlagRequired("namespace") | ||
| migrateCmd.MarkFlagRequired("releases") | ||
| migrateCmd.MarkFlagRequired("repo") | ||
| migrateCmd.MarkFlagRequired("path") | ||
| migrateCmd.MarkFlagRequired("version") | ||
|
|
||
| helmCmd.AddCommand(migrateCmd) | ||
| } | ||
|
|
||
| func runMigrate(cmd *cobra.Command, args []string) { | ||
| // 1. Validate template existence | ||
| log.Printf("[1/3] Validating template %s/%s:%s …", repo, path, version) | ||
| if err := utility.ValidateTemplate(repo, path, version); err != nil { | ||
| log.Fatalf("Error validating template: %v", err) | ||
| } | ||
| log.Printf("✓ Template validated successfully") | ||
|
|
||
| // 2. Setup clients | ||
| log.Printf("[2/3] Setting up clients …") | ||
|
|
||
| // Create Kubernetes client for Module operations | ||
| k8sClient, err := k8sclient.New("cyclops", namespace, "", logr.Discard()) | ||
| if err != nil { | ||
| log.Fatalf("Error creating Kubernetes client: %v", err) | ||
| } | ||
|
|
||
| // Create Helm action configuration | ||
| settings := cli.New() | ||
| settings.SetNamespace(namespace) | ||
|
|
||
| actionConfig := new(action.Configuration) | ||
| if err := actionConfig.Init(settings.RESTClientGetter(), namespace, "", log.Printf); err != nil { | ||
| log.Fatalf("Error creating Helm action config: %v", err) | ||
| } | ||
|
|
||
| log.Printf("✓ Clients ready") | ||
|
|
||
| // 3. Migrate each specified release | ||
| log.Printf("[3/3] Migrating %d releases …", len(releases)) | ||
|
|
||
| successCount := 0 | ||
| for i, releaseName := range releases { | ||
| log.Printf("→ [%d/%d] Migrating release %q", i+1, len(releases), releaseName) | ||
|
|
||
| // Step 1: Validate release exists and get its values | ||
| getValues := action.NewGetValues(actionConfig) | ||
| getValues.AllValues = true | ||
|
|
||
| values, err := getValues.Run(releaseName) | ||
| if err != nil { | ||
| log.Printf(" • [ERROR] failed to get values for release %q: %v", releaseName, err) | ||
| continue | ||
| } | ||
|
|
||
| // Step 2: Marshal values to JSON | ||
| rawJSON, err := json.Marshal(values) | ||
| if err != nil { | ||
| log.Printf(" • [ERROR] failed to marshal values for %q: %v", releaseName, err) | ||
| continue | ||
| } | ||
|
|
||
| // Step 3: Create Module CR | ||
| module := v1alpha1.Module{ | ||
| TypeMeta: metav1.TypeMeta{ | ||
| Kind: "Module", | ||
| APIVersion: "cyclops-ui.com/v1alpha1", | ||
| }, | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: releaseName, | ||
| Namespace: "cyclops", | ||
| }, | ||
| Spec: v1alpha1.ModuleSpec{ | ||
| TargetNamespace: namespace, | ||
| TemplateRef: v1alpha1.TemplateRef{ | ||
| URL: repo, | ||
| Path: path, | ||
| Version: version, | ||
| }, | ||
| Values: apiextensionsv1.JSON{Raw: rawJSON}, | ||
| }, | ||
| } | ||
|
|
||
| if err := k8sClient.CreateModule(module); err != nil { | ||
| log.Printf(" • [ERROR] failed to create Module for %q: %v", releaseName, err) | ||
| continue | ||
| } | ||
|
|
||
| // Step 4: Clean up Helm release secrets (makes release disappear from 'helm list') | ||
| if err := k8sClient.DeleteReleaseSecret(releaseName, namespace); err != nil { | ||
| log.Printf(" • [WARNING] Module created but failed to clean up Helm release secret for %q: %v", releaseName, err) | ||
| log.Printf(" (Release will still appear in 'helm list' but Module is functional)") | ||
| } | ||
|
|
||
| successCount++ | ||
| log.Printf(" ✔ Successfully migrated %q → Module/%s", releaseName, releaseName) | ||
| } | ||
|
|
||
| log.Printf("Migration completed! %d/%d releases successfully migrated.", successCount, len(releases)) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the Kubernetes client from cyclops-ctrl/pkg instead? This way, you don't have to reimplement the utility package