Skip to content

Commit

Permalink
Merge branch 'incubation' into cleanup-nim-tp
Browse files Browse the repository at this point in the history
  • Loading branch information
zdtsw authored Nov 14, 2024
2 parents 610ea2e + e589680 commit 78dd970
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ This will ensure that the doc for the apis are updated accordingly.

### Enabled logging

Global logger configuration can be changed with a command line switch `--log-mode <mode>`
for example from CSV. Valid values for `<mode>`: "" (as default) || prod || production || devel || development.
Global logger configuration can be changed with an environemnt variable `ZAP_LOG_LEVEL`
or a command line switch `--log-mode <mode>` for example from CSV.
Command line switch has higher priority.
Valid values for `<mode>`: "" (as default) || prod || production || devel || development.

Verbosity level is INFO.
To fine tune zap backend [standard operator sdk zap switches](https://sdk.operatorframework.io/docs/building-operators/golang/references/logging/)
Expand Down
13 changes: 13 additions & 0 deletions pkg/feature/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func WaitForPodsToBeReady(namespace string) Action {
return false, err
}

podList.Items = filterEvictedPods(podList.Items)
readyPods := 0
totalPods := len(podList.Items)

Expand Down Expand Up @@ -102,6 +103,18 @@ func WaitForPodsToBeReady(namespace string) Action {
}
}

func filterEvictedPods(pods []corev1.Pod) []corev1.Pod {
var filteredPods []corev1.Pod

for _, pod := range pods {
if pod.Status.Phase != corev1.PodFailed || pod.Status.Reason != "Evicted" {
filteredPods = append(filteredPods, pod)
}
}

return filteredPods
}

func WaitForResourceToBeCreated(namespace string, gvk schema.GroupVersionKind) Action {
return func(ctx context.Context, cli client.Client, f *Feature) error {
f.Log.Info("waiting for resource to be created", "namespace", namespace, "resource", gvk)
Expand Down
22 changes: 19 additions & 3 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
)

const envVarName = "ZAP_LOG_LEVEL"

var defaultLogLevel = zap.InfoLevel

var logLevel atomic.Value

// copy from controller-runtime/pkg/log/zap/flag.go.
Expand Down Expand Up @@ -63,16 +67,28 @@ func SetLevel(levelStr string) error {
return nil
}

func levelFromEnvOrDefault() zapcore.Level {
levelStr := os.Getenv(envVarName)
if levelStr == "" {
return defaultLogLevel
}
level, err := stringToLevel(levelStr)
if err != nil {
return defaultLogLevel
}
return level
}

func NewLogger(mode string, override *ctrlzap.Options) logr.Logger {
opts := newOptions(mode)
opts := newOptions(mode, levelFromEnvOrDefault())
overrideOptions(opts, override)
logLevel.Store(opts.Level)
return ctrlzap.New(ctrlzap.UseFlagOptions(opts))
}

func newOptions(mode string) *ctrlzap.Options {
func newOptions(mode string, defaultLevel zapcore.Level) *ctrlzap.Options {
var opts ctrlzap.Options
level := zap.NewAtomicLevelAt(zapcore.InfoLevel)
level := zap.NewAtomicLevelAt(defaultLevel)

switch mode {
case "devel", "development": // the most logging verbosity
Expand Down

0 comments on commit 78dd970

Please sign in to comment.