Skip to content

Commit

Permalink
SaveAnnotations and test
Browse files Browse the repository at this point in the history
  • Loading branch information
kuritka committed Feb 22, 2021
1 parent eab1509 commit 7f5ee78
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
7 changes: 5 additions & 2 deletions controllers/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"reflect"

"github.com/AbsaOSS/k8gb/controllers/internal/utils"

k8gbv1beta1 "github.com/AbsaOSS/k8gb/api/v1beta1"
v1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -64,10 +66,11 @@ func (r *GslbReconciler) saveIngress(instance *k8gbv1beta1.Gslb, i *v1beta1.Ingr
// Update existing object with new spec and annotations
if !ingressCompare(found, i) {
found.Spec = i.Spec
found.Annotations = i.Annotations
found.Annotations = utils.SaveAnnotations(found.Annotations, i.Annotations)
err = r.Update(context.TODO(), found)
if errors.IsConflict(err) {
r.Log.Info("CONFLICT, Ingress updated by third-party")
r.Log.Info("Ingress has been modified outside of controller, retrying reconciliation",
"Ingress.Namespace", found.Namespace, "Ingress.Name", found.Name)
return nil
}
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions controllers/internal/utils/annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package utils

// SaveAnnotations adds or updates annotations from a2 from a1 and returns merge
func SaveAnnotations(a1 map[string]string, a2 map[string]string) map[string]string {
if a1 == nil {
a1 = make(map[string]string)
}
if a2 == nil {
a2 = make(map[string]string)
}
for k, v := range a2 {
if a1[k] != v {
a1[k] = v
}
}
return a1
}
76 changes: 76 additions & 0 deletions controllers/internal/utils/annotations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
)

var a2 = map[string]string{"k8gb.io/primary-geotag": "270", "k8gb.io/strategy": "failover"}
var a1 = map[string]string{"field.cattle.io/publicEndpoints": "dummy"}

func TestAddNewAnnotations(t *testing.T) {
// arrange
// act
repaired := SaveAnnotations(a1, a2)
// assert
assert.Equal(t, 3, len(repaired))
assert.Equal(t, "270", repaired["k8gb.io/primary-geotag"])
assert.Equal(t, "dummy", repaired["field.cattle.io/publicEndpoints"])
}

func TestAddExistingAnnotations(t *testing.T) {
// arrange
for k, v := range a2 {
a1[k] = v
}
// act
repaired := SaveAnnotations(a1, a2)
// assert
assert.Equal(t, 3, len(repaired))
assert.Equal(t, "270", repaired["k8gb.io/primary-geotag"])
assert.Equal(t, "dummy", repaired["field.cattle.io/publicEndpoints"])
assert.Equal(t, "failover", repaired["k8gb.io/strategy"])
}

func TestUpdateExistingRecords(t *testing.T) {
// arrange
for k, v := range a2 {
a1[k] = v
}
a1["k8gb.io/primary-geotag"] = "-"
// act
repaired := SaveAnnotations(a1, a2)
// assert
assert.Equal(t, 3, len(repaired))
assert.Equal(t, "270", repaired["k8gb.io/primary-geotag"])
assert.Equal(t, "dummy", repaired["field.cattle.io/publicEndpoints"])
assert.Equal(t, "failover", repaired["k8gb.io/strategy"])
}

func TestEqualAnnotationsWithNilA1(t *testing.T) {
// arrange
// act
repaired := SaveAnnotations(nil, a2)
// assert
assert.Equal(t, 2, len(repaired))
assert.Equal(t, "270", repaired["k8gb.io/primary-geotag"])
assert.Equal(t, "failover", repaired["k8gb.io/strategy"])
}

func TestEqualAnnotationsWithNilA2(t *testing.T) {
// arrange
// act
repaired := SaveAnnotations(a1, nil)
// assert
assert.Equal(t, 1, len(repaired))
assert.Equal(t, "dummy", repaired["field.cattle.io/publicEndpoints"])
}

func TestEqualAnnotationsWithNilInput(t *testing.T) {
// arrange
// act
repaired := SaveAnnotations(a1, nil)
// assert
assert.Equal(t, 0, len(repaired))
}

0 comments on commit 7f5ee78

Please sign in to comment.