Skip to content

bugfix: make setAnnotation do what the signature implies #1253

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions api/v1/mongodbcommunity_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1
import (
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"net/url"
"strings"

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

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

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

func (m *MongodConfiguration) DeepCopy() *MongodConfiguration {
return &MongodConfiguration{
Object: runtime.DeepCopyJSON(m.Object),
if m != nil && m.Object != nil {
return &MongodConfiguration{
Object: runtime.DeepCopyJSON(m.Object),
}
}
c := NewMongodConfiguration()
return &c
}

// NewMongodConfiguration returns an empty MongodConfiguration
Expand Down
150 changes: 75 additions & 75 deletions cmd/readiness/testdata/health-status-no-replication.json
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
{
"mmsStatus": {
"bar": {
"errorString": "",
"errorCode": 0,
"plans": [
"mmsStatus": {
"bar": {
"errorString": "",
"errorCode": 0,
"plans": [
{
"moves": [
{
"steps": [
{
"moves": [
{
"steps": [
{
"result": "success",
"completed": "2019-09-11T14:20:55.645615846Z",
"started": "2019-09-11T14:20:40.631404367Z",
"isWaitStep": false,
"stepDoc": "Download mongodb binaries (may take a while)",
"step": "Download"
}
],
"moveDoc": "Download mongodb binaries",
"move": "Download"
},
{
"steps": [
{
"result": "success",
"completed": "2019-09-11T14:20:59.325129842Z",
"started": "2019-09-11T14:20:55.645743003Z",
"isWaitStep": false,
"stepDoc": "Start a mongo instance (start fresh)",
"step": "StartFresh"
}
],
"moveDoc": "Start the process",
"move": "Start"
},
{
"steps": [
{
"result": "wait",
"completed": null,
"started": "2019-09-11T14:20:59.325272608Z",
"isWaitStep": true,
"stepDoc": "Wait for the replica set to be initialized by another member",
"step": "WaitRsInit"
}
],
"moveDoc": "Wait for the replica set to be initialized by another member",
"move": "WaitRsInit"
},
{
"steps": [
{
"result": "",
"completed": null,
"started": null,
"isWaitStep": true,
"stepDoc": "Wait for featureCompatibilityVersion to be right",
"step": "WaitFeatureCompatibilityVersionCorrect"
}
],
"moveDoc": "Wait for featureCompatibilityVersion to be right",
"move": "WaitFeatureCompatibilityVersionCorrect"
}
],
"completed": "2019-09-11T14:21:42.034934358Z",
"started": "2019-09-11T14:20:40.631348806Z"
"result": "success",
"completed": "2019-09-11T14:20:55.645615846Z",
"started": "2019-09-11T14:20:40.631404367Z",
"isWaitStep": false,
"stepDoc": "Download mongodb binaries (may take a while)",
"step": "Download"
}
],
"lastGoalVersionAchieved": 5,
"name": "bar"
}
},
"statuses": {
"bar": {
"ExpectedToBeUp": true,
"LastMongoUpTime": 1568222195,
"IsInGoalState": true
],
"moveDoc": "Download mongodb binaries",
"move": "Download"
},
{
"steps": [
{
"result": "success",
"completed": "2019-09-11T14:20:59.325129842Z",
"started": "2019-09-11T14:20:55.645743003Z",
"isWaitStep": false,
"stepDoc": "Start a mongo instance (start fresh)",
"step": "StartFresh"
}
],
"moveDoc": "Start the process",
"move": "Start"
},
{
"steps": [
{
"result": "wait",
"completed": null,
"started": "2019-09-11T14:20:59.325272608Z",
"isWaitStep": true,
"stepDoc": "Wait for the replica set to be initialized by another member",
"step": "WaitRsInit"
}
],
"moveDoc": "Wait for the replica set to be initialized by another member",
"move": "WaitRsInit"
},
{
"steps": [
{
"result": "",
"completed": null,
"started": null,
"isWaitStep": true,
"stepDoc": "Wait for featureCompatibilityVersion to be right",
"step": "WaitFeatureCompatibilityVersionCorrect"
}
],
"moveDoc": "Wait for featureCompatibilityVersion to be right",
"move": "WaitFeatureCompatibilityVersionCorrect"
}
],
"completed": "2019-09-11T14:21:42.034934358Z",
"started": "2019-09-11T14:20:40.631348806Z"
}
],
"lastGoalVersionAchieved": 5,
"name": "bar"
}
},
"statuses": {
"bar": {
"ExpectedToBeUp": true,
"LastMongoUpTime": 1568222195,
"IsInGoalState": true
}
}
}
152 changes: 76 additions & 76 deletions cmd/readiness/testdata/health-status-ok-no-replica-status.json
Original file line number Diff line number Diff line change
@@ -1,82 +1,82 @@
{
"mmsStatus": {
"bar": {
"errorString": "",
"errorCode": 0,
"plans": [
"mmsStatus": {
"bar": {
"errorString": "",
"errorCode": 0,
"plans": [
{
"moves": [
{
"steps": [
{
"moves": [
{
"steps": [
{
"result": "success",
"completed": "2019-09-11T14:20:55.645615846Z",
"started": "2019-09-11T14:20:40.631404367Z",
"isWaitStep": false,
"stepDoc": "Download mongodb binaries (may take a while)",
"step": "Download"
}
],
"moveDoc": "Download mongodb binaries",
"move": "Download"
},
{
"steps": [
{
"result": "success",
"completed": "2019-09-11T14:20:59.325129842Z",
"started": "2019-09-11T14:20:55.645743003Z",
"isWaitStep": false,
"stepDoc": "Start a mongo instance (start fresh)",
"step": "StartFresh"
}
],
"moveDoc": "Start the process",
"move": "Start"
},
{
"steps": [
{
"result": "wait",
"completed": null,
"started": "2019-09-11T14:20:59.325272608Z",
"isWaitStep": true,
"stepDoc": "Wait for the replica set to be initialized by another member",
"step": "WaitRsInit"
}
],
"moveDoc": "Wait for the replica set to be initialized by another member",
"move": "WaitRsInit"
},
{
"steps": [
{
"result": "",
"completed": null,
"started": null,
"isWaitStep": true,
"stepDoc": "Wait for featureCompatibilityVersion to be right",
"step": "WaitFeatureCompatibilityVersionCorrect"
}
],
"moveDoc": "Wait for featureCompatibilityVersion to be right",
"move": "WaitFeatureCompatibilityVersionCorrect"
}
],
"completed": "2019-09-11T14:21:42.034934358Z",
"started": "2019-09-11T14:20:40.631348806Z"
"result": "success",
"completed": "2019-09-11T14:20:55.645615846Z",
"started": "2019-09-11T14:20:40.631404367Z",
"isWaitStep": false,
"stepDoc": "Download mongodb binaries (may take a while)",
"step": "Download"
}
],
"lastGoalVersionAchieved": 5,
"name": "bar"
}
},
"statuses": {
"bar": {
"ReplicationStatus": null,
"ExpectedToBeUp": true,
"LastMongoUpTime": 1568222195,
"IsInGoalState": true
],
"moveDoc": "Download mongodb binaries",
"move": "Download"
},
{
"steps": [
{
"result": "success",
"completed": "2019-09-11T14:20:59.325129842Z",
"started": "2019-09-11T14:20:55.645743003Z",
"isWaitStep": false,
"stepDoc": "Start a mongo instance (start fresh)",
"step": "StartFresh"
}
],
"moveDoc": "Start the process",
"move": "Start"
},
{
"steps": [
{
"result": "wait",
"completed": null,
"started": "2019-09-11T14:20:59.325272608Z",
"isWaitStep": true,
"stepDoc": "Wait for the replica set to be initialized by another member",
"step": "WaitRsInit"
}
],
"moveDoc": "Wait for the replica set to be initialized by another member",
"move": "WaitRsInit"
},
{
"steps": [
{
"result": "",
"completed": null,
"started": null,
"isWaitStep": true,
"stepDoc": "Wait for featureCompatibilityVersion to be right",
"step": "WaitFeatureCompatibilityVersionCorrect"
}
],
"moveDoc": "Wait for featureCompatibilityVersion to be right",
"move": "WaitFeatureCompatibilityVersionCorrect"
}
],
"completed": "2019-09-11T14:21:42.034934358Z",
"started": "2019-09-11T14:20:40.631348806Z"
}
],
"lastGoalVersionAchieved": 5,
"name": "bar"
}
},
"statuses": {
"bar": {
"ReplicationStatus": null,
"ExpectedToBeUp": true,
"LastMongoUpTime": 1568222195,
"IsInGoalState": true
}
}
}
2 changes: 1 addition & 1 deletion controllers/replicaset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ func TestAutomationConfig_CustomMongodConfig(t *testing.T) {
mdb := newTestReplicaSet()

mongodConfig := objx.New(map[string]interface{}{})
mongodConfig.Set("net.port", 1000)
mongodConfig.Set("net.port", float64(1000))
mongodConfig.Set("storage.other", "value")
mongodConfig.Set("arbitrary.config.path", "value")
mdb.Spec.AdditionalMongodConfig.Object = mongodConfig
Expand Down
9 changes: 7 additions & 2 deletions pkg/kube/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ func GetAnnotation(object client.Object, key string) string {
return value
}

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

patch := client.RawPatch(types.JSONPatchType, data)
return kubeClient.Patch(context.TODO(), object, patch)
if err = kubeClient.Patch(context.TODO(), currentObject, patch); err != nil {
return err
}
object.SetAnnotations(currentObject.GetAnnotations())
return nil
}

func UpdateLastAppliedMongoDBVersion(mdb Versioned, kubeClient client.Client) error {
Expand Down
Loading