Make use of kubernetes client-go v4

* client-go v4.0.0-beta0
* remove unnecessary methods for tpr object
* rest client: use interface instead of structure pointer
* proper names for constants; some clean up for log messages
* remove teams api client from controller and make it per cluster
This commit is contained in:
Murat Kabilov
2017-07-25 15:25:17 +02:00
committed by GitHub
parent 4455f1b639
commit 1f8b37f33d
31 changed files with 579 additions and 666 deletions
+8 -29
View File
@@ -6,9 +6,7 @@ import (
"strings"
"time"
"k8s.io/client-go/pkg/api/meta"
"k8s.io/client-go/pkg/api/unversioned"
"k8s.io/client-go/pkg/api/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// MaintenanceWindow describes the time window when the operator is allowed to do maintenance on a cluster.
@@ -71,8 +69,8 @@ const (
// Postgresql defines PostgreSQL Third Party (resource) Object.
type Postgresql struct {
unversioned.TypeMeta `json:",inline"`
Metadata v1.ObjectMeta `json:"metadata"`
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
Spec PostgresSpec `json:"spec"`
Status PostgresStatus `json:"status,omitempty"`
@@ -88,7 +86,7 @@ type PostgresSpec struct {
TeamID string `json:"teamId"`
AllowedSourceRanges []string `json:"allowedSourceRanges"`
// EnableLoadBalancer is a pointer, since it is importat to know if that parameters is omited from the manifest
// EnableLoadBalancer is a pointer, since it is importat to know if that parameters is omitted from the manifest
UseLoadBalancer *bool `json:"useLoadBalancer,omitempty"`
ReplicaLoadBalancer bool `json:"replicaLoadBalancer,omitempty"`
NumberOfInstances int32 `json:"numberOfInstances"`
@@ -99,8 +97,8 @@ type PostgresSpec struct {
// PostgresqlList defines a list of PostgreSQL clusters.
type PostgresqlList struct {
unversioned.TypeMeta `json:",inline"`
Metadata unversioned.ListMeta `json:"metadata"`
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`
Items []Postgresql `json:"items"`
}
@@ -191,21 +189,6 @@ func (m *MaintenanceWindow) UnmarshalJSON(data []byte) error {
return nil
}
// GetObject implements Object interface for PostgreSQL TPR spec object.
func (p *Postgresql) GetObjectKind() unversioned.ObjectKind {
return &p.TypeMeta
}
// GetObjectMeta implements ObjectMetaAccessor interface for PostgreSQL TPR spec object.
func (p *Postgresql) GetObjectMeta() meta.Object {
return &p.Metadata
}
// GetListMeta implements ListMetaAccessor interface for PostgreSQL TPR List spec object.
func (pl *PostgresqlList) GetListMeta() unversioned.List {
return &pl.Metadata
}
func extractClusterName(clusterName string, teamName string) (string, error) {
teamNameLen := len(teamName)
if len(clusterName) < teamNameLen+2 {
@@ -223,10 +206,6 @@ func extractClusterName(clusterName string, teamName string) (string, error) {
return clusterName[teamNameLen+1:], nil
}
// The code below is used only to work around a known problem with third-party
// resources and ugorji. If/when these issues are resolved, the code below
// should no longer be required.
//
type postgresqlListCopy PostgresqlList
type postgresqlCopy Postgresql
@@ -236,7 +215,7 @@ func (p *Postgresql) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, &tmp)
if err != nil {
metaErr := json.Unmarshal(data, &tmp.Metadata)
metaErr := json.Unmarshal(data, &tmp.ObjectMeta)
if metaErr != nil {
return err
}
@@ -250,7 +229,7 @@ func (p *Postgresql) UnmarshalJSON(data []byte) error {
}
tmp2 := Postgresql(tmp)
clusterName, err := extractClusterName(tmp2.Metadata.Name, tmp2.Spec.TeamID)
clusterName, err := extractClusterName(tmp2.ObjectMeta.Name, tmp2.Spec.TeamID)
if err == nil {
tmp2.Spec.ClusterName = clusterName
} else {
+17 -18
View File
@@ -8,8 +8,7 @@ import (
"testing"
"time"
"k8s.io/client-go/pkg/api/unversioned"
"k8s.io/client-go/pkg/api/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var parseTimeTests = []struct {
@@ -104,11 +103,11 @@ var unmarshalCluster = []struct {
"kind": "Postgresql","apiVersion": "acid.zalan.do/v1",
"metadata": {"name": "acid-testcluster1"}, "spec": {"teamId": 100}}`),
Postgresql{
TypeMeta: unversioned.TypeMeta{
TypeMeta: metav1.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: "acid-testcluster1",
},
Status: ClusterStatusInvalid,
@@ -184,11 +183,11 @@ var unmarshalCluster = []struct {
}
}`),
Postgresql{
TypeMeta: unversioned.TypeMeta{
TypeMeta: metav1.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: "acid-testcluster1",
},
Spec: PostgresSpec{
@@ -250,11 +249,11 @@ var unmarshalCluster = []struct {
{
[]byte(`{"kind": "Postgresql","apiVersion": "acid.zalan.do/v1","metadata": {"name": "teapot-testcluster1"}, "spec": {"teamId": "acid"}}`),
Postgresql{
TypeMeta: unversioned.TypeMeta{
TypeMeta: metav1.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: "teapot-testcluster1",
},
Spec: PostgresSpec{TeamID: "acid"},
@@ -278,16 +277,16 @@ var postgresqlList = []struct {
}{
{[]byte(`{"apiVersion":"v1","items":[{"apiVersion":"acid.zalan.do/v1","kind":"Postgresql","metadata":{"labels":{"team":"acid"},"name":"acid-testcluster42","namespace":"default","resourceVersion":"30446957","selfLink":"/apis/acid.zalan.do/v1/namespaces/default/postgresqls/acid-testcluster42","uid":"857cd208-33dc-11e7-b20a-0699041e4b03"},"spec":{"allowedSourceRanges":["185.85.220.0/22"],"numberOfInstances":1,"postgresql":{"version":"9.6"},"teamId":"acid","volume":{"size":"10Gi"}},"status":"Running"}],"kind":"List","metadata":{},"resourceVersion":"","selfLink":""}`),
PostgresqlList{
TypeMeta: unversioned.TypeMeta{
TypeMeta: metav1.TypeMeta{
Kind: "List",
APIVersion: "v1",
},
Items: []Postgresql{{
TypeMeta: unversioned.TypeMeta{
TypeMeta: metav1.TypeMeta{
Kind: "Postgresql",
APIVersion: "acid.zalan.do/v1",
},
Metadata: v1.ObjectMeta{
ObjectMeta: metav1.ObjectMeta{
Name: "acid-testcluster42",
Namespace: "default",
Labels: map[string]string{"team": "acid"},
@@ -363,7 +362,7 @@ func TestClusterName(t *testing.T) {
continue
}
if name != tt.clusterName {
t.Errorf("Expected cluserName: %s, got: %s", tt.clusterName, name)
t.Errorf("Expected cluserName: %q, got: %q", tt.clusterName, name)
}
}
}
@@ -400,7 +399,7 @@ func TestMarshalMaintenanceWindow(t *testing.T) {
}
if !bytes.Equal(s, tt.in) {
t.Errorf("Expected Marshal: %s, got: %s", string(tt.in), string(s))
t.Errorf("Expected Marshal: %q, got: %q", string(tt.in), string(s))
}
}
}
@@ -435,7 +434,7 @@ func TestMarshal(t *testing.T) {
continue
}
if !bytes.Equal(m, tt.marshal) {
t.Errorf("Marshal Postgresql expected: %s, got: %s", string(tt.marshal), string(m))
t.Errorf("Marshal Postgresql expected: %q, got: %q", string(tt.marshal), string(m))
}
}
}
@@ -446,8 +445,8 @@ func TestPostgresMeta(t *testing.T) {
t.Errorf("GetObjectKindMeta expected: %v, got: %v", tt.out.TypeMeta, a)
}
if a := tt.out.GetObjectMeta(); reflect.DeepEqual(a, tt.out.Metadata) {
t.Errorf("GetObjectMeta expected: %v, got: %v", tt.out.Metadata, a)
if a := tt.out.GetObjectMeta(); reflect.DeepEqual(a, tt.out.ObjectMeta) {
t.Errorf("GetObjectMeta expected: %v, got: %v", tt.out.ObjectMeta, a)
}
}
}
@@ -476,8 +475,8 @@ func TestPostgresListMeta(t *testing.T) {
t.Errorf("GetObjectKindMeta expected: %v, got: %v", tt.out.TypeMeta, a)
}
if a := tt.out.GetListMeta(); reflect.DeepEqual(a, tt.out.Metadata) {
t.Errorf("GetObjectMeta expected: %v, got: %v", tt.out.Metadata, a)
if a := tt.out.GetListMeta(); reflect.DeepEqual(a, tt.out.ListMeta) {
t.Errorf("GetObjectMeta expected: %v, got: %v", tt.out.ListMeta, a)
}
return
+2 -2
View File
@@ -1,12 +1,12 @@
package spec
import (
"database/sql"
"fmt"
"strings"
"database/sql"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/types"
)
// EvenType contains type of the events for the TPRs and Pods received from Kubernetes
+1 -1
View File
@@ -49,7 +49,7 @@ func TestNamespacedNameError(t *testing.T) {
var actual NamespacedName
err := actual.Decode(tt)
if err == nil {
t.Errorf("Error expected for '%s', got: %#v", tt, actual)
t.Errorf("Error expected for %q, got: %#v", tt, actual)
}
}
}