Skip to content

Commit

Permalink
Merge pull request #2870 from spiffxp/unmarshal-strict
Browse files Browse the repository at this point in the history
keps: centralize yaml calls, use UnmarshalStrict
  • Loading branch information
k8s-ci-robot authored Aug 17, 2021
2 parents 0ec64c8 + 1bee1d9 commit d037553
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 24 deletions.
5 changes: 3 additions & 2 deletions api/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (

"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"

"k8s.io/enhancements/pkg/yaml"
)

type PRRApprovals []*PRRApproval
Expand Down Expand Up @@ -108,7 +109,7 @@ func (p *PRRHandler) Parse(in io.Reader) (*PRRApproval, error) {
return approval, errors.Wrap(err, "reading file")
}

if err := yaml.Unmarshal(body.Bytes(), &approval); err != nil {
if err := yaml.UnmarshalStrict(body.Bytes(), &approval); err != nil {
p.Errors = append(p.Errors, errors.Wrap(err, "error unmarshalling YAML"))
return approval, errors.Wrap(err, "unmarshalling YAML")
}
Expand Down
7 changes: 4 additions & 3 deletions api/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

"github.com/pkg/errors"

"sigs.k8s.io/yaml"
"k8s.io/enhancements/pkg/yaml"
)

// GroupFetcher is responsible for getting informationg about groups in the
Expand Down Expand Up @@ -147,9 +147,10 @@ func (f *RemoteGroupFetcher) FetchPRRApprovers() ([]string, error) {
}

config := &struct {
Data map[string][]string `json:"aliases,omitempty"`
Data map[string][]string `json:"aliases,omitempty" yaml:"aliases,omitempty"`
}{}
if err := yaml.Unmarshal(body, config); err != nil {

if err := yaml.UnmarshalStrict(body, config); err != nil {
return nil, errors.Wrap(err, "unmarshalling owners aliases")
}

Expand Down
13 changes: 8 additions & 5 deletions api/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (

"github.com/go-playground/validator/v10"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"

"k8s.io/enhancements/pkg/yaml"
)

var ValidStages = []string{
Expand Down Expand Up @@ -119,7 +120,7 @@ func (k *KEPHandler) Parse(in io.Reader) (*Proposal, error) {
kep.Contents = ""
}

if err := yaml.Unmarshal(metadata, &kep); err != nil {
if err := yaml.UnmarshalStrict(metadata, &kep); err != nil {
k.Errors = append(k.Errors, errors.Wrap(err, "error unmarshalling YAML"))
return kep, errors.Wrap(err, "unmarshalling YAML")
}
Expand Down Expand Up @@ -194,9 +195,11 @@ func (k *KEPHandler) Validate(p *Proposal) []error {
}

type Milestone struct {
Alpha string `json:"alpha" yaml:"alpha"`
Beta string `json:"beta" yaml:"beta"`
Stable string `json:"stable" yaml:"stable"`
Alpha string `json:"alpha" yaml:"alpha"`
Beta string `json:"beta" yaml:"beta"`
Stable string `json:"stable" yaml:"stable"`
Deprecated string `json:"deprecated" yaml:"deprecated,omitempty"`
Removed string `json:"removed" yaml:"removed,omitempty"`
}

type FeatureGate struct {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ require (
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
k8s.io/release v0.7.1-0.20210218090651-d71805402dab
k8s.io/test-infra v0.0.0-20200813194141-e9678d500461
sigs.k8s.io/yaml v1.2.0
)
2 changes: 1 addition & 1 deletion keps/sig-network/2595-expanded-dns-config/kep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ latest-milestone: "v1.22"
milestone:
alpha: "v1.22"
beta: "x.y"
GA: "x.y"
stable: "x.y"

# The following PRR answers are required at alpha release
# List the feature gate name and the components for which it must be enabled
Expand Down
2 changes: 1 addition & 1 deletion keps/sig-node/2727-grpc-probe/kep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ participating-sigs:
- sig-network
status: implementable
creation-date: 2020-04-04
modified-date: 2021-05-12
last-updated: 2021-05-12
reviewers:
- "@thockin"
- "@mrunalp"
Expand Down
2 changes: 1 addition & 1 deletion pkg/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"strings"

"github.com/olekukonko/tablewriter"
"gopkg.in/yaml.v3"

"k8s.io/enhancements/api"
"k8s.io/enhancements/pkg/yaml"
)

const (
Expand Down
9 changes: 5 additions & 4 deletions pkg/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"

"k8s.io/enhancements/api"
"k8s.io/enhancements/pkg/kepval"
krgh "k8s.io/release/pkg/github"
"k8s.io/test-infra/prow/git"

"k8s.io/enhancements/api"
"k8s.io/enhancements/pkg/kepval"
"k8s.io/enhancements/pkg/yaml"
)

const (
Expand Down Expand Up @@ -439,7 +440,7 @@ func (r *Repo) loadKEPFromYaml(kepPath string) (*api.Proposal, error) {
}

var p api.Proposal
err = yaml.Unmarshal(b, &p)
err = yaml.UnmarshalStrict(b, &p)
if err != nil {
return nil, fmt.Errorf("unable to load KEP metadata: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"k8s.io/release/pkg/log"

"k8s.io/enhancements/api"
"k8s.io/enhancements/pkg/repo"
"k8s.io/release/pkg/log"
"k8s.io/enhancements/pkg/yaml"
)

var fixture = struct {
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestProposalValidate(t *testing.T) {
require.NoError(t, err)

var p api.Proposal
err = yaml.Unmarshal(b, &p)
err = yaml.UnmarshalStrict(b, &p)
require.NoError(t, err)

errs := parser.Validate(&p)
Expand Down
3 changes: 2 additions & 1 deletion pkg/repo/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
"path/filepath"

"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"k8s.io/enhancements/api"

"k8s.io/enhancements/pkg/yaml"
)

func (r *Repo) WriteKEP(kep *api.Proposal) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/repo/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (

"k8s.io/enhancements/api"
"k8s.io/enhancements/pkg/repo"
"sigs.k8s.io/yaml"

"k8s.io/enhancements/pkg/yaml"
)

// TODO: Consider using afero to mock the filesystem here
Expand Down Expand Up @@ -126,7 +127,7 @@ func TestWriteKep(t *testing.T) {
require.NoError(t, err)

var p api.Proposal
err = yaml.Unmarshal(b, &p)
err = yaml.UnmarshalStrict(b, &p)
require.NoError(t, err)

p.OwningSIG = tc.sig
Expand Down
39 changes: 39 additions & 0 deletions pkg/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
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 yaml

import (
"bytes"

"gopkg.in/yaml.v3"
)

// UnmarshalStrict unmarshals the contents of body into the given interface,
// and returns an error if any duplicate or unknown fields are encountered
func UnmarshalStrict(body []byte, v interface{}) error {
r := bytes.NewReader(body)
d := yaml.NewDecoder(r)
d.KnownFields(true)
err := d.Decode(v)
return err
}

// Marshal returns a byte array containing a YAML representation of the
// given interface, and a non-nil error if there was an error
func Marshal(v interface{}) ([]byte, error) {
return yaml.Marshal(v)
}

0 comments on commit d037553

Please sign in to comment.