Skip to content

Commit

Permalink
Decouple runtime name and namespace (#679)
Browse files Browse the repository at this point in the history
* fix

* fix

* fixes according to code review

* fixes

* bump
  • Loading branch information
danielm-codefresh authored Mar 14, 2023
1 parent 0507dd1 commit 9c96245
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.1.37
VERSION=v0.1.38

OUT_DIR=dist
YEAR?=$(shell date +"%Y")
Expand Down
12 changes: 10 additions & 2 deletions cmd/commands/git-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type (
GsCloneOpts *git.CloneOptions
GsName string
RuntimeName string
RuntimeNamespace string
CreateDemoResources bool
Exclude string
Include string
Expand Down Expand Up @@ -186,11 +187,18 @@ func NewGitSourceCreateCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

runtimeNamespace := args[0]
namespace := cmd.Flag("namespace").Value.String()
if namespace != "" {
runtimeNamespace = namespace
}

return RunGitSourceCreate(ctx, &GitSourceCreateOptions{
GsCloneOpts: gsCloneOpts,
GitProvider: gitProvider,
GsName: args[1],
RuntimeName: args[0],
RuntimeNamespace: runtimeNamespace,
CreateDemoResources: false,
Include: include,
Exclude: exclude,
Expand Down Expand Up @@ -223,7 +231,7 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
AppName: opts.GsName,
AppSpecifier: appSpecifier,
DestServer: store.Get().InCluster,
DestNamespace: &opts.RuntimeName,
DestNamespace: &opts.RuntimeNamespace,
IsInternal: &isInternal,
Include: &opts.Include,
Exclude: &opts.Exclude,
Expand Down Expand Up @@ -1424,7 +1432,7 @@ func legacyGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) er

appDef.IsInternal = util.StringIndexOf(store.Get().CFInternalGitSources, appDef.Name) > -1

if err := appDef.CreateApp(ctx, nil, opts.InsCloneOpts, opts.RuntimeName, store.Get().CFGitSourceType); err != nil {
if err := appDef.CreateApp(ctx, nil, opts.InsCloneOpts, opts.RuntimeName, opts.RuntimeNamespace, store.Get().CFGitSourceType); err != nil {
return fmt.Errorf("failed to create git-source application. Err: %w", err)
}

Expand Down
34 changes: 29 additions & 5 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
type (
RuntimeUninstallOptions struct {
RuntimeName string
RuntimeNamespace string
Timeout time.Duration
CloneOpts *apgit.CloneOptions
KubeFactory kube.Factory
Expand All @@ -71,6 +72,7 @@ type (

RuntimeUpgradeOptions struct {
RuntimeName string
RuntimeNamespace string
CloneOpts *apgit.CloneOptions
CommonConfig *runtime.CommonConfig
SuggestedSharedConfigRepo string
Expand Down Expand Up @@ -178,7 +180,7 @@ func runtimeUninstallCommandPreRunHandler(cmd *cobra.Command, args []string, opt
return err
}

if !opts.Managed {
if !opts.Managed && !opts.SkipChecks {
err = ensureRepo(cmd, opts.RuntimeName, opts.CloneOpts, true)
}
handleCliStep(reporter.UninstallStepPreCheckEnsureRuntimeRepo, "Getting runtime repo", err, true, false)
Expand Down Expand Up @@ -216,6 +218,8 @@ func runtimeUpgradeCommandPreRunHandler(cmd *cobra.Command, args []string, opts
return err
}

opts.RuntimeNamespace = *rt.Metadata.Namespace

if rt.Managed {
return fmt.Errorf("manual upgrades are not allowed for hosted runtimes and are managed by Codefresh operational team")
}
Expand Down Expand Up @@ -454,6 +458,7 @@ func NewRuntimeUninstallCommand() *cobra.Command {
finalParameters = map[string]string{
"Codefresh context": cfConfig.CurrentContext,
"Runtime name": opts.RuntimeName,
"Runtime namespace": opts.RuntimeNamespace,
}

if !opts.Managed {
Expand Down Expand Up @@ -541,7 +546,7 @@ func runRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err

if !opts.Managed {
err = apcmd.RunRepoUninstall(ctx, &apcmd.RepoUninstallOptions{
Namespace: opts.RuntimeName,
Namespace: opts.RuntimeNamespace,
KubeContextName: opts.kubeContext,
Timeout: opts.Timeout,
CloneOptions: opts.CloneOpts,
Expand Down Expand Up @@ -577,7 +582,7 @@ func runRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
}

if !opts.Managed {
err = runPostUninstallCleanup(ctx, opts.KubeFactory, opts.RuntimeName)
err = runPostUninstallCleanup(ctx, opts.KubeFactory, opts.RuntimeNamespace)
if err != nil {
errorMsg := fmt.Sprintf("failed to do post uninstall cleanup: %v", err)
if !opts.Force {
Expand Down Expand Up @@ -875,7 +880,7 @@ func runRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error {
log.G(ctx).Info("Downloading runtime definition")

runtimeDef := getRuntimeDef(opts.runtimeDef, opts.versionStr)
newRt, err := runtime.Download(runtimeDef, opts.RuntimeName, opts.featuresToInstall)
newRt, err := runtime.Download(runtimeDef, opts.RuntimeName, opts.RuntimeNamespace, opts.featuresToInstall)
handleCliStep(reporter.UpgradeStepDownloadRuntimeDefinition, "Downloading runtime definition", err, true, false)
if err != nil {
return fmt.Errorf("failed to download runtime definition: %w", err)
Expand Down Expand Up @@ -930,7 +935,7 @@ func runRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error {
for _, component := range newComponents {
log.G(ctx).Infof("Installing new component \"%s\"", component.Name)
component.IsInternal = true
err = component.CreateApp(ctx, nil, opts.CloneOpts, opts.RuntimeName, store.Get().CFComponentType)
err = component.CreateApp(ctx, nil, opts.CloneOpts, opts.RuntimeName, opts.RuntimeNamespace, store.Get().CFComponentType)
if err != nil {
err = fmt.Errorf("failed to create \"%s\" application: %w", component.Name, err)
break
Expand Down Expand Up @@ -1156,3 +1161,22 @@ func createAnalyticsReporter(ctx context.Context, flow reporter.FlowType, disabl

reporter.Init(user, flow)
}

func getRuntimeNamespace(cmd *cobra.Command, runtimeName string, runtimeVersion *semver.Version) string {
namespace := runtimeName
differentNamespaceSupportVer := semver.MustParse("0.1.26")
hasdifferentNamespaceSupport := runtimeVersion.GreaterThan(differentNamespaceSupportVer)

if !hasdifferentNamespaceSupport {
log.G().Infof("To specify a different namespace please use runtime version >= %s", differentNamespaceSupportVer.String())
_ = cmd.Flag("namespace").Value.Set("")
return namespace
}

namespaceVal := cmd.Flag("namespace").Value.String()
if namespaceVal != "" {
namespace = namespaceVal
}

return namespace
}
Loading

0 comments on commit 9c96245

Please sign in to comment.