Files
cozystack/pkg/apiserver/kind_serializer_test.go
T
Aleksei Sviridkin 3dde6503ec refactor(api): keep each app's kind without the apimachinery fork
Every apps kind is registered on the one Application Go type, and the
stock Scheme.ConvertToVersion picks the first kind registered for a
type, so without help a Redis comes back from the API as whichever app
kind was registered first. So far the fix lived in a fork of
k8s.io/apimachinery that patches ConvertToVersion. Upstream has not
taken the change, and the fork has to be rebased onto every Kubernetes
minor before the libraries can move.

The apps group now encodes through a serializer that picks the kind the
object carries, along the lines upstream suggested instead of changing
Scheme. The replace directive goes, and with it the Renovate rule that
kept the fork's stale release tags from being proposed as updates. The
serializer relies on the storage stamping the kind on every object it
returns, which it does. A test drives create, get, list, update, watch,
apply and table through the real handler chain for two kinds and fails
without the serializer on upstream apimachinery.

One path converts through the Scheme rather than the serializer and
goes back to the first registered kind: the row objects of a Table
requested with includeObject=Object, which kubectl asks for only with
--sort-by. Admission webhooks and ValidatingAdmissionPolicy get the
request kind set explicitly.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-09-26 16:08:24 +03:00

128 lines
4.5 KiB
Go

/*
Copyright 2026 The Cozystack 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 apiserver
import (
"bytes"
"encoding/json"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1"
"github.com/cozystack/cozystack/pkg/config"
)
// newTwoKindCodecs registers Postgres before Redis on a fresh scheme, so
// Postgres is the kind a stock conversion falls back to.
func newTwoKindCodecs(t *testing.T) serializer.CodecFactory {
t.Helper()
s := runtime.NewScheme()
metav1.AddToGroupVersion(s, appsv1alpha1.SchemeGroupVersion)
if err := appsv1alpha1.RegisterDynamicTypes(s, &config.ResourceConfig{Resources: []config.Resource{
{Application: config.ApplicationConfig{Kind: "Postgres"}},
{Application: config.ApplicationConfig{Kind: "Redis"}},
}}); err != nil {
t.Fatal(err)
}
return serializer.NewCodecFactory(s)
}
func encodedKind(t *testing.T, enc runtime.Encoder, obj runtime.Object) string {
t.Helper()
var b bytes.Buffer
if err := enc.Encode(obj, &b); err != nil {
t.Fatal(err)
}
var m struct {
Kind string `json:"kind"`
}
if err := json.Unmarshal(b.Bytes(), &m); err != nil {
t.Fatal(err)
}
return m.Kind
}
func TestKindPreservingSerializer_Encode(t *testing.T) {
codecs := newTwoKindCodecs(t)
info, ok := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
if !ok {
t.Fatal("no JSON serializer")
}
enc := kindPreservingSerializer{codecs}.EncoderForVersion(info.Serializer, appsv1alpha1.SchemeGroupVersion)
gv := appsv1alpha1.SchemeGroupVersion.String()
for _, tc := range []struct {
name string
obj runtime.Object
want string
}{
{"object keeps its kind", &appsv1alpha1.Application{TypeMeta: metav1.TypeMeta{APIVersion: gv, Kind: "Redis"}}, "Redis"},
{"list keeps its kind", &appsv1alpha1.ApplicationList{TypeMeta: metav1.TypeMeta{APIVersion: gv, Kind: "RedisList"}}, "RedisList"},
// Without a kind on the object there is nothing to keep, so the
// stock choice stands; the storages always stamp one.
{"object without a kind falls back to the stock choice", &appsv1alpha1.Application{}, "Postgres"},
} {
t.Run(tc.name, func(t *testing.T) {
if got := encodedKind(t, enc, tc.obj); got != tc.want {
t.Fatalf("kind = %q, want %q", got, tc.want)
}
})
}
}
// The output differs from the wrapped encoder's, so a serialization cache keyed
// by encoder identifier must not share entries between them.
func TestKindPreservingSerializer_IdentifierDiffersFromWrapped(t *testing.T) {
codecs := newTwoKindCodecs(t)
info, _ := runtime.SerializerInfoForMediaType(codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
gv := appsv1alpha1.SchemeGroupVersion
wrapped := codecs.EncoderForVersion(info.Serializer, gv).Identifier()
if got := (kindPreservingSerializer{codecs}).EncoderForVersion(info.Serializer, gv).Identifier(); got == wrapped {
t.Fatalf("identifier %q equals the wrapped encoder's", got)
}
}
func TestExactKindVersioner(t *testing.T) {
apps := appsv1alpha1.SchemeGroupVersion
postgres, redis := apps.WithKind("Postgres"), apps.WithKind("Redis")
kinds := []schema.GroupVersionKind{postgres, redis}
other := schema.GroupVersion{Group: "example.com", Version: "v1"}
for _, tc := range []struct {
name string
want schema.GroupVersionKind
fallback runtime.GroupVersioner
wantGVK schema.GroupVersionKind
wantOK bool
}{
{"picks the object's kind", redis, apps, redis, true},
{"defers when the target is another group version", redis, other, schema.GroupVersionKind{}, false},
{"defers when the kind is not registered for the type", apps.WithKind("Kafka"), apps, postgres, true},
} {
t.Run(tc.name, func(t *testing.T) {
got, ok := exactKindVersioner{want: tc.want, fallback: tc.fallback}.KindForGroupVersionKinds(kinds)
if got != tc.wantGVK || ok != tc.wantOK {
t.Fatalf("got %v, %v; want %v, %v", got, ok, tc.wantGVK, tc.wantOK)
}
})
}
}