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 6 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 {
cmd.PrintErrln("Error: microk8s snap is installed, please remove it and try again.")
eaudetcobello marked this conversation as resolved.
Show resolved Hide resolved
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
31 changes: 31 additions & 0 deletions src/k8s/pkg/client/snapd/snap_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package snapd

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

type snapdSnapInfoResponse struct {
StatusCode int `json:"status-code"`
}

func (c *Client) GetSnapInfo(snap string) (*snapdSnapInfoResponse, 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 snapdSnapInfoResponse
if err := json.Unmarshal(resBody, &snapInfoResponse); err != nil {
return nil, fmt.Errorf("client: could not unmarshal response body: %w", err)
}

return &snapInfoResponse, nil
}
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