Skip to content

Commit a84c818

Browse files
committed
only look at the spec field
1 parent ae469e5 commit a84c818

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

gen/deprecations/templates/test.tpl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ func TestDeprecatedFieldWarnings(t *testing.T) {
4545
tc := tc
4646
t.Run(tc.name, func(t *testing.T) {
4747
t.Parallel()
48-
warnings, err := deprecations.FindDeprecatedFieldWarnings(tc.obj)
49-
require.NoError(t, err)
50-
require.ElementsMatch(t, tc.wantWarnings, warnings)
48+
require.ElementsMatch(t, tc.wantWarnings, deprecations.FindDeprecatedFieldWarnings(tc.obj))
5149
})
5250
}
5351
}

operator/api/redpanda/v1alpha2/zz_generated.deprecations_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,7 @@ func TestDeprecatedFieldWarnings(t *testing.T) {
620620
tc := tc
621621
t.Run(tc.name, func(t *testing.T) {
622622
t.Parallel()
623-
warnings, err := deprecations.FindDeprecatedFieldWarnings(tc.obj)
624-
require.NoError(t, err)
625-
require.ElementsMatch(t, tc.wantWarnings, warnings)
623+
require.ElementsMatch(t, tc.wantWarnings, deprecations.FindDeprecatedFieldWarnings(tc.obj))
626624
})
627625
}
628626
}

pkg/deprecations/warnings.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,30 @@ const DeprecatedPrefix = "Deprecated"
2323
// deprecation warning messages for any deeply nested struct fields that have a field
2424
// prefixed with "Deprecated" and whose value is not the zero value. The name shown in the
2525
// warning is taken from the field's full json path from the root of the CRD.
26-
func FindDeprecatedFieldWarnings(obj client.Object) ([]string, error) {
26+
func FindDeprecatedFieldWarnings(obj client.Object) []string {
2727
v := reflect.ValueOf(obj)
2828
if v.Kind() == reflect.Pointer {
2929
if v.IsNil() {
30-
return nil, fmt.Errorf("object is a nil pointer")
30+
// nothing to do
31+
return nil
3132
}
3233
v = v.Elem()
3334
}
3435

3536
if v.Kind() != reflect.Struct {
36-
return nil, fmt.Errorf("object must be a struct or pointer to struct")
37+
// if we don't have a struct, then it can't have fields
38+
return nil
3739
}
3840

39-
return deprecatedFields(v, v.Type(), "", make(map[uintptr]struct{})), nil
41+
// Only inspect the Spec field and its children
42+
spec, ok := v.Type().FieldByName("Spec")
43+
if !ok {
44+
// we only warn on user-supplied input, which comes from
45+
// Spec
46+
return nil
47+
}
48+
49+
return deprecatedFields(v.FieldByName("Spec"), spec.Type, "spec", make(map[uintptr]struct{}))
4050
}
4151

4252
func deprecatedFields(value reflect.Value, reflectType reflect.Type, path string, visited map[uintptr]struct{}) []string {

0 commit comments

Comments
 (0)