Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if Microk8s is installed #785

Merged
merged 9 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/k8s/cmd/k8s/k8s_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

apiv1 "github.com/canonical/k8s-snap-api/api/v1"
cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/canonical/k8s/pkg/client/snapd"
"github.com/canonical/k8s/pkg/config"
"github.com/canonical/k8s/pkg/k8sd/features"
"github.com/canonical/k8s/pkg/utils"
Expand Down Expand Up @@ -47,6 +48,24 @@ func newBootstrapCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
Long: "Generate certificates, configure service arguments and start the Kubernetes services.",
PreRun: chainPreRunHooks(hookRequireRoot(env), hookInitializeFormatter(env, &opts.outputFormat), hookCheckLXD()),
Run: func(cmd *cobra.Command, args []string) {
snapdClient, err := snapd.NewClient()
if err != nil {
cmd.PrintErrln("Error: failed to create snapd client: %w", err)
env.Exit(1)
return
}
microk8sInfo, err := snapdClient.GetSnapInfo("microk8s")
if err != nil {
cmd.PrintErrln("Error: failed to check if microk8s is installed: %w", err)
env.Exit(1)
return
}
if microk8sInfo.StatusCode == 200 && microk8sInfo.HasInstallDate() {
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
}

if opts.interactive && opts.configFile != "" {
cmd.PrintErrln("Error: --interactive and --file flags cannot be set at the same time.")
env.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.6
require (
dario.cat/mergo v1.0.0
github.com/canonical/go-dqlite v1.22.0
github.com/canonical/k8s-snap-api v1.0.12
github.com/canonical/k8s-snap-api v1.0.13
github.com/canonical/lxd v0.0.0-20240822122218-e7b2a7a83230
github.com/canonical/microcluster/v3 v3.0.0-20240827143335-f7a4d3984970
github.com/go-logr/logr v1.4.2
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 h1:nvj0OLI3YqYXe
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
github.com/canonical/go-dqlite v1.22.0 h1:DuJmfcREl4gkQJyvZzjl2GHFZROhbPyfdjDRQXpkOyw=
github.com/canonical/go-dqlite v1.22.0/go.mod h1:Uvy943N8R4CFUAs59A1NVaziWY9nJ686lScY7ywurfg=
github.com/canonical/k8s-snap-api v1.0.12 h1:ofS2+JRlPMnpWgHLmnE4QEUqWv9Dgrmsv3hrjI0O4zQ=
github.com/canonical/k8s-snap-api v1.0.12/go.mod h1:LDPoIYCeYnfgOFrwVPJ/4edGU264w7BB7g0GsVi36AY=
github.com/canonical/k8s-snap-api v1.0.13 h1:Z+IW6Knvycu+DrkmH+9qB1UNyYiHfL+rFvT9DtSO2+g=
github.com/canonical/k8s-snap-api v1.0.13/go.mod h1:LDPoIYCeYnfgOFrwVPJ/4edGU264w7BB7g0GsVi36AY=
github.com/canonical/lxd v0.0.0-20240822122218-e7b2a7a83230 h1:YOqZ+/14OPZ+/TOXpRHIX3KLT0C+wZVpewKIwlGUmW0=
github.com/canonical/lxd v0.0.0-20240822122218-e7b2a7a83230/go.mod h1:YVGI7HStOKsV+cMyXWnJ7RaMPaeWtrkxyIPvGWbgACc=
github.com/canonical/microcluster/v3 v3.0.0-20240827143335-f7a4d3984970 h1:UrnpglbXELlxtufdk6DGDytu2JzyzuS3WTsOwPrkQLI=
Expand Down
41 changes: 41 additions & 0 deletions src/k8s/pkg/client/snapd/snap_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package snapd

import (
"encoding/json"
"fmt"
"io"
"time"
)

type SnapInfoResult struct {
InstallDate time.Time `json:"install-date"`
}

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)
}
defer resp.Body.Close()

resBody, err := io.ReadAll(resp.Body)
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, fmt.Errorf("client: could not read response body: %w", err)
}

var snapInfoResponse SnapInfoResponse
if err := json.Unmarshal(resBody, &snapInfoResponse); err != nil {
return nil, fmt.Errorf("client: could not unmarshal response body: %w", err)
}

return &snapInfoResponse, nil
}

func (s SnapInfoResponse) HasInstallDate() bool {
return !s.Result.InstallDate.IsZero()
}
16 changes: 16 additions & 0 deletions tests/integration/tests/test_util/test_bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright 2024 Canonical, Ltd.
#
from typing import List

import pytest
from test_util import harness


@pytest.mark.node_count(1)
@pytest.mark.disable_k8s_bootstrapping()
def test_microk8s_installed(instances: List[harness.Instance]):
instance = instances[0]
instance.exec("snap install microk8s --classic".split())
result = instance.exec("k8s bootstrap".split(), capture_output=True, check=False)
assert "Error: microk8s snap is installed" in result.stderr.decode()
Loading