Merge pull request #54957 from apelisse/update-kube-openapi

Automatic merge from submit-queue (batch tested with PRs 55004, 54957). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Update kube-openapi to use validation

**What this PR does / why we need it**: Moves openapi validation code to kube-openapi, so that we can move the rest of the code to apimachinery repository, so that later we can use it from both the client and the server.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #Nothing

**Special notes for your reviewer**:

**Release note**:
```release-note
NONE
```

Kubernetes-commit: 55e216f56eac0082acc6be655d9ae09cf9ba38a8
This commit is contained in:
Kubernetes Publisher
2017-11-02 11:17:33 -07:00
49 changed files with 2527 additions and 1712 deletions
+27 -3
View File
@@ -22,6 +22,7 @@ import (
"sort"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/mergepatch"
forkedjson "k8s.io/apimachinery/third_party/forked/golang/json"
@@ -515,6 +516,9 @@ func normalizeSliceOrder(toSort, order []interface{}, mergeKey string, kind refl
return nil, err
}
toSort, toDelete, err = extractToDeleteItems(toSort)
if err != nil {
return nil, err
}
}
sort.SliceStable(toSort, func(i, j int) bool {
@@ -554,7 +558,13 @@ func diffLists(original, modified []interface{}, t reflect.Type, mergeKey string
switch kind {
case reflect.Map:
patchList, deleteList, err = diffListsOfMaps(original, modified, t, mergeKey, diffOptions)
if err != nil {
return nil, nil, nil, err
}
patchList, err = normalizeSliceOrder(patchList, modified, mergeKey, kind)
if err != nil {
return nil, nil, nil, err
}
orderSame, err := isOrderSame(original, modified, mergeKey)
if err != nil {
return nil, nil, nil, err
@@ -580,6 +590,9 @@ func diffLists(original, modified []interface{}, t reflect.Type, mergeKey string
return nil, nil, nil, mergepatch.ErrNoListOfLists
default:
patchList, deleteList, err = diffListsOfScalars(original, modified, diffOptions)
if err != nil {
return nil, nil, nil, err
}
patchList, err = normalizeSliceOrder(patchList, modified, mergeKey, kind)
// generate the setElementOrder list when there are content changes or order changes
if diffOptions.SetElementOrder && ((!diffOptions.IgnoreDeletions && len(deleteList) > 0) ||
@@ -816,7 +829,7 @@ func handleUnmarshal(j []byte) (map[string]interface{}, error) {
return m, nil
}
// StrategicMergePatch applies a strategic merge patch. The original and patch documents
// StrategicMergeMapPatch applies a strategic merge patch. The original and patch documents
// must be JSONMap. A patch can be created from an original and modified document by
// calling CreateTwoWayMergeMapPatch.
// Warning: the original and patch JSONMap objects are mutated by this function and should not be reused.
@@ -825,6 +838,17 @@ func StrategicMergeMapPatch(original, patch JSONMap, dataStruct interface{}) (JS
if err != nil {
return nil, err
}
// We need the go struct tags `patchMergeKey` and `patchStrategy` for fields that support a strategic merge patch.
// For native resources, we can easily figure out these tags since we know the fields.
// Because custom resources are decoded as Unstructured and because we're missing the metadata about how to handle
// each field in a strategic merge patch, we can't find the go struct tags. Hence, we can't easily do a strategic merge
// for custom resources. So we should fail fast and return an error.
if _, ok := dataStruct.(*unstructured.Unstructured); ok {
return nil, mergepatch.ErrUnsupportedStrategicMergePatchFormat
}
mergeOptions := MergeOptions{
MergeParallelList: true,
IgnoreUnmatchedNulls: true,
@@ -1520,7 +1544,7 @@ func findMapInSliceBasedOnKeyValue(m []interface{}, key string, value interface{
for k, v := range m {
typedV, ok := v.(map[string]interface{})
if !ok {
return nil, 0, false, fmt.Errorf("value for key %v is not a map.", k)
return nil, 0, false, fmt.Errorf("value for key %v is not a map", k)
}
valueToMatch, ok := typedV[key]
@@ -1540,7 +1564,7 @@ func sortMergeListsByName(mapJSON []byte, dataStruct interface{}) ([]byte, error
var m map[string]interface{}
err := json.Unmarshal(mapJSON, &m)
if err != nil {
return nil, err
return nil, mergepatch.ErrBadJSONDoc
}
newM, err := sortMergeListsByNameMap(m, reflect.TypeOf(dataStruct))