Skip to content

Commit 0c29c92

Browse files
authored
bugfix: make setAnnotation do what the signature implies (#1253)
1 parent cedb088 commit 0c29c92

File tree

7 files changed

+199
-158
lines changed

7 files changed

+199
-158
lines changed

api/v1/mongodbcommunity_types.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package v1
33
import (
44
"encoding/json"
55
"fmt"
6+
"k8s.io/apimachinery/pkg/runtime"
67
"net/url"
78
"strings"
89

@@ -17,7 +18,6 @@ import (
1718
"github.com/mongodb/mongodb-kubernetes-operator/pkg/automationconfig"
1819
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/scale"
1920

20-
"k8s.io/apimachinery/pkg/runtime"
2121
"k8s.io/apimachinery/pkg/runtime/schema"
2222

2323
"k8s.io/apimachinery/pkg/types"
@@ -350,9 +350,13 @@ func (m *MongodConfiguration) UnmarshalJSON(data []byte) error {
350350
}
351351

352352
func (m *MongodConfiguration) DeepCopy() *MongodConfiguration {
353-
return &MongodConfiguration{
354-
Object: runtime.DeepCopyJSON(m.Object),
353+
if m != nil && m.Object != nil {
354+
return &MongodConfiguration{
355+
Object: runtime.DeepCopyJSON(m.Object),
356+
}
355357
}
358+
c := NewMongodConfiguration()
359+
return &c
356360
}
357361

358362
// NewMongodConfiguration returns an empty MongodConfiguration
Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
11
{
2-
"mmsStatus": {
3-
"bar": {
4-
"errorString": "",
5-
"errorCode": 0,
6-
"plans": [
2+
"mmsStatus": {
3+
"bar": {
4+
"errorString": "",
5+
"errorCode": 0,
6+
"plans": [
7+
{
8+
"moves": [
9+
{
10+
"steps": [
711
{
8-
"moves": [
9-
{
10-
"steps": [
11-
{
12-
"result": "success",
13-
"completed": "2019-09-11T14:20:55.645615846Z",
14-
"started": "2019-09-11T14:20:40.631404367Z",
15-
"isWaitStep": false,
16-
"stepDoc": "Download mongodb binaries (may take a while)",
17-
"step": "Download"
18-
}
19-
],
20-
"moveDoc": "Download mongodb binaries",
21-
"move": "Download"
22-
},
23-
{
24-
"steps": [
25-
{
26-
"result": "success",
27-
"completed": "2019-09-11T14:20:59.325129842Z",
28-
"started": "2019-09-11T14:20:55.645743003Z",
29-
"isWaitStep": false,
30-
"stepDoc": "Start a mongo instance (start fresh)",
31-
"step": "StartFresh"
32-
}
33-
],
34-
"moveDoc": "Start the process",
35-
"move": "Start"
36-
},
37-
{
38-
"steps": [
39-
{
40-
"result": "wait",
41-
"completed": null,
42-
"started": "2019-09-11T14:20:59.325272608Z",
43-
"isWaitStep": true,
44-
"stepDoc": "Wait for the replica set to be initialized by another member",
45-
"step": "WaitRsInit"
46-
}
47-
],
48-
"moveDoc": "Wait for the replica set to be initialized by another member",
49-
"move": "WaitRsInit"
50-
},
51-
{
52-
"steps": [
53-
{
54-
"result": "",
55-
"completed": null,
56-
"started": null,
57-
"isWaitStep": true,
58-
"stepDoc": "Wait for featureCompatibilityVersion to be right",
59-
"step": "WaitFeatureCompatibilityVersionCorrect"
60-
}
61-
],
62-
"moveDoc": "Wait for featureCompatibilityVersion to be right",
63-
"move": "WaitFeatureCompatibilityVersionCorrect"
64-
}
65-
],
66-
"completed": "2019-09-11T14:21:42.034934358Z",
67-
"started": "2019-09-11T14:20:40.631348806Z"
12+
"result": "success",
13+
"completed": "2019-09-11T14:20:55.645615846Z",
14+
"started": "2019-09-11T14:20:40.631404367Z",
15+
"isWaitStep": false,
16+
"stepDoc": "Download mongodb binaries (may take a while)",
17+
"step": "Download"
6818
}
69-
],
70-
"lastGoalVersionAchieved": 5,
71-
"name": "bar"
72-
}
73-
},
74-
"statuses": {
75-
"bar": {
76-
"ExpectedToBeUp": true,
77-
"LastMongoUpTime": 1568222195,
78-
"IsInGoalState": true
19+
],
20+
"moveDoc": "Download mongodb binaries",
21+
"move": "Download"
22+
},
23+
{
24+
"steps": [
25+
{
26+
"result": "success",
27+
"completed": "2019-09-11T14:20:59.325129842Z",
28+
"started": "2019-09-11T14:20:55.645743003Z",
29+
"isWaitStep": false,
30+
"stepDoc": "Start a mongo instance (start fresh)",
31+
"step": "StartFresh"
32+
}
33+
],
34+
"moveDoc": "Start the process",
35+
"move": "Start"
36+
},
37+
{
38+
"steps": [
39+
{
40+
"result": "wait",
41+
"completed": null,
42+
"started": "2019-09-11T14:20:59.325272608Z",
43+
"isWaitStep": true,
44+
"stepDoc": "Wait for the replica set to be initialized by another member",
45+
"step": "WaitRsInit"
46+
}
47+
],
48+
"moveDoc": "Wait for the replica set to be initialized by another member",
49+
"move": "WaitRsInit"
50+
},
51+
{
52+
"steps": [
53+
{
54+
"result": "",
55+
"completed": null,
56+
"started": null,
57+
"isWaitStep": true,
58+
"stepDoc": "Wait for featureCompatibilityVersion to be right",
59+
"step": "WaitFeatureCompatibilityVersionCorrect"
60+
}
61+
],
62+
"moveDoc": "Wait for featureCompatibilityVersion to be right",
63+
"move": "WaitFeatureCompatibilityVersionCorrect"
64+
}
65+
],
66+
"completed": "2019-09-11T14:21:42.034934358Z",
67+
"started": "2019-09-11T14:20:40.631348806Z"
7968
}
69+
],
70+
"lastGoalVersionAchieved": 5,
71+
"name": "bar"
72+
}
73+
},
74+
"statuses": {
75+
"bar": {
76+
"ExpectedToBeUp": true,
77+
"LastMongoUpTime": 1568222195,
78+
"IsInGoalState": true
8079
}
80+
}
8181
}
Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,82 @@
11
{
2-
"mmsStatus": {
3-
"bar": {
4-
"errorString": "",
5-
"errorCode": 0,
6-
"plans": [
2+
"mmsStatus": {
3+
"bar": {
4+
"errorString": "",
5+
"errorCode": 0,
6+
"plans": [
7+
{
8+
"moves": [
9+
{
10+
"steps": [
711
{
8-
"moves": [
9-
{
10-
"steps": [
11-
{
12-
"result": "success",
13-
"completed": "2019-09-11T14:20:55.645615846Z",
14-
"started": "2019-09-11T14:20:40.631404367Z",
15-
"isWaitStep": false,
16-
"stepDoc": "Download mongodb binaries (may take a while)",
17-
"step": "Download"
18-
}
19-
],
20-
"moveDoc": "Download mongodb binaries",
21-
"move": "Download"
22-
},
23-
{
24-
"steps": [
25-
{
26-
"result": "success",
27-
"completed": "2019-09-11T14:20:59.325129842Z",
28-
"started": "2019-09-11T14:20:55.645743003Z",
29-
"isWaitStep": false,
30-
"stepDoc": "Start a mongo instance (start fresh)",
31-
"step": "StartFresh"
32-
}
33-
],
34-
"moveDoc": "Start the process",
35-
"move": "Start"
36-
},
37-
{
38-
"steps": [
39-
{
40-
"result": "wait",
41-
"completed": null,
42-
"started": "2019-09-11T14:20:59.325272608Z",
43-
"isWaitStep": true,
44-
"stepDoc": "Wait for the replica set to be initialized by another member",
45-
"step": "WaitRsInit"
46-
}
47-
],
48-
"moveDoc": "Wait for the replica set to be initialized by another member",
49-
"move": "WaitRsInit"
50-
},
51-
{
52-
"steps": [
53-
{
54-
"result": "",
55-
"completed": null,
56-
"started": null,
57-
"isWaitStep": true,
58-
"stepDoc": "Wait for featureCompatibilityVersion to be right",
59-
"step": "WaitFeatureCompatibilityVersionCorrect"
60-
}
61-
],
62-
"moveDoc": "Wait for featureCompatibilityVersion to be right",
63-
"move": "WaitFeatureCompatibilityVersionCorrect"
64-
}
65-
],
66-
"completed": "2019-09-11T14:21:42.034934358Z",
67-
"started": "2019-09-11T14:20:40.631348806Z"
12+
"result": "success",
13+
"completed": "2019-09-11T14:20:55.645615846Z",
14+
"started": "2019-09-11T14:20:40.631404367Z",
15+
"isWaitStep": false,
16+
"stepDoc": "Download mongodb binaries (may take a while)",
17+
"step": "Download"
6818
}
69-
],
70-
"lastGoalVersionAchieved": 5,
71-
"name": "bar"
72-
}
73-
},
74-
"statuses": {
75-
"bar": {
76-
"ReplicationStatus": null,
77-
"ExpectedToBeUp": true,
78-
"LastMongoUpTime": 1568222195,
79-
"IsInGoalState": true
19+
],
20+
"moveDoc": "Download mongodb binaries",
21+
"move": "Download"
22+
},
23+
{
24+
"steps": [
25+
{
26+
"result": "success",
27+
"completed": "2019-09-11T14:20:59.325129842Z",
28+
"started": "2019-09-11T14:20:55.645743003Z",
29+
"isWaitStep": false,
30+
"stepDoc": "Start a mongo instance (start fresh)",
31+
"step": "StartFresh"
32+
}
33+
],
34+
"moveDoc": "Start the process",
35+
"move": "Start"
36+
},
37+
{
38+
"steps": [
39+
{
40+
"result": "wait",
41+
"completed": null,
42+
"started": "2019-09-11T14:20:59.325272608Z",
43+
"isWaitStep": true,
44+
"stepDoc": "Wait for the replica set to be initialized by another member",
45+
"step": "WaitRsInit"
46+
}
47+
],
48+
"moveDoc": "Wait for the replica set to be initialized by another member",
49+
"move": "WaitRsInit"
50+
},
51+
{
52+
"steps": [
53+
{
54+
"result": "",
55+
"completed": null,
56+
"started": null,
57+
"isWaitStep": true,
58+
"stepDoc": "Wait for featureCompatibilityVersion to be right",
59+
"step": "WaitFeatureCompatibilityVersionCorrect"
60+
}
61+
],
62+
"moveDoc": "Wait for featureCompatibilityVersion to be right",
63+
"move": "WaitFeatureCompatibilityVersionCorrect"
64+
}
65+
],
66+
"completed": "2019-09-11T14:21:42.034934358Z",
67+
"started": "2019-09-11T14:20:40.631348806Z"
8068
}
69+
],
70+
"lastGoalVersionAchieved": 5,
71+
"name": "bar"
72+
}
73+
},
74+
"statuses": {
75+
"bar": {
76+
"ReplicationStatus": null,
77+
"ExpectedToBeUp": true,
78+
"LastMongoUpTime": 1568222195,
79+
"IsInGoalState": true
8180
}
81+
}
8282
}

controllers/replicaset_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ func TestAutomationConfig_CustomMongodConfig(t *testing.T) {
715715
mdb := newTestReplicaSet()
716716

717717
mongodConfig := objx.New(map[string]interface{}{})
718-
mongodConfig.Set("net.port", 1000)
718+
mongodConfig.Set("net.port", float64(1000))
719719
mongodConfig.Set("storage.other", "value")
720720
mongodConfig.Set("arbitrary.config.path", "value")
721721
mdb.Spec.AdditionalMongodConfig.Object = mongodConfig

pkg/kube/annotations/annotations.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ func GetAnnotation(object client.Object, key string) string {
3434
return value
3535
}
3636

37+
// SetAnnotations updates the objects.Annotation with the supplied annotation and does the same with the object backed in kubernetes.
3738
func SetAnnotations(object client.Object, annotations map[string]string, kubeClient client.Client) error {
38-
currentObject := object
39+
currentObject := object.DeepCopyObject().(client.Object)
3940
err := kubeClient.Get(context.TODO(), types.NamespacedName{Name: object.GetName(), Namespace: object.GetNamespace()}, currentObject)
4041
if err != nil {
4142
return err
@@ -67,7 +68,11 @@ func SetAnnotations(object client.Object, annotations map[string]string, kubeCli
6768
}
6869

6970
patch := client.RawPatch(types.JSONPatchType, data)
70-
return kubeClient.Patch(context.TODO(), object, patch)
71+
if err = kubeClient.Patch(context.TODO(), currentObject, patch); err != nil {
72+
return err
73+
}
74+
object.SetAnnotations(currentObject.GetAnnotations())
75+
return nil
7176
}
7277

7378
func UpdateLastAppliedMongoDBVersion(mdb Versioned, kubeClient client.Client) error {

0 commit comments

Comments
 (0)