ensure GVK is configured

This commit is contained in:
Ryan McKinley
2026-01-13 16:34:59 +03:00
parent 4b24e63e0b
commit 8af89b1210
3 changed files with 28 additions and 9 deletions
@@ -60,7 +60,7 @@ func (s *LocalInlineSecureValueService) CanReference(ctx context.Context, owner
}
if owner.APIGroup == "" || owner.APIVersion == "" || owner.Kind == "" || owner.Name == "" {
return fmt.Errorf("owner reference must have a valid API group, API version, kind and name")
return fmt.Errorf("owner reference must have a valid API group, API version, kind and name [CanReference]")
}
if len(names) == 0 {
@@ -167,7 +167,7 @@ func (s *LocalInlineSecureValueService) verifyOwnerAndAuth(ctx context.Context,
}
if owner.Namespace == "" || owner.APIGroup == "" || owner.APIVersion == "" || owner.Kind == "" || owner.Name == "" {
return nil, fmt.Errorf("owner reference must have a valid API group, API version, kind, namespace and name")
return nil, fmt.Errorf("owner reference must have a valid API group, API version, kind, namespace and name [verifyOwnerAndAuth:%+v]", owner)
}
return authInfo, nil
+24 -5
View File
@@ -85,6 +85,9 @@ func (s *Storage) prepareObjectForStorage(ctx context.Context, newObject runtime
if !ok {
return v, errors.New("missing auth info")
}
if err := s.checkGVK(newObject); err != nil {
return v, err
}
obj, err := utils.MetaAccessor(newObject)
if err != nil {
@@ -153,6 +156,9 @@ func (s *Storage) prepareObjectForUpdate(ctx context.Context, updateObject runti
if !ok {
return v, errors.New("missing auth info")
}
if err := s.checkGVK(updateObject); err != nil {
return v, err
}
obj, err := utils.MetaAccessor(updateObject)
if err != nil {
@@ -273,10 +279,9 @@ func (s *Storage) handleLargeResources(ctx context.Context, obj utils.GrafanaMet
return nil
}
func (s *Storage) encode(obj runtime.Object, w io.Writer) error {
// The standard encoder is fine when only one type maps to a group
func (s *Storage) checkGVK(obj runtime.Object) error {
if s.opts.Scheme == nil {
return s.codec.Encode(obj, w)
return nil // we can not do anything
}
// Ensure group+version+kind are configured
@@ -285,7 +290,7 @@ func (s *Storage) encode(obj runtime.Object, w io.Writer) error {
if gvk.Group == "" || gvk.Kind == "" || gvk.Version == "" {
gvks, _, err := s.opts.Scheme.ObjectKinds(obj)
if err != nil {
return fmt.Errorf("unable to encode object %w", err)
return fmt.Errorf("unknown object kind %w", err)
}
for _, v := range gvks {
if v.Group != s.gr.Group {
@@ -297,9 +302,23 @@ func (s *Storage) encode(obj runtime.Object, w io.Writer) error {
gvk.Version = v.Version
}
info.SetGroupVersionKind(gvk)
break
return nil
}
}
return nil
}
func (s *Storage) encode(obj runtime.Object, w io.Writer) error {
// The standard encoder is fine when only one type maps to a group
if s.opts.Scheme == nil {
return s.codec.Encode(obj, w)
}
if err := s.checkGVK(obj); err != nil {
return err
}
// This will always write the saved GVK, unlike:
// https://github.com/kubernetes/kubernetes/blob/v1.34.3/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/versioning/versioning.go#L267
// that picks an arbitrary GVK that may not match the same group!
return json.NewEncoder(w).Encode(obj)
}
+2 -2
View File
@@ -36,8 +36,8 @@ func TestIntegrationTestDatasource(t *testing.T) {
for _, mode := range []grafanarest.DualWriterMode{
grafanarest.Mode0, // Legacy only
// grafanarest.Mode2, // write both, read legacy
// grafanarest.Mode3, // write both, read unified
grafanarest.Mode2, // write both, read legacy
grafanarest.Mode3, // write both, read unified
grafanarest.Mode5, // Unified only
} {
t.Run(fmt.Sprintf("testdata (mode:%d)", mode), func(t *testing.T) {