Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmd/cache-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ import (
utilerrors "k8s.io/apimachinery/pkg/util/errors"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/component-base/cli"
logsapiv1 "k8s.io/component-base/logs/api/v1"

"github.com/kcp-dev/embeddedetcd"
"github.com/kcp-dev/sdk/cmd/help"

cacheserver "github.com/kcp-dev/kcp/pkg/cache/server"
"github.com/kcp-dev/kcp/pkg/cache/server/options"
kcpfeatures "github.com/kcp-dev/kcp/pkg/features"

_ "github.com/kcp-dev/kcp/pkg/logging/json/register"
)

func main() {
Expand Down Expand Up @@ -72,6 +76,11 @@ func main() {
`),

RunE: func(c *cobra.Command, args []string) error {
// run as early as possible to avoid races later when some components (e.g. grpc) start early using klog
if err := logsapiv1.ValidateAndApply(serverOptions.Logs, kcpfeatures.DefaultFeatureGate); err != nil {
return err
}

completed, err := serverOptions.Complete()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/kcp-front-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
kcpfeatures "github.com/kcp-dev/kcp/pkg/features"
"github.com/kcp-dev/kcp/pkg/proxy"

_ "k8s.io/component-base/logs/json/register"
_ "github.com/kcp-dev/kcp/pkg/logging/json/register"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/kcp/kcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
kcpfeatures "github.com/kcp-dev/kcp/pkg/features"
"github.com/kcp-dev/kcp/pkg/server"

_ "k8s.io/component-base/logs/json/register"
_ "github.com/kcp-dev/kcp/pkg/logging/json/register"
)

func main() {
Expand Down
2 changes: 2 additions & 0 deletions cmd/virtual-workspaces/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (

cacheoptions "github.com/kcp-dev/kcp/pkg/cache/client/options"
corevwoptions "github.com/kcp-dev/kcp/pkg/virtual/options"

_ "github.com/kcp-dev/kcp/pkg/logging/json/register"
)

// DefaultRootPathPrefix is basically constant forever, or we risk a breaking change. The
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/xrstf/mockoidc v0.0.0-20250721141841-711cc4e835f6
go.uber.org/goleak v1.3.1-0.20251210191316-2b7fd8a0d244
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/sys v0.39.0
gopkg.in/square/go-jose.v2 v2.6.0
k8s.io/api v0.35.1
Expand Down Expand Up @@ -180,7 +181,6 @@ require (
go.opentelemetry.io/otel/sdk v1.36.0 // indirect
go.opentelemetry.io/otel/trace v1.36.0 // indirect
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/crypto v0.46.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions pkg/cache/server/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

genericoptions "k8s.io/apiserver/pkg/server/options"
"k8s.io/apiserver/pkg/storage/storagebackend"
"k8s.io/component-base/logs"
logsapiv1 "k8s.io/component-base/logs/api/v1"
kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options"

etcdoptions "github.com/kcp-dev/embeddedetcd/options"
Expand All @@ -36,6 +38,7 @@ type Options struct {
Authorization *genericoptions.DelegatingAuthorizationOptions
APIEnablement *genericoptions.APIEnablementOptions
EmbeddedEtcd etcdoptions.Options
Logs *logs.Options
SyntheticDelay time.Duration
}

Expand All @@ -47,6 +50,7 @@ type completedOptions struct {
Authorization *genericoptions.DelegatingAuthorizationOptions
APIEnablement *genericoptions.APIEnablementOptions
EmbeddedEtcd etcdoptions.CompletedOptions
Logs *logs.Options
SyntheticDelay time.Duration
}

Expand Down Expand Up @@ -76,6 +80,7 @@ func NewOptions(rootDir string) *Options {
Authorization: genericoptions.NewDelegatingAuthorizationOptions(),
APIEnablement: genericoptions.NewAPIEnablementOptions(),
EmbeddedEtcd: *etcdoptions.NewOptions(rootDir),
Logs: logs.NewOptions(),
}

o.SecureServing.ServerCert.CertDirectory = rootDir
Expand Down Expand Up @@ -110,12 +115,14 @@ func (o *Options) Complete() (*CompletedOptions, error) {
Authorization: o.Authorization,
APIEnablement: o.APIEnablement,
EmbeddedEtcd: o.EmbeddedEtcd.Complete(o.Etcd),
Logs: o.Logs,
}}, nil
}

func (o *Options) AddFlags(fs *pflag.FlagSet) {
o.Etcd.AddFlags(fs)
o.EmbeddedEtcd.AddFlags(fs)
o.SecureServing.AddFlags(fs)
logsapiv1.AddFlags(o.Logs, fs)
fs.DurationVar(&o.SyntheticDelay, "synthetic-delay", 0, "The duration of time the cache server will inject a delay for to all inbound requests. Useful for testing.")
}
21 changes: 21 additions & 0 deletions pkg/logging/json/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Copyright 2026 The kcp Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package json exists solely to change the date formatting in the JSON log
// output, which Kubernetes hardcodes to be float-based UNIX timestamps,
// but which we want as much more useful ISO 8601 date strings. Sadly there
// is no way to just configure this without replicating the entire JSON logger.
package json
80 changes: 80 additions & 0 deletions pkg/logging/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright 2026 The kcp Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package json

import (
"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"

"k8s.io/component-base/featuregate"
logsapi "k8s.io/component-base/logs/api/v1"
upstreamjson "k8s.io/component-base/logs/json"
)

// Factory produces JSON logger instances with ISO8601 timestamps.
type Factory struct{}

var _ logsapi.LogFormatFactory = Factory{}

func (f Factory) Feature() featuregate.Feature {
return logsapi.LoggingBetaOptions
}

func (f Factory) Create(c logsapi.LoggingConfiguration, o logsapi.LoggingOptions) (logr.Logger, logsapi.RuntimeControl) {
// We intentionally avoid all os.File.Sync calls. Output is unbuffered,
// therefore we don't need to flush, and calling the underlying fsync
// would just slow down writing.
//
// The assumption is that logging only needs to ensure that data gets
// written to the output stream before the process terminates, but
// doesn't need to worry about data not being written because of a
// system crash or powerloss.
stderr := zapcore.Lock(upstreamjson.AddNopSync(o.ErrorStream))

// Custom encoder config with ISO8601 timestamps
encoderConfig := &zapcore.EncoderConfig{
MessageKey: "msg",
CallerKey: "caller",
NameKey: "logger",
TimeKey: "ts",
// This right here is the only line we want to change compared to upstream.
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}

if c.Options.JSON.SplitStream {
stdout := zapcore.Lock(upstreamjson.AddNopSync(o.InfoStream))
size := c.Options.JSON.InfoBufferSize.Value()
if size > 0 {
// Prevent integer overflow.
if size > 2*1024*1024*1024 {
size = 2 * 1024 * 1024 * 1024
}
stdout = &zapcore.BufferedWriteSyncer{
WS: stdout,
Size: int(size),
}
}

// stdout for info messages, stderr for errors.
return upstreamjson.NewJSONLogger(c.Verbosity, stdout, stderr, encoderConfig)
}

// Write info messages and errors to stderr to prevent mixing with normal program output.
return upstreamjson.NewJSONLogger(c.Verbosity, stderr, nil, encoderConfig)
}
30 changes: 30 additions & 0 deletions pkg/logging/json/register/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2026 The kcp Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package register

import (
logsapi "k8s.io/component-base/logs/api/v1"

"github.com/kcp-dev/kcp/pkg/logging/json"
)

func init() {
// JSON format with ISO8601 timestamps instead of epoch milliseconds
if err := logsapi.RegisterLogFormat(logsapi.JSONLogFormat, json.Factory{}, logsapi.LoggingBetaOptions); err != nil {
panic(err)
}
}