Skip to content

Commit

Permalink
Rework microcluster k8sd.Client package (#518)
Browse files Browse the repository at this point in the history
* Remove hidden auth token CLI commands

* Move pkg/k8s/client.k8sdClient to pkg/client/k8sd.Client

* rename api package to apiv1

* add Snap.K8sdClient() to retrieve k8sd client

* Use new k8sd.Client client in the cmd/k8s

* allow remote k8sd clients
  • Loading branch information
neoaggelos authored Jul 1, 2024
1 parent 00612a7 commit 9c26047
Show file tree
Hide file tree
Showing 58 changed files with 428 additions and 637 deletions.
2 changes: 1 addition & 1 deletion src/k8s/api/v1/bootstrap_config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/bootstrap_config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1_test
package apiv1_test

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/capi_config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

// SetClusterAPIAuthTokenRequest is used to request to set the auth token for ClusterAPI.
type SetClusterAPIAuthTokenRequest struct {
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/cluster.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

// GetClusterStatusRequest is used to request the current status of the cluster.
type GetClusterStatusRequest struct{}
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/cluster_config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/cluster_node.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

// JoinClusterRequest is used to request to add a node to the cluster.
type JoinClusterRequest struct {
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/join_config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/kubernetes_auth_tokens.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

// GenerateKubernetesAuthTokenRequest is used to request a new Kubernetes auth token.
type GenerateKubernetesAuthTokenRequest struct {
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/node.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

// GetNodeStatusResponse is the response for "GET 1.0/k8sd/node".
type GetNodeStatusResponse struct {
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/tokens.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

// GetJoinTokenRequest is used to request a token for joining a node to the cluster.
type GetJoinTokenRequest struct {
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/types_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1_test
package apiv1_test

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

func getField[T any](val *T) T {
if val != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/api/v1/worker.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package v1
package apiv1

// WorkerNodeInfoRequest is used by a worker node to retrieve the required credentials
// to join a cluster.
Expand Down
2 changes: 0 additions & 2 deletions src/k8s/cmd/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ func NewRootCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
addCommands(
cmd,
nil,
newGenerateAuthTokenCmd(env),
newLocalNodeStatusCommand(env),
newRevokeAuthTokenCmd(env),
newGenerateDocsCmd(env),
newHelmCmd(env),
xPrintShimPidsCmd,
Expand Down
6 changes: 3 additions & 3 deletions src/k8s/cmd/k8s/k8s_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ func newBootstrapCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
return
}

client, err := env.Client(cmd.Context())
client, err := env.Snap.K8sdClient("")
if err != nil {
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err)
env.Exit(1)
return
}

if client.IsBootstrapped(cmd.Context()) {
if _, err := client.NodeStatus(cmd.Context()); err == nil {
cmd.PrintErrln("Error: The node is already part of a cluster")
env.Exit(1)
return
Expand Down Expand Up @@ -134,7 +134,7 @@ func newBootstrapCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
ctx, cancel := context.WithTimeout(cmd.Context(), opts.timeout)
cobra.OnFinalize(cancel)

node, err := client.Bootstrap(ctx, request)
node, err := client.BootstrapCluster(ctx, request)
if err != nil {
cmd.PrintErrf("Error: Failed to bootstrap the cluster.\n\nThe error was: %v\n", err)
env.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion src/k8s/cmd/k8s/k8s_bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var testCases = []testCase{
{
name: "InvalidKeys",
yamlConfig: bootstrapConfigInvalidKeys,
expectedError: "field cluster-cidr not found in type v1.BootstrapConfig",
expectedError: "field cluster-cidr not found in type apiv1.BootstrapConfig",
},
{
name: "InvalidYAML",
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/cmd/k8s/k8s_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func newKubeConfigCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
opts.timeout = minTimeout
}

client, err := env.Client(cmd.Context())
client, err := env.Snap.K8sdClient("")
if err != nil {
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err)
env.Exit(1)
return
}

if !client.IsBootstrapped(cmd.Context()) {
if _, err := client.NodeStatus(cmd.Context()); err != nil {
cmd.PrintErrln("Error: The node is not part of a Kubernetes cluster. You can bootstrap a new cluster with:\n\n sudo k8s bootstrap")
env.Exit(1)
return
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/cmd/k8s/k8s_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func newDisableCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
Config: config,
}

client, err := env.Client(cmd.Context())
client, err := env.Snap.K8sdClient("")
if err != nil {
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err)
env.Exit(1)
Expand All @@ -90,7 +90,7 @@ func newDisableCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
cmd.PrintErrf("Disabling %s from the cluster. This may take a few seconds, please wait.\n", strings.Join(args, ", "))
ctx, cancel := context.WithTimeout(cmd.Context(), opts.timeout)
cobra.OnFinalize(cancel)
if err := client.UpdateClusterConfig(ctx, request); err != nil {
if err := client.SetClusterConfig(ctx, request); err != nil {
cmd.PrintErrf("Error: Failed to disable %s from the cluster.\n\nThe error was: %v\n", strings.Join(args, ", "), err)
env.Exit(1)
return
Expand Down
17 changes: 9 additions & 8 deletions src/k8s/cmd/k8s/k8s_disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package k8s_test

import (
"bytes"
"context"
"github.com/canonical/k8s/pkg/utils"
"testing"

apiv1 "github.com/canonical/k8s/api/v1"
"github.com/canonical/k8s/cmd/k8s"
cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/canonical/k8s/pkg/k8s/client"
"github.com/canonical/k8s/pkg/k8s/client/mock"
k8sdmock "github.com/canonical/k8s/pkg/client/k8sd/mock"
snapmock "github.com/canonical/k8s/pkg/snap/mock"
"github.com/canonical/k8s/pkg/utils"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -64,14 +63,16 @@ func TestDisableCmd(t *testing.T) {

stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
mockClient := &mock.Client{}
mockClient := &k8sdmock.Mock{}
var returnCode int
env := cmdutil.ExecutionEnvironment{
Stdout: stdout,
Stderr: stderr,
Getuid: func() int { return 0 },
Client: func(ctx context.Context) (client.Client, error) {
return mockClient, nil
Snap: &snapmock.Snap{
Mock: snapmock.Mock{
K8sdClient: mockClient,
},
},
Exit: func(rc int) { returnCode = rc },
}
Expand All @@ -85,7 +86,7 @@ func TestDisableCmd(t *testing.T) {
g.Expect(returnCode).To(Equal(tt.expectedCode))

if tt.expectedCode == 0 {
g.Expect(mockClient.UpdateClusterConfigCalledWith).To(Equal(tt.expectedCall))
g.Expect(mockClient.SetClusterConfigCalledWith).To(Equal(tt.expectedCall))
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/cmd/k8s/k8s_enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func newEnableCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
Config: config,
}

client, err := env.Client(cmd.Context())
client, err := env.Snap.K8sdClient("")
if err != nil {
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err)
env.Exit(1)
Expand All @@ -90,7 +90,7 @@ func newEnableCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
cmd.PrintErrf("Enabling %s on the cluster. This may take a few seconds, please wait.\n", strings.Join(args, ", "))
ctx, cancel := context.WithTimeout(cmd.Context(), opts.timeout)
cobra.OnFinalize(cancel)
if err := client.UpdateClusterConfig(ctx, request); err != nil {
if err := client.SetClusterConfig(ctx, request); err != nil {
cmd.PrintErrf("Error: Failed to enable %s on the cluster.\n\nThe error was: %v\n", strings.Join(args, ", "), err)
env.Exit(1)
return
Expand Down
17 changes: 9 additions & 8 deletions src/k8s/cmd/k8s/k8s_enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package k8s_test

import (
"bytes"
"context"
"github.com/canonical/k8s/pkg/utils"
"testing"

apiv1 "github.com/canonical/k8s/api/v1"
"github.com/canonical/k8s/cmd/k8s"
cmdutil "github.com/canonical/k8s/cmd/util"
"github.com/canonical/k8s/pkg/k8s/client"
"github.com/canonical/k8s/pkg/k8s/client/mock"
k8sdmock "github.com/canonical/k8s/pkg/client/k8sd/mock"
snapmock "github.com/canonical/k8s/pkg/snap/mock"
"github.com/canonical/k8s/pkg/utils"
. "github.com/onsi/gomega"
)

Expand Down Expand Up @@ -64,14 +63,16 @@ func TestK8sEnableCmd(t *testing.T) {

stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
mockClient := &mock.Client{}
mockClient := &k8sdmock.Mock{}
var returnCode int
env := cmdutil.ExecutionEnvironment{
Stdout: stdout,
Stderr: stderr,
Getuid: func() int { return 0 },
Client: func(ctx context.Context) (client.Client, error) {
return mockClient, nil
Snap: &snapmock.Snap{
Mock: snapmock.Mock{
K8sdClient: mockClient,
},
},
Exit: func(rc int) { returnCode = rc },
}
Expand All @@ -85,7 +86,7 @@ func TestK8sEnableCmd(t *testing.T) {
g.Expect(returnCode).To(Equal(tt.expectedCode))

if tt.expectedCode == 0 {
g.Expect(mockClient.UpdateClusterConfigCalledWith).To(Equal(tt.expectedCall))
g.Expect(mockClient.SetClusterConfigCalledWith).To(Equal(tt.expectedCall))
}
})
}
Expand Down
48 changes: 0 additions & 48 deletions src/k8s/cmd/k8s/k8s_generate_auth_token.go

This file was deleted.

4 changes: 2 additions & 2 deletions src/k8s/cmd/k8s/k8s_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func newGetCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
opts.timeout = minTimeout
}

client, err := env.Client(cmd.Context())
client, err := env.Snap.K8sdClient("")
if err != nil {
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err)
env.Exit(1)
Expand All @@ -38,7 +38,7 @@ func newGetCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
ctx, cancel := context.WithTimeout(cmd.Context(), opts.timeout)
cobra.OnFinalize(cancel)

config, err := client.GetClusterConfig(ctx, apiv1.GetClusterConfigRequest{})
config, err := client.GetClusterConfig(ctx)
if err != nil {
cmd.PrintErrf("Error: Failed to get the current cluster configuration.\n\nThe error was: %v\n", err)
env.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/cmd/k8s/k8s_get_join_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func newGetJoinTokenCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
opts.timeout = minTimeout
}

client, err := env.Client(cmd.Context())
client, err := env.Snap.K8sdClient("")
if err != nil {
cmd.PrintErrf("Error: Failed to create a k8sd client. Make sure that the k8sd service is running.\n\nThe error was: %v\n", err)
env.Exit(1)
Expand All @@ -46,7 +46,7 @@ func newGetJoinTokenCmd(env cmdutil.ExecutionEnvironment) *cobra.Command {
return
}

cmd.Println(token)
cmd.Println(token.EncodedToken)
},
}

Expand Down
Loading

0 comments on commit 9c26047

Please sign in to comment.