Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output streams #104873

Merged
merged 2 commits into from Oct 11, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/kubelet/apis/config/helpers_test.go
Expand Up @@ -188,6 +188,14 @@ var (
"HealthzBindAddress",
"HealthzPort",
"Logging.Format",
"Logging.Options.JSON.InfoBufferSize.Quantity.Format",
"Logging.Options.JSON.InfoBufferSize.Quantity.d.Dec.scale",
"Logging.Options.JSON.InfoBufferSize.Quantity.d.Dec.unscaled.abs[*]",
"Logging.Options.JSON.InfoBufferSize.Quantity.d.Dec.unscaled.neg",
"Logging.Options.JSON.InfoBufferSize.Quantity.i.scale",
"Logging.Options.JSON.InfoBufferSize.Quantity.i.value",
"Logging.Options.JSON.InfoBufferSize.Quantity.s",
"Logging.Options.JSON.SplitStream",
"Logging.Sanitization",
"TLSCipherSuites[*]",
"TLSMinVersion",
Expand Down
Expand Up @@ -54,6 +54,9 @@ kubeAPIBurst: 10
kubeAPIQPS: 5
logging:
format: text
options:
json:
infoBufferSize: "0"
makeIPTablesUtilChains: true
maxOpenFiles: 1000000
maxPods: 110
Expand Down
Expand Up @@ -54,6 +54,9 @@ kubeAPIBurst: 10
kubeAPIQPS: 5
logging:
format: text
options:
json:
infoBufferSize: "0"
makeIPTablesUtilChains: true
maxOpenFiles: 1000000
maxPods: 110
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/apis/config/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 41 additions & 16 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/generated.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/quantity.go
Expand Up @@ -764,3 +764,30 @@ func (q *Quantity) SetScaled(value int64, scale Scale) {
q.d.Dec = nil
q.i = int64Amount{value: value, scale: scale}
}

// QuantityValue makes it possible to use a Quantity as value for a command
// line parameter.
//
// +protobuf=true
// +protobuf.embed=string
// +protobuf.options.marshal=false
// +protobuf.options.(gogoproto.goproto_stringer)=false
// +k8s:deepcopy-gen=true
type QuantityValue struct {
Quantity
}

// Set implements pflag.Value.Set and Go flag.Value.Set.
func (q *QuantityValue) Set(s string) error {
quantity, err := ParseQuantity(s)
if err != nil {
return err
}
q.Quantity = quantity
return nil
}

// Type implements pflag.Value.Type.
func (q QuantityValue) Type() string {
return "quantity"
}
41 changes: 41 additions & 0 deletions staging/src/k8s.io/apimachinery/pkg/api/resource/quantity_test.go
Expand Up @@ -21,11 +21,13 @@ import (
"fmt"
"math"
"math/rand"
"os"
"strings"
"testing"
"unicode"

fuzz "github.com/google/gofuzz"
"github.com/spf13/pflag"

inf "gopkg.in/inf.v0"
)
Expand Down Expand Up @@ -1475,3 +1477,42 @@ func BenchmarkQuantityAsApproximateFloat64(b *testing.B) {
}
b.StopTimer()
}

var _ pflag.Value = &QuantityValue{}

func TestQuantityValueSet(t *testing.T) {
q := QuantityValue{}

if err := q.Set("invalid"); err == nil {

t.Error("'invalid' did not trigger a parse error")
}

if err := q.Set("1Mi"); err != nil {
t.Errorf("parsing 1Mi should have worked, got: %v", err)
}
if q.Value() != 1024*1024 {
t.Errorf("quantity should have been set to 1Mi, got: %v", q)
}

data, err := json.Marshal(q)
if err != nil {
t.Errorf("unexpected encoding error: %v", err)
}
expected := `"1Mi"`
if string(data) != expected {
t.Errorf("expected 1Mi value to be encoded as %q, got: %q", expected, string(data))
}
}

func ExampleQuantityValue() {
q := QuantityValue{
Quantity: MustParse("1Mi"),
}
fs := pflag.FlagSet{}
fs.SetOutput(os.Stdout)
fs.Var(&q, "mem", "sets amount of memory")
fs.PrintDefaults()
// Output:
// --mem quantity sets amount of memory (default 1Mi)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions staging/src/k8s.io/component-base/config/types.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package config

import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -88,4 +89,25 @@ type LoggingConfiguration struct {
// [Experimental] When enabled prevents logging of fields tagged as sensitive (passwords, keys, tokens).
// Runtime log sanitization may introduce significant computation overhead and therefore should not be enabled in production.`)
Sanitization bool
// [Experimental] Options holds additional parameters that are specific
// to the different logging formats. Only the options for the selected
// format get used, but all of them get validated.
Options FormatOptions
}

// FormatOptions contains options for the different logging formats.
type FormatOptions struct {
// [Experimental] JSON contains options for logging format "json".
JSON JSONOptions
}

// JSONOptions contains options for logging format "json".
type JSONOptions struct {
// [Experimental] SplitStream redirects error messages to stderr while
// info messages go to stdout, with buffering. The default is to write
// both to stdout, without buffering.
SplitStream bool
// [Experimental] InfoBufferSize sets the size of the info stream when
// using split streams. The default is zero, which disables buffering.
InfoBufferSize resource.QuantityValue
}
12 changes: 12 additions & 0 deletions staging/src/k8s.io/component-base/config/v1alpha1/defaults.go
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
"time"

"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilpointer "k8s.io/utils/pointer"
)
Expand Down Expand Up @@ -110,4 +111,15 @@ func RecommendedLoggingConfiguration(obj *LoggingConfiguration) {
if obj.Format == "" {
obj.Format = "text"
}
var empty resource.QuantityValue
if obj.Options.JSON.InfoBufferSize == empty {
obj.Options.JSON.InfoBufferSize = resource.QuantityValue{
// This is similar, but not quite the same as a default
// constructed instance.
Quantity: *resource.NewQuantity(0, resource.DecimalSI),
}
// This sets the unexported Quantity.s which will be compared
// by reflect.DeepEqual in some tests.
_ = obj.Options.JSON.InfoBufferSize.String()
}
}
22 changes: 22 additions & 0 deletions staging/src/k8s.io/component-base/config/v1alpha1/types.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -90,4 +91,25 @@ type LoggingConfiguration struct {
// [Experimental] When enabled prevents logging of fields tagged as sensitive (passwords, keys, tokens).
// Runtime log sanitization may introduce significant computation overhead and therefore should not be enabled in production.`)
Sanitization bool `json:"sanitization,omitempty"`
// [Experimental] Options holds additional parameters that are specific
// to the different logging formats. Only the options for the selected
// format get used, but all of them get validated.
Options FormatOptions `json:"options,omitempty"`
}

// FormatOptions contains options for the different logging formats.
type FormatOptions struct {
// [Experimental] JSON contains options for logging format "json".
JSON JSONOptions `json:"json,omitempty"`
}

// JSONOptions contains options for logging format "json".
type JSONOptions struct {
// [Experimental] SplitStream redirects error messages to stderr while
// info messages go to stdout, with buffering. The default is to write
// both to stdout, without buffering.
SplitStream bool `json:"splitStream,omitempty"`
// [Experimental] InfoBufferSize sets the size of the info stream when
// using split streams. The default is zero, which disables buffering.
InfoBufferSize resource.QuantityValue `json:"infoBufferSize,omitempty"`
}