Respond to code review

This commit is contained in:
Sergey Dudoladov
2018-02-20 14:43:02 +01:00
parent b1fae716b1
commit dcfc9925f6
7 changed files with 40 additions and 63 deletions
+25 -44
View File
@@ -14,6 +14,7 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/apiserver"
"github.com/zalando-incubator/postgres-operator/pkg/cluster"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"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"
"github.com/zalando-incubator/postgres-operator/pkg/util/k8sutil"
@@ -97,41 +98,7 @@ func (c *Controller) initOperatorConfig() {
c.logger.Infoln("no ConfigMap specified. Loading default values")
}
watchedNsConfigMapVar, isPresentInOperatorConfigMap := configMapData["watched_namespace"]
watchedNsEnvVar, isPresentInOperatorEnv := os.LookupEnv("WATCHED_NAMESPACE")
if (!isPresentInOperatorConfigMap) && (!isPresentInOperatorEnv) {
c.logger.Infof("No namespace to watch specified. By convention, the operator falls back to watching the namespace it is deployed to: '%v' \n", spec.GetOperatorNamespace())
configMapData["watched_namespace"] = spec.GetOperatorNamespace()
}
if (isPresentInOperatorConfigMap) && (!isPresentInOperatorEnv) {
// explicitly specify the policy for handling all namespaces
// note that '*' is not a valid namespace name
if watchedNsConfigMapVar == "*" {
c.logger.Infof("The watched namespace field in the operator config map evaluates to '*', meaning watching all namespaces.\n")
configMapData["watched_namespace"] = v1.NamespaceAll
}
}
if isPresentInOperatorEnv {
if isPresentInOperatorConfigMap {
c.logger.Infof("Both WATCHED_NAMESPACE=%q env var and wacthed_namespace=%q field in operator config map are defined. The env variable takes priority over the configMap param\n", watchedNsEnvVar, watchedNsConfigMapVar)
}
// handle all namespaces consistently
if watchedNsEnvVar == "*" {
c.logger.Infof("The watched namespace field in the operator config map evaluates to '*', meaning watching all namespaces.\n")
configMapData["watched_namespace"] = v1.NamespaceAll
} else {
c.logger.Infof("Watch the %q namespace specified in the env variable WATCHED_NAMESPACE\n", watchedNsEnvVar)
configMapData["watched_namespace"] = watchedNsEnvVar
}
}
configMapData["watched_namespace"] = c.getEffectiveNamespace(os.Getenv("WATCHED_NAMESPACE"), configMapData["watched_namespace"])
if c.config.NoDatabaseAccess {
configMapData["enable_database_access"] = "false"
@@ -152,15 +119,6 @@ func (c *Controller) initController() {
c.initClients()
c.initOperatorConfig()
// earliest point where we can check if the namespace to watch actually exists
if c.opConfig.WatchedNamespace != v1.NamespaceAll {
_, err := c.KubeClient.Namespaces().Get(c.opConfig.WatchedNamespace, metav1.GetOptions{})
if err != nil {
c.logger.Fatalf("Operator was told to watch the %q namespace but was unable to find it via Kubernetes API.", c.opConfig.WatchedNamespace)
}
}
c.initSharedInformers()
c.logger.Infof("config: %s", c.opConfig.MustMarshal())
@@ -290,3 +248,26 @@ func (c *Controller) kubeNodesInformer(stopCh <-chan struct{}, wg *sync.WaitGrou
c.nodesInformer.Run(stopCh)
}
func (c *Controller) getEffectiveNamespace(namespaceFromEnvironment, namespaceFromConfigMap string) string {
namespace := util.Coalesce(namespaceFromEnvironment, util.Coalesce(namespaceFromConfigMap, spec.GetOperatorNamespace()))
if namespace == "*" {
namespace = v1.NamespaceAll
c.logger.Infof("Listening to all namespaces")
} else {
if _, err := c.KubeClient.Namespaces().Get(namespace, metav1.GetOptions{}); err != nil {
// the namespace may be created manually at runtime
c.logger.Warnf("Could not find the watched namespace %q", namespace)
} else {
c.logger.Infof("Listenting to the specific namespace %q", namespace)
}
}
return namespace
}
-12
View File
@@ -16,10 +16,6 @@ import (
// ClusterStatus provides status of the cluster
func (c *Controller) ClusterStatus(team, namespace, cluster string) (*spec.ClusterStatus, error) {
if namespace == "" {
namespace = c.opConfig.WatchedNamespace
}
clusterName := spec.NamespacedName{
Namespace: namespace,
Name: team + "-" + cluster,
@@ -97,10 +93,6 @@ func (c *Controller) GetStatus() *spec.ControllerStatus {
// ClusterLogs dumps cluster ring logs
func (c *Controller) ClusterLogs(team, namespace, name string) ([]*spec.LogEntry, error) {
if namespace == "" {
namespace = c.opConfig.WatchedNamespace
}
clusterName := spec.NamespacedName{
Namespace: namespace,
Name: team + "-" + name,
@@ -224,10 +216,6 @@ func (c *Controller) WorkerStatus(workerID uint32) (*spec.WorkerStatus, error) {
// ClusterHistory dumps history of cluster changes
func (c *Controller) ClusterHistory(team, namespace, name string) ([]*spec.Diff, error) {
if namespace == "" {
namespace = c.opConfig.WatchedNamespace
}
clusterName := spec.NamespacedName{
Namespace: namespace,
Name: team + "-" + name,