forked from grafana/dskit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fabeae0
commit 5c38440
Showing
3 changed files
with
132 additions
and
45 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,70 @@ | ||
package kubernetes | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
) | ||
|
||
type operation struct { | ||
Op string `json:"op"` | ||
Path string `json:"path"` | ||
// Value should be a base64-encoded value or nil. | ||
Value *string `json:"value"` | ||
} | ||
|
||
// preparePatch prepares a JSON patch (RFC 6902) to update a configmap key. If oldHash is empty or nil | ||
// this is equivalent to adding a key. | ||
func preparePatch(key string, oldHash, newVal, newHash []byte) ([]byte, error) { | ||
hashKey := "/binaryData/" + convertKeyToStoreHash(key) | ||
|
||
b64 := func(b []byte) *string { | ||
str := base64.StdEncoding.EncodeToString(b) | ||
return &str | ||
} | ||
|
||
// testing with nil is equivalent to testing that the key doesn't exist | ||
var expectedHash *string | ||
if len(oldHash) > 0 { | ||
expectedHash = b64(oldHash) | ||
} | ||
|
||
testHashOp := operation{ | ||
Op: "test", | ||
Path: hashKey, | ||
Value: expectedHash, | ||
} | ||
|
||
setHashOp := operation{ | ||
Op: "replace", | ||
Path: hashKey, | ||
Value: b64(newHash), | ||
} | ||
|
||
setDataOp := operation{ | ||
Op: "replace", | ||
Path: "/binaryData/" + convertKeyToStore(key), | ||
Value: b64(newVal), | ||
} | ||
|
||
patch := []operation{testHashOp, setHashOp, setDataOp} | ||
|
||
return json.Marshal(patch) | ||
} | ||
|
||
func prepareDeletePatch(key string) ([]byte, error) { | ||
removeHashOp := operation{ | ||
Op: "remove", | ||
Path: "/binaryData/" + convertKeyToStoreHash(key), | ||
Value: nil, | ||
} | ||
|
||
removeObjectOp := operation{ | ||
Op: "remove", | ||
Path: "/binaryData/" + convertKeyToStore(key), | ||
Value: nil, | ||
} | ||
|
||
patch := []operation{removeHashOp, removeObjectOp} | ||
|
||
return json.Marshal(patch) | ||
} |
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