Skip to content

Commit

Permalink
improve error msg, check content of response
Browse files Browse the repository at this point in the history
  • Loading branch information
eaudetcobello committed Nov 14, 2024
1 parent e19af9f commit dee0603
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/k8s/cmd/k8s/k8s_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions src/k8s/pkg/client/snapd/snap_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down

0 comments on commit dee0603

Please sign in to comment.