k8s源码设计模式之Template Method

模板方法是一种行为设计模式,用于定义一个操作的算法骨架,而将一些步骤的实现推迟到子类中。它可以使子类在不改变算法结构的情况下重定义算法中的某些步骤。

k8s源码中client-go中模版方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// staging/src/k8s.io/client-go/rest/client.go
type Interface interface {
GetRateLimiter() flowcontrol.RateLimiter
Verb(verb string) *Request
Post() *Request
Put() *Request
Patch(pt types.PatchType) *Request
Get() *Request
Delete() *Request
APIVersion() schema.GroupVersion
}

// RESTClient实现了Interface
type RESTClient struct {
// base is the root URL for all invocations of the client
base *url.URL
// versionedAPIPath is a path segment connecting the base URL to the resource root
versionedAPIPath string

// content describes how a RESTClient encodes and decodes responses.
content ClientContentConfig

// creates BackoffManager that is passed to requests.
createBackoffMgr func() BackoffManager

// rateLimiter is shared among all requests created by this client unless specifically
// overridden.
rateLimiter flowcontrol.RateLimiter

// warningHandler is shared among all requests created by this client.
// If not set, defaultWarningHandler is used.
warningHandler WarningHandler

// Set specific behavior of the client. If not set http.DefaultClient will be used.
Client *http.Client
}
1
2
3
4
5
6
7
8
9
10
11
// staging/src/k8s.io/client-go/dynamic/simple.go
type DynamicClient struct {
client rest.Interface
}

// dynamicResourceClient对需要重写的方法进行了重写
type dynamicResourceClient struct {
client *DynamicClient
namespace string
resource schema.GroupVersionResource
}

REF:
1.staging/src/k8s.io/client-go/rest/client.go
2.staging/src/k8s.io/client-go/dynamic/simple.go