Skip to content

Commit

Permalink
[release-1.2] override unmarshalJSON for supportedfeature to ensure c…
Browse files Browse the repository at this point in the history
…omptability (#3465)

* override json unmarshal for supportedfeature to ensure backward compatability

* add issue link

---------

Co-authored-by: Lior Lieberman <[email protected]>
  • Loading branch information
1 parent f735045 commit 20b8e4e
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
59 changes: 59 additions & 0 deletions apis/v1/gatewayclass_types_overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2024 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 v1

import (
"encoding/json"
"errors"
)

// Below code handles the experimental field breaking change introduced in
// https://github.com/kubernetes-sigs/gateway-api/pull/3200/.
// We are overriding the UnmarshalJSON function to be able to handle cases where
// users had the old version of the GatewayClass CRD applied with SupportedFeatures
// as a list of strings and not list of objects.
// See https://github.com/kubernetes-sigs/gateway-api/issues/3464
// for more information.

func (s *SupportedFeature) UnmarshalJSON(data []byte) error {
var oldSupportedFeature oldSupportedFeature
var unmarshalTypeErr *json.UnmarshalTypeError
if err := json.Unmarshal(data, &oldSupportedFeature); err == nil {
s.Name = FeatureName(oldSupportedFeature)
return nil
} else if !errors.As(err, &unmarshalTypeErr) {
// If the error is not a type error, return it
return err
}

var si supportedFeatureInternal
if err := json.Unmarshal(data, &si); err != nil {
return err
}
s.Name = si.Name
return nil
}

// This is solely for the purpose of ensuring backward compatibility and
// SHOULD NOT be used elsewhere.
type supportedFeatureInternal struct {
Name FeatureName `json:"name"`
}

// This is solely for the purpose of ensuring backward compatibility and
// SHOULD NOT be used elsewhere.
type oldSupportedFeature string
67 changes: 67 additions & 0 deletions apis/v1/gatewayclass_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Copyright 2024 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 v1

import (
"encoding/json"
"testing"
)

func TestSupportedFeature_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input string
expected SupportedFeature
expectError bool
}{
{
name: "old struct input",
input: `"featureA"`,
expected: SupportedFeature{Name: "featureA"},
},
{
name: "new struct input",
input: `{"name": "featureB"}`,
expected: SupportedFeature{Name: "featureB"},
},
{
name: "expected error",
input: `["featureA", "featureB"]`,
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result SupportedFeature
err := json.Unmarshal([]byte(tt.input), &result)

if tt.expectError {
if err == nil {
t.Errorf("Expected an error, but got nil")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if result != tt.expected {
t.Errorf("Expected %+v, got %+v", tt.expected, result)
}
}
})
}
}
25 changes: 23 additions & 2 deletions pkg/generated/openapi/zz_generated.openapi.go

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

0 comments on commit 20b8e4e

Please sign in to comment.