Skip to content

Commit

Permalink
feat: switch controller-runtime to zap.Logger
Browse files Browse the repository at this point in the history
Enable logging using default development config with some fine tuning.
Additionally, now `info` and below logs go to kmsg.

Signed-off-by: Artem Chernyshev <[email protected]>
  • Loading branch information
Unix4ever authored and talos-bot committed May 25, 2021
1 parent f7cf64d commit 1db301e
Show file tree
Hide file tree
Showing 36 changed files with 288 additions and 134 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/containernetworking/plugins v0.9.1
github.com/coreos/go-iptables v0.6.0
github.com/coreos/go-semver v0.3.0
github.com/cosi-project/runtime v0.0.0-20210409233936-10d6103c19ab
github.com/cosi-project/runtime v0.0.0-20210521140557-8a4533ce68e2
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v20.10.6+incompatible
github.com/docker/go-connections v0.4.0
Expand Down Expand Up @@ -85,6 +85,7 @@ require (
go.etcd.io/etcd/client/pkg/v3 v3.5.0-beta.3
go.etcd.io/etcd/client/v3 v3.5.0-beta.3
go.etcd.io/etcd/etcdutl/v3 v3.5.0-beta.3
go.uber.org/zap v1.16.1-0.20210329175301-c23abee72d19
golang.org/x/net v0.0.0-20210505214959-0714010a04ed
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210503173754-0981d6026fa6
Expand Down
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ github.com/coreos/go-systemd/v22 v22.3.1 h1:7OO2CXWMYNDdaAzP51t4lCCZWwpQHmvPbm9s
github.com/coreos/go-systemd/v22 v22.3.1/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cosi-project/runtime v0.0.0-20210409233936-10d6103c19ab h1:u8p7SXdsdYV89RnXtPExlvIKDrXLeGhAUo66LmOtYV4=
github.com/cosi-project/runtime v0.0.0-20210409233936-10d6103c19ab/go.mod h1:ebaL1B5YO0vy0YuhHS59QrRfZEYPNB9U3bzdLrlUfzo=
github.com/cosi-project/runtime v0.0.0-20210521140557-8a4533ce68e2 h1:+dYE69Cq6tZMs+qWoiSUU/i8tHzSRJesl+4qBzcBXJg=
github.com/cosi-project/runtime v0.0.0-20210521140557-8a4533ce68e2/go.mod h1:v/3MIWNuuOSdXXMl3QgCSwZrAk1fTOmQHEnTAfvDqP4=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
Expand Down Expand Up @@ -1357,17 +1358,20 @@ go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5/go.mod h1:nmDLcffg48OtT/PSW0H
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.10 h1:z+mqJhf6ss6BSfSM671tgKyZBFPTTJM+HLxnhPC3wu0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
go.uber.org/zap v1.16.1-0.20210329175301-c23abee72d19 h1:040c3dLNhgFQkoojH2AMpHCy4SrvhmxdU72d9GLGGE0=
go.uber.org/zap v1.16.1-0.20210329175301-c23abee72d19/go.mod h1:aMfIlz3TDBfB0BwTCKFU1XbEmj9zevr5S5LcBr85MXw=
go4.org/intern v0.0.0-20210108033219-3eb7198706b2 h1:VFTf+jjIgsldaz/Mr00VaCSswHJrI2hIjQygE/W4IMg=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ package config
import (
"context"
"fmt"
"log"
"strings"

"github.com/AlekSi/pointer"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/state"
talosnet "github.com/talos-systems/net"
"go.uber.org/zap"

"github.com/talos-systems/talos/pkg/images"
talosconfig "github.com/talos-systems/talos/pkg/machinery/config"
Expand Down Expand Up @@ -62,7 +62,7 @@ func (ctrl *K8sControlPlaneController) Outputs() []controller.Output {
// Run implements controller.Controller interface.
//
//nolint:gocyclo
func (ctrl *K8sControlPlaneController) Run(ctx context.Context, r controller.Runtime, logger *log.Logger) error {
func (ctrl *K8sControlPlaneController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -104,7 +104,7 @@ func (ctrl *K8sControlPlaneController) Run(ctx context.Context, r controller.Run
continue
}

for _, f := range []func(context.Context, controller.Runtime, *log.Logger, talosconfig.Provider) error{
for _, f := range []func(context.Context, controller.Runtime, *zap.Logger, talosconfig.Provider) error{
ctrl.manageAPIServerConfig,
ctrl.manageControllerManagerConfig,
ctrl.manageSchedulerConfig,
Expand Down Expand Up @@ -133,7 +133,7 @@ func convertVolumes(volumes []talosconfig.VolumeMount) []config.K8sExtraVolume {
return result
}

func (ctrl *K8sControlPlaneController) manageAPIServerConfig(ctx context.Context, r controller.Runtime, logger *log.Logger, cfgProvider talosconfig.Provider) error {
func (ctrl *K8sControlPlaneController) manageAPIServerConfig(ctx context.Context, r controller.Runtime, logger *zap.Logger, cfgProvider talosconfig.Provider) error {
var cloudProvider string
if cfgProvider.Cluster().ExternalCloudProvider().Enabled() {
cloudProvider = "external"
Expand All @@ -155,7 +155,7 @@ func (ctrl *K8sControlPlaneController) manageAPIServerConfig(ctx context.Context
})
}

func (ctrl *K8sControlPlaneController) manageControllerManagerConfig(ctx context.Context, r controller.Runtime, logger *log.Logger, cfgProvider talosconfig.Provider) error {
func (ctrl *K8sControlPlaneController) manageControllerManagerConfig(ctx context.Context, r controller.Runtime, logger *zap.Logger, cfgProvider talosconfig.Provider) error {
var cloudProvider string
if cfgProvider.Cluster().ExternalCloudProvider().Enabled() {
cloudProvider = "external"
Expand All @@ -175,7 +175,7 @@ func (ctrl *K8sControlPlaneController) manageControllerManagerConfig(ctx context
})
}

func (ctrl *K8sControlPlaneController) manageSchedulerConfig(ctx context.Context, r controller.Runtime, logger *log.Logger, cfgProvider talosconfig.Provider) error {
func (ctrl *K8sControlPlaneController) manageSchedulerConfig(ctx context.Context, r controller.Runtime, logger *zap.Logger, cfgProvider talosconfig.Provider) error {
return r.Modify(ctx, config.NewK8sControlPlaneScheduler(), func(r resource.Resource) error {
r.(*config.K8sControlPlane).SetScheduler(config.K8sControlPlaneSchedulerSpec{
Image: cfgProvider.Cluster().Scheduler().Image(),
Expand All @@ -187,7 +187,7 @@ func (ctrl *K8sControlPlaneController) manageSchedulerConfig(ctx context.Context
})
}

func (ctrl *K8sControlPlaneController) manageManifestsConfig(ctx context.Context, r controller.Runtime, logger *log.Logger, cfgProvider talosconfig.Provider) error {
func (ctrl *K8sControlPlaneController) manageManifestsConfig(ctx context.Context, r controller.Runtime, logger *zap.Logger, cfgProvider talosconfig.Provider) error {
dnsServiceIPs, err := cfgProvider.Cluster().Network().DNSServiceIPs()
if err != nil {
return fmt.Errorf("error calculating DNS service IPs: %w", err)
Expand Down Expand Up @@ -239,7 +239,7 @@ func (ctrl *K8sControlPlaneController) manageManifestsConfig(ctx context.Context
})
}

func (ctrl *K8sControlPlaneController) manageExtraManifestsConfig(ctx context.Context, r controller.Runtime, logger *log.Logger, cfgProvider talosconfig.Provider) error {
func (ctrl *K8sControlPlaneController) manageExtraManifestsConfig(ctx context.Context, r controller.Runtime, logger *zap.Logger, cfgProvider talosconfig.Provider) error {
return r.Modify(ctx, config.NewK8sExtraManifests(), func(r resource.Resource) error {
spec := config.K8sExtraManifestsSpec{}

Expand Down Expand Up @@ -282,6 +282,6 @@ func (ctrl *K8sControlPlaneController) manageExtraManifestsConfig(ctx context.Co
})
}

func (ctrl *K8sControlPlaneController) teardownAll(ctx context.Context, r controller.Runtime, logger *log.Logger) error {
func (ctrl *K8sControlPlaneController) teardownAll(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/talos-systems/go-retry/retry"

configctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/config"
"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
"github.com/talos-systems/talos/pkg/resources/config"
Expand All @@ -49,9 +50,7 @@ func (suite *K8sControlPlaneSuite) SetupTest() {

var err error

logger := log.New(log.Writer(), "controller-runtime: ", log.Flags())

suite.runtime, err = runtime.NewRuntime(suite.state, logger)
suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(&configctrl.K8sControlPlaneController{}))
Expand Down
4 changes: 2 additions & 2 deletions internal/app/machined/pkg/controllers/config/machine_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package config
import (
"context"
"fmt"
"log"

"github.com/AlekSi/pointer"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/state"
"go.uber.org/zap"

"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
"github.com/talos-systems/talos/pkg/resources/config"
Expand Down Expand Up @@ -49,7 +49,7 @@ func (ctrl *MachineTypeController) Outputs() []controller.Output {
}

// Run implements controller.Controller interface.
func (ctrl *MachineTypeController) Run(ctx context.Context, r controller.Runtime, logger *log.Logger) error {
func (ctrl *MachineTypeController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ package k8s
import (
"context"
"fmt"
"log"
"path/filepath"
"strings"

"github.com/AlekSi/pointer"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/state"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
apiresource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -63,7 +63,7 @@ func (ctrl *ControlPlaneStaticPodController) Outputs() []controller.Output {
// Run implements controller.Controller interface.
//
//nolint:gocyclo
func (ctrl *ControlPlaneStaticPodController) Run(ctx context.Context, r controller.Runtime, logger *log.Logger) error {
func (ctrl *ControlPlaneStaticPodController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
Expand All @@ -87,7 +87,7 @@ func (ctrl *ControlPlaneStaticPodController) Run(ctx context.Context, r controll
secretsVersion := secretsStatusResource.(*k8s.SecretsStatus).Status().Version

for _, pod := range []struct {
f func(context.Context, controller.Runtime, *log.Logger, *config.K8sControlPlane, string) error
f func(context.Context, controller.Runtime, *zap.Logger, *config.K8sControlPlane, string) error
id resource.ID
}{
{
Expand Down Expand Up @@ -167,7 +167,7 @@ func volumes(volumes []config.K8sExtraVolume) []v1.Volume {
return result
}

func (ctrl *ControlPlaneStaticPodController) manageAPIServer(ctx context.Context, r controller.Runtime, logger *log.Logger, configResource *config.K8sControlPlane, secretsVersion string) error {
func (ctrl *ControlPlaneStaticPodController) manageAPIServer(ctx context.Context, r controller.Runtime, logger *zap.Logger, configResource *config.K8sControlPlane, secretsVersion string) error {
cfg := configResource.APIServer()

args := []string{
Expand Down Expand Up @@ -293,7 +293,7 @@ func (ctrl *ControlPlaneStaticPodController) manageAPIServer(ctx context.Context
}

func (ctrl *ControlPlaneStaticPodController) manageControllerManager(ctx context.Context, r controller.Runtime,
logger *log.Logger, configResource *config.K8sControlPlane, secretsVersion string) error {
logger *zap.Logger, configResource *config.K8sControlPlane, secretsVersion string) error {
cfg := configResource.ControllerManager()

args := []string{
Expand Down Expand Up @@ -394,7 +394,7 @@ func (ctrl *ControlPlaneStaticPodController) manageControllerManager(ctx context
}

func (ctrl *ControlPlaneStaticPodController) manageScheduler(ctx context.Context, r controller.Runtime,
logger *log.Logger, configResource *config.K8sControlPlane, secretsVersion string) error {
logger *zap.Logger, configResource *config.K8sControlPlane, secretsVersion string) error {
cfg := configResource.Scheduler()

args := []string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
v1 "k8s.io/api/core/v1"

k8sctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/machinery/constants"
"github.com/talos-systems/talos/pkg/resources/config"
"github.com/talos-systems/talos/pkg/resources/k8s"
Expand All @@ -47,9 +48,7 @@ func (suite *ControlPlaneStaticPodSuite) SetupTest() {

var err error

logger := log.New(log.Writer(), "controller-runtime: ", log.Flags())

suite.runtime, err = runtime.NewRuntime(suite.state, logger)
suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(&k8sctrl.ControlPlaneStaticPodController{}))
Expand Down
10 changes: 5 additions & 5 deletions internal/app/machined/pkg/controllers/k8s/extra_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/cosi-project/runtime/pkg/state"
"github.com/hashicorp/go-getter"
"github.com/hashicorp/go-multierror"
"go.uber.org/zap"

"github.com/talos-systems/talos/pkg/resources/config"
"github.com/talos-systems/talos/pkg/resources/k8s"
Expand Down Expand Up @@ -64,7 +64,7 @@ func (ctrl *ExtraManifestController) Outputs() []controller.Output {
// Run implements controller.Controller interface.
//
//nolint:gocyclo
func (ctrl *ExtraManifestController) Run(ctx context.Context, r controller.Runtime, logger *log.Logger) error {
func (ctrl *ExtraManifestController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -139,7 +139,7 @@ func (ctrl *ExtraManifestController) Run(ctx context.Context, r controller.Runti
}
}

func (ctrl *ExtraManifestController) process(ctx context.Context, r controller.Runtime, logger *log.Logger, manifest config.ExtraManifest) (id resource.ID, err error) {
func (ctrl *ExtraManifestController) process(ctx context.Context, r controller.Runtime, logger *zap.Logger, manifest config.ExtraManifest) (id resource.ID, err error) {
id = fmt.Sprintf("%s-%s", manifest.Priority, manifest.Name)

// inline manifests don't require download
Expand All @@ -150,7 +150,7 @@ func (ctrl *ExtraManifestController) process(ctx context.Context, r controller.R
return id, ctrl.processURL(ctx, r, logger, manifest, id)
}

func (ctrl *ExtraManifestController) processURL(ctx context.Context, r controller.Runtime, logger *log.Logger, manifest config.ExtraManifest, id resource.ID) (err error) {
func (ctrl *ExtraManifestController) processURL(ctx context.Context, r controller.Runtime, logger *zap.Logger, manifest config.ExtraManifest, id resource.ID) (err error) {
var tmpDir string

tmpDir, err = ioutil.TempDir("", "talos")
Expand Down Expand Up @@ -195,7 +195,7 @@ func (ctrl *ExtraManifestController) processURL(ctx context.Context, r controlle
return
}

logger.Printf("downloaded manifest %q", manifest.URL)
logger.Sugar().Infof("downloaded manifest %q", manifest.URL)

var contents []byte

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/talos-systems/go-retry/retry"

k8sctrl "github.com/talos-systems/talos/internal/app/machined/pkg/controllers/k8s"
"github.com/talos-systems/talos/pkg/logging"
"github.com/talos-systems/talos/pkg/resources/config"
"github.com/talos-systems/talos/pkg/resources/k8s"
"github.com/talos-systems/talos/pkg/resources/v1alpha1"
Expand All @@ -47,9 +48,7 @@ func (suite *ExtraManifestSuite) SetupTest() {

var err error

logger := log.New(log.Writer(), "controller-runtime: ", log.Flags())

suite.runtime, err = runtime.NewRuntime(suite.state, logger)
suite.runtime, err = runtime.NewRuntime(suite.state, logging.Wrap(log.Writer()))
suite.Require().NoError(err)

suite.Require().NoError(suite.runtime.RegisterController(&k8sctrl.ExtraManifestController{}))
Expand Down
Loading

0 comments on commit 1db301e

Please sign in to comment.