Skip to content

Commit

Permalink
refactor: simplify logging and add fields
Browse files Browse the repository at this point in the history
Signed-off-by: Dustin Scott <[email protected]>
  • Loading branch information
scottd018 committed Nov 27, 2024
1 parent c4d8545 commit d9f3233
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ func main() {
// set the handlers and start
http.HandleFunc("/validate", w.Validate)
http.HandleFunc("/healthz", w.HealthZ)
w.Log.Info().Msg("Starting webhook server on :8443")
w.Log.Fatal().Msg(http.ListenAndServeTLS(":8443", "/ssl_certs/tls.crt", "/ssl_certs/tls.key", nil).Error())
w.Logger.Info().Msg("Starting webhook server on :8443")
w.Logger.Fatal().Msg(http.ListenAndServeTLS(":8443", "/ssl_certs/tls.crt", "/ssl_certs/tls.key", nil).Error())
}
2 changes: 2 additions & 0 deletions resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resources

import (
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
)

type WindowsValidationResult struct {
Expand All @@ -18,6 +19,7 @@ type WindowsInstanceValidator interface {

GetName() string
GetNamespace() string
GetObjectKind() schema.ObjectKind
}

// SupportedResourceTypes returns the supported resources for this webhook.
Expand Down
6 changes: 5 additions & 1 deletion webhook/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ type operation struct {
func NewOperation(w http.ResponseWriter, r *http.Request) (*operation, error) {
// create the base operation object
op := &operation{
response: &response{allowed: true, uid: types.UID(""), writer: w},
response: &response{
allowed: true,
uid: types.UID(""),
writer: w,
},
}

// create the request object
Expand Down
32 changes: 25 additions & 7 deletions webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type webhook struct {
KubeClient *kubernetes.Clientset
VirtClient kubecli.KubevirtClient
NodeFilter resources.NodeFilter
Log zerolog.Logger
Logger zerolog.Logger
}

// NewWebhook returns a new instance of a webhook object.
Expand Down Expand Up @@ -53,7 +53,7 @@ func NewWebhook() (*webhook, error) {
KubeClient: kubeClient,
VirtClient: virtClient,
NodeFilter: resources.NewNodeFilter(os.Getenv(resources.EnvLabelKey), os.Getenv(resources.EnvLabelValues)),
Log: zerolog.New(os.Stdout).Level(logLevel),
Logger: zerolog.New(os.Stdout).Level(logLevel),
}, nil
}

Expand All @@ -64,8 +64,8 @@ func (wh *webhook) Validate(w http.ResponseWriter, r *http.Request) {
if err != nil {
wh.respond(op, err.Error(), true)
}
wh.Log.Info().Msg("received validation request")
wh.Log.Debug().Msgf("OBJECT: %+v", op.object)
wh.log(op).Msg("received validation request")
wh.debug(op).Msgf("OBJECT: %+v", op.object)

// return immediately if we do not need validation
validationResult := op.object.NeedsValidation()
Expand All @@ -74,7 +74,7 @@ func (wh *webhook) Validate(w http.ResponseWriter, r *http.Request) {
return
}

wh.Log.Info().Msgf("validating request for reason [%s]", validationResult.Reason)
wh.log(op).Msgf("validating request for reason [%s]", validationResult.Reason)

// get the requested capacity from the request
requested := op.object.SumCPU()
Expand Down Expand Up @@ -106,7 +106,7 @@ func (wh *webhook) Validate(w http.ResponseWriter, r *http.Request) {

available := total - used

wh.Log.Info().Msg(fmt.Sprintf(
wh.log(op).Msg(fmt.Sprintf(
"capacity: total=[%d], requested=[%d], used=[%d], available=[%d]",
total,
requested,
Expand Down Expand Up @@ -146,10 +146,28 @@ func (wh *webhook) HealthZ(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}

// log logs an info message.
func (wh *webhook) log(op *operation) *zerolog.Event {
return wh.Logger.Info().
Str("uid", string(op.response.uid)).
Str("kind", op.object.GetObjectKind().GroupVersionKind().Kind).
Str("name", op.object.GetName()).
Str("namespace", op.object.GetNamespace())
}

// debug logs a debug message.
func (wh *webhook) debug(op *operation) *zerolog.Event {
return wh.Logger.Debug().
Str("uid", string(op.response.uid)).
Str("kind", op.object.GetObjectKind().GroupVersionKind().Kind).
Str("name", op.object.GetName()).
Str("namespace", op.object.GetNamespace())
}

// respond sends a response for a webhook operation, optionally logging if requested.
func (wh *webhook) respond(op *operation, msg string, logToStdout bool) {
if logToStdout {
wh.Log.Info().Msgf("returning with message: [%s]", msg)
wh.log(op).Msgf("returning with message: [%s]", msg)
}

op.response.send(msg)
Expand Down

0 comments on commit d9f3233

Please sign in to comment.