mirror of
https://github.com/zalando/postgres-operator.git
synced 2026-09-30 10:52:01 +02:00
Get config from environment variables;
ignore pg major version change; get rid of resources package;
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/cluster"
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/spec"
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/util/constants"
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/util/config"
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/util/teams"
|
||||
)
|
||||
|
||||
@@ -25,7 +25,8 @@ type Config struct {
|
||||
}
|
||||
|
||||
type Controller struct {
|
||||
config Config
|
||||
Config
|
||||
opConfig *config.Config
|
||||
logger *logrus.Entry
|
||||
clusters map[spec.ClusterName]*cluster.Cluster
|
||||
stopChMap map[spec.ClusterName]chan struct{}
|
||||
@@ -37,9 +38,10 @@ type Controller struct {
|
||||
podCh chan spec.PodEvent
|
||||
}
|
||||
|
||||
func New(cfg *Config) *Controller {
|
||||
func New(controllerConfig *Config, operatorConfig *config.Config) *Controller {
|
||||
return &Controller{
|
||||
config: *cfg,
|
||||
Config: *controllerConfig,
|
||||
opConfig: operatorConfig,
|
||||
logger: logrus.WithField("pkg", "controller"),
|
||||
clusters: make(map[spec.ClusterName]*cluster.Cluster),
|
||||
stopChMap: make(map[spec.ClusterName]chan struct{}),
|
||||
@@ -52,12 +54,12 @@ func (c *Controller) Run(stopCh <-chan struct{}, wg *sync.WaitGroup) {
|
||||
wg.Add(1)
|
||||
|
||||
c.initController()
|
||||
if err := c.initEtcdClient(); err != nil {
|
||||
if err := c.initEtcdClient(c.opConfig.EtcdHost); err != nil {
|
||||
c.logger.Errorf("Can't get etcd client: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
c.logger.Infof("'%s' namespace will be watched", c.config.PodNamespace)
|
||||
c.logger.Infof("'%s' namespace will be watched", c.PodNamespace)
|
||||
go c.runInformers(stopCh)
|
||||
|
||||
c.logger.Info("Started working in background")
|
||||
@@ -68,7 +70,7 @@ func (c *Controller) initController() {
|
||||
c.logger.Fatalf("Can't register ThirdPartyResource: %s", err)
|
||||
}
|
||||
|
||||
c.config.TeamsAPIClient.RefreshTokenAction = c.getOAuthToken
|
||||
c.TeamsAPIClient.RefreshTokenAction = c.getOAuthToken
|
||||
|
||||
// Postgresqls
|
||||
clusterLw := &cache.ListWatch{
|
||||
@@ -78,7 +80,7 @@ func (c *Controller) initController() {
|
||||
c.postgresqlInformer = cache.NewSharedIndexInformer(
|
||||
clusterLw,
|
||||
&spec.Postgresql{},
|
||||
constants.ResyncPeriodTPR,
|
||||
c.opConfig.ResyncPeriod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
|
||||
c.postgresqlInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
@@ -96,7 +98,7 @@ func (c *Controller) initController() {
|
||||
c.podInformer = cache.NewSharedIndexInformer(
|
||||
podLw,
|
||||
&v1.Pod{},
|
||||
constants.ResyncPeriodPod,
|
||||
c.opConfig.ResyncPeriodPod,
|
||||
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
|
||||
|
||||
c.podInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
|
||||
@@ -4,12 +4,11 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/util/constants"
|
||||
etcdclient "github.com/coreos/etcd/client"
|
||||
)
|
||||
|
||||
func (c *Controller) initEtcdClient() error {
|
||||
etcdUrl := fmt.Sprintf("http://%s", constants.EtcdHost)
|
||||
func (c *Controller) initEtcdClient(etcdHost string) error {
|
||||
etcdUrl := fmt.Sprintf("http://%s", etcdHost)
|
||||
|
||||
cfg, err := etcdclient.New(etcdclient.Config{
|
||||
Endpoints: []string{etcdUrl},
|
||||
@@ -20,7 +19,7 @@ func (c *Controller) initEtcdClient() error {
|
||||
return err
|
||||
}
|
||||
|
||||
c.config.EtcdClient = etcdclient.NewKeysAPI(cfg)
|
||||
c.EtcdClient = etcdclient.NewKeysAPI(cfg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func (c *Controller) podListFunc(options api.ListOptions) (runtime.Object, error
|
||||
TimeoutSeconds: options.TimeoutSeconds,
|
||||
}
|
||||
|
||||
return c.config.KubeClient.CoreV1().Pods(c.config.PodNamespace).List(opts)
|
||||
return c.KubeClient.CoreV1().Pods(c.PodNamespace).List(opts)
|
||||
}
|
||||
|
||||
func (c *Controller) podWatchFunc(options api.ListOptions) (watch.Interface, error) {
|
||||
@@ -52,7 +52,7 @@ func (c *Controller) podWatchFunc(options api.ListOptions) (watch.Interface, err
|
||||
TimeoutSeconds: options.TimeoutSeconds,
|
||||
}
|
||||
|
||||
return c.config.KubeClient.CoreV1Client.Pods(c.config.PodNamespace).Watch(opts)
|
||||
return c.KubeClient.CoreV1Client.Pods(c.PodNamespace).Watch(opts)
|
||||
}
|
||||
|
||||
func PodNameFromMeta(meta v1.ObjectMeta) spec.PodName {
|
||||
|
||||
@@ -18,8 +18,8 @@ import (
|
||||
|
||||
func (c *Controller) clusterListFunc(options api.ListOptions) (runtime.Object, error) {
|
||||
c.logger.Info("Getting list of currently running clusters")
|
||||
object, err := c.config.RestClient.Get().
|
||||
Namespace(c.config.PodNamespace).
|
||||
object, err := c.RestClient.Get().
|
||||
Namespace(c.PodNamespace).
|
||||
Resource(constants.ResourceName).
|
||||
VersionedParams(&options, api.ParameterCodec).
|
||||
FieldsSelectorParam(fields.Everything()).
|
||||
@@ -67,9 +67,9 @@ func (c *Controller) clusterListFunc(options api.ListOptions) (runtime.Object, e
|
||||
}
|
||||
|
||||
func (c *Controller) clusterWatchFunc(options api.ListOptions) (watch.Interface, error) {
|
||||
return c.config.RestClient.Get().
|
||||
return c.RestClient.Get().
|
||||
Prefix("watch").
|
||||
Namespace(c.config.PodNamespace).
|
||||
Namespace(c.PodNamespace).
|
||||
Resource(constants.ResourceName).
|
||||
VersionedParams(&options, api.ParameterCodec).
|
||||
FieldsSelectorParam(fields.Everything()).
|
||||
@@ -128,7 +128,6 @@ func (c *Controller) postgresqlUpdate(prev, cur interface{}) {
|
||||
}
|
||||
|
||||
//TODO: Do not update cluster which is currently creating
|
||||
|
||||
if pgPrev.Metadata.ResourceVersion == pgNew.Metadata.ResourceVersion {
|
||||
c.logger.Infof("Skipping update with no resource version change")
|
||||
return
|
||||
|
||||
+27
-11
@@ -3,25 +3,28 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/client-go/pkg/api"
|
||||
"k8s.io/client-go/pkg/api/v1"
|
||||
extv1beta "k8s.io/client-go/pkg/apis/extensions/v1beta1"
|
||||
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/cluster"
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/util/constants"
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/util/k8sutil"
|
||||
"github.bus.zalan.do/acid/postgres-operator/pkg/util/resources"
|
||||
"k8s.io/client-go/pkg/api"
|
||||
)
|
||||
|
||||
func (c *Controller) makeClusterConfig() cluster.Config {
|
||||
return cluster.Config{
|
||||
KubeClient: c.config.KubeClient,
|
||||
RestClient: c.config.RestClient,
|
||||
EtcdClient: c.config.EtcdClient,
|
||||
TeamsAPIClient: c.config.TeamsAPIClient,
|
||||
KubeClient: c.KubeClient,
|
||||
RestClient: c.RestClient,
|
||||
EtcdClient: c.EtcdClient,
|
||||
TeamsAPIClient: c.TeamsAPIClient,
|
||||
OpConfig: c.opConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) getOAuthToken() (string, error) {
|
||||
// Temporary getting postgresql-operator secret from the NamespaceDefault
|
||||
credentialsSecret, err := c.config.KubeClient.Secrets(api.NamespaceDefault).Get(constants.OAuthTokenSecretName)
|
||||
credentialsSecret, err := c.KubeClient.Secrets(api.NamespaceDefault).Get(c.opConfig.OAuthTokenSecretName)
|
||||
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("Can't get credentials Secret: %s", err)
|
||||
@@ -35,11 +38,24 @@ func (c *Controller) getOAuthToken() (string, error) {
|
||||
return string(data["read-only-token-secret"]), nil
|
||||
}
|
||||
|
||||
func thirdPartyResource(TPRName string) *extv1beta.ThirdPartyResource {
|
||||
return &extv1beta.ThirdPartyResource{
|
||||
ObjectMeta: v1.ObjectMeta{
|
||||
//ThirdPartyResources are cluster-wide
|
||||
Name: TPRName,
|
||||
},
|
||||
Versions: []extv1beta.APIVersion{
|
||||
{Name: constants.TPRApiVersion},
|
||||
},
|
||||
Description: constants.TPRDescription,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) createTPR() error {
|
||||
TPRName := fmt.Sprintf("%s.%s", constants.TPRName, constants.TPRVendor)
|
||||
tpr := resources.ThirdPartyResource(TPRName)
|
||||
tpr := thirdPartyResource(TPRName)
|
||||
|
||||
_, err := c.config.KubeClient.ExtensionsV1beta1().ThirdPartyResources().Create(tpr)
|
||||
_, err := c.KubeClient.ExtensionsV1beta1().ThirdPartyResources().Create(tpr)
|
||||
if err != nil {
|
||||
if !k8sutil.ResourceAlreadyExists(err) {
|
||||
return err
|
||||
@@ -50,7 +66,7 @@ func (c *Controller) createTPR() error {
|
||||
c.logger.Infof("ThirdPartyResource '%s' has been registered", TPRName)
|
||||
}
|
||||
|
||||
restClient := c.config.RestClient
|
||||
restClient := c.RestClient
|
||||
|
||||
return k8sutil.WaitTPRReady(restClient, constants.TPRReadyWaitInterval, constants.TPRReadyWaitTimeout, c.config.PodNamespace)
|
||||
return k8sutil.WaitTPRReady(restClient, c.opConfig.TPR.ReadyWaitInterval, c.opConfig.TPR.ReadyWaitTimeout, c.PodNamespace)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user