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
+20 -30
View File
@@ -14,11 +14,12 @@ import (
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/apps/v1beta1"
"k8s.io/client-go/pkg/types"
ktypes "k8s.io/client-go/pkg/types"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util"
"github.com/zalando-incubator/postgres-operator/pkg/util/config"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
@@ -28,17 +29,6 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/util/volumes"
)
type Interface interface {
Create() error
Delete() error
ExecCommand(podName *spec.NamespacedName, command ...string) (string, error)
ReceivePodEvent(event spec.PodEvent)
Run(stopCh <-chan struct{})
Sync() error
Update(newSpec *spec.Postgresql) error
SetFailed(err error)
}
type PostgresRole string
const (
@@ -53,13 +43,13 @@ type Config struct {
RestConfig *rest.Config
TeamsAPIClient *teams.API
OpConfig config.Config
InfrastructureRoles map[string]spec.PgUser // inherited from the controller
InfrastructureRoles map[string]types.PgUser // inherited from the controller
}
type kubeResources struct {
Service map[PostgresRole]*v1.Service
Endpoint *v1.Endpoints
Secrets map[types.UID]*v1.Secret
Secrets map[ktypes.UID]*v1.Secret
Statefulset *v1beta1.StatefulSet
//Pods are treated separately
//PVCs are treated separately
@@ -70,14 +60,14 @@ type Cluster struct {
spec.Postgresql
Config
logger *logrus.Entry
pgUsers map[string]spec.PgUser
systemUsers map[string]spec.PgUser
podSubscribers map[spec.NamespacedName]chan spec.PodEvent
pgUsers map[string]types.PgUser
systemUsers map[string]types.PgUser
podSubscribers map[types.NamespacedName]chan types.PodEvent
podSubscribersMu sync.RWMutex
pgDb *sql.DB
mu sync.Mutex
masterLess bool
userSyncStrategy spec.UserSyncer
userSyncStrategy types.UserSyncer
deleteOptions *v1.DeleteOptions
podEventsQueue *cache.FIFO
}
@@ -92,11 +82,11 @@ type compareStatefulsetResult struct {
// New creates a new cluster. This function should be called from a controller.
func New(cfg Config, pgSpec spec.Postgresql, logger *logrus.Entry) *Cluster {
lg := logger.WithField("pkg", "cluster").WithField("cluster-name", pgSpec.Metadata.Name)
kubeResources := kubeResources{Secrets: make(map[types.UID]*v1.Secret), Service: make(map[PostgresRole]*v1.Service)}
kubeResources := kubeResources{Secrets: make(map[ktypes.UID]*v1.Secret), Service: make(map[PostgresRole]*v1.Service)}
orphanDependents := true
podEventsQueue := cache.NewFIFO(func(obj interface{}) (string, error) {
e, ok := obj.(spec.PodEvent)
e, ok := obj.(types.PodEvent)
if !ok {
return "", fmt.Errorf("could not cast to PodEvent")
}
@@ -108,9 +98,9 @@ func New(cfg Config, pgSpec spec.Postgresql, logger *logrus.Entry) *Cluster {
Config: cfg,
Postgresql: pgSpec,
logger: lg,
pgUsers: make(map[string]spec.PgUser),
systemUsers: make(map[string]spec.PgUser),
podSubscribers: make(map[spec.NamespacedName]chan spec.PodEvent),
pgUsers: make(map[string]types.PgUser),
systemUsers: make(map[string]types.PgUser),
podSubscribers: make(map[types.NamespacedName]chan types.PodEvent),
kubeResources: kubeResources,
masterLess: false,
userSyncStrategy: users.DefaultUserSyncStrategy{},
@@ -121,7 +111,7 @@ func New(cfg Config, pgSpec spec.Postgresql, logger *logrus.Entry) *Cluster {
return cluster
}
func (c *Cluster) clusterName() spec.NamespacedName {
func (c *Cluster) clusterName() types.NamespacedName {
return util.NameFromMeta(c.Metadata)
}
@@ -543,14 +533,14 @@ func (c *Cluster) Delete() error {
}
// ReceivePodEvent is called back by the controller in order to add the cluster's pod event to the queue.
func (c *Cluster) ReceivePodEvent(event spec.PodEvent) {
func (c *Cluster) ReceivePodEvent(event types.PodEvent) {
if err := c.podEventsQueue.Add(event); err != nil {
c.logger.Errorf("error when receiving pod events: %v", err)
}
}
func (c *Cluster) processPodEvent(obj interface{}) error {
event, ok := obj.(spec.PodEvent)
event, ok := obj.(types.PodEvent)
if !ok {
return fmt.Errorf("could not cast to PodEvent")
}
@@ -588,11 +578,11 @@ func (c *Cluster) initSystemUsers() {
// task to Patroni. Those definitions are only used to create
// secrets, therefore, setting flags like SUPERUSER or REPLICATION
// is not necessary here
c.systemUsers[constants.SuperuserKeyName] = spec.PgUser{
c.systemUsers[constants.SuperuserKeyName] = types.PgUser{
Name: c.OpConfig.SuperUsername,
Password: util.RandomPassword(constants.PasswordLength),
}
c.systemUsers[constants.ReplicationUserKeyName] = spec.PgUser{
c.systemUsers[constants.ReplicationUserKeyName] = types.PgUser{
Name: c.OpConfig.ReplicationUsername,
Password: util.RandomPassword(constants.PasswordLength),
}
@@ -609,7 +599,7 @@ func (c *Cluster) initRobotUsers() error {
return fmt.Errorf("invalid flags for user '%v': %v", username, err)
}
c.pgUsers[username] = spec.PgUser{
c.pgUsers[username] = types.PgUser{
Name: username,
Password: util.RandomPassword(constants.PasswordLength),
Flags: flags,
@@ -627,7 +617,7 @@ func (c *Cluster) initHumanUsers() error {
for _, username := range teamMembers {
flags := []string{constants.RoleFlagLogin, constants.RoleFlagSuperuser}
memberOf := []string{c.OpConfig.PamRoleName}
c.pgUsers[username] = spec.PgUser{Name: username, Flags: flags, MemberOf: memberOf}
c.pgUsers[username] = types.PgUser{Name: username, Flags: flags, MemberOf: memberOf}
}
return nil
+3 -3
View File
@@ -4,12 +4,12 @@ import (
"fmt"
"strings"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
"github.com/zalando-incubator/postgres-operator/pkg/util/filesystems"
)
func (c *Cluster) getPostgresFilesystemInfo(podName *spec.NamespacedName) (device, fstype string, err error) {
func (c *Cluster) getPostgresFilesystemInfo(podName *types.NamespacedName) (device, fstype string, err error) {
out, err := c.ExecCommand(podName, "bash", "-c", fmt.Sprintf("df -T %s|tail -1", constants.PostgresDataMount))
if err != nil {
return "", "", err
@@ -22,7 +22,7 @@ func (c *Cluster) getPostgresFilesystemInfo(podName *spec.NamespacedName) (devic
return fields[0], fields[1], nil
}
func (c *Cluster) resizePostgresFilesystem(podName *spec.NamespacedName, resizers []filesystems.FilesystemResizer) error {
func (c *Cluster) resizePostgresFilesystem(podName *types.NamespacedName, resizers []filesystems.FilesystemResizer) error {
// resize2fs always writes to stderr, and ExecCommand considers a non-empty stderr an error
// first, determine the device and the filesystem
deviceName, fsType, err := c.getPostgresFilesystemInfo(podName)
+2 -1
View File
@@ -11,6 +11,7 @@ import (
"k8s.io/client-go/pkg/util/intstr"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
)
@@ -404,7 +405,7 @@ func (c *Cluster) genUserSecrets() (secrets map[string]*v1.Secret) {
return
}
func (c *Cluster) genSingleUserSecret(namespace string, pgUser spec.PgUser) *v1.Secret {
func (c *Cluster) genSingleUserSecret(namespace string, pgUser types.PgUser) *v1.Secret {
//Skip users with no password i.e. human users (they'll be authenticated using pam)
if pgUser.Password == "" {
return nil
+4 -4
View File
@@ -7,7 +7,7 @@ import (
"github.com/lib/pq"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
)
@@ -60,9 +60,9 @@ func (c *Cluster) initDbConn() (err error) {
return nil
}
func (c *Cluster) readPgUsersFromDatabase(userNames []string) (users spec.PgUserMap, err error) {
func (c *Cluster) readPgUsersFromDatabase(userNames []string) (users types.PgUserMap, err error) {
var rows *sql.Rows
users = make(spec.PgUserMap)
users = make(types.PgUserMap)
if rows, err = c.pgDb.Query(getUserSQL, pq.Array(userNames)); err != nil {
return nil, fmt.Errorf("error when querying users: %v", err)
}
@@ -85,7 +85,7 @@ func (c *Cluster) readPgUsersFromDatabase(userNames []string) (users spec.PgUser
}
flags := makeUserFlags(rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolcanlogin)
// XXX: the code assumes the password we get from pg_authid is always MD5
users[rolname] = spec.PgUser{Name: rolname, Password: rolpassword, Flags: flags, MemberOf: memberof}
users[rolname] = types.PgUser{Name: rolname, Password: rolpassword, Flags: flags, MemberOf: memberof}
}
return users, nil
+5 -5
View File
@@ -5,7 +5,7 @@ import (
"k8s.io/client-go/pkg/api/v1"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
)
@@ -50,7 +50,7 @@ func (c *Cluster) deletePods() error {
return nil
}
func (c *Cluster) deletePod(podName spec.NamespacedName) error {
func (c *Cluster) deletePod(podName types.NamespacedName) error {
ch := c.registerPodSubscriber(podName)
defer c.unregisterPodSubscriber(podName)
@@ -65,7 +65,7 @@ func (c *Cluster) deletePod(podName spec.NamespacedName) error {
return nil
}
func (c *Cluster) unregisterPodSubscriber(podName spec.NamespacedName) {
func (c *Cluster) unregisterPodSubscriber(podName types.NamespacedName) {
c.podSubscribersMu.Lock()
defer c.podSubscribersMu.Unlock()
@@ -77,11 +77,11 @@ func (c *Cluster) unregisterPodSubscriber(podName spec.NamespacedName) {
delete(c.podSubscribers, podName)
}
func (c *Cluster) registerPodSubscriber(podName spec.NamespacedName) chan spec.PodEvent {
func (c *Cluster) registerPodSubscriber(podName types.NamespacedName) chan types.PodEvent {
c.podSubscribersMu.Lock()
defer c.podSubscribersMu.Unlock()
ch := make(chan spec.PodEvent)
ch := make(chan types.PodEvent)
if _, ok := c.podSubscribers[podName]; ok {
panic("pod '" + podName.String() + "' is already subscribed")
}
+2 -2
View File
@@ -7,7 +7,7 @@ import (
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/apps/v1beta1"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
"github.com/zalando-incubator/postgres-operator/pkg/util/k8sutil"
@@ -318,7 +318,7 @@ func (c *Cluster) applySecrets() error {
for secretUsername, secretSpec := range secrets {
secret, err := c.KubeClient.Secrets(secretSpec.Namespace).Create(secretSpec)
if k8sutil.ResourceAlreadyExists(err) {
var userMap map[string]spec.PgUser
var userMap map[string]types.PgUser
curSecret, err := c.KubeClient.Secrets(secretSpec.Namespace).Get(secretSpec.Name)
if err != nil {
return fmt.Errorf("could not get current secret: %v", err)
+5 -4
View File
@@ -16,6 +16,7 @@ import (
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
"github.com/zalando-incubator/postgres-operator/pkg/util/retryutil"
@@ -159,7 +160,7 @@ func (c *Cluster) getTeamMembers() ([]string, error) {
return teamInfo.Members, nil
}
func (c *Cluster) waitForPodLabel(podEvents chan spec.PodEvent) error {
func (c *Cluster) waitForPodLabel(podEvents chan types.PodEvent) error {
for {
select {
case podEvent := <-podEvents:
@@ -176,11 +177,11 @@ func (c *Cluster) waitForPodLabel(podEvents chan spec.PodEvent) error {
}
}
func (c *Cluster) waitForPodDeletion(podEvents chan spec.PodEvent) error {
func (c *Cluster) waitForPodDeletion(podEvents chan types.PodEvent) error {
for {
select {
case podEvent := <-podEvents:
if podEvent.EventType == spec.EventDelete {
if podEvent.EventType == types.EventDelete {
return nil
}
case <-time.After(c.OpConfig.PodDeletionWaitTimeout):
@@ -313,7 +314,7 @@ func (c *Cluster) podSpiloRole(pod *v1.Pod) string {
return pod.Labels[c.OpConfig.PodRoleLabel]
}
func (c *Cluster) ExecCommand(podName *spec.NamespacedName, command ...string) (string, error) {
func (c *Cluster) ExecCommand(podName *types.NamespacedName, command ...string) (string, error) {
var (
execOut bytes.Buffer
execErr bytes.Buffer
+3 -2
View File
@@ -9,6 +9,7 @@ import (
"k8s.io/client-go/pkg/api/v1"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/types"
"github.com/zalando-incubator/postgres-operator/pkg/util"
"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
"github.com/zalando-incubator/postgres-operator/pkg/util/filesystems"
@@ -172,11 +173,11 @@ func (c *Cluster) listVolumesWithManifestSize(newVolume spec.Volume) ([]*v1.Pers
}
// getPodNameFromPersistentVolume returns a pod name that it extracts from the volume claim ref.
func getPodNameFromPersistentVolume(pv *v1.PersistentVolume) *spec.NamespacedName {
func getPodNameFromPersistentVolume(pv *v1.PersistentVolume) *types.NamespacedName {
namespace := pv.Spec.ClaimRef.Namespace
name := pv.Spec.ClaimRef.Name[len(constants.DataVolumeName)+1:]
return &spec.NamespacedName{namespace, name}
return &types.NamespacedName{namespace, name}
}
func quantityToGigabyte(q resource.Quantity) int64 {