-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12945 from hashicorp/f-k8s-config-map-patch
kubernetes: Ignore internal K8S annotations in config_map + use PATCH
- Loading branch information
Showing
5 changed files
with
343 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func diffStringMap(pathPrefix string, oldV, newV map[string]interface{}) PatchOperations { | ||
ops := make([]PatchOperation, 0, 0) | ||
|
||
pathPrefix = strings.TrimRight(pathPrefix, "/") | ||
|
||
// This is suboptimal for adding whole new map from scratch | ||
// or deleting the whole map, but it's actually intention. | ||
// There may be some other map items managed outside of TF | ||
// and we don't want to touch these. | ||
|
||
for k, _ := range oldV { | ||
if _, ok := newV[k]; ok { | ||
continue | ||
} | ||
ops = append(ops, &RemoveOperation{Path: pathPrefix + "/" + k}) | ||
} | ||
|
||
for k, v := range newV { | ||
newValue := v.(string) | ||
|
||
if oldValue, ok := oldV[k].(string); ok { | ||
if oldValue == newValue { | ||
continue | ||
} | ||
|
||
ops = append(ops, &ReplaceOperation{ | ||
Path: pathPrefix + "/" + k, | ||
Value: newValue, | ||
}) | ||
continue | ||
} | ||
|
||
ops = append(ops, &AddOperation{ | ||
Path: pathPrefix + "/" + k, | ||
Value: newValue, | ||
}) | ||
} | ||
|
||
return ops | ||
} | ||
|
||
type PatchOperations []PatchOperation | ||
|
||
func (po PatchOperations) MarshalJSON() ([]byte, error) { | ||
var v []PatchOperation = po | ||
return json.Marshal(v) | ||
} | ||
|
||
func (po PatchOperations) Equal(ops []PatchOperation) bool { | ||
var v []PatchOperation = po | ||
|
||
sort.Slice(v, sortByPathAsc(ops)) | ||
sort.Slice(ops, sortByPathAsc(ops)) | ||
|
||
return reflect.DeepEqual(v, ops) | ||
} | ||
|
||
func sortByPathAsc(ops []PatchOperation) func(i, j int) bool { | ||
return func(i, j int) bool { | ||
return ops[i].GetPath() < ops[j].GetPath() | ||
} | ||
} | ||
|
||
type PatchOperation interface { | ||
MarshalJSON() ([]byte, error) | ||
GetPath() string | ||
} | ||
|
||
type ReplaceOperation struct { | ||
Path string `json:"path"` | ||
Value interface{} `json:"value"` | ||
Op string `json:"op"` | ||
} | ||
|
||
func (o *ReplaceOperation) GetPath() string { | ||
return o.Path | ||
} | ||
|
||
func (o *ReplaceOperation) MarshalJSON() ([]byte, error) { | ||
o.Op = "replace" | ||
return json.Marshal(*o) | ||
} | ||
|
||
func (o *ReplaceOperation) String() string { | ||
b, _ := o.MarshalJSON() | ||
return string(b) | ||
} | ||
|
||
type AddOperation struct { | ||
Path string `json:"path"` | ||
Value interface{} `json:"value"` | ||
Op string `json:"op"` | ||
} | ||
|
||
func (o *AddOperation) GetPath() string { | ||
return o.Path | ||
} | ||
|
||
func (o *AddOperation) MarshalJSON() ([]byte, error) { | ||
o.Op = "add" | ||
return json.Marshal(*o) | ||
} | ||
|
||
func (o *AddOperation) String() string { | ||
b, _ := o.MarshalJSON() | ||
return string(b) | ||
} | ||
|
||
type RemoveOperation struct { | ||
Path string `json:"path"` | ||
Op string `json:"op"` | ||
} | ||
|
||
func (o *RemoveOperation) GetPath() string { | ||
return o.Path | ||
} | ||
|
||
func (o *RemoveOperation) MarshalJSON() ([]byte, error) { | ||
o.Op = "remove" | ||
return json.Marshal(*o) | ||
} | ||
|
||
func (o *RemoveOperation) String() string { | ||
b, _ := o.MarshalJSON() | ||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestDiffStringMap(t *testing.T) { | ||
testCases := []struct { | ||
Path string | ||
Old map[string]interface{} | ||
New map[string]interface{} | ||
ExpectedOps PatchOperations | ||
}{ | ||
{ | ||
Path: "/parent/", | ||
Old: map[string]interface{}{ | ||
"one": "111", | ||
"two": "222", | ||
}, | ||
New: map[string]interface{}{ | ||
"one": "111", | ||
"two": "222", | ||
"three": "333", | ||
}, | ||
ExpectedOps: []PatchOperation{ | ||
&AddOperation{ | ||
Path: "/parent/three", | ||
Value: "333", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Path: "/parent/", | ||
Old: map[string]interface{}{ | ||
"one": "111", | ||
"two": "222", | ||
}, | ||
New: map[string]interface{}{ | ||
"one": "111", | ||
"two": "abcd", | ||
}, | ||
ExpectedOps: []PatchOperation{ | ||
&ReplaceOperation{ | ||
Path: "/parent/two", | ||
Value: "abcd", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Path: "/parent/", | ||
Old: map[string]interface{}{ | ||
"one": "111", | ||
"two": "222", | ||
}, | ||
New: map[string]interface{}{ | ||
"two": "abcd", | ||
"three": "333", | ||
}, | ||
ExpectedOps: []PatchOperation{ | ||
&RemoveOperation{Path: "/parent/one"}, | ||
&ReplaceOperation{ | ||
Path: "/parent/two", | ||
Value: "abcd", | ||
}, | ||
&AddOperation{ | ||
Path: "/parent/three", | ||
Value: "333", | ||
}, | ||
}, | ||
}, | ||
{ | ||
Path: "/parent/", | ||
Old: map[string]interface{}{ | ||
"one": "111", | ||
"two": "222", | ||
}, | ||
New: map[string]interface{}{ | ||
"two": "222", | ||
}, | ||
ExpectedOps: []PatchOperation{ | ||
&RemoveOperation{Path: "/parent/one"}, | ||
}, | ||
}, | ||
{ | ||
Path: "/parent/", | ||
Old: map[string]interface{}{ | ||
"one": "111", | ||
"two": "222", | ||
}, | ||
New: map[string]interface{}{}, | ||
ExpectedOps: []PatchOperation{ | ||
&RemoveOperation{Path: "/parent/one"}, | ||
&RemoveOperation{Path: "/parent/two"}, | ||
}, | ||
}, | ||
{ | ||
Path: "/parent/", | ||
Old: map[string]interface{}{}, | ||
New: map[string]interface{}{ | ||
"one": "111", | ||
"two": "222", | ||
}, | ||
ExpectedOps: []PatchOperation{ | ||
&AddOperation{ | ||
Path: "/parent/one", | ||
Value: "111", | ||
}, | ||
&AddOperation{ | ||
Path: "/parent/two", | ||
Value: "222", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for i, tc := range testCases { | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
ops := diffStringMap(tc.Path, tc.Old, tc.New) | ||
if !tc.ExpectedOps.Equal(ops) { | ||
t.Fatalf("Operations don't match.\nExpected: %v\nGiven: %v\n", tc.ExpectedOps, ops) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.