Skip to content

Commit

Permalink
Fix server-side diff when immutable properties change
Browse files Browse the repository at this point in the history
  • Loading branch information
lblackstone committed Feb 12, 2020
1 parent 27deebc commit 4594737
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
10 changes: 5 additions & 5 deletions pkg/openapi/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"

jsonpatch "github.com/evanphx/json-patch"
"k8s.io/client-go/util/jsonpath"
Expand All @@ -27,20 +28,19 @@ func PatchPropertiesChanged(patch map[string]interface{}, paths []string) ([]str
j := jsonpath.New("")
matches := []string{}
for _, path := range paths {
j.AllowMissingKeys(true)
j.AllowMissingKeys(false) // Explicitly handle any returned errors.
err := j.Parse(fmt.Sprintf("{%s}", path))
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
err = j.Execute(buf, patch)
if err != nil {
if err != nil && strings.Contains(err.Error(), "not found") {
continue
}

if len(buf.String()) > 0 {
matches = append(matches, path)
}
// If no error was returned, then this is a match.
matches = append(matches, path)
}

return matches, nil
Expand Down
26 changes: 17 additions & 9 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -1165,15 +1166,22 @@ func (k *kubeProvider) Diff(
patch, patchBase, err = k.dryRunPatch(oldInputs, newInputs)

// Fall back to input patch.
se, isStatusError := err.(*errors.StatusError)
if isStatusError && se.Status().Code == 400 &&
(se.Status().Message == "the dryRun alpha feature is disabled" ||
se.Status().Message == "the dryRun beta feature is disabled" ||
strings.Contains(se.Status().Message, "does not support dry run")) {

isInputPatch = true
patch, err = k.inputPatch(oldInputs, newInputs)
patchBase = oldInputs
if se, isStatusError := err.(*errors.StatusError); isStatusError {
if se.Status().Code == http.StatusBadRequest &&
(se.Status().Message == "the dryRun alpha feature is disabled" ||
se.Status().Message == "the dryRun beta feature is disabled" ||
strings.Contains(se.Status().Message, "does not support dry run")) {

isInputPatch = true
patch, err = k.inputPatch(oldInputs, newInputs)
patchBase = oldInputs
} else if se.Status().Code == http.StatusUnprocessableEntity ||
strings.Contains(se.ErrStatus.Message, "field is immutable") {
// Immutable field requires replacement.
isInputPatch = true
patch, err = k.inputPatch(oldInputs, newInputs)
patchBase = oldInputs
}
}
} else {
isInputPatch = true
Expand Down

0 comments on commit 4594737

Please sign in to comment.