Use deepcopy to propagate the spec to clusters.

Avoid sharing pointers to the same spec data between the informer
and the clusters. The only catch is that the error field is cleared
during deepcopy, since it is an interface that may contain private
fields that cannot be copied, however, the error is only used when
the manifest is parsed and before it is queued, therefore, we never
refer to that field in the cluster structure.
This commit is contained in:
Oleksii Kliukin
2018-05-17 16:05:12 +02:00
parent ebe50abccb
commit da4cc2705b
5 changed files with 42 additions and 22 deletions
+14
View File
@@ -3,6 +3,7 @@ package spec
import (
"encoding/json"
"fmt"
"github.com/mohae/deepcopy"
"regexp"
"strings"
"time"
@@ -138,6 +139,19 @@ var (
serviceNameRegex = regexp.MustCompile(serviceNameRegexString)
)
// Clone makes a deepcopy of the Postgresql structure. The Error field is nulled-out,
// as there is no guaratee that the actual implementation of the error interface
// will not contain any private fields not-reachable to deepcopy. This should be ok,
// since Error is never read from a Kubernetes object.
func (p *Postgresql) Clone() *Postgresql {
if p == nil {
return nil
}
c := deepcopy.Copy(p).(*Postgresql)
c.Error = nil
return c
}
func parseTime(s string) (time.Time, error) {
parts := strings.Split(s, ":")
if len(parts) != 2 {
+13 -2
View File
@@ -4,11 +4,10 @@ import (
"bytes"
"encoding/json"
"errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"testing"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var parseTimeTests = []struct {
@@ -546,3 +545,15 @@ func TestPostgresListMeta(t *testing.T) {
return
}
}
func TestPostgresqlClone(t *testing.T) {
for _, tt := range unmarshalCluster {
cp := &tt.out
cp.Error = nil
clone := cp.Clone()
if !reflect.DeepEqual(clone, cp) {
t.Errorf("TestPostgresqlClone expected: \n%#v\n, got \n%#v", cp, clone)
}
}
}