Skip to content

Commit

Permalink
detect kind provider correctly (#436)
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey authored Nov 7, 2024
1 parent 5ecab77 commit 8c6cd97
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 322 deletions.
7 changes: 5 additions & 2 deletions pkg/cmd/delete/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"

"github.com/cnoe-io/idpbuilder/pkg/cmd/helpers"
"github.com/cnoe-io/idpbuilder/pkg/kind"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/spf13/cobra"
"sigs.k8s.io/kind/pkg/cluster"
)
Expand Down Expand Up @@ -32,11 +34,12 @@ func preDeleteE(cmd *cobra.Command, args []string) error {
func deleteE(cmd *cobra.Command, args []string) error {
logger := helpers.CmdLogger
logger.Info("deleting cluster", "clusterName", name)
detectOpt, err := cluster.DetectNodeProvider()
detectOpt, err := util.DetectKindNodeProvider()
if err != nil {
return err
}
provider := cluster.NewProvider(detectOpt)

provider := cluster.NewProvider(cluster.ProviderWithLogger(kind.KindLoggerFromLogr(&logger)), detectOpt)
if err := provider.Delete(name, ""); err != nil {
return fmt.Errorf("failed to delete cluster %s: %w", name, err)
}
Expand Down
52 changes: 5 additions & 47 deletions pkg/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import (
"strings"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
"github.com/cnoe-io/idpbuilder/pkg/runtime"
"github.com/cnoe-io/idpbuilder/pkg/util"
"github.com/go-logr/logr"
"sigs.k8s.io/controller-runtime/pkg/log"
kindv1alpha4 "sigs.k8s.io/kind/pkg/apis/config/v1alpha4"
"sigs.k8s.io/kind/pkg/cluster"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/cluster/nodeutils"
kindexec "sigs.k8s.io/kind/pkg/exec"
"sigs.k8s.io/yaml"
)
Expand All @@ -34,7 +32,6 @@ var (

type Cluster struct {
provider IProvider
runtime runtime.IRuntime
name string
kubeVersion string
kubeConfigPath string
Expand Down Expand Up @@ -122,22 +119,15 @@ func (c *Cluster) getConfig() ([]byte, error) {
}

func NewCluster(name, kubeVersion, kubeConfigPath, kindConfigPath, extraPortsMapping string, cfg v1alpha1.BuildCustomizationSpec, cliLogger logr.Logger) (*Cluster, error) {
detectOpt, err := cluster.DetectNodeProvider()
detectOpt, err := util.DetectKindNodeProvider()
if err != nil {
return nil, err
}

provider := cluster.NewProvider(detectOpt, cluster.ProviderWithLogger(kindLoggerFromLogr(&cliLogger)))

rt, err := runtime.DetectRuntime()
if err != nil {
return nil, err
}
setupLog.Info("Runtime detected", "provider", rt.Name())
provider := cluster.NewProvider(cluster.ProviderWithLogger(KindLoggerFromLogr(&cliLogger)), detectOpt)

return &Cluster{
provider: provider,
runtime: rt,
name: name,
kindConfigPath: kindConfigPath,
kubeVersion: kubeVersion,
Expand All @@ -161,31 +151,6 @@ func (c *Cluster) Exists() (bool, error) {
return false, nil
}

func (c *Cluster) RunsOnRightPort(ctx context.Context) (bool, error) {
allNodes, err := c.provider.ListNodes(c.name)
if err != nil {
return false, err
}

cpNodes, err := nodeutils.ControlPlaneNodes(allNodes)
if err != nil {
return false, err
}

var cpNodeName string
for _, cpNode := range cpNodes {
if strings.Contains(cpNode.String(), c.name) {
cpNodeName = cpNode.String()
}
}
if cpNodeName == "" {
return false, nil
}

return c.runtime.ContainerWithPort(ctx, cpNodeName, c.cfg.Port)

}

func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
clusterExitsts, err := c.Exists()
if err != nil {
Expand All @@ -195,18 +160,11 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
if clusterExitsts {
if recreate {
setupLog.Info("Existing cluster found. Deleting.", "cluster", c.name)
c.provider.Delete(c.name, "")
} else {
rightPort, err := c.RunsOnRightPort(ctx)
err := c.provider.Delete(c.name, "")
if err != nil {
return err
}

if !rightPort {
return fmt.Errorf("can't serve port %s. cluster %s is already running on a different port", c.cfg.Port, c.name)
return fmt.Errorf("deleting cluster %w", err)
}

// reuse if there is no port conflict
} else {
setupLog.Info("Cluster already exists", "cluster", c.name)
return nil
}
Expand Down
44 changes: 0 additions & 44 deletions pkg/kind/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"testing"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
runtime "github.com/cnoe-io/idpbuilder/pkg/runtime"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/go-logr/logr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"sigs.k8s.io/kind/pkg/cluster/constants"
"sigs.k8s.io/kind/pkg/cluster/nodes"
"sigs.k8s.io/kind/pkg/exec"
)
Expand Down Expand Up @@ -204,7 +202,6 @@ func (m *mockProvider) ListNodes(name string) ([]nodes.Node, error) {

type mockRuntime struct {
mock.Mock
runtime.IRuntime
}

func (m *mockRuntime) ContainerWithPort(ctx context.Context, name string, port string) (bool, error) {
Expand Down Expand Up @@ -257,44 +254,3 @@ func (n *NodeMock) CommandContext(ctx context.Context, cmd string, args ...strin
mockArgs := n.Called(nil)
return mockArgs.Get(0).(exec.Cmd)
}

func TestRunsOnWrongPort(t *testing.T) {
// Mock node
mockNode := &NodeMock{}
mockNode.On("Role").Return(constants.ControlPlaneNodeRoleValue, nil)
mockNode.On("String").Return("test-cluster")

mockNodes := []nodes.Node{
mockNode,
}

// Mock provider
mockProvider := &mockProvider{}
mockProvider.On("ListNodes", "test-cluster").Return(mockNodes, nil)

cluster := &Cluster{
name: "test-cluster",
provider: mockProvider,
cfg: v1alpha1.BuildCustomizationSpec{
Port: "8080",
},
}

// Mock runtime
mockRuntime1 := &mockRuntime{}
mockRuntime1.On("ContainerWithPort", context.Background(), "test-cluster", "8080").Return(true, nil)
cluster.runtime = mockRuntime1

result, err := cluster.RunsOnRightPort(context.Background())
assert.NoError(t, err)
assert.True(t, result)

// Mock Docker client
mockRuntime2 := &mockRuntime{}
mockRuntime2.On("ContainerWithPort", context.Background(), "test-cluster", "8080").Return(false, nil)
cluster.runtime = mockRuntime2
result, err = cluster.RunsOnRightPort(context.Background())

assert.NoError(t, err)
assert.False(t, result)
}
4 changes: 2 additions & 2 deletions pkg/kind/kindlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func (l *kindLogger) V(level kindlog.Level) kindlog.InfoLogger {
return newKindInfoLogger(l.cliLogger, int(level))
}

// this is a wrapper of logr.Logger made specifically for kind's InfoLogger.
// KindLoggerFromLogr is a wrapper of logr.Logger made specifically for kind's InfoLogger.
// https://github.com/kubernetes-sigs/kind/blob/1a8f0473a0785e0975e26739524513e8ee696be3/pkg/log/types.go
func kindLoggerFromLogr(logrLogger *logr.Logger) *kindLogger {
func KindLoggerFromLogr(logrLogger *logr.Logger) *kindLogger {
return &kindLogger{
cliLogger: logrLogger,
}
Expand Down
86 changes: 0 additions & 86 deletions pkg/runtime/docker.go

This file was deleted.

89 changes: 0 additions & 89 deletions pkg/runtime/finch.go

This file was deleted.

17 changes: 0 additions & 17 deletions pkg/runtime/provider.go

This file was deleted.

Loading

0 comments on commit 8c6cd97

Please sign in to comment.