mirror of https://github.com/k3s-io/k3s
change all jsonmerge to strategicpatch
Signed-off-by: PingWang <wang.ping5@zte.com.cn> update preconditions define Signed-off-by: PingWang <wang.ping5@zte.com.cn>pull/6/head
parent
bf4e9e9db8
commit
121f6fb289
|
@ -35,7 +35,6 @@ import (
|
|||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/editor"
|
||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/jsonmerge"
|
||||
"k8s.io/kubernetes/pkg/kubectl/resource"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/util/crlf"
|
||||
|
@ -392,25 +391,18 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
|
|||
return nil
|
||||
}
|
||||
|
||||
patch, err := strategicpatch.CreateStrategicMergePatch(originalJS, editedJS, currOriginalObj)
|
||||
// TODO: change all jsonmerge to strategicpatch
|
||||
// for checking preconditions
|
||||
preconditions := []jsonmerge.PreconditionFunc{}
|
||||
preconditions := []strategicpatch.PreconditionFunc{strategicpatch.RequireKeyUnchanged("apiVersion"),
|
||||
strategicpatch.RequireKeyUnchanged("kind"), strategicpatch.RequireMetadataKeyUnchanged("name")}
|
||||
patch, err := strategicpatch.CreateTwoWayMergePatch(originalJS, editedJS, currOriginalObj, preconditions...)
|
||||
if err != nil {
|
||||
glog.V(4).Infof("Unable to calculate diff, no merge is possible: %v", err)
|
||||
return err
|
||||
} else {
|
||||
preconditions = append(preconditions, jsonmerge.RequireKeyUnchanged("apiVersion"))
|
||||
preconditions = append(preconditions, jsonmerge.RequireKeyUnchanged("kind"))
|
||||
preconditions = append(preconditions, jsonmerge.RequireMetadataKeyUnchanged("name"))
|
||||
results.version = defaultVersion
|
||||
}
|
||||
|
||||
if hold, msg := jsonmerge.TestPreconditionsHold(patch, preconditions); !hold {
|
||||
fmt.Fprintf(errOut, "error: %s", msg)
|
||||
if strategicpatch.IsPreconditionFailed(err) {
|
||||
return preservedFile(nil, file, errOut)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
results.version = defaultVersion
|
||||
patched, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, api.StrategicMergePatchType, patch)
|
||||
if err != nil {
|
||||
fmt.Fprintln(out, results.addError(err, info))
|
||||
|
|
|
@ -67,7 +67,7 @@ func RequireKeyUnchanged(key string) PreconditionFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// RequireKeyUnchanged creates a precondition function that fails
|
||||
// RequireMetadataKeyUnchanged creates a precondition function that fails
|
||||
// if the metadata.key is present in the diff (indicating its value
|
||||
// has changed).
|
||||
func RequireMetadataKeyUnchanged(key string) PreconditionFunc {
|
||||
|
|
|
@ -110,6 +110,28 @@ func RequireKeyUnchanged(key string) PreconditionFunc {
|
|||
}
|
||||
}
|
||||
|
||||
// RequireMetadataKeyUnchanged creates a precondition function that fails
|
||||
// if the metadata.key is present in the patch (indicating its value
|
||||
// has changed).
|
||||
func RequireMetadataKeyUnchanged(key string) PreconditionFunc {
|
||||
return func(patch interface{}) bool {
|
||||
patchMap, ok := patch.(map[string]interface{})
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
patchMap1, ok := patchMap["metadata"]
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
patchMap2, ok := patchMap1.(map[string]interface{})
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
_, ok = patchMap2[key]
|
||||
return !ok
|
||||
}
|
||||
}
|
||||
|
||||
// Deprecated: Use the synonym CreateTwoWayMergePatch, instead.
|
||||
func CreateStrategicMergePatch(original, modified []byte, dataStruct interface{}) ([]byte, error) {
|
||||
return CreateTwoWayMergePatch(original, modified, dataStruct)
|
||||
|
|
Loading…
Reference in New Issue