Skip to content

Commit

Permalink
use DeepCopyStruct to scrub log
Browse files Browse the repository at this point in the history
  • Loading branch information
iQQBot committed Oct 13, 2023
1 parent 0135b55 commit 68e87af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
12 changes: 12 additions & 0 deletions components/common-go/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,15 @@ type jsonEntry struct {
Msg string `json:"msg,omitempty"`
Time *time.Time `json:"time,omitempty"`
}

// TrustedValueWrap is a simple wrapper that treats the entire value as trusted, which are not processed by the scrubber.
// During JSON marshal, only the Value itself will be processed, without including Wrap.
type TrustedValueWrap struct {
Value any
}

func (TrustedValueWrap) IsTrustedValue() {}

func (t TrustedValueWrap) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Value)
}
14 changes: 3 additions & 11 deletions components/ws-manager-mk2/controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log"

wsk8s "github.com/gitpod-io/gitpod/common-go/kubernetes"
"github.com/gitpod-io/gitpod/components/scrubber"
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/maintenance"
config "github.com/gitpod-io/gitpod/ws-manager/api/config"
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
Expand Down Expand Up @@ -126,20 +125,13 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
r.updateMetrics(ctx, &workspace)
r.emitPhaseEvents(ctx, &workspace, oldStatus)

var scrubbedPodStatus *corev1.PodStatus
var podStatus *corev1.PodStatus
if len(workspacePods.Items) > 0 {
scrubbedPodStatus = workspacePods.Items[0].Status.DeepCopy()
if err = scrubber.Default.Struct(scrubbedPodStatus); err != nil {
log.Error(err, "failed to scrub pod status")
}
}
scrubbedStatus := workspace.Status.DeepCopy()
if err = scrubber.Default.Struct(scrubbedStatus); err != nil {
log.Error(err, "failed to scrub workspace status")
podStatus = &workspacePods.Items[0].Status
}

if !equality.Semantic.DeepDerivative(oldStatus, workspace.Status) {
log.Info("updating workspace status", "status", scrubbedStatus, "podStatus", scrubbedPodStatus)
log.Info("updating workspace status", "status", workspace.Status, "podStatus", podStatus)
}

err = r.Status().Update(ctx, &workspace)
Expand Down
9 changes: 8 additions & 1 deletion components/ws-manager-mk2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/bombsimon/logrusr/v4"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -43,6 +44,7 @@ import (
config "github.com/gitpod-io/gitpod/ws-manager/api/config"
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"

"github.com/gitpod-io/gitpod/components/scrubber"
"github.com/gitpod-io/gitpod/ws-manager-mk2/controllers"
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/maintenance"
imgproxy "github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/proxy"
Expand Down Expand Up @@ -77,7 +79,12 @@ func main() {
flag.Parse()

log.Init(ServiceName, Version, jsonLog, verbose)
baseLogger := logrusr.New(log.Log)

l := log.WithFields(logrus.Fields{})
l.Logger.SetReportCaller(false)
baseLogger := logrusr.New(l, logrusr.WithFormatter(func(i interface{}) interface{} {
return &log.TrustedValueWrap{Value: scrubber.Default.DeepCopyStruct(i)}
}))
ctrl.SetLogger(baseLogger)
// Set the logger used by k8s (e.g. client-go).
klog.SetLogger(baseLogger)
Expand Down

0 comments on commit 68e87af

Please sign in to comment.