reflector: refactor setting typeDescription & expectedGVK

Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>

Kubernetes-commit: 784ec157e67c86bc3383b326bbfe8ee70737aa4d
This commit is contained in:
Andy Goldstein 2022-12-02 12:39:58 -05:00 committed by Kubernetes Publisher
parent 37897aff8d
commit fbb7f087d1
2 changed files with 28 additions and 48 deletions

View File

@ -217,48 +217,49 @@ func NewReflectorWithOptions(lw ListerWatcher, expectedType interface{}, store S
r.name = naming.GetNameFromCallsite(internalPackages...)
}
r.setTypeDescription(expectedType)
r.setExpectedGVK(expectedType)
if r.typeDescription == "" {
r.typeDescription = getTypeDescriptionFromObject(expectedType)
}
if r.expectedGVK == nil {
r.expectedGVK = getExpectedGVKFromObject(expectedType)
}
return r
}
func (r *Reflector) setTypeDescription(expectedType interface{}) {
if r.typeDescription != "" {
return
func getTypeDescriptionFromObject(expectedType interface{}) string {
if expectedType == nil {
return defaultExpectedTypeName
}
if expectedType == nil {
r.typeDescription = defaultExpectedTypeName
} else {
r.typeDescription = reflect.TypeOf(expectedType).String()
}
reflectDescription := reflect.TypeOf(expectedType).String()
obj, ok := expectedType.(*unstructured.Unstructured)
if !ok {
return
return reflectDescription
}
gvk := obj.GroupVersionKind()
if gvk.Empty() {
return
return reflectDescription
}
r.typeDescription = gvk.String()
return gvk.String()
}
func (r *Reflector) setExpectedGVK(expectedType interface{}) {
func getExpectedGVKFromObject(expectedType interface{}) *schema.GroupVersionKind {
obj, ok := expectedType.(*unstructured.Unstructured)
if !ok {
return
return nil
}
gvk := obj.GroupVersionKind()
if gvk.Empty() {
return
return nil
}
r.expectedGVK = &gvk
return &gvk
}
// internalPackages are packages that ignored when creating a default reflector name. These packages are in the common

View File

@ -979,7 +979,7 @@ func TestReflectorFullListIfTooLarge(t *testing.T) {
}
}
func TestReflectorSetTypeDescription(t *testing.T) {
func TestGetTypeDescriptionFromObject(t *testing.T) {
obj := &unstructured.Unstructured{}
gvk := schema.GroupVersionKind{
Group: "mygroup",
@ -990,7 +990,6 @@ func TestReflectorSetTypeDescription(t *testing.T) {
testCases := map[string]struct {
inputType interface{}
customDescription string
expectedTypeDescription string
}{
"Nil type": {
@ -1000,45 +999,26 @@ func TestReflectorSetTypeDescription(t *testing.T) {
inputType: &v1.Pod{},
expectedTypeDescription: "*v1.Pod",
},
"Normal type with custom description": {
inputType: &v1.Pod{},
customDescription: "foo",
expectedTypeDescription: "foo",
},
"Unstructured type without GVK": {
inputType: &unstructured.Unstructured{},
expectedTypeDescription: "*unstructured.Unstructured",
},
"Unstructured type without GVK, with custom description": {
inputType: &unstructured.Unstructured{},
customDescription: "foo",
expectedTypeDescription: "foo",
},
"Unstructured type with GVK": {
inputType: obj,
expectedTypeDescription: gvk.String(),
},
"Unstructured type with GVK, with custom type description": {
inputType: obj,
customDescription: "foo",
expectedTypeDescription: "foo",
},
}
for testName, tc := range testCases {
t.Run(testName, func(t *testing.T) {
r := &Reflector{
typeDescription: tc.customDescription,
}
r.setTypeDescription(tc.inputType)
if tc.expectedTypeDescription != r.typeDescription {
t.Fatalf("Expected typeDescription %v, got %v", tc.expectedTypeDescription, r.typeDescription)
typeDescription := getTypeDescriptionFromObject(tc.inputType)
if tc.expectedTypeDescription != typeDescription {
t.Fatalf("Expected typeDescription %v, got %v", tc.expectedTypeDescription, typeDescription)
}
})
}
}
func TestReflectorSetExpectedGVK(t *testing.T) {
func TestGetExpectedGVKFromObject(t *testing.T) {
obj := &unstructured.Unstructured{}
gvk := schema.GroupVersionKind{
Group: "mygroup",
@ -1065,14 +1045,13 @@ func TestReflectorSetExpectedGVK(t *testing.T) {
}
for testName, tc := range testCases {
t.Run(testName, func(t *testing.T) {
r := &Reflector{}
r.setExpectedGVK(tc.inputType)
gvkNotEqual := (tc.expectedGVK == nil) != (r.expectedGVK == nil)
if tc.expectedGVK != nil && r.expectedGVK != nil {
gvkNotEqual = *tc.expectedGVK != *r.expectedGVK
expectedGVK := getExpectedGVKFromObject(tc.inputType)
gvkNotEqual := (tc.expectedGVK == nil) != (expectedGVK == nil)
if tc.expectedGVK != nil && expectedGVK != nil {
gvkNotEqual = *tc.expectedGVK != *expectedGVK
}
if gvkNotEqual {
t.Fatalf("Expected expectedGVK %v, got %v", tc.expectedGVK, r.expectedGVK)
t.Fatalf("Expected expectedGVK %v, got %v", tc.expectedGVK, expectedGVK)
}
})
}