Skip to content

Commit fcd73ea

Browse files
authored
Fix-migration-gs-ns (#724)
## What removed hard-coded namespace from git-source template ## Why it shouldn't be there. the namespace will be supplied by the in-cluster app that actually syncs those applications to the cluster. ## Notes <!-- Add any additional notes here -->
1 parent c614f06 commit fcd73ea

File tree

5 files changed

+48
-27
lines changed

5 files changed

+48
-27
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=v0.1.53
1+
VERSION=v0.1.54
22

33
OUT_DIR=dist
44
YEAR?=$(shell date +"%Y")

cmd/commands/migrate.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ import (
4747

4848
type (
4949
MigrateOptions struct {
50-
runtimeName string
51-
cloneOpts *apgit.CloneOptions
52-
helm helm.Helm
53-
kubeContext string
54-
kubeFactory apkube.Factory
50+
runtimeName string
51+
helmReleaseName string
52+
cloneOpts *apgit.CloneOptions
53+
helm helm.Helm
54+
kubeContext string
55+
kubeFactory apkube.Factory
5556
}
5657
)
5758

@@ -61,7 +62,7 @@ func NewMigrateCommand() *cobra.Command {
6162
cmd := &cobra.Command{
6263
Use: "migrate",
6364
Short: "migrate a cli-runtime to the new helm-runtime",
64-
Example: util.Doc("<BIN> helm migrate [RUNTIME_NAME]"),
65+
Example: util.Doc("<BIN> helm migrate [RUNTIME_NAME] --helm-release-name [HELM_RELEASE_NAME]"),
6566
Args: cobra.MaximumNArgs(1),
6667
PreRunE: func(cmd *cobra.Command, args []string) error {
6768
var err error
@@ -94,13 +95,15 @@ func NewMigrateCommand() *cobra.Command {
9495
},
9596
}
9697

98+
cmd.Flags().StringVarP(&opts.helmReleaseName, "helm-release-name", "r", "", "The expected helm release name, after the migration")
9799
opts.cloneOpts = apu.AddRepoFlags(cmd, &apu.CloneFlagsOptions{
98100
CreateIfNotExist: false,
99101
CloneForWrite: true,
100102
Optional: false,
101103
})
102104
opts.helm, _ = helm.AddFlags(cmd.Flags())
103105
opts.kubeFactory = apkube.AddFlags(cmd.Flags())
106+
util.Die(cobra.MarkFlagRequired(cmd.Flags(), "helm-release-name"))
104107

105108
return cmd
106109
}
@@ -179,7 +182,7 @@ func runHelmMigrate(ctx context.Context, opts *MigrateOptions) error {
179182
log.G(ctx).Infof("Pushed changes to shared-config-repo %q, sha: %s", *runtime.Repo, sha)
180183
log.G(ctx).Infof("Done migrating resources from %q to %q", *runtime.Repo, *user.ActiveAccount.SharedConfigRepo)
181184

182-
err = removeFromCluster(ctx, *runtime.Metadata.Namespace, opts.kubeContext, srcCloneOpts, opts.kubeFactory)
185+
err = removeFromCluster(ctx, opts.helmReleaseName, *runtime.Metadata.Namespace, opts.kubeContext, srcCloneOpts, opts.kubeFactory)
183186
if err != nil {
184187
return fmt.Errorf("failed removing runtime from cluster: %w", err)
185188
}
@@ -506,7 +509,7 @@ func addSuffix(str, suffix string, length int) string {
506509
return str[:length-len(suffix)] + suffix
507510
}
508511

509-
func removeFromCluster(ctx context.Context, runtimeNamespace, kubeContext string, cloneOptions *apgit.CloneOptions, kubeFactory apkube.Factory) error {
512+
func removeFromCluster(ctx context.Context, releaseName, runtimeNamespace, kubeContext string, cloneOptions *apgit.CloneOptions, kubeFactory apkube.Factory) error {
510513
err := switchManagedByLabel(ctx, kubeFactory, runtimeNamespace)
511514
if err != nil {
512515
return fmt.Errorf("failed preserving codefresh token secret: %w", err)
@@ -525,7 +528,7 @@ func removeFromCluster(ctx context.Context, runtimeNamespace, kubeContext string
525528
return fmt.Errorf("failed uninstalling runtime: %w", err)
526529
}
527530

528-
err = patchCrds(ctx, kubeFactory)
531+
err = patchCrds(ctx, kubeFactory, releaseName, runtimeNamespace)
529532
if err != nil {
530533
return fmt.Errorf("failed updating argoproj CRDs: %w", err)
531534
}
@@ -563,7 +566,7 @@ func switchManagedByLabel(ctx context.Context, kubeFactory apkube.Factory, names
563566
return nil
564567
}
565568

566-
func patchCrds(ctx context.Context, kubeFactory apkube.Factory) error {
569+
func patchCrds(ctx context.Context, kubeFactory apkube.Factory, releaseName, releaseNamespace string) error {
567570
gvr := schema.GroupVersionResource(apiextv1.SchemeGroupVersion.WithResource("customresourcedefinitions"))
568571
crdInterface := kube.GetDynamicClientOrDie(kubeFactory).Resource(gvr)
569572
crds, err := crdInterface.List(ctx, metav1.ListOptions{})
@@ -580,7 +583,7 @@ func patchCrds(ctx context.Context, kubeFactory apkube.Factory) error {
580583
ctx,
581584
crd.GetName(),
582585
types.StrategicMergePatchType,
583-
[]byte(getLabelPatch("Helm")),
586+
[]byte(getCrdPatch(releaseName, releaseNamespace)),
584587
metav1.PatchOptions{},
585588
)
586589
if err != nil {
@@ -601,6 +604,20 @@ func getLabelPatch(value string) string {
601604
return fmt.Sprintf(`{ "metadata": { "labels": { "%s": "%s" } } }`, apstore.Default.LabelKeyAppManagedBy, value)
602605
}
603606

607+
func getCrdPatch(releaseName, releaseNamespace string) string {
608+
return fmt.Sprintf(`{
609+
"metadata": {
610+
"annotations": {
611+
"%s": "%s",
612+
"%s": "%s"
613+
},
614+
"labels": {
615+
"%s": "Helm"
616+
}
617+
}
618+
}`, store.Get().AnnotationKeyReleaseName, releaseName, store.Get().AnnotationKeyReleaseNamespace, releaseNamespace, apstore.Default.LabelKeyAppManagedBy)
619+
}
620+
604621
func filterStatus(manifest []byte) []byte {
605622
lines := strings.Split(string(manifest), "\n")
606623
var res []string

docs/commands/cli-v2_migrate.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ cli-v2 migrate [flags]
99
### Examples
1010

1111
```
12-
cli-v2 helm migrate [RUNTIME_NAME]
12+
cli-v2 helm migrate [RUNTIME_NAME] --helm-release-name [HELM_RELEASE_NAME]
1313
```
1414

1515
### Options
1616

1717
```
18-
--context string The name of the kubeconfig context to use
19-
--devel use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored
20-
--git-server-crt string Git Server certificate file
21-
-t, --git-token string Your git provider api token [GIT_TOKEN]
22-
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
23-
-h, --help help for migrate
24-
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
25-
-n, --namespace string If present, the namespace scope for this CLI request
26-
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
27-
--version string specify a version constraint for the chart version to use. This constraint can be a specific tag (e.g. 1.1.1) or it may reference a valid range (e.g. ^2.0.0). If this is not specified, the latest version is used
18+
--context string The name of the kubeconfig context to use
19+
--devel use development versions, too. Equivalent to version '>0.0.0-0'. If --version is set, this is ignored
20+
--git-server-crt string Git Server certificate file
21+
-t, --git-token string Your git provider api token [GIT_TOKEN]
22+
-u, --git-user string Your git provider user name [GIT_USER] (not required in GitHub)
23+
-r, --helm-release-name string The expected helm release name, after the migration
24+
-h, --help help for migrate
25+
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
26+
-n, --namespace string If present, the namespace scope for this CLI request
27+
--request-timeout string The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
28+
--version string specify a version constraint for the chart version to use. This constraint can be a specific tag (e.g. 1.1.1) or it may reference a valid range (e.g. ^2.0.0). If this is not specified, the latest version is used
2829
```
2930

3031
### Options inherited from parent commands

pkg/store/store.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type Version struct {
5050
type Store struct {
5151
AddClusterJobName string
5252
AnalysisRunResourceName string
53+
AnnotationKeyReleaseName string
54+
AnnotationKeyReleaseNamespace string
5355
AnnotationKeySyncWave string
5456
AppProxyIngressName string
5557
AppProxyIngressPath string
@@ -112,8 +114,8 @@ type Store struct {
112114
GitTokensLink string
113115
GsCreateFlow string
114116
InClusterName string
115-
InClusterServerURL string
116117
InClusterPath string
118+
InClusterServerURL string
117119
IngressHost string
118120
InsecureIngressHost bool
119121
InternalRouterIngressFilePath string
@@ -125,10 +127,10 @@ type Store struct {
125127
IsDownloadRuntimeLogs bool
126128
KubeVersionConstrint *semver.Constraints
127129
LabelFieldCFType string
128-
LabelKeyCFInternal string
129-
LabelKeyCFType string
130130
LabelGitIntegrationTypeKey string
131131
LabelGitIntegrationTypeValue string
132+
LabelKeyCFInternal string
133+
LabelKeyCFType string
132134
LabelSelectorGitIntegrationSecret string
133135
LabelSelectorSealedSecret string
134136
LastRuntimeVersionInCLI *semver.Version
@@ -179,6 +181,8 @@ func (s *Store) IsCustomDefURL(orgRepo string) bool {
179181
func init() {
180182
s.AddClusterJobName = "csdp-add-cluster-job-"
181183
s.AnalysisRunResourceName = "analysisruns"
184+
s.AnnotationKeyReleaseName = "meta.helm.sh/release-name"
185+
s.AnnotationKeyReleaseNamespace = "meta.helm.sh/release-namespace"
182186
s.AnnotationKeySyncWave = "argocd.argoproj.io/sync-wave"
183187
s.AppProxyIngressName = "-cap-app-proxy"
184188
s.AppProxyIngressPath = "/app-proxy"

pkg/templates/git-source.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ metadata:
1111
codefresh.io/entity: '{{ index .Labels "codefresh_io_entity" }}'
1212
codefresh.io/internal: '{{ index .Labels "codefresh_io_internal" }}'
1313
name: '{{ .RuntimeName }}-{{ .UserGivenName }}'
14-
namespace: runtime
1514
spec:
1615
destination:
1716
namespace: '{{ .DestNamespace }}'

0 commit comments

Comments
 (0)