mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-21 18:32:50 +00:00
reflector: refactor setting typeDescription & expectedGVK
Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com> Kubernetes-commit: 784ec157e67c86bc3383b326bbfe8ee70737aa4d
This commit is contained in:
parent
37897aff8d
commit
fbb7f087d1
35
tools/cache/reflector.go
vendored
35
tools/cache/reflector.go
vendored
@ -217,48 +217,49 @@ func NewReflectorWithOptions(lw ListerWatcher, expectedType interface{}, store S
|
|||||||
r.name = naming.GetNameFromCallsite(internalPackages...)
|
r.name = naming.GetNameFromCallsite(internalPackages...)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.setTypeDescription(expectedType)
|
if r.typeDescription == "" {
|
||||||
r.setExpectedGVK(expectedType)
|
r.typeDescription = getTypeDescriptionFromObject(expectedType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.expectedGVK == nil {
|
||||||
|
r.expectedGVK = getExpectedGVKFromObject(expectedType)
|
||||||
|
}
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Reflector) setTypeDescription(expectedType interface{}) {
|
func getTypeDescriptionFromObject(expectedType interface{}) string {
|
||||||
if r.typeDescription != "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if expectedType == nil {
|
if expectedType == nil {
|
||||||
r.typeDescription = defaultExpectedTypeName
|
return defaultExpectedTypeName
|
||||||
} else {
|
|
||||||
r.typeDescription = reflect.TypeOf(expectedType).String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reflectDescription := reflect.TypeOf(expectedType).String()
|
||||||
|
|
||||||
obj, ok := expectedType.(*unstructured.Unstructured)
|
obj, ok := expectedType.(*unstructured.Unstructured)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return reflectDescription
|
||||||
}
|
}
|
||||||
|
|
||||||
gvk := obj.GroupVersionKind()
|
gvk := obj.GroupVersionKind()
|
||||||
if gvk.Empty() {
|
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)
|
obj, ok := expectedType.(*unstructured.Unstructured)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
gvk := obj.GroupVersionKind()
|
gvk := obj.GroupVersionKind()
|
||||||
if gvk.Empty() {
|
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
|
// internalPackages are packages that ignored when creating a default reflector name. These packages are in the common
|
||||||
|
41
tools/cache/reflector_test.go
vendored
41
tools/cache/reflector_test.go
vendored
@ -979,7 +979,7 @@ func TestReflectorFullListIfTooLarge(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReflectorSetTypeDescription(t *testing.T) {
|
func TestGetTypeDescriptionFromObject(t *testing.T) {
|
||||||
obj := &unstructured.Unstructured{}
|
obj := &unstructured.Unstructured{}
|
||||||
gvk := schema.GroupVersionKind{
|
gvk := schema.GroupVersionKind{
|
||||||
Group: "mygroup",
|
Group: "mygroup",
|
||||||
@ -990,7 +990,6 @@ func TestReflectorSetTypeDescription(t *testing.T) {
|
|||||||
|
|
||||||
testCases := map[string]struct {
|
testCases := map[string]struct {
|
||||||
inputType interface{}
|
inputType interface{}
|
||||||
customDescription string
|
|
||||||
expectedTypeDescription string
|
expectedTypeDescription string
|
||||||
}{
|
}{
|
||||||
"Nil type": {
|
"Nil type": {
|
||||||
@ -1000,45 +999,26 @@ func TestReflectorSetTypeDescription(t *testing.T) {
|
|||||||
inputType: &v1.Pod{},
|
inputType: &v1.Pod{},
|
||||||
expectedTypeDescription: "*v1.Pod",
|
expectedTypeDescription: "*v1.Pod",
|
||||||
},
|
},
|
||||||
"Normal type with custom description": {
|
|
||||||
inputType: &v1.Pod{},
|
|
||||||
customDescription: "foo",
|
|
||||||
expectedTypeDescription: "foo",
|
|
||||||
},
|
|
||||||
"Unstructured type without GVK": {
|
"Unstructured type without GVK": {
|
||||||
inputType: &unstructured.Unstructured{},
|
inputType: &unstructured.Unstructured{},
|
||||||
expectedTypeDescription: "*unstructured.Unstructured",
|
expectedTypeDescription: "*unstructured.Unstructured",
|
||||||
},
|
},
|
||||||
"Unstructured type without GVK, with custom description": {
|
|
||||||
inputType: &unstructured.Unstructured{},
|
|
||||||
customDescription: "foo",
|
|
||||||
expectedTypeDescription: "foo",
|
|
||||||
},
|
|
||||||
"Unstructured type with GVK": {
|
"Unstructured type with GVK": {
|
||||||
inputType: obj,
|
inputType: obj,
|
||||||
expectedTypeDescription: gvk.String(),
|
expectedTypeDescription: gvk.String(),
|
||||||
},
|
},
|
||||||
"Unstructured type with GVK, with custom type description": {
|
|
||||||
inputType: obj,
|
|
||||||
customDescription: "foo",
|
|
||||||
expectedTypeDescription: "foo",
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
for testName, tc := range testCases {
|
for testName, tc := range testCases {
|
||||||
t.Run(testName, func(t *testing.T) {
|
t.Run(testName, func(t *testing.T) {
|
||||||
r := &Reflector{
|
typeDescription := getTypeDescriptionFromObject(tc.inputType)
|
||||||
typeDescription: tc.customDescription,
|
if tc.expectedTypeDescription != typeDescription {
|
||||||
}
|
t.Fatalf("Expected typeDescription %v, got %v", tc.expectedTypeDescription, typeDescription)
|
||||||
|
|
||||||
r.setTypeDescription(tc.inputType)
|
|
||||||
if tc.expectedTypeDescription != r.typeDescription {
|
|
||||||
t.Fatalf("Expected typeDescription %v, got %v", tc.expectedTypeDescription, r.typeDescription)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReflectorSetExpectedGVK(t *testing.T) {
|
func TestGetExpectedGVKFromObject(t *testing.T) {
|
||||||
obj := &unstructured.Unstructured{}
|
obj := &unstructured.Unstructured{}
|
||||||
gvk := schema.GroupVersionKind{
|
gvk := schema.GroupVersionKind{
|
||||||
Group: "mygroup",
|
Group: "mygroup",
|
||||||
@ -1065,14 +1045,13 @@ func TestReflectorSetExpectedGVK(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for testName, tc := range testCases {
|
for testName, tc := range testCases {
|
||||||
t.Run(testName, func(t *testing.T) {
|
t.Run(testName, func(t *testing.T) {
|
||||||
r := &Reflector{}
|
expectedGVK := getExpectedGVKFromObject(tc.inputType)
|
||||||
r.setExpectedGVK(tc.inputType)
|
gvkNotEqual := (tc.expectedGVK == nil) != (expectedGVK == nil)
|
||||||
gvkNotEqual := (tc.expectedGVK == nil) != (r.expectedGVK == nil)
|
if tc.expectedGVK != nil && expectedGVK != nil {
|
||||||
if tc.expectedGVK != nil && r.expectedGVK != nil {
|
gvkNotEqual = *tc.expectedGVK != *expectedGVK
|
||||||
gvkNotEqual = *tc.expectedGVK != *r.expectedGVK
|
|
||||||
}
|
}
|
||||||
if gvkNotEqual {
|
if gvkNotEqual {
|
||||||
t.Fatalf("Expected expectedGVK %v, got %v", tc.expectedGVK, r.expectedGVK)
|
t.Fatalf("Expected expectedGVK %v, got %v", tc.expectedGVK, expectedGVK)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user