diff --git a/pkg/build/build.go b/pkg/build/build.go index 632adb82..0da1cb4f 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -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 diff --git a/pkg/kind/cluster.go b/pkg/kind/cluster.go index 72cba5b3..c921945a 100644 --- a/pkg/kind/cluster.go +++ b/pkg/kind/cluster.go @@ -3,6 +3,7 @@ package kind import ( "context" "embed" + "errors" "fmt" "io/fs" "os" @@ -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" ) @@ -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 { @@ -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 diff --git a/pkg/kind/cluster_test.go b/pkg/kind/cluster_test.go index f697d904..67f4b283 100644 --- a/pkg/kind/cluster_test.go +++ b/pkg/kind/cluster_test.go @@ -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" @@ -82,7 +83,7 @@ containerdConfigPatches: Host: c.host, Port: c.port, UsePathRouting: c.usePathRouting, - }) + }, logr.Discard()) assert.NoError(t, err) cfg, err := cluster.getConfig() @@ -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) } @@ -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 { diff --git a/pkg/kind/kindlogger.go b/pkg/kind/kindlogger.go new file mode 100644 index 00000000..026bcd56 --- /dev/null +++ b/pkg/kind/kindlogger.go @@ -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() +}