Skip to content

Commit f01a33c

Browse files
committed
use DeepCopyStruct to scrub log
1 parent 1bbe3dc commit f01a33c

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

components/common-go/log/log.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,15 @@ type jsonEntry struct {
231231
Msg string `json:"msg,omitempty"`
232232
Time *time.Time `json:"time,omitempty"`
233233
}
234+
235+
// TrustedValueWrap is a simple wrapper that treats the entire value as trusted, which are not processed by the scrubber.
236+
// During JSON marshal, only the Value itself will be processed, without including Wrap.
237+
type TrustedValueWrap struct {
238+
Value any
239+
}
240+
241+
func (TrustedValueWrap) IsTrustedValue() {}
242+
243+
func (t TrustedValueWrap) MarshalJSON() ([]byte, error) {
244+
return json.Marshal(t.Value)
245+
}

components/ws-manager-mk2/controllers/workspace_controller.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/log"
3030

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

129-
var scrubbedPodStatus *corev1.PodStatus
128+
var podStatus *corev1.PodStatus
130129
if len(workspacePods.Items) > 0 {
131-
scrubbedPodStatus = workspacePods.Items[0].Status.DeepCopy()
132-
if err = scrubber.Default.Struct(scrubbedPodStatus); err != nil {
133-
log.Error(err, "failed to scrub pod status")
134-
}
135-
}
136-
scrubbedStatus := workspace.Status.DeepCopy()
137-
if err = scrubber.Default.Struct(scrubbedStatus); err != nil {
138-
log.Error(err, "failed to scrub workspace status")
130+
podStatus = &workspacePods.Items[0].Status
139131
}
140132

141133
if !equality.Semantic.DeepDerivative(oldStatus, workspace.Status) {
142-
log.Info("updating workspace status", "status", scrubbedStatus, "podStatus", scrubbedPodStatus)
134+
log.Info("updating workspace status", "status", workspace.Status, "podStatus", podStatus)
143135
}
144136

145137
err = r.Status().Update(ctx, &workspace)

components/ws-manager-mk2/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/bombsimon/logrusr/v4"
2424
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
2525
"github.com/prometheus/client_golang/prometheus"
26+
"github.com/sirupsen/logrus"
2627
"k8s.io/apimachinery/pkg/runtime"
2728
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2829
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -43,6 +44,7 @@ import (
4344
config "github.com/gitpod-io/gitpod/ws-manager/api/config"
4445
workspacev1 "github.com/gitpod-io/gitpod/ws-manager/api/crd/v1"
4546

47+
"github.com/gitpod-io/gitpod/components/scrubber"
4648
"github.com/gitpod-io/gitpod/ws-manager-mk2/controllers"
4749
"github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/maintenance"
4850
imgproxy "github.com/gitpod-io/gitpod/ws-manager-mk2/pkg/proxy"
@@ -77,7 +79,12 @@ func main() {
7779
flag.Parse()
7880

7981
log.Init(ServiceName, Version, jsonLog, verbose)
80-
baseLogger := logrusr.New(log.Log)
82+
83+
l := log.WithFields(logrus.Fields{})
84+
l.Logger.SetReportCaller(false)
85+
baseLogger := logrusr.New(l, logrusr.WithFormatter(func(i interface{}) interface{} {
86+
return &log.TrustedValueWrap{Value: scrubber.Default.DeepCopyStruct(i)}
87+
}))
8188
ctrl.SetLogger(baseLogger)
8289
// Set the logger used by k8s (e.g. client-go).
8390
klog.SetLogger(baseLogger)

0 commit comments

Comments
 (0)