-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use patch instead of update to mutate annotations.
This helps prevent some race conditions, where we try to update an object when simultaneous updates are happening. They eventually work themselves out because we continue to retry, but this helps speed things along.
- Loading branch information
1 parent
453bb9f
commit c678b44
Showing
5 changed files
with
96 additions
and
11 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,21 @@ | ||
package patch | ||
|
||
import "encoding/json" | ||
|
||
// GetAnnotationsPatch returns patch bytes that can be used with kubectl patch | ||
func GetAnnotationsPatch(newAnnotations map[string]string) ([]byte, error) { | ||
p := patch{ | ||
Metadata: metadata{ | ||
Annotations: newAnnotations, | ||
}, | ||
} | ||
return json.Marshal(p) | ||
} | ||
|
||
// These are used to get proper json formatting | ||
type patch struct { | ||
Metadata metadata `json:"metadata,omitempty"` | ||
} | ||
type metadata struct { | ||
Annotations map[string]string `json:"annotations,omitempty"` | ||
} |
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,49 @@ | ||
package patch | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetAnnotationsPatch(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
newAnnotations map[string]string | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty", | ||
newAnnotations: map[string]string{}, | ||
want: `{"metadata":{}}`, | ||
}, | ||
{ | ||
name: "one", | ||
newAnnotations: map[string]string{ | ||
"foo": "bar", | ||
}, | ||
want: `{"metadata":{"annotations":{"foo":"bar"}}}`, | ||
}, | ||
{ | ||
name: "many", | ||
newAnnotations: map[string]string{ | ||
"foo": "bar", | ||
"baz": "bat", | ||
}, | ||
want: `{"metadata":{"annotations":{"baz":"bat","foo":"bar"}}}`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GetAnnotationsPatch(tt.newAnnotations) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GetAnnotationsPatch() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
gotStr := string(got) | ||
if !reflect.DeepEqual(gotStr, tt.want) { | ||
t.Errorf("GetAnnotationsPatch() = %v, want %v", gotStr, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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
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