diff --git a/cmd/yamlfmt/OWNERS b/cmd/yamlfmt/OWNERS deleted file mode 100644 index f1467bc8cba..00000000000 --- a/cmd/yamlfmt/OWNERS +++ /dev/null @@ -1,10 +0,0 @@ -# See the OWNERS docs at https://go.k8s.io/owners - -reviewers: - - bentheelder - - dims -approvers: - - dims -labels: - - sig/testing - - sig/contributor-experience diff --git a/cmd/yamlfmt/yamlfmt.go b/cmd/yamlfmt/yamlfmt.go deleted file mode 100644 index ebde76f2854..00000000000 --- a/cmd/yamlfmt/yamlfmt.go +++ /dev/null @@ -1,72 +0,0 @@ -/* -Copyright 2021 The Kubernetes 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 main - -import ( - "flag" - "fmt" - "io" - "os" - - yaml "go.yaml.in/yaml/v3" -) - -func main() { - indent := flag.Int("indent", 2, "default indent") - flag.Parse() - for _, path := range flag.Args() { - sourceYaml, err := os.ReadFile(path) - if err != nil { - fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) - continue - } - rootNode, err := fetchYaml(sourceYaml) - if err != nil { - fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) - continue - } - writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) - if err != nil { - fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) - continue - } - err = streamYaml(writer, indent, rootNode) - if err != nil { - fmt.Fprintf(os.Stderr, "%s: %v\n", path, err) - continue - } - } -} - -func fetchYaml(sourceYaml []byte) (*yaml.Node, error) { - rootNode := yaml.Node{} - err := yaml.Unmarshal(sourceYaml, &rootNode) - if err != nil { - return nil, err - } - return &rootNode, nil -} - -func streamYaml(writer io.Writer, indent *int, in *yaml.Node) error { - encoder := yaml.NewEncoder(writer) - encoder.SetIndent(*indent) - err := encoder.Encode(in) - if err != nil { - return err - } - return encoder.Close() -} diff --git a/cmd/yamlfmt/yamlfmt_test.go b/cmd/yamlfmt/yamlfmt_test.go deleted file mode 100644 index c0e9816dfcc..00000000000 --- a/cmd/yamlfmt/yamlfmt_test.go +++ /dev/null @@ -1,53 +0,0 @@ -/* -Copyright 2021 The Kubernetes 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 main - -import ( - "bufio" - "bytes" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestFetchYaml(t *testing.T) { - sourceYaml := ` # See the OWNERS docs at https://go.k8s.io/owners -approvers: -- dep-approvers -- thockin # Network -- liggitt - -labels: -- sig/architecture -` - - outputYaml := `# See the OWNERS docs at https://go.k8s.io/owners -approvers: - - dep-approvers - - thockin # Network - - liggitt -labels: - - sig/architecture -` - node, _ := fetchYaml([]byte(sourceYaml)) - var output bytes.Buffer - indent := 2 - writer := bufio.NewWriter(&output) - _ = streamYaml(writer, &indent, node) - _ = writer.Flush() - assert.Equal(t, outputYaml, output.String(), "yaml was not formatted correctly") -} diff --git a/go.mod b/go.mod index f064aa5cccc..5f06bd62243 100644 --- a/go.mod +++ b/go.mod @@ -72,7 +72,6 @@ require ( go.uber.org/goleak v1.3.0 go.uber.org/zap v1.27.0 go.yaml.in/yaml/v2 v2.4.2 - go.yaml.in/yaml/v3 v3.0.4 golang.org/x/crypto v0.36.0 golang.org/x/net v0.38.0 golang.org/x/oauth2 v0.27.0 @@ -208,6 +207,7 @@ require ( go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.34.0 // indirect go.uber.org/multierr v1.11.0 // indirect + go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect golang.org/x/mod v0.21.0 // indirect golang.org/x/text v0.23.0 // indirect diff --git a/hack/tools/go.mod b/hack/tools/go.mod index 09c8dacef7b..61a21552ee0 100644 --- a/hack/tools/go.mod +++ b/hack/tools/go.mod @@ -18,6 +18,7 @@ require ( gotest.tools/gotestsum v1.12.0 honnef.co/go/tools v0.6.1 k8s.io/publishing-bot v0.5.0 + sigs.k8s.io/yaml v1.6.0 ) require ( @@ -51,6 +52,7 @@ require ( github.com/spf13/viper v1.20.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect go.uber.org/multierr v1.11.0 // indirect + go.yaml.in/yaml/v3 v3.0.3 // indirect golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect golang.org/x/sync v0.13.0 // indirect golang.org/x/sys v0.32.0 // indirect diff --git a/hack/tools/go.sum b/hack/tools/go.sum index 6b36bee9dc9..cb6a866c667 100644 --- a/hack/tools/go.sum +++ b/hack/tools/go.sum @@ -115,6 +115,10 @@ go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs= go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= +go.yaml.in/yaml/v3 v3.0.3 h1:bXOww4E/J3f66rav3pX3m8w6jDE4knZjGOw8b5Y6iNE= +go.yaml.in/yaml/v3 v3.0.3/go.mod h1:tBHosrYAkRZjRAOREWbDnBXUf08JOwYq++0QNwQiWzI= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -242,3 +246,7 @@ k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= k8s.io/publishing-bot v0.5.0 h1:Hfnhltr+khEcqvoK4GBYrtaA8dHJ50Xjyi+0KGUfU3I= k8s.io/publishing-bot v0.5.0/go.mod h1:S5+zQQhsVUEqdcaohbYf8O+2BeeWRtuYzp4tQLr5An8= k8s.io/utils v0.0.0-20210802155522-efc7438f0176/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= +sigs.k8s.io/randfill v1.0.0 h1:JfjMILfT8A6RbawdsK2JXGBR5AQVfd+9TbzrlneTyrU= +sigs.k8s.io/randfill v1.0.0/go.mod h1:XeLlZ/jmk4i1HRopwe7/aU3H5n1zNUcX6TM94b3QxOY= +sigs.k8s.io/yaml v1.6.0 h1:G8fkbMSAFqgEFgh4b1wmtzDnioxFCUgTZhlbj5P9QYs= +sigs.k8s.io/yaml v1.6.0/go.mod h1:796bPqUfzR/0jLAl6XjHl3Ck7MiyVv8dbTdyT3/pMf4= diff --git a/hack/tools/tools.go b/hack/tools/tools.go index 4e80e14af29..a990be87879 100644 --- a/hack/tools/tools.go +++ b/hack/tools/tools.go @@ -45,4 +45,7 @@ import ( // protobuf generation _ "google.golang.org/grpc/cmd/protoc-gen-go-grpc" _ "google.golang.org/protobuf/cmd/protoc-gen-go" + + // yamlfmt + _ "sigs.k8s.io/yaml/yamlfmt" ) diff --git a/hack/update-vendor.sh b/hack/update-vendor.sh index f260c92a453..45c063bd4b0 100755 --- a/hack/update-vendor.sh +++ b/hack/update-vendor.sh @@ -314,9 +314,9 @@ options: # make root approval non-recursive no_parent_owners: true approvers: -- dep-approvers + - dep-approvers reviewers: -- dep-reviewers + - dep-reviewers __EOF__ # === Disallow transitive dependencies on k8s.io/kubernetes diff --git a/hack/update-yamlfmt.sh b/hack/update-yamlfmt.sh index 0c347815364..0f95332dee7 100755 --- a/hack/update-yamlfmt.sh +++ b/hack/update-yamlfmt.sh @@ -26,15 +26,11 @@ kube::golang::setup_env cd "${KUBE_ROOT}" find_files() { - find . -not \( \ - \( \ - -wholename './.git' \ - -o -wholename './_output' \ - -o -wholename './release' \ - -o -wholename './target' \ - -o -wholename '*/vendor/*' \ - \) -prune \ - \) -name 'OWNERS*' + git ls-files \ + 'OWNERS*' \ + '**/OWNERS*' \ + ':!:vendor/*/OWNERS*' } -find_files | xargs go run cmd/yamlfmt/yamlfmt.go +go -C "${KUBE_ROOT}/hack/tools" install sigs.k8s.io/yaml/yamlfmt +find_files | xargs yamlfmt -o yaml -w diff --git a/vendor/OWNERS b/vendor/OWNERS index 163a7393ee8..5cdf6b569ef 100644 --- a/vendor/OWNERS +++ b/vendor/OWNERS @@ -4,6 +4,6 @@ options: # make root approval non-recursive no_parent_owners: true approvers: -- dep-approvers + - dep-approvers reviewers: -- dep-reviewers + - dep-reviewers