Strategy也就是策略模式
k8s中策略模式的实现
k8s中为不同的资源对象实现了不同的策略。
1 | // vendor/k8s.io/apiserver/pkg/registry/generic/registry/store.go |
Deployment实现的策略
1 | //pkg/registry/apps/deployment/strategy.go |
ReplicaSet实现的策略
1 | // pkg/registry/apps/replicaset/strategy.go |
Store如何与对应的Strategy关联
1 | // 创建对应store时指定了Deployment对应的strategy |
何时调用Strategy
例如CreateStrategy,在创建Deployment
的时候会执行1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21// staging/src/k8s.io/apiserver/pkg/registry/generic/registry/store.go
func (e *Store) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
var finishCreate FinishFunc = finishNothing
// Init metadata as early as possible.
if objectMeta, err := meta.Accessor(obj); err != nil {
return nil, err
} else {
rest.FillObjectMetaSystemFields(objectMeta)
if len(objectMeta.GetGenerateName()) > 0 && len(objectMeta.GetName()) == 0 {
objectMeta.SetName(e.CreateStrategy.GenerateName(objectMeta.GetGenerateName()))
}
}
...
if err := rest.BeforeCreate(e.CreateStrategy, ctx, obj); err != nil {
return nil, err
}
...
return out, nil
}
REF:
1.pkg/registry/generic/registry/store.go
2.pkg/registry/apps/replicaset/strategy.go
3.pkg/registry/apps/replicaset/storage/storage.go
4.pkg/registry/apps/deployment/strategy.go
5.pkg/registry/apps/deployment/storage/storage.go