其他RESTMapper

之前的两次关于RESTMapper的分析都是和DefaultRESTMapper相关的。除了DefaultRESTMapper以外,Kubernetes还实现了PriorityRESTMapper,MuiltiRESTMaper和FirstHitRESTMapper。本次分析就来看下这些RESTMapper的实现。

PriorityRESTMapper

PriorityRESTMapper定义在/pkg/api/meta/priority.go中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// PriorityRESTMapper is a wrapper for automatically choosing a particular Resource or Kind
// when multiple matches are possible
type PriorityRESTMapper struct {
// Delegate is the RESTMapper to use to locate all the Kind and Resource matches
Delegate RESTMapper
// ResourcePriority is a list of priority patterns to apply to matching resources.
// The list of all matching resources is narrowed based on the patterns until only one remains.
// A pattern with no matches is skipped. A pattern with more than one match uses its
// matches as the list to continue matching against.
ResourcePriority []unversioned.GroupVersionResource
// KindPriority is a list of priority patterns to apply to matching kinds.
// The list of all matching kinds is narrowed based on the patterns until only one remains.
// A pattern with no matches is skipped. A pattern with more than one match uses its
// matches as the list to continue matching against.
KindPriority []unversioned.GroupVersionKind
}

PriorityRESTMapper封装了一个RESTMapper,及ResourcePriority和KindPriority。与DefaultRESTMapper多值处理方式(出错处理)不同的是,PriorityRESTMapper会依据ResourcePriority和KindPriority对多值进行过滤,直到只有一个结果为止。
具体PriorityRESTMapper方法这里就不再详细分析。

MultiRESTMapper

MultiRESTMapper定义在/pkg/api/meta/multirestmapper.go中:

1
type MultiRESTMapper []RESTMapper

MultiRESTMapper中封装了多个RESTMapper,对于KindsFor(),ResourcesFor(),RESTMappings(),MultiRESTMapper会收集所有RESTMapper的结果;对于KindFor(),ResourceFor(),RESTMapping()的多值处理方式和DefaultRESTMapper相同,直接出错处理。
具体MultiRESTMapper方法这里就不再详细分析。

FirstHitRESTMapper

FirstHitRESTMapper定义在/pkg/api/meta/firsthit_restmapper.go中:

1
2
3
type FirstHitRESTMapper struct {
MultiRESTMapper
}

FirstHitRESTMapper本质也封装了多个RESTMapper。与其他RESTMapper不同的是,FirstHitRESTMapper并没有实现KindsFor(),ResourcesFor(),RESTMappings()。从”FirstHitRESTMapper”这名字来看,也可以理解FirstHitRESTMapper的处理方式:当有一个RESTMapper成功,则直接返回。如KindFor()方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
func (m FirstHitRESTMapper) KindFor(resource unversioned.GroupVersionResource) (unversioned.GroupVersionKind, error) {
errors := []error{}
//***如果有一个RESTMapper符合,则立即返回***//
for _, t := range m.MultiRESTMapper {
ret, err := t.KindFor(resource)
if err == nil {
return ret, nil
}
errors = append(errors, err)
}
return unversioned.GroupVersionKind{}, collapseAggregateErrors(errors)
}

具体FirstHitRESTMapper其他方法这里就不再详细分析。

至此,RESTMapper分析完毕。