move types from spec into separate package

This commit is contained in:
Murat Kabilov
2017-06-09 17:31:45 +02:00
parent 685e0c533e
commit 8f8d3d5148
20 changed files with 182 additions and 168 deletions
-103
View File
@@ -1,103 +0,0 @@
package spec
import (
"database/sql"
"fmt"
"strings"
"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
type EventType string
// NamespacedName describes the namespace/name pairs used in Kubernetes names.
type NamespacedName types.NamespacedName
// Possible values for the EventType
const (
EventAdd EventType = "ADD"
EventUpdate EventType = "UPDATE"
EventDelete EventType = "DELETE"
EventSync EventType = "SYNC"
)
// ClusterEvent carries the payload of the Cluster TPR events.
type ClusterEvent struct {
UID types.UID
EventType EventType
OldSpec *Postgresql
NewSpec *Postgresql
WorkerID uint32
}
type syncUserOperation int
// Possible values for the sync user operation (removal of users is not supported yet)
const (
PGSyncUserAdd = iota
PGsyncUserAlter
)
// PodEvent describes the event for a single Pod
type PodEvent struct {
ResourceVersion string
ClusterName NamespacedName
PodName NamespacedName
PrevPod *v1.Pod
CurPod *v1.Pod
EventType EventType
}
// PgUser contains information about a single user.
type PgUser struct {
Name string
Password string
Flags []string
MemberOf []string
}
// PgUserMap maps user names to the definitions.
type PgUserMap map[string]PgUser
// PgSyncUserRequest has information about a single request to sync a user.
type PgSyncUserRequest struct {
Kind syncUserOperation
User PgUser
}
// UserSyncer defines an interface for the implementations to sync users from the manifest to the DB.
type UserSyncer interface {
ProduceSyncRequests(dbUsers PgUserMap, newUsers PgUserMap) (req []PgSyncUserRequest)
ExecuteSyncRequests(req []PgSyncUserRequest, db *sql.DB) error
}
func (n NamespacedName) String() string {
return types.NamespacedName(n).String()
}
// MarshalJSON defines marshaling rule for the namespaced name type.
func (n NamespacedName) MarshalJSON() ([]byte, error) {
return []byte("\"" + n.String() + "\""), nil
}
// Decode converts a (possibly unqualified) string into the namespaced name object.
func (n *NamespacedName) Decode(value string) error {
name := types.NewNamespacedNameFromString(value)
if strings.Trim(value, string(types.Separator)) != "" && name == (types.NamespacedName{}) {
name.Name = value
name.Namespace = v1.NamespaceDefault
} else if name.Namespace == "" {
name.Namespace = v1.NamespaceDefault
}
if name.Name == "" {
return fmt.Errorf("Incorrect namespaced name")
}
*n = NamespacedName(name)
return nil
}
-55
View File
@@ -1,55 +0,0 @@
package spec
import (
"bytes"
"testing"
)
var nnTests = []struct {
s string
expected NamespacedName
expectedMarshal []byte
}{
{`acid/cluster`, NamespacedName{Namespace: "acid", Name: "cluster"}, []byte(`"acid/cluster"`)},
{`/name`, NamespacedName{Namespace: "default", Name: "name"}, []byte(`"default/name"`)},
{`test`, NamespacedName{Namespace: "default", Name: "test"}, []byte(`"default/test"`)},
}
var nnErr = []string{"test/", "/", "", "//"}
func TestNamespacedNameDecode(t *testing.T) {
for _, tt := range nnTests {
var actual NamespacedName
err := actual.Decode(tt.s)
if err != nil {
t.Errorf("Decode error: %v", err)
}
if actual != tt.expected {
t.Errorf("Expected: %v, got %#v", tt.expected, actual)
}
}
}
func TestNamespacedNameMarshal(t *testing.T) {
for _, tt := range nnTests {
var actual NamespacedName
m, err := actual.MarshalJSON()
if err != nil {
t.Errorf("Marshal error: %v", err)
}
if bytes.Equal(m, tt.expectedMarshal) {
t.Errorf("Expected marshal: %v, got %#v", tt.expected, actual)
}
}
}
func TestNamespacedNameError(t *testing.T) {
for _, tt := range nnErr {
var actual NamespacedName
err := actual.Decode(tt)
if err == nil {
t.Errorf("Error expected for '%s', got: %#v", tt, actual)
}
}
}