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

fix: do not require namespaces when parsing errors about cluster scoped resources #5764

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/dataplane/sendconfig/inmemory_error_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"

"github.com/kong/kubernetes-ingress-controller/v3/internal/util"
kongv1 "github.com/kong/kubernetes-ingress-controller/v3/pkg/apis/configuration/v1"
)

// rawResourceError is a Kong configuration error associated with a Kubernetes resource with Kubernetes metadata stored
Expand Down Expand Up @@ -158,11 +159,12 @@ func parseRawResourceError(raw rawResourceError) (ResourceError, error) {
re.UID = strings.TrimPrefix(tag, util.K8sUIDTagPrefix)
}
}

re.APIVersion, re.Kind = gvk.ToAPIVersionAndKind()
if re.Name == "" {
return re, fmt.Errorf("no name")
}
if re.Namespace == "" {
if re.Namespace == "" && !gvkIsClusterScoped(gvk) {
return re, fmt.Errorf("no namespace")
}
if re.Kind == "" {
Expand All @@ -173,3 +175,10 @@ func parseRawResourceError(raw rawResourceError) (ResourceError, error) {
}
return re, nil
}

func gvkIsClusterScoped(gvk schema.GroupVersionKind) bool {
if gvk.Group == kongv1.GroupVersion.Group && gvk.Version == kongv1.GroupVersion.Version {
return gvk.Kind == "KongClusterPlugin" || gvk.Kind == "KongLicense" || gvk.Kind == "KongVault"
}
return false
}
84 changes: 84 additions & 0 deletions internal/dataplane/sendconfig/inmemory_error_handling_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package sendconfig

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestParseRawResourceError(t *testing.T) {
testcases := []struct {
name string
input rawResourceError
expected ResourceError
expectedErr bool
}{
{
name: "KongClusterPlugin invalid schema - unknown field",
input: rawResourceError{
Name: "prometheus",
ID: "",
Tags: []string{
"k8s-name:default-kong",
"k8s-kind:KongClusterPlugin",
"k8s-uid:f9439f18-a1f8-4090-a248-3d0071c234d1",
"k8s-group:configuration.konghq.com",
"k8s-version:v1",
},
Problems: map[string]string{
"config.config": "unknown field",
},
},
expected: ResourceError{
Name: "default-kong",
Kind: "KongClusterPlugin",
UID: "f9439f18-a1f8-4090-a248-3d0071c234d1",
APIVersion: "configuration.konghq.com/v1",
Namespace: "",
Problems: map[string]string{
"config.config": "unknown field",
},
},
},
{
name: "KongPlugin invalid schema - unknown field",
input: rawResourceError{
Name: "prometheus",
ID: "",
Tags: []string{
"k8s-name:default-kong",
"k8s-namespace:kong",
"k8s-kind:KongClusterPlugin",
"k8s-uid:f9439f18-a1f8-4090-a248-3d0071c234d1",
"k8s-group:configuration.konghq.com",
"k8s-version:v1",
},
Problems: map[string]string{
"config.config": "unknown field",
},
},
expected: ResourceError{
Name: "default-kong",
Kind: "KongClusterPlugin",
UID: "f9439f18-a1f8-4090-a248-3d0071c234d1",
APIVersion: "configuration.konghq.com/v1",
Namespace: "kong",
Problems: map[string]string{
"config.config": "unknown field",
},
},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
resErr, err := parseRawResourceError(tc.input)
if tc.expectedErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tc.expected, resErr)
}
})
}
}
Loading