Skip to content

Commit

Permalink
add interface
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Okhlopkov <[email protected]>
  • Loading branch information
Pavel Okhlopkov committed Oct 25, 2024
1 parent ab6d66f commit e15549b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/700-go-hook/global-hooks/global-go-hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ var _ = sdk.RegisterFunc(&go_hook.HookConfig{
}, handler)

func handler(input *go_hook.HookInput) error {
input.LogEntry.Infof("Start Global Go hook")
input.Logger.Infof("Start Global Go hook")
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func ObjFilter(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
func run(input *go_hook.HookInput) error {
for _, o := range input.Snapshots["pods"] {
podSpec := o.(*podSpecFilteredObj)
input.LogEntry.Infof("Got podSpec: %+v", podSpec)
input.Logger.Infof("Got podSpec: %+v", podSpec)
}

input.LogEntry.Infof("Hello from on_kube.pods2! I have %d snapshots\n",
input.Logger.Infof("Hello from on_kube.pods2! I have %d snapshots\n",
len(input.Snapshots))

input.MetricsCollector.Add("addon_go_hooks_total", 1.0, nil)
Expand Down
35 changes: 34 additions & 1 deletion pkg/module_manager/go_hook/go_hook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package go_hook

import (
"context"
"io"
"log/slog"
"time"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -41,13 +44,43 @@ type FilterResult interface{}

type Snapshots map[string][]FilterResult

type ILogger interface {
Debug(msg string, args ...any)
DebugContext(ctx context.Context, msg string, args ...any)
Debugf(format string, args ...any)
Enabled(ctx context.Context, level slog.Level) bool
Error(msg string, args ...any)
ErrorContext(ctx context.Context, msg string, args ...any)
Errorf(format string, args ...any)
Fatal(msg string, args ...any)
Fatalf(format string, args ...any)
GetLevel() unilogger.Level
Handler() slog.Handler
Info(msg string, args ...any)
InfoContext(ctx context.Context, msg string, args ...any)
Infof(format string, args ...any)
Log(ctx context.Context, level slog.Level, msg string, args ...any)
LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
Logf(ctx context.Context, level unilogger.Level, format string, args ...any)
Named(name string) *unilogger.Logger
SetLevel(level unilogger.Level)
SetOutput(w io.Writer)
Trace(msg string, args ...any)
Tracef(format string, args ...any)
Warn(msg string, args ...any)
WarnContext(ctx context.Context, msg string, args ...any)
Warnf(format string, args ...any)
With(args ...any) *unilogger.Logger
WithGroup(name string) *unilogger.Logger
}

type HookInput struct {
Snapshots Snapshots
Values *PatchableValues
ConfigValues *PatchableValues
MetricsCollector MetricsCollector
PatchCollector *object_patch.PatchCollector
LogEntry *unilogger.Logger
Logger ILogger
BindingActions *[]BindingAction
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/module_manager/models/hooks/kind/gohook.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (h *GoHook) Execute(_ string, bContext []binding_context.BindingContext, _
Values: patchableValues,
ConfigValues: patchableConfigValues,
PatchCollector: patchCollector,
LogEntry: logEntry,
Logger: logEntry,
MetricsCollector: metricsCollector,
BindingActions: bindingActions,
})
Expand Down

0 comments on commit e15549b

Please sign in to comment.