From dee060364d6b8cbcf83d01841122f80791af114a Mon Sep 17 00:00:00 2001 From: Etienne Audet-Cobello Date: Thu, 14 Nov 2024 09:56:07 -0500 Subject: [PATCH] improve error msg, check content of response --- src/k8s/cmd/k8s/k8s_bootstrap.go | 4 ++-- src/k8s/pkg/client/snapd/snap_info.go | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/k8s/cmd/k8s/k8s_bootstrap.go b/src/k8s/cmd/k8s/k8s_bootstrap.go index c43450723..e8b2b3371 100644 --- a/src/k8s/cmd/k8s/k8s_bootstrap.go +++ b/src/k8s/cmd/k8s/k8s_bootstrap.go @@ -60,8 +60,8 @@ func newBootstrapCmd(env cmdutil.ExecutionEnvironment) *cobra.Command { env.Exit(1) return } - if microk8sInfo.StatusCode == 200 { - cmd.PrintErrln("Error: microk8s snap is installed, please remove it and try again.") + if microk8sInfo.StatusCode == 200 && !microk8sInfo.Result.InstallDate.IsZero() { + cmd.PrintErrln("Error: microk8s snap is installed. Please remove it using the following command and try again:\n\n sudo snap remove microk8s") env.Exit(1) return } diff --git a/src/k8s/pkg/client/snapd/snap_info.go b/src/k8s/pkg/client/snapd/snap_info.go index 4c843d101..b78eda0bc 100644 --- a/src/k8s/pkg/client/snapd/snap_info.go +++ b/src/k8s/pkg/client/snapd/snap_info.go @@ -4,13 +4,19 @@ import ( "encoding/json" "fmt" "io" + "time" ) -type snapdSnapInfoResponse struct { - StatusCode int `json:"status-code"` +type SnapInfoResult struct { + InstallDate time.Time `json:"install-date"` } -func (c *Client) GetSnapInfo(snap string) (*snapdSnapInfoResponse, error) { +type SnapInfoResponse struct { + StatusCode int `json:"status-code"` + Result SnapInfoResult `json:"result"` +} + +func (c *Client) GetSnapInfo(snap string) (*SnapInfoResponse, error) { resp, err := c.client.Get(fmt.Sprintf("http://localhost/v2/snaps/%s", snap)) if err != nil { return nil, fmt.Errorf("failed to get snapd snap info: %w", err) @@ -22,7 +28,7 @@ func (c *Client) GetSnapInfo(snap string) (*snapdSnapInfoResponse, error) { return nil, fmt.Errorf("client: could not read response body: %w", err) } - var snapInfoResponse snapdSnapInfoResponse + var snapInfoResponse SnapInfoResponse if err := json.Unmarshal(resBody, &snapInfoResponse); err != nil { return nil, fmt.Errorf("client: could not unmarshal response body: %w", err) }