Skip to content

Commit

Permalink
move from tpr to crd
Browse files Browse the repository at this point in the history
  • Loading branch information
mkabilov committed Oct 6, 2017
1 parent bd9b0b6 commit cc9a55a
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 113 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

BINARY ?= postgres-operator
BUILD_FLAGS ?= -v
ifeq ("$(shell uname)", "Darwin")
BUILD_FLAGS += -i
endif

CGO_ENABLED ?= 0
ifeq ($(RACE),1)
BUILD_FLAGS += -race -a
Expand Down
23 changes: 16 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import:
- service/ec2
- package: github.com/lib/pq
- package: github.com/motomux/pretty
- package: k8s.io/apiextensions-apiserver
subpackages:
- pkg/client/clientset/clientset
- package: k8s.io/apimachinery
version: release-1.7
subpackages:
- pkg/api/errors
- pkg/api/meta
Expand All @@ -26,7 +30,7 @@ import:
- pkg/util/remotecommand
- pkg/watch
- package: k8s.io/client-go
version: ^4.0.0
version: v4.0.0
subpackages:
- kubernetes
- pkg/api
Expand All @@ -36,4 +40,4 @@ import:
- rest
- tools/cache
- tools/clientcmd
- tools/remotecommand
- tools/remotecommand
2 changes: 1 addition & 1 deletion manifests/testpostgresql.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: "acid.zalan.do/v1"
kind: "Postgresql"
kind: postgresql

metadata:
name: acid-testcluster
Expand Down
6 changes: 4 additions & 2 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ func (c *Cluster) setStatus(status spec.PostgresStatus) {
}
request := []byte(fmt.Sprintf(`{"status": %s}`, string(b))) //TODO: Look into/wait for k8s go client methods

_, err = c.KubeClient.RESTClient.Patch(types.MergePatchType).
RequestURI(c.GetSelfLink()).
_, err = c.KubeClient.CRDREST.Patch(types.MergePatchType).
Namespace(c.Namespace).
Resource(constants.CRDResource).
Name(c.Name).
Body(request).
DoRaw()

Expand Down
2 changes: 1 addition & 1 deletion pkg/cluster/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *Cluster) updateStatefulSet(newStatefulSet *v1beta1.StatefulSet) error {
return nil
}

// replaceStatefulSet deletes an old StatefulSet and creates the new using spec in the PostgreSQL TPR.
// replaceStatefulSet deletes an old StatefulSet and creates the new using spec in the PostgreSQL CRD.
func (c *Cluster) replaceStatefulSet(newStatefulSet *v1beta1.StatefulSet) error {
if c.Statefulset == nil {
return fmt.Errorf("there is no statefulset in the cluster")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ func (c *Cluster) credentialSecretNameForCluster(username string, clusterName st
return c.OpConfig.SecretNameTemplate.Format(
"username", strings.Replace(username, "_", "-", -1),
"clustername", clusterName,
"tprkind", constants.TPRKind,
"tprgroup", constants.TPRGroup)
"tprkind", constants.CRDKind,
"tprgroup", constants.CRDGroup)
}

func (c *Cluster) podSpiloRole(pod *v1.Pod) string {
Expand Down
16 changes: 5 additions & 11 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"

"github.com/zalando-incubator/postgres-operator/pkg/apiserver"
Expand All @@ -27,7 +26,6 @@ type Controller struct {

logger *logrus.Entry
KubeClient k8sutil.KubernetesClient
RestClient rest.Interface // kubernetes API group REST client
apiserver *apiserver.Server

stopCh chan struct{}
Expand Down Expand Up @@ -69,15 +67,11 @@ func NewController(controllerConfig *spec.ControllerConfig) *Controller {
}

func (c *Controller) initClients() {
client, err := k8sutil.ClientSet(c.config.RestConfig)
if err != nil {
c.logger.Fatalf("couldn't create client: %v", err)
}
c.KubeClient = k8sutil.NewFromKubernetesInterface(client)
var err error

c.RestClient, err = k8sutil.KubernetesRestClient(*c.config.RestConfig)
c.KubeClient, err = k8sutil.NewFromConfig(c.config.RestConfig)
if err != nil {
c.logger.Fatalf("couldn't create rest client: %v", err)
c.logger.Fatalf("could not create kubernetes clients: %v", err)
}
}

Expand Down Expand Up @@ -119,8 +113,8 @@ func (c *Controller) initController() {
c.logger.Logger.Level = logrus.DebugLevel
}

if err := c.createTPR(); err != nil {
c.logger.Fatalf("could not register ThirdPartyResource: %v", err)
if err := c.createCRD(); err != nil {
c.logger.Fatalf("could not register CustomResourceDefinition: %v", err)
}

if infraRoles, err := c.getInfrastructureRoles(&c.opConfig.InfrastructureRolesSecretName); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ func (c *Controller) clusterListFunc(options metav1.ListOptions) (runtime.Object
var list spec.PostgresqlList
var activeClustersCnt, failedClustersCnt int

req := c.RestClient.
req := c.KubeClient.CRDREST.
Get().
Namespace(c.opConfig.Namespace).
Resource(constants.ResourceName).
Resource(constants.CRDResource).
VersionedParams(&options, metav1.ParameterCodec)

b, err := req.DoRaw()
Expand Down Expand Up @@ -109,10 +109,10 @@ func (d *tprDecoder) Decode() (action watch.EventType, object runtime.Object, er

func (c *Controller) clusterWatchFunc(options metav1.ListOptions) (watch.Interface, error) {
options.Watch = true
r, err := c.RestClient.
r, err := c.KubeClient.CRDREST.
Get().
Namespace(c.opConfig.Namespace).
Resource(constants.ResourceName).
Resource(constants.CRDResource).
VersionedParams(&options, metav1.ParameterCodec).
FieldsSelectorParam(nil).
Stream()
Expand Down
66 changes: 45 additions & 21 deletions pkg/controller/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"fmt"
"hash/crc32"

apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/pkg/api/v1"
extv1beta "k8s.io/client-go/pkg/apis/extensions/v1beta1"

"github.com/zalando-incubator/postgres-operator/pkg/cluster"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
Expand All @@ -28,36 +29,59 @@ func (c *Controller) makeClusterConfig() cluster.Config {
}
}

func thirdPartyResource(TPRName string) *extv1beta.ThirdPartyResource {
return &extv1beta.ThirdPartyResource{
ObjectMeta: metav1.ObjectMeta{
//ThirdPartyResources are cluster-wide
Name: TPRName,
},
Versions: []extv1beta.APIVersion{
{Name: constants.TPRApiVersion},
},
Description: constants.TPRDescription,
}
}

func (c *Controller) clusterWorkerID(clusterName spec.NamespacedName) uint32 {
return crc32.ChecksumIEEE([]byte(clusterName.String())) % c.opConfig.Workers
}

func (c *Controller) createTPR() error {
tpr := thirdPartyResource(constants.TPRName)
func (c *Controller) createCRD() error {
crd := &apiextv1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: constants.CRDResource + "." + constants.CRDGroup,
},
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
Group: constants.CRDGroup,
Version: constants.CRDApiVersion,
Names: apiextv1beta1.CustomResourceDefinitionNames{
Plural: constants.CRDResource,
Singular: constants.CRDKind,
ShortNames: []string{constants.CRDShort},
Kind: constants.CRDKind,
ListKind: constants.CRDKind + "List",
},
Scope: apiextv1beta1.NamespaceScoped,
},
}

if _, err := c.KubeClient.ThirdPartyResources().Create(tpr); err != nil {
if _, err := c.KubeClient.CustomResourceDefinitions().Create(crd); err != nil {
if !k8sutil.ResourceAlreadyExists(err) {
return err
return fmt.Errorf("could not create customResourceDefinition: %v", err)
}
c.logger.Infof("thirdPartyResource %q is already registered", constants.TPRName)
c.logger.Infof("customResourceDefinition %q is already registered", crd.Name)
} else {
c.logger.Infof("thirdPartyResource %q' has been registered", constants.TPRName)
c.logger.Infof("customResourceDefinition %q has been registered", crd.Name)
}

return k8sutil.WaitTPRReady(c.RestClient, c.opConfig.TPR.ReadyWaitInterval, c.opConfig.TPR.ReadyWaitTimeout, c.opConfig.Namespace)
return wait.Poll(c.opConfig.CRD.ReadyWaitInterval, c.opConfig.CRD.ReadyWaitTimeout, func() (bool, error) {
c, err := c.KubeClient.CustomResourceDefinitions().Get(crd.Name, metav1.GetOptions{})
if err != nil {
return false, err
}

for _, cond := range c.Status.Conditions {
switch cond.Type {
case apiextv1beta1.Established:
if cond.Status == apiextv1beta1.ConditionTrue {
return true, err
}
case apiextv1beta1.NamesAccepted:
if cond.Status == apiextv1beta1.ConditionFalse {
return false, fmt.Errorf("name conflict: %v", cond.Reason)
}
}
}

return false, err
})
}

func (c *Controller) getInfrastructureRoles(rolesSecret *spec.NamespacedName) (result map[string]spec.PgUser, err error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/util/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/spec"
)

// TPR describes ThirdPartyResource specific configuration parameters
type TPR struct {
// CRD describes CustomResourceDefinition specific configuration parameters
type CRD struct {
ReadyWaitInterval time.Duration `name:"ready_wait_interval" default:"4s"`
ReadyWaitTimeout time.Duration `name:"ready_wait_timeout" default:"30s"`
ResyncPeriod time.Duration `name:"resync_period" default:"5m"`
Expand Down Expand Up @@ -44,7 +44,7 @@ type Auth struct {

// Config describes operator config
type Config struct {
TPR
CRD
Resources
Auth
Namespace string `name:"namespace"`
Expand Down
11 changes: 5 additions & 6 deletions pkg/util/constants/thirdpartyresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package constants

// Different properties of the PostgreSQL Third Party Resources
const (
TPRKind = "postgresql"
TPRGroup = "acid.zalan.do"
TPRDescription = "Managed PostgreSQL clusters"
TPRApiVersion = "v1"
TPRName = TPRKind + "." + TPRGroup
ResourceName = TPRKind + "s"
CRDKind = "postgresql"
CRDResource = "postgresqls"
CRDShort = "pg"
CRDGroup = "acid.zalan.do"
CRDApiVersion = "v1"
)

0 comments on commit cc9a55a

Please sign in to comment.