refactor: read managed fields through FieldsV1 accessors

Direct access to FieldsV1.Raw is deprecated; GetRawBytes returns the
same bytes and is nil-safe, and NewFieldsV1 builds the test fixtures.

The write-options test still covers a raw-only orphanDependents delete
option, now decoded from its wire form, and asserts that the converted
options keep it.

Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-13 12:35:03 +03:00
1 parent 50959ccca2
commit 939a1ec44b
3 files changed
+18 -7

No files matched your search

@@ -486,11 +486,12 @@ func ownsSuspensionManagedFields(hr *helmv2.HelmRelease) bool {
// fieldsManageSuspend reports whether a managed-fields set includes spec.suspend,
// encoded by the apiserver as {"f:spec":{"f:suspend":{}}}.
func fieldsManageSuspend(fs *metav1.FieldsV1) bool {
if fs == nil || len(fs.Raw) == 0 {
raw := fs.GetRawBytes()
if len(raw) == 0 {
return false
}
var top map[string]json.RawMessage
if err := json.Unmarshal(fs.Raw, &top); err != nil {
if err := json.Unmarshal(raw, &top); err != nil {
return false
}
specRaw, ok := top["f:spec"]
@@ -83,7 +83,7 @@ func suspendManagedFields(manager string, op metav1.ManagedFieldsOperationType)
Operation: op,
APIVersion: helmv2.GroupVersion.String(),
FieldsType: "FieldsV1",
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:suspend":{}}}`)},
FieldsV1: metav1.NewFieldsV1(`{"f:spec":{"f:suspend":{}}}`),
}}
}
@@ -286,7 +286,7 @@ func TestOwnsSuspensionManagedFields(t *testing.T) {
{"no managed fields", nil, false},
{"plunger apply of other fields", []metav1.ManagedFieldsEntry{{
Manager: suspendFieldManager, Operation: metav1.ManagedFieldsOperationApply,
FieldsType: "FieldsV1", FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:chart":{}}}`)},
FieldsType: "FieldsV1", FieldsV1: metav1.NewFieldsV1(`{"f:spec":{"f:chart":{}}}`),
}}, false},
}
for _, tc := range cases {
+13 -3
View File
@@ -17,7 +17,9 @@ limitations under the License.
package registry
import (
"encoding/json"
"reflect"
"strings"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -26,7 +28,6 @@ import (
func TestClientWriteOptionsPreserveAPISemantics(t *testing.T) {
grace := int64(7)
orphan := true
propagation := metav1.DeletePropagationForeground
uid := types.UID("expected-uid")
rv := "42"
@@ -74,11 +75,20 @@ func TestClientWriteOptionsPreserveAPISemantics(t *testing.T) {
t.Fatalf("conversion mutated API options: create=%#v update=%#v delete=%#v", create, update, deleteOptions)
}
orphanDelete := &metav1.DeleteOptions{OrphanDependents: &orphan, DryRun: []string{metav1.DryRunAll}}
// orphanDependents is deprecated in Go but still accepted on the wire,
// which is how a raw-only option reaches the registry.
orphanDelete := &metav1.DeleteOptions{}
if err := json.Unmarshal([]byte(`{"orphanDependents":true,"dryRun":["All"]}`), orphanDelete); err != nil {
t.Fatal(err)
}
orphanExpected := orphanDelete.DeepCopy()
if got := ClientDeleteOptions(orphanDelete).AsDeleteOptions(); !reflect.DeepEqual(got, orphanExpected) {
got := ClientDeleteOptions(orphanDelete).AsDeleteOptions()
if !reflect.DeepEqual(got, orphanExpected) {
t.Fatalf("raw-only delete option lost: got %#v, want %#v", got, orphanExpected)
}
if wire, err := json.Marshal(got); err != nil || !strings.Contains(string(wire), `"orphanDependents":true`) {
t.Fatalf("converted delete options = %s (%v), want orphanDependents kept", wire, err)
}
if !reflect.DeepEqual(orphanDelete, orphanExpected) {
t.Fatalf("raw-only delete conversion mutated input: got %#v, want %#v", orphanDelete, orphanExpected)
}