Skip to content

Commit 4b7f15f

Browse files
CR-9648 uninstall runtime on different kube context bug (#456)
* get current context name explicitly * ensuring runtime is on chosen kube context * logs * bump version * small change
1 parent ed019c8 commit 4b7f15f

File tree

7 files changed

+66
-25
lines changed

7 files changed

+66
-25
lines changed

Makefile

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

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

cmd/commands/common.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,11 @@ func getKubeContextName(context, kubeconfig *pflag.Flag) (string, error) {
425425
}
426426
}
427427

428+
if contextName == "" {
429+
contextName = util.KubeCurrentContextName(kubeconfigPath)
430+
log.G().Infof("Using current kube context '%s'", contextName)
431+
}
432+
428433
return contextName, context.Value.Set(contextName)
429434
}
430435

@@ -657,3 +662,23 @@ func isRuntimeManaged(ctx context.Context, runtimeName string) (bool, error) {
657662

658663
return rt.Managed, nil
659664
}
665+
666+
func ensureRuntimeOnKubeContext(ctx context.Context, kubeconfig string, runtimeName string, kubeContextName string) error {
667+
rt, err := cfConfig.NewClient().V2().Runtime().Get(ctx, runtimeName)
668+
if err != nil {
669+
return fmt.Errorf("failed to get runtime from platform. error: %w", err)
670+
}
671+
672+
runtimeClusterServer := rt.Cluster
673+
674+
kubeContextServer, err := util.KubeServerByContextName(kubeContextName, kubeconfig)
675+
if err != nil {
676+
return err
677+
}
678+
679+
if *runtimeClusterServer != kubeContextServer {
680+
return fmt.Errorf("runtime '%s' does not exist on context '%s'. make sure you are providing the right kube context", runtimeName, kubeContextName)
681+
}
682+
683+
return nil
684+
}

cmd/commands/runtime.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,15 @@ func runtimeUninstallCommandPreRunHandler(cmd *cobra.Command, args []string, opt
455455
return err
456456
}
457457

458+
if !opts.Managed {
459+
kubeconfig := cmd.Flag("kubeconfig").Value.String()
460+
err = ensureRuntimeOnKubeContext(cmd.Context(), kubeconfig, opts.RuntimeName, opts.kubeContext)
461+
}
462+
handleCliStep(reporter.UninstallStepPreCheckEnsureRuntimeOnKubeContext, "Ensuring runtime is on the kube context", err, true, false)
463+
if err != nil {
464+
return err
465+
}
466+
458467
if !opts.Managed {
459468
err = ensureRepo(cmd, opts.RuntimeName, opts.CloneOpts, true)
460469
}
@@ -1637,12 +1646,13 @@ func RunRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
16371646

16381647
if !opts.Managed {
16391648
err = apcmd.RunRepoUninstall(ctx, &apcmd.RepoUninstallOptions{
1640-
Namespace: opts.RuntimeName,
1641-
Timeout: opts.Timeout,
1642-
CloneOptions: opts.CloneOpts,
1643-
KubeFactory: opts.KubeFactory,
1644-
Force: opts.Force,
1645-
FastExit: opts.FastExit,
1649+
Namespace: opts.RuntimeName,
1650+
KubeContextName: opts.kubeContext,
1651+
Timeout: opts.Timeout,
1652+
CloneOptions: opts.CloneOpts,
1653+
KubeFactory: opts.KubeFactory,
1654+
Force: opts.Force,
1655+
FastExit: opts.FastExit,
16461656
})
16471657
}
16481658
cancel() // to tell the progress to stop displaying even if it's not finished

docs/releases/release_notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ cf version
2323

2424
```bash
2525
# download and extract the binary
26-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.403/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.404/cf-linux-amd64.tar.gz | tar zx
2727

2828
# move the binary to your $PATH
2929
mv ./cf-linux-amd64 /usr/local/bin/cf
@@ -36,7 +36,7 @@ cf version
3636

3737
```bash
3838
# download and extract the binary
39-
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.403/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.404/cf-darwin-amd64.tar.gz | tar zx
4040

4141
# move the binary to your $PATH
4242
mv ./cf-darwin-amd64 /usr/local/bin/cf

manifests/runtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ metadata:
55
namespace: "{{ namespace }}"
66
spec:
77
defVersion: 1.0.1
8-
version: 0.0.403
8+
version: 0.0.404
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

pkg/reporter/reporter.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,22 @@ const (
9696
InstallPhaseFinish CliStep = "install.run.phase.finish"
9797

9898
// Uninstall
99-
UninstallPhasePreCheckStart CliStep = "uninstall.pre-check.phase.start"
100-
UninstallStepPreCheckGetKubeContext CliStep = "uninstall.pre-check.step.get-kube-context"
101-
UninstallStepPreCheckEnsureRuntimeName CliStep = "uninstall.pre-check.step.ensure-runtime-name"
102-
UninstallStepPreCheckRuntimeNameValidation CliStep = "uninstall.pre-check.step.runtime-name-validation"
103-
UninstallStepPreCheckEnsureRuntimeRepo CliStep = "uninstall.pre-check.step.ensure-runtime-repo"
104-
UninstallStepPreCheckEnsureGitToken CliStep = "uninstall.pre-check.step.ensure-git-token"
105-
UninstallPhasePreCheckFinish CliStep = "uninstall.pre-check.phase.finish"
106-
UninstallPhaseStart CliStep = "uninstall.run.phase.start"
107-
UninstallStepCheckRuntimeExists CliStep = "uninstall.run.step.check-runtime-exists"
108-
UninstallStepUninstallRepo CliStep = "uninstall.run.step.uninstall-repo"
109-
UninstallStepRemoveGitIntegrations CliStep = "uninstall.run.step.remove-git-integrations"
110-
UninstallStepRemoveRuntimeIsc CliStep = "uninstall.run.step.remove-runtime-isc"
111-
UninstallStepDeleteRuntimeFromPlatform CliStep = "uninstall.run.step.delete-runtime-from-platform"
112-
UninstallStepDeleteManagedRuntimeFromPlatform CliStep = "uninstall.run.step.delete-managed-runtime-from-platform"
113-
UninstallPhaseFinish CliStep = "uninstall.run.phase.finish"
99+
UninstallPhasePreCheckStart CliStep = "uninstall.pre-check.phase.start"
100+
UninstallStepPreCheckGetKubeContext CliStep = "uninstall.pre-check.step.get-kube-context"
101+
UninstallStepPreCheckEnsureRuntimeOnKubeContext CliStep = "uninstall.pre-check.step.ensure-runtime-on-kube-context"
102+
UninstallStepPreCheckEnsureRuntimeName CliStep = "uninstall.pre-check.step.ensure-runtime-name"
103+
UninstallStepPreCheckRuntimeNameValidation CliStep = "uninstall.pre-check.step.runtime-name-validation"
104+
UninstallStepPreCheckEnsureRuntimeRepo CliStep = "uninstall.pre-check.step.ensure-runtime-repo"
105+
UninstallStepPreCheckEnsureGitToken CliStep = "uninstall.pre-check.step.ensure-git-token"
106+
UninstallPhasePreCheckFinish CliStep = "uninstall.pre-check.phase.finish"
107+
UninstallPhaseStart CliStep = "uninstall.run.phase.start"
108+
UninstallStepCheckRuntimeExists CliStep = "uninstall.run.step.check-runtime-exists"
109+
UninstallStepUninstallRepo CliStep = "uninstall.run.step.uninstall-repo"
110+
UninstallStepRemoveGitIntegrations CliStep = "uninstall.run.step.remove-git-integrations"
111+
UninstallStepRemoveRuntimeIsc CliStep = "uninstall.run.step.remove-runtime-isc"
112+
UninstallStepDeleteRuntimeFromPlatform CliStep = "uninstall.run.step.delete-runtime-from-platform"
113+
UninstallStepDeleteManagedRuntimeFromPlatform CliStep = "uninstall.run.step.delete-managed-runtime-from-platform"
114+
UninstallPhaseFinish CliStep = "uninstall.run.phase.finish"
114115

115116
// Upgrade
116117
UpgradePhasePreCheckStart CliStep = "upgrade.pre-check.phase.start"

pkg/util/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ func KubeCurrentServer(kubeconfig string) (string, error) {
225225
return KubeServerByContextName("", kubeconfig)
226226
}
227227

228+
func KubeCurrentContextName(kubeconfig string) string {
229+
conf := kubeConfig(kubeconfig)
230+
return conf.CurrentContext
231+
}
232+
228233
func CurrentAccount(user *codefresh.User) (string, error) {
229234
for i := range user.Accounts {
230235
if user.Accounts[i].Name == user.ActiveAccountName {

0 commit comments

Comments
 (0)