1. 环境说明
源码链接:https://github.com/banzaicloud/k8s-objectmatcher
版本/Tag:v1.8.0
适用场景:用于匹配K8S
资源对象
作者写该库的动机:在编写一些诸如Istio, Vault, Kafka
的复制Operator
的时候,遇到了大量的不必要的对象更新。大多数Operator
都是通过reflect.DeepEquals
来匹配给定对象的Spec
。不幸的是,这个解决方案并不完美,因为每个Kubernetes
对象在提交的时候会被修改默认值,作者写这个库的目标是为了能够提供更加精细的对象匹配功能,以避免不必要的更新和客户端的可观测性
工作原理:作者使用three way merge方法计算补丁。然后,为了能够使three way merge
能够正常工作,我们需要跟踪对象的最后应用版本,我们称它为original
。不幸的是,Kubernetes
并不会跟踪我们之前提交的对象版本;万幸的是,我们可以通过模仿kubectl apply
在对象提交的时候注入Annotation
用于记录当前对象的配置信息。下一次查询的时候我们就能够去对象的current
状态,并且从original
中获取对象以前的状态。一旦我们有了original, current, modified
,剩下的时候就可以交给这个库了。
2. 3-Way Merge
TODO
3. Demo
我们以v1.Service
来举例该库的使用
首先,我们创建一个Service
,用Annotation
标注它,然后再提交
original := &v1.Service{
...
}
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(original); err != nil {
...
}
client.CoreV1().Services(original.GetNamespace()).Create(original)
当我们需要更新的时候,需要调用patch.DefaultPatchMaker.Calculate()
方法计算current
与modified
的不同,然后再去更新
modified := &v1.Service{
...
}
current, err := client.CoreV1().Services(modified.GetNamespace()).Get(modified.GetName(), metav1.Getoptions{})
patchResult, err := patch.DefaultPatchMaker.Calculate(current, modified)
if err != nil {
return err
}
if !patchResult.IsEmpty() {
if err := patch.DefaultAnnotator.SetLastAppliedAnnotation(modified); err != nil {
...
}
client.CoreV1().Services(modified.GetNamespace()).Update(modified)
}
4. 源码分析
TODO
var DefaultPatchMaker = NewPatchMaker(DefaultAnnotator, &K8sStrategicMergePatcher{}, &BaseJSONMergePatcher{})
type Maker interface {
Calculate(currentObject, modifiedObject runtime.Object, opts ...CalculateOption) (*PatchResult, error)
}
type PatchMaker struct {
annotator *Annotator
strategicMergePatcher StrategicMergePatcher
jsonMergePatcher JSONMergePatcher
}
func NewPatchMaker(annotator *Annotator, strategicMergePatcher StrategicMergePatcher, jsonMergePatcher JSONMergePatcher) Maker {
return &PatchMaker{
annotator: annotator,
strategicMergePatcher: strategicMergePatcher,
jsonMergePatcher: jsonMergePatcher,
}
}
func (p *PatchMaker) Calculate(currentObject, modifiedObject runtime.Object, opts ...CalculateOption) (*PatchResult, error) {
current, err := json.ConfigCompatibleWithStandardLibrary.Marshal(currentObject)
if err != nil {
return nil, errors.Wrap(err, "Failed to convert current object to byte sequence")
}
modified, err := json.ConfigCompatibleWithStandardLibrary.Marshal(modifiedObject)
if err != nil {
return nil, errors.Wrap(err, "Failed to convert current object to byte sequence")
}
for _, opt := range opts {
current, modified, err = opt(current, modified)
if err != nil {
return nil, errors.Wrap(err, "Failed to apply option function")
}
}
current, _, err = DeleteNullInJson(current)
if err != nil {
return nil, errors.Wrap(err, "Failed to delete null from current object")
}
modified, _, err = DeleteNullInJson(modified)
if err != nil {
return nil, errors.Wrap(err, "Failed to delete null from modified object")
}
original, err := p.annotator.GetOriginalConfiguration(currentObject)
if err != nil {
return nil, errors.Wrap(err, "Failed to get original configuration")
}
var patch []byte
switch currentObject.(type) {
default:
patch, err = p.strategicMergePatcher.CreateThreeWayMergePatch(original, modified, current, currentObject)
if err != nil {
return nil, errors.Wrap(err, "Failed to generate strategic merge patch")
}
// $setElementOrder can make it hard to decide whether there is an actual diff or not.
// In cases like that trying to apply the patch locally on current will make it clear.
if string(patch) != "{}" {
patchCurrent, err := p.strategicMergePatcher.StrategicMergePatch(current, patch, currentObject)
if err != nil {
return nil, errors.Wrap(err, "Failed to apply patch again to check for an actual diff")
}
patch, err = p.strategicMergePatcher.CreateTwoWayMergePatch(current, patchCurrent, currentObject)
if err != nil {
return nil, errors.Wrap(err, "Failed to create patch again to check for an actual diff")
}
}
case *unstructured.Unstructured:
patch, err = p.unstructuredJsonMergePatch(original, modified, current)
if err != nil {
return nil, errors.Wrap(err, "Failed to generate merge patch")
}
}
return &PatchResult{
Patch: patch,
Current: current,
Modified: modified,
Original: original,
}, nil
}