Skip to content

Commit b72d268

Browse files
fix cannot uninstall managed runtime without isc (#457)
* fix cannot uninstall managed runtime without isc * fix kube context validation on force * bump * fix-message * fix api-url formatting * wip * increase cyclo
1 parent 4b7f15f commit b72d268

File tree

11 files changed

+92
-58
lines changed

11 files changed

+92
-58
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ linters-settings:
3434

3535
gocyclo:
3636
# minimal code complexity to report, 30 by default (but we recommend 10-20)
37-
min-complexity: 18
37+
min-complexity: 20

Makefile

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

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

cmd/commands/cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func newClusterAddCommand() *cobra.Command {
101101
if err != nil {
102102
return err
103103
}
104-
104+
105105
setClusterName(&opts)
106106
err = validateClusterName(opts.clusterName)
107107

@@ -296,7 +296,7 @@ func newClusterListCommand() *cobra.Command {
296296
func runClusterList(ctx context.Context, runtimeName string) error {
297297
clusters, err := cfConfig.NewClient().V2().Cluster().List(ctx, runtimeName)
298298
if err != nil {
299-
return err
299+
return fmt.Errorf("failed to list clusters: %w", err)
300300
}
301301

302302
if len(clusters) == 0 {

cmd/commands/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func getRuntimeNameFromUserSelect(ctx context.Context, allowManaged bool) (strin
210210
continue
211211
}
212212
rtDisplay = fmt.Sprintf("%s (hosted)", rtDisplay)
213-
}
213+
}
214214
runtimeNames = append(runtimeNames, rtDisplay)
215215
}
216216

@@ -677,7 +677,7 @@ func ensureRuntimeOnKubeContext(ctx context.Context, kubeconfig string, runtimeN
677677
}
678678

679679
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)
680+
return fmt.Errorf("runtime '%s' does not exist on context '%s'. Make sure you are providing the right kube context or use --force to bypass this check", runtimeName, kubeContextName)
681681
}
682682

683683
return nil

cmd/commands/integrations.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func NewGitIntegrationAddCommand(client *sdk.AppProxyAPI) *cobra.Command {
193193
var (
194194
opts model.AddGitIntegrationArgs
195195
provider string
196+
apiURL string
196197
accountAdminsOnly bool
197198
)
198199

@@ -207,6 +208,8 @@ func NewGitIntegrationAddCommand(client *sdk.AppProxyAPI) *cobra.Command {
207208
opts.Name = &args[0]
208209
}
209210

211+
opts.APIURL = &apiURL
212+
210213
if opts.Provider, err = parseGitProvider(provider); err != nil {
211214
return err
212215
}
@@ -221,7 +224,7 @@ func NewGitIntegrationAddCommand(client *sdk.AppProxyAPI) *cobra.Command {
221224
}
222225

223226
cmd.Flags().StringVar(&provider, "provider", "github", "One of github|gitlab")
224-
cmd.Flags().StringVar(&opts.APIURL, "api-url", "", "Git provider API Url")
227+
cmd.Flags().StringVar(&apiURL, "api-url", "", "Git provider API Url")
225228
cmd.Flags().BoolVar(&accountAdminsOnly, "account-admins-only", false,
226229
"If true, this integration would only be visible to account admins (default: false)")
227230

@@ -244,6 +247,7 @@ func RunGitIntegrationAddCommand(ctx context.Context, client sdk.AppProxyAPI, op
244247
func NewGitIntegrationEditCommand(client *sdk.AppProxyAPI) *cobra.Command {
245248
var (
246249
opts model.EditGitIntegrationArgs
250+
apiURL string
247251
accountAdminsOnly bool
248252
)
249253

@@ -256,6 +260,8 @@ func NewGitIntegrationEditCommand(client *sdk.AppProxyAPI) *cobra.Command {
256260
opts.Name = &args[0]
257261
}
258262

263+
opts.APIURL = &apiURL
264+
259265
opts.SharingPolicy = model.SharingPolicyAllUsersInAccount
260266
if accountAdminsOnly {
261267
opts.SharingPolicy = model.SharingPolicyAccountAdmins
@@ -265,7 +271,7 @@ func NewGitIntegrationEditCommand(client *sdk.AppProxyAPI) *cobra.Command {
265271
},
266272
}
267273

268-
cmd.Flags().StringVar(&opts.APIURL, "api-url", "", "Git provider API Url")
274+
cmd.Flags().StringVar(&apiURL, "api-url", "", "Git provider API Url")
269275
cmd.Flags().BoolVar(&accountAdminsOnly, "account-admins-only", false,
270276
"If true, this integration would only be visible to account admins (default: false)")
271277

cmd/commands/runtime.go

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ type (
122122
FastExit bool
123123
DisableTelemetry bool
124124
Managed bool
125-
kubeContext string
125+
126+
kubeContext string
127+
skipAutopilotUninstall bool
126128
}
127129

128130
RuntimeUpgradeOptions struct {
@@ -188,8 +190,10 @@ func NewRuntimeCommand() *cobra.Command {
188190

189191
func NewRuntimeInstallCommand() *cobra.Command {
190192
var (
193+
gitIntegrationApiURL = ""
191194
gitIntegrationCreationOpts = apmodel.AddGitIntegrationArgs{
192195
SharingPolicy: apmodel.SharingPolicyAllUsersInAccount,
196+
APIURL: &gitIntegrationApiURL,
193197
}
194198
installationOpts = RuntimeInstallOptions{
195199
GitIntegrationCreationOpts: &gitIntegrationCreationOpts,
@@ -269,7 +273,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
269273
cmd.Flags().BoolVar(&installationOpts.SkipClusterChecks, "skip-cluster-checks", false, "Skips the cluster's checks")
270274
cmd.Flags().BoolVar(&installationOpts.DisableRollback, "disable-rollback", false, "If true, will not perform installation rollback after a failed installation")
271275
cmd.Flags().DurationVar(&store.Get().WaitTimeout, "wait-timeout", store.Get().WaitTimeout, "How long to wait for the runtime components to be ready")
272-
cmd.Flags().StringVar(&gitIntegrationCreationOpts.APIURL, "provider-api-url", "", "Git provider API url")
276+
cmd.Flags().StringVar(&gitIntegrationApiURL, "provider-api-url", "", "Git provider API url")
273277
cmd.Flags().BoolVar(&store.Get().SkipIngress, "skip-ingress", false, "Skips the creation of ingress resources")
274278
cmd.Flags().BoolVar(&store.Get().BypassIngressClassCheck, "bypass-ingress-class-check", false, "Disables the ingress class check during pre-installation")
275279
cmd.Flags().BoolVar(&installationOpts.DisableTelemetry, "disable-telemetry", false, "If true, will disable the analytics reporting for the installation process")
@@ -458,6 +462,12 @@ func runtimeUninstallCommandPreRunHandler(cmd *cobra.Command, args []string, opt
458462
if !opts.Managed {
459463
kubeconfig := cmd.Flag("kubeconfig").Value.String()
460464
err = ensureRuntimeOnKubeContext(cmd.Context(), kubeconfig, opts.RuntimeName, opts.kubeContext)
465+
466+
if err != nil && opts.Force {
467+
log.G(cmd.Context()).Warn("Failed to verify runtime is installed on the selected kubernetes context, installation repository will not be cleaned")
468+
err = nil
469+
opts.skipAutopilotUninstall = true // will not touch the cluster and repo
470+
}
461471
}
462472
handleCliStep(reporter.UninstallStepPreCheckEnsureRuntimeOnKubeContext, "Ensuring runtime is on the kube context", err, true, false)
463473
if err != nil {
@@ -845,19 +855,24 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
845855
handleCliStep(reporter.InstallStepCreateDefaultGitIntegration, "-skipped-", err, false, true)
846856
handleCliStep(reporter.InstallStepRegisterToDefaultGitIntegration, "-skipped-", err, false, true)
847857

858+
var apiURL string
859+
if opts.GitIntegrationCreationOpts.APIURL != nil {
860+
apiURL = fmt.Sprintf("--api-url %s", *opts.GitIntegrationCreationOpts.APIURL)
861+
}
862+
848863
skipIngressInfoMsg := util.Doc(fmt.Sprintf(`
849864
To complete the installation:
850865
1. Configure your cluster's routing service with path to '/%s' and \"%s\"
851866
2. Create and register Git integration using the commands:
852867
853-
<BIN> integration git add default --runtime %s --api-url %s
868+
<BIN> integration git add default --runtime %s %s
854869
855870
<BIN> integration git register default --runtime %s --token <AUTHENTICATION_TOKEN>
856871
`,
857872
store.Get().AppProxyIngressPath,
858873
util.GenerateIngressEventSourcePath(opts.RuntimeName),
859874
opts.RuntimeName,
860-
opts.GitIntegrationCreationOpts.APIURL,
875+
apiURL,
861876
opts.RuntimeName))
862877
summaryArr = append(summaryArr, summaryLog{skipIngressInfoMsg, Info})
863878
} else {
@@ -1064,11 +1079,16 @@ func removeGitIntegrations(ctx context.Context, opts *RuntimeUninstallOptions) e
10641079

10651080
func addDefaultGitIntegration(ctx context.Context, appProxyClient codefresh.AppProxyAPI, runtime string, opts *apmodel.AddGitIntegrationArgs) error {
10661081
if err := RunGitIntegrationAddCommand(ctx, appProxyClient, opts); err != nil {
1082+
var apiURL string
1083+
if opts.APIURL != nil {
1084+
apiURL = fmt.Sprintf("--api-url %s", *opts.APIURL)
1085+
}
1086+
10671087
commandAdd := util.Doc(fmt.Sprintf(
1068-
"\t<BIN> integration git add default --runtime %s --provider %s --api-url %s",
1088+
"\t<BIN> integration git add default --runtime %s --provider %s %s",
10691089
runtime,
10701090
strings.ToLower(opts.Provider.String()),
1071-
opts.APIURL,
1091+
apiURL,
10721092
))
10731093

10741094
commandRegister := util.Doc(fmt.Sprintf(
@@ -1637,51 +1657,47 @@ func RunRuntimeUninstall(ctx context.Context, opts *RuntimeUninstallOptions) err
16371657
return fmt.Errorf("failed to remove runtime isc: %w", err)
16381658
}
16391659

1640-
subCtx, cancel := context.WithCancel(ctx)
1641-
go func() {
1642-
if err := printApplicationsState(subCtx, opts.RuntimeName, opts.KubeFactory, opts.Managed); err != nil {
1643-
log.G(ctx).WithError(err).Debug("failed to print uninstallation progress")
1660+
if !opts.skipAutopilotUninstall {
1661+
subCtx, cancel := context.WithCancel(ctx)
1662+
go func() {
1663+
if err := printApplicationsState(subCtx, opts.RuntimeName, opts.KubeFactory, opts.Managed); err != nil {
1664+
log.G(ctx).WithError(err).Debug("failed to print uninstallation progress")
1665+
}
1666+
}()
1667+
1668+
if !opts.Managed {
1669+
err = apcmd.RunRepoUninstall(ctx, &apcmd.RepoUninstallOptions{
1670+
Namespace: opts.RuntimeName,
1671+
KubeContextName: opts.kubeContext,
1672+
Timeout: opts.Timeout,
1673+
CloneOptions: opts.CloneOpts,
1674+
KubeFactory: opts.KubeFactory,
1675+
Force: opts.Force,
1676+
FastExit: opts.FastExit,
1677+
})
1678+
}
1679+
cancel() // to tell the progress to stop displaying even if it's not finished
1680+
if opts.Force {
1681+
err = nil
16441682
}
1645-
}()
1646-
1647-
if !opts.Managed {
1648-
err = apcmd.RunRepoUninstall(ctx, &apcmd.RepoUninstallOptions{
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,
1656-
})
1657-
}
1658-
cancel() // to tell the progress to stop displaying even if it's not finished
1659-
if opts.Force {
1660-
err = nil
16611683
}
1662-
handleCliStep(reporter.UninstallStepUninstallRepo, "Uninstalling repo", err, false, !opts.Managed)
1684+
handleCliStep(reporter.UninstallStepUninstallRepo, "Uninstalling repo", err, false, !opts.Managed && !opts.skipAutopilotUninstall)
16631685
if err != nil {
16641686
summaryArr = append(summaryArr, summaryLog{"you can attempt to uninstall again with the \"--force\" flag", Info})
16651687
return err
16661688
}
16671689

1668-
if !opts.Managed {
1690+
log.G(ctx).Infof("Deleting runtime '%s' from platform", opts.RuntimeName)
1691+
if opts.Managed {
1692+
_, err = cfConfig.NewClient().V2().Runtime().DeleteManaged(ctx, opts.RuntimeName)
1693+
} else {
16691694
err = deleteRuntimeFromPlatform(ctx, opts)
16701695
}
16711696
handleCliStep(reporter.UninstallStepDeleteRuntimeFromPlatform, "Deleting runtime from platform", err, false, !opts.Managed)
16721697
if err != nil {
16731698
return fmt.Errorf("failed to delete runtime from the platform: %w", err)
16741699
}
16751700

1676-
if opts.Managed {
1677-
log.G(ctx).Infof("Deleting runtime '%s' from platform", opts.RuntimeName)
1678-
_, err = cfConfig.NewClient().V2().Runtime().DeleteManaged(ctx, opts.RuntimeName)
1679-
}
1680-
handleCliStep(reporter.UninstallStepDeleteManagedRuntimeFromPlatform, "Deleting hosted runtime from platform", err, false, opts.Managed)
1681-
if err != nil {
1682-
return fmt.Errorf("failed to delete hosted runtime from the platform: %w", err)
1683-
}
1684-
16851701
if cfConfig.GetCurrentContext().DefaultRuntime == opts.RuntimeName {
16861702
cfConfig.GetCurrentContext().DefaultRuntime = ""
16871703
}
@@ -1806,14 +1822,24 @@ func getApplicationChecklistState(name string, a *argocdv1alpha1.Application, ru
18061822
}
18071823

18081824
func removeRuntimeIsc(ctx context.Context, runtimeName string) error {
1825+
me, err := cfConfig.NewClient().V2().UsersV2().GetCurrent(ctx)
1826+
if err != nil {
1827+
return fmt.Errorf("failed to get current user information: %w", err)
1828+
}
1829+
1830+
if me.ActiveAccount.SharedConfigRepo == nil || *me.ActiveAccount.SharedConfigRepo == "" {
1831+
log.G(ctx).Info("Skipped removing runtime from ISC repo. ISC repo not defined")
1832+
return nil
1833+
}
1834+
18091835
appProxyClient, err := cfConfig.NewClient().AppProxy(ctx, runtimeName, store.Get().InsecureIngressHost)
18101836
if err != nil {
18111837
return fmt.Errorf("failed to build app-proxy client while removing runtime isc: %w", err)
18121838
}
18131839

18141840
_, err = appProxyClient.AppProxyIsc().RemoveRuntimeFromIscRepo(ctx)
18151841
if err == nil {
1816-
log.G(ctx).Info("Removed runtime from isc repo")
1842+
log.G(ctx).Info("Removed runtime from ISC repo")
18171843
}
18181844

18191845
return err
@@ -2611,7 +2637,7 @@ func ensureGitIntegrationOpts(opts *RuntimeInstallOptions) error {
26112637
opts.GitIntegrationCreationOpts.Provider = apmodel.GitProviders(strings.ToUpper(opts.InsCloneOpts.Provider))
26122638
}
26132639

2614-
if opts.GitIntegrationCreationOpts.APIURL == "" {
2640+
if opts.GitIntegrationCreationOpts.APIURL == nil || *opts.GitIntegrationCreationOpts.APIURL == "" {
26152641
if opts.GitIntegrationCreationOpts.APIURL, err = inferAPIURLForGitProvider(opts.GitIntegrationCreationOpts.Provider); err != nil {
26162642
return err
26172643
}
@@ -2637,17 +2663,20 @@ func inferProviderFromCloneURL(cloneURL string) (apmodel.GitProviders, error) {
26372663
return apmodel.GitProviders(""), fmt.Errorf("failed to infer git provider from clone url: %s, %s", cloneURL, suggest)
26382664
}
26392665

2640-
func inferAPIURLForGitProvider(provider apmodel.GitProviders) (string, error) {
2666+
func inferAPIURLForGitProvider(provider apmodel.GitProviders) (*string, error) {
26412667
const suggest = "you can specify a git provider explicitly with --provider-api-url"
2668+
var res string
26422669

26432670
switch provider {
26442671
case apmodel.GitProvidersGithub:
2645-
return "https://api.github.com", nil
2672+
res = "https://api.github.com"
2673+
return &res, nil
26462674
case apmodel.GitProvidersGitlab:
2647-
return "https://gitlab.com/api/v4", nil
2675+
res = "https://gitlab.com/api/v4"
2676+
return &res, nil
26482677
}
26492678

2650-
return "", fmt.Errorf("cannot infer api-url for git provider %s, %s", provider, suggest)
2679+
return nil, fmt.Errorf("cannot infer api-url for git provider %s, %s", provider, suggest)
26512680
}
26522681

26532682
// display the user the old vs. the new configurations that will be changed upon recovery

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.404/cf-linux-amd64.tar.gz | tar zx
26+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.405/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.404/cf-darwin-amd64.tar.gz | tar zx
39+
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.405/cf-darwin-amd64.tar.gz | tar zx
4040

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

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/argoproj/argo-events v0.17.1-0.20220327045437-70eaafe9afec
1111
github.com/argoproj/argo-workflows/v3 v3.3.1
1212
github.com/briandowns/spinner v1.18.1
13-
github.com/codefresh-io/go-sdk v0.44.1
13+
github.com/codefresh-io/go-sdk v0.44.2
1414
github.com/fatih/color v1.13.0
1515
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
1616
github.com/go-git/go-billy/v5 v5.3.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h
254254
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
255255
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
256256
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
257-
github.com/codefresh-io/go-sdk v0.44.1 h1:wpiGh2MBF45I1OV/oE4fGm0/K6S6vP7ttkGJ0sf/CXg=
258-
github.com/codefresh-io/go-sdk v0.44.1/go.mod h1:CcoVmTFWHGkbrSW8LyOGB/vJe5Vzr3iC/pNE2QIBTyg=
257+
github.com/codefresh-io/go-sdk v0.44.2 h1:1hc9putocn8FTTlDxgeFaFid9aPHxLDpSfop7Egwods=
258+
github.com/codefresh-io/go-sdk v0.44.2/go.mod h1:CcoVmTFWHGkbrSW8LyOGB/vJe5Vzr3iC/pNE2QIBTyg=
259259
github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM=
260260
github.com/codeskyblue/go-sh v0.0.0-20190412065543-76bd3d59ff27/go.mod h1:VQx0hjo2oUeQkQUET7wRwradO6f+fN5jzXgB/zROxxE=
261261
github.com/container-storage-interface/spec v1.5.0/go.mod h1:8K96oQNkJ7pFcC2R9Z1ynGGBB1I93kcS6PGg3SsOk8s=

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.404
8+
version: 0.0.405
99
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
1010
components:
1111
- name: events

0 commit comments

Comments
 (0)