Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kube-client] replace info with debug #19

Merged
merged 9 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ linters:
- nolintlint
- prealloc
- revive
- sloglint
- staticcheck
- stylecheck
- unconvert
Expand All @@ -44,6 +45,33 @@ linters-settings:
deny:
- pkg: github.com/evanphx/json-patch
desc: "The 'github.com/evanphx/json-patch' package is superseded. Use pkg/utils/jsonpatch.go instead."
sloglint:
# Enforce not mixing key-value pairs and attributes.
no-mixed-args: true
# Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only).
kv-only: false
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only).
attr-only: false
# Enforce not using global loggers.
no-global: ""
# Enforce using methods that accept a context.
context: ""
# Enforce using static values for log messages.
static-msg: false
# Enforce using constants instead of raw keys.
no-raw-keys: false
# Enforce a single key naming convention.
key-naming-case: ""
# Enforce not using specific keys.
forbidden-keys:
- level
- msg
- logger
- source
- stacktrace
- time
# Enforce putting arguments on separate lines.
args-on-sep-lines: false
issues:
exclude:
# Using underscores is a common practice, refactor in the future
Expand Down
41 changes: 32 additions & 9 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"fmt"
"log/slog"
"os"
"strings"
"time"
Expand Down Expand Up @@ -35,8 +36,27 @@ const (
kubeNamespaceFilePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)

func New() *Client {
return &Client{}
type Option func(client *Client)

func WithLogger(logger *log.Logger) Option {
return func(client *Client) {
client.logger = logger.With("operator.component", "KubernetesAPIClient")
}
}

// TODO: refactor all "with" methods
func New(opts ...Option) *Client {
c := &Client{}

for _, fn := range opts {
fn(c)
}

if c.logger == nil {
c.logger = log.NewLogger(log.Options{}).Named("kubernetes-api-client").With("operator.component", "KubernetesAPIClient")
}

return c
}

func NewFake(gvr map[schema.GroupVersionResource]string) *Client {
Expand All @@ -47,6 +67,7 @@ func NewFake(gvr map[schema.GroupVersionResource]string) *Client {
dynamicClient: fakedynamic.NewSimpleDynamicClientWithCustomListKinds(sc, gvr),
metadataClient: fakemetadata.NewSimpleMetadataClient(sc),
schema: sc,
logger: log.NewNop(),
}
}

Expand All @@ -67,6 +88,7 @@ type Client struct {
metricLabels map[string]string
schema *runtime.Scheme
restConfig *rest.Config
logger *log.Logger
}

// ReloadDynamic creates new dynamic client with the new set of CRDs.
Expand Down Expand Up @@ -125,8 +147,9 @@ func (c *Client) RestConfig() *rest.Config {
}

func (c *Client) Init() error {
logger := log.NewLogger(log.Options{})
logger = logger.With("operator.component", "KubernetesAPIClient")
if c.logger == nil {
c.logger = log.NewLogger(log.Options{}).Named("kubernetes-api-client").With("operator.component", "KubernetesAPIClient")
}

var err error
var config *rest.Config
Expand All @@ -146,18 +169,18 @@ func (c *Client) Init() error {
if c.configPath != "" || c.contextName != "" {
if outOfClusterErr != nil {
err = fmt.Errorf("out-of-cluster config error: %v, in-cluster config error: %v", outOfClusterErr, err)
logger.Errorf("configuration problems: %s", err)
c.logger.Error("configuration problems", slog.String("error", err.Error()))
return err
}
return fmt.Errorf("in-cluster config is not found")
}
logger.Errorf("in-cluster problem: %s", err)
c.logger.Error("in-cluster problem", slog.String("error", err.Error()))
return err
}
} else {
// if not in cluster return outOfCluster error
if outOfClusterErr != nil {
logger.Errorf("out-of-cluster problem: %s", outOfClusterErr)
c.logger.Error("out-of-cluster problem", slog.String("error", outOfClusterErr.Error()))
return outOfClusterErr
}
return fmt.Errorf("no kubernetes client config found")
Expand All @@ -183,7 +206,7 @@ func (c *Client) Init() error {

c.Interface, err = kubernetes.NewForConfig(config)
if err != nil {
logger.Errorf("configuration problem: %s", err)
c.logger.Error("configuration problem", slog.String("error", err.Error()))
return err
}

Expand Down Expand Up @@ -227,7 +250,7 @@ func (c *Client) Init() error {
}

c.restConfig = config
logger.Infof("Kubernetes client is configured successfully with '%s' config", configType)
c.logger.Debug("Kubernetes client is configured successfully with config", slog.String("config", configType))

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.8
toolchain go1.23.1

require (
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241102110904-83c5e473e0ca
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e
github.com/onsi/gomega v1.29.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.16.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241102110904-83c5e473e0ca h1:qbVHWHa4sDRW55Xbui4vpL1XgJbrrdVr6iQARARiFWQ=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241102110904-83c5e473e0ca/go.mod h1:Mk5HRzkc5pIcDIZ2JJ6DPuuqnwhXVkb3you8M8Mg+4w=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e h1:QUQy+5Bv7/UzhfrytiG3c5gfLGhPppepVbRpbMisVIw=
github.com/deckhouse/deckhouse/pkg/log v0.0.0-20241106140903-258b93b3334e/go.mod h1:Mk5HRzkc5pIcDIZ2JJ6DPuuqnwhXVkb3you8M8Mg+4w=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
Expand Down
Loading