reflector: detects unsupported meta.Table gvks for watchlist

Kubernetes-commit: da7c55e0d2f962c8493864a069be5acb8902579f
This commit is contained in:
Lukasz Szaszkiewicz 2025-07-14 13:13:44 +02:00 committed by Kubernetes Publisher
parent 3a4ad9c658
commit c1d15a3638
2 changed files with 82 additions and 0 deletions

View File

@ -31,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/naming"
@ -912,6 +913,15 @@ loop:
continue
}
}
// For now, lets block unsupported Table
// resources for watchlist only
// see #132926 for more info
if exitOnWatchListBookmarkReceived {
if unsupportedGVK := isUnsupportedTableObject(event.Object); unsupportedGVK {
utilruntime.HandleErrorWithContext(ctx, nil, "Unsupported watch event object gvk", "reflector", name, "actualGVK", event.Object.GetObjectKind().GroupVersionKind())
continue
}
}
meta, err := meta.Accessor(event.Object)
if err != nil {
utilruntime.HandleErrorWithContext(ctx, err, "Unable to understand watch event", "reflector", name, "event", event)
@ -1179,3 +1189,22 @@ func (e *VeryShortWatchError) Error() string {
return fmt.Sprintf("very short watch: %s: Unexpected watch close - "+
"watch lasted less than a second and no items received", e.Name)
}
var unsupportedTableGVK = map[schema.GroupVersionKind]bool{
metav1beta1.SchemeGroupVersion.WithKind("Table"): true,
metav1.SchemeGroupVersion.WithKind("Table"): true,
}
// isUnsupportedTableObject checks whether the given runtime.Object
// is a "Table" object that belongs to a set of well-known unsupported GroupVersionKinds.
func isUnsupportedTableObject(rawObject runtime.Object) bool {
unstructuredObj, ok := rawObject.(*unstructured.Unstructured)
if !ok {
return false
}
if unstructuredObj.GetKind() != "Table" {
return false
}
return unsupportedTableGVK[rawObject.GetObjectKind().GroupVersionKind()]
}

View File

@ -2361,3 +2361,56 @@ func BenchmarkReflectorList(b *testing.B) {
})
}
}
func TestIsUnsupportedTableObject(t *testing.T) {
tests := []struct {
name string
obj runtime.Object
expected bool
}{
{
name: "Unsupported Table object in meta.k8s.io/v1beta1",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "meta.k8s.io/v1beta1",
"kind": "Table",
},
},
expected: true,
},
{
name: "Unsupported Table object in meta.k8s.io/v1",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "meta.k8s.io/v1",
"kind": "Table",
},
},
expected: true,
},
{
name: "Pod obj is not a Table",
obj: &v1.Pod{},
expected: false,
},
{
name: "Table object with unrecognised API group",
obj: &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "custom.group/v1",
"kind": "Table",
},
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isUnsupportedTableObject(tt.obj)
if result != tt.expected {
t.Errorf("Expected %v, got %v", tt.expected, result)
}
})
}
}