Skip to content

Commit

Permalink
ensure kind command outputs are available (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
nabuskey authored Nov 6, 2024
1 parent be892de commit c90dbd8
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewBuild(opts NewBuildOptions) *Build {

func (b *Build) ReconcileKindCluster(ctx context.Context, recreateCluster bool) error {
// Initialize Kind Cluster
cluster, err := kind.NewCluster(b.name, b.kubeVersion, b.kubeConfigPath, b.kindConfigPath, b.extraPortsMapping, b.cfg)
cluster, err := kind.NewCluster(b.name, b.kubeVersion, b.kubeConfigPath, b.kindConfigPath, b.extraPortsMapping, b.cfg, setupLog)
if err != nil {
setupLog.Error(err, "Error Creating kind cluster")
return err
Expand Down
15 changes: 11 additions & 4 deletions pkg/kind/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kind
import (
"context"
"embed"
"errors"
"fmt"
"io/fs"
"os"
Expand All @@ -12,11 +13,13 @@ import (
"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 Down Expand Up @@ -118,13 +121,13 @@ func (c *Cluster) getConfig() ([]byte, error) {
return retBuff, nil
}

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

if err != nil {
return nil, err
}
provider := cluster.NewProvider(detectOpt)

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

rt, err := runtime.DetectRuntime()
if err != nil {
Expand Down Expand Up @@ -219,13 +222,17 @@ func (c *Cluster) Reconcile(ctx context.Context, recreate bool) error {
fmt.Print("\n######################### config end ############################\n")

setupLog.Info("Creating kind cluster", "cluster", c.name)

if err = c.provider.Create(
c.name,
cluster.CreateWithRawConfig(rawConfig),
); err != nil {
t := &kindexec.RunError{}
if errors.As(err, &t) {
return fmt.Errorf("%w: %s", err, t.Output)
}
return err
}

setupLog.Info("Done creating cluster", "cluster", c.name)

return nil
Expand Down
7 changes: 4 additions & 3 deletions pkg/kind/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
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"
Expand Down Expand Up @@ -82,7 +83,7 @@ containerdConfigPatches:
Host: c.host,
Port: c.port,
UsePathRouting: c.usePathRouting,
})
}, logr.Discard())
assert.NoError(t, err)

cfg, err := cluster.getConfig()
Expand All @@ -96,7 +97,7 @@ func TestExtraPortMappings(t *testing.T) {
cluster, err := NewCluster("testcase", "v1.26.3", "", "", "22:32222", v1alpha1.BuildCustomizationSpec{
Host: "cnoe.localtest.me",
Port: "8443",
})
}, logr.Discard())
if err != nil {
t.Fatalf("Initializing cluster resource: %v", err)
}
Expand Down Expand Up @@ -177,7 +178,7 @@ func TestGetConfigCustom(t *testing.T) {
Host: "cnoe.localtest.me",
Port: v.hostPort,
Protocol: v.protocol,
})
}, logr.Discard())

b, err := c.getConfig()
if v.error {
Expand Down
68 changes: 68 additions & 0 deletions pkg/kind/kindlogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package kind

import (
"fmt"

"github.com/go-logr/logr"
kindlog "sigs.k8s.io/kind/pkg/log"
)

// this is a wrapper of logr.Logger made specifically for kind' logger.
// this is needed because kind's implementation is internal.
// https://github.com/kubernetes-sigs/kind/blob/1a8f0473a0785e0975e26739524513e8ee696be3/pkg/log/types.go
type kindLogger struct {
cliLogger *logr.Logger
}

func (l *kindLogger) Warn(message string) {
l.cliLogger.Info(message)
}

func (l *kindLogger) Warnf(message string, args ...interface{}) {
l.cliLogger.Info(fmt.Sprintf(message, args...))
}

func (l *kindLogger) Error(message string) {
l.cliLogger.Error(fmt.Errorf(message), "")
}

func (l *kindLogger) Errorf(message string, args ...interface{}) {
msg := fmt.Sprintf(message, args...)
l.cliLogger.Error(fmt.Errorf(msg), "")
}

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.
// https://github.com/kubernetes-sigs/kind/blob/1a8f0473a0785e0975e26739524513e8ee696be3/pkg/log/types.go
func kindLoggerFromLogr(logrLogger *logr.Logger) *kindLogger {
return &kindLogger{
cliLogger: logrLogger,
}
}

func newKindInfoLogger(logrLogger *logr.Logger, level int) *kindInfoLogger {
return &kindInfoLogger{
cliLogger: logrLogger,
level: level + 1, // push log level down. e.g. info log becomes debug+1.
}
}

type kindInfoLogger struct {
cliLogger *logr.Logger
level int
}

func (k *kindInfoLogger) Info(message string) {
k.cliLogger.V(k.level).Info(message)
}

func (k *kindInfoLogger) Infof(message string, args ...interface{}) {
k.cliLogger.V(k.level).Info(fmt.Sprintf(message, args...))
}

func (k *kindInfoLogger) Enabled() bool {
return k.cliLogger.Enabled()
}

0 comments on commit c90dbd8

Please sign in to comment.