From 79d591e2b01c4c4825de56f743f2f78de0d1fda4 Mon Sep 17 00:00:00 2001 From: Phani Teja Marupaka Date: Tue, 4 Feb 2020 09:34:10 -0800 Subject: [PATCH] Opt in to use schema --- cmd/config/internal/commands/fmt.go | 7 +- kyaml/kio/filters/fmtr.go | 11 ++- kyaml/kio/filters/fmtr_test.go | 107 +++++++++++++++++++++++----- 3 files changed, 103 insertions(+), 22 deletions(-) diff --git a/cmd/config/internal/commands/fmt.go b/cmd/config/internal/commands/fmt.go index d175d6bc9..3a481d36b 100644 --- a/cmd/config/internal/commands/fmt.go +++ b/cmd/config/internal/commands/fmt.go @@ -31,6 +31,8 @@ formatting substitution verbs {'%n': 'metadata.name', '%s': 'metadata.namespace' `if true, keep index and filename annotations set on Resources.`) c.Flags().BoolVar(&r.Override, "override", false, `if true, override existing filepath annotations.`) + c.Flags().BoolVar(&r.UseSchema, "use-schema", false, + `if true, uses openapi resource schema to format resources.`) r.Command = c return r } @@ -46,6 +48,7 @@ type FmtRunner struct { SetFilenames bool KeepAnnotations bool Override bool + UseSchema bool } func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error { @@ -56,7 +59,9 @@ func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error { } func (r *FmtRunner) runE(c *cobra.Command, args []string) error { - f := []kio.Filter{filters.FormatFilter{}} + f := []kio.Filter{filters.FormatFilter{ + UseSchema: r.UseSchema, + }} // format with file names if r.SetFilenames { diff --git a/kyaml/kio/filters/fmtr.go b/kyaml/kio/filters/fmtr.go index 0266fc550..5cb2b0c00 100644 --- a/kyaml/kio/filters/fmtr.go +++ b/kyaml/kio/filters/fmtr.go @@ -56,7 +56,8 @@ func FormatFileOrDirectory(path string) error { } type FormatFilter struct { - Process func(n *yaml.Node) error + Process func(n *yaml.Node) error + UseSchema bool } var _ kio.Filter = FormatFilter{} @@ -78,7 +79,12 @@ func (f FormatFilter) Filter(slice []*yaml.RNode) ([]*yaml.RNode, error) { continue } kind, apiVersion := kindNode.YNode().Value, apiVersionNode.YNode().Value - s := openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: apiVersion, Kind: kind}) + var s *openapi.ResourceSchema + if f.UseSchema { + s = openapi.SchemaForResourceType(yaml.TypeMeta{APIVersion: apiVersion, Kind: kind}) + } else { + s = nil + } err = (&formatter{apiVersion: apiVersion, kind: kind, process: f.Process}). fmtNode(slice[i].YNode(), "", s) if err != nil { @@ -151,7 +157,6 @@ func (f *formatter) fmtNode(n *yaml.Node, path string, schema *openapi.ResourceS s = schema.Elements() } } - // format the node using the schema err := f.fmtNode(n.Content[i], p, s) if err != nil { diff --git a/kyaml/kio/filters/fmtr_test.go b/kyaml/kio/filters/fmtr_test.go index dff6148f6..67e1be700 100644 --- a/kyaml/kio/filters/fmtr_test.go +++ b/kyaml/kio/filters/fmtr_test.go @@ -74,6 +74,73 @@ spec: containerPort: 80 ` + buff := &bytes.Buffer{} + err := kio.Pipeline{ + Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}}, + Filters: []kio.Filter{FormatFilter{ + UseSchema: true, + }}, + Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}}, + }.Execute() + assert.NoError(t, err) + assert.Equal(t, expected, buff.String()) +} + +func TestFormat_UnsortedInput_No_Schema(t *testing.T) { + y := ` +apiVersion: apps/v1 +spec: + template: + spec: + containers: + - name: nginx + image: nginx:1.0.0 + args: + - on + - 1 + - hello + ports: + - name: http + targetPort: 80 + containerPort: 80 +kind: Deployment +metadata: + name: foo + labels: + foo: on + foo2: hello1 + annotations: + bar: 1 + bar2: hello2 +` + + // keep the style on values that parse as non-string types + expected := `apiVersion: apps/v1 +kind: Deployment +metadata: + name: foo + labels: + foo: on + foo2: hello1 + annotations: + bar: 1 + bar2: hello2 +spec: + template: + spec: + containers: + - name: nginx + image: nginx:1.0.0 + args: + - on + - 1 + - hello + ports: + - name: http + targetPort: 80 + containerPort: 80 +` + buff := &bytes.Buffer{} err := kio.Pipeline{ Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}}, @@ -120,16 +187,18 @@ spec: buff := &bytes.Buffer{} err := kio.Pipeline{ Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}}, - Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error { - if yaml.IsYaml1_1NonString(n) { - // don't change these styles, they are important for backwards compatibility - // e.g. "on" must remain quoted, on must remain unquoted + Filters: []kio.Filter{FormatFilter{ + UseSchema: true, + Process: func(n *yaml.Node) error { + if yaml.IsYaml1_1NonString(n) { + // don't change these styles, they are important for backwards compatibility + // e.g. "on" must remain quoted, on must remain unquoted + return nil + } + // style does not have semantic meaning + n.Style = 0 return nil - } - // style does not have semantic meaning - n.Style = 0 - return nil - }}}, + }}}, Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}}, }.Execute() assert.NoError(t, err) @@ -186,16 +255,18 @@ spec: buff = &bytes.Buffer{} err = kio.Pipeline{ Inputs: []kio.Reader{&kio.ByteReader{Reader: strings.NewReader(y)}}, - Filters: []kio.Filter{FormatFilter{Process: func(n *yaml.Node) error { - if yaml.IsYaml1_1NonString(n) { - // don't change these styles, they are important for backwards compatibility - // e.g. "on" must remain quoted, on must remain unquoted + Filters: []kio.Filter{FormatFilter{ + UseSchema: true, + Process: func(n *yaml.Node) error { + if yaml.IsYaml1_1NonString(n) { + // don't change these styles, they are important for backwards compatibility + // e.g. "on" must remain quoted, on must remain unquoted + return nil + } + // style does not have semantic meaning + n.Style = yaml.SingleQuotedStyle return nil - } - // style does not have semantic meaning - n.Style = yaml.SingleQuotedStyle - return nil - }}}, + }}}, Outputs: []kio.Writer{kio.ByteWriter{Writer: buff}}, }.Execute() assert.NoError(t, err)