Update the sample configuratin, fix most unmarshaling issues.

This commit is contained in:
Oleksii Kliukin 2018-06-13 17:12:52 +02:00
parent 6bebb87d8f
commit 144c08695b
5 changed files with 57 additions and 20 deletions

View File

@ -70,7 +70,7 @@ configuration:
scalyr: scalyr:
scalyr_cpu_request: 100m scalyr_cpu_request: 100m
scalyr_memory_request: 50Mi scalyr_memory_request: 50Mi
scalyr_cpu_limit: 1 scalyr_cpu_limit: "1"
scalyr_memory_limit: 1Gi scalyr_memory_limit: 1Gi
# scalyr_api_key: "" # scalyr_api_key: ""
# scalyr_image: "" # scalyr_image: ""

View File

@ -7,6 +7,7 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/util/config" "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/constants"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"time"
) )
func (c *Controller) readOperatorConfigurationFromCRD(configObjectNamespace, configObjectName string) (*config.OperatorConfiguration, error) { func (c *Controller) readOperatorConfigurationFromCRD(configObjectNamespace, configObjectName string) (*config.OperatorConfiguration, error) {
@ -40,14 +41,14 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *config.OperatorConfigur
result.Workers = fromCRD.Workers result.Workers = fromCRD.Workers
result.MinInstances = fromCRD.MinInstances result.MinInstances = fromCRD.MinInstances
result.MaxInstances = fromCRD.MaxInstances result.MaxInstances = fromCRD.MaxInstances
result.ResyncPeriod = fromCRD.ResyncPeriod result.ResyncPeriod = time.Duration(fromCRD.ResyncPeriod)
result.SuperUsername = fromCRD.PostgresUsersConfiguration.SuperUsername result.SuperUsername = fromCRD.PostgresUsersConfiguration.SuperUsername
result.ReplicationUsername = fromCRD.PostgresUsersConfiguration.ReplicationUsername result.ReplicationUsername = fromCRD.PostgresUsersConfiguration.ReplicationUsername
result.PodServiceAccountName = fromCRD.Kubernetes.PodServiceAccountName result.PodServiceAccountName = fromCRD.Kubernetes.PodServiceAccountName
result.PodServiceAccountDefinition = fromCRD.Kubernetes.PodServiceAccountDefinition result.PodServiceAccountDefinition = fromCRD.Kubernetes.PodServiceAccountDefinition
result.PodTerminateGracePeriod = fromCRD.Kubernetes.PodTerminateGracePeriod result.PodTerminateGracePeriod = time.Duration(fromCRD.Kubernetes.PodTerminateGracePeriod)
result.WatchedNamespace = fromCRD.Kubernetes.WatchedNamespace result.WatchedNamespace = fromCRD.Kubernetes.WatchedNamespace
result.PDBNameFormat = fromCRD.Kubernetes.PDBNameFormat result.PDBNameFormat = fromCRD.Kubernetes.PDBNameFormat
result.SecretNameTemplate = fromCRD.Kubernetes.SecretNameTemplate result.SecretNameTemplate = fromCRD.Kubernetes.SecretNameTemplate
@ -63,12 +64,12 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *config.OperatorConfigur
result.DefaultCPULimit = fromCRD.PostgresPodResources.DefaultCPULimit result.DefaultCPULimit = fromCRD.PostgresPodResources.DefaultCPULimit
result.DefaultMemoryLimit = fromCRD.PostgresPodResources.DefaultMemoryLimit result.DefaultMemoryLimit = fromCRD.PostgresPodResources.DefaultMemoryLimit
result.ResourceCheckInterval = fromCRD.Timeouts.ResourceCheckInterval result.ResourceCheckInterval = time.Duration(fromCRD.Timeouts.ResourceCheckInterval)
result.ResourceCheckTimeout = fromCRD.Timeouts.ResourceCheckTimeout result.ResourceCheckTimeout = time.Duration(fromCRD.Timeouts.ResourceCheckTimeout)
result.PodLabelWaitTimeout = fromCRD.Timeouts.PodLabelWaitTimeout result.PodLabelWaitTimeout = time.Duration(fromCRD.Timeouts.PodLabelWaitTimeout)
result.PodDeletionWaitTimeout = fromCRD.Timeouts.PodDeletionWaitTimeout result.PodDeletionWaitTimeout = time.Duration(fromCRD.Timeouts.PodDeletionWaitTimeout)
result.ReadyWaitInterval = fromCRD.Timeouts.ReadyWaitInterval result.ReadyWaitInterval = time.Duration(fromCRD.Timeouts.ReadyWaitInterval)
result.ReadyWaitTimeout = fromCRD.Timeouts.ReadyWaitTimeout result.ReadyWaitTimeout = time.Duration(fromCRD.Timeouts.ReadyWaitTimeout)
result.DbHostedZone = fromCRD.LoadBalancer.DbHostedZone result.DbHostedZone = fromCRD.LoadBalancer.DbHostedZone
result.EnableMasterLoadBalancer = fromCRD.LoadBalancer.EnableMasterLoadBalancer result.EnableMasterLoadBalancer = fromCRD.LoadBalancer.EnableMasterLoadBalancer

View File

@ -190,7 +190,7 @@ func (m *MaintenanceWindow) MarshalJSON() ([]byte, error) {
m.EndTime.Format("15:04"))), nil m.EndTime.Format("15:04"))), nil
} }
// UnmarshalJSON convets a JSON to the maintenance window definition. // UnmarshalJSON converts a JSON to the maintenance window definition.
func (m *MaintenanceWindow) UnmarshalJSON(data []byte) error { func (m *MaintenanceWindow) UnmarshalJSON(data []byte) error {
var ( var (
got MaintenanceWindow got MaintenanceWindow

View File

@ -15,6 +15,7 @@ import (
"k8s.io/client-go/pkg/apis/apps/v1beta1" "k8s.io/client-go/pkg/apis/apps/v1beta1"
policyv1beta1 "k8s.io/client-go/pkg/apis/policy/v1beta1" policyv1beta1 "k8s.io/client-go/pkg/apis/policy/v1beta1"
"k8s.io/client-go/rest" "k8s.io/client-go/rest"
"encoding/json"
) )
// EventType contains type of the events for the TPRs and Pods received from Kubernetes // EventType contains type of the events for the TPRs and Pods received from Kubernetes
@ -187,6 +188,15 @@ func (n *NamespacedName) Decode(value string) error {
return n.DecodeWorker(value, GetOperatorNamespace()) return n.DecodeWorker(value, GetOperatorNamespace())
} }
func (n *NamespacedName) UnmarshalJSON(data []byte) error {
result := NamespacedName{}
if err := result.Decode(string(data)); err != nil {
return err
}
*n = result
return nil
}
// DecodeWorker separates the decode logic to (unit) test // DecodeWorker separates the decode logic to (unit) test
// from obtaining the operator namespace that depends on k8s mounting files at runtime // from obtaining the operator namespace that depends on k8s mounting files at runtime
func (n *NamespacedName) DecodeWorker(value, operatorNamespace string) error { func (n *NamespacedName) DecodeWorker(value, operatorNamespace string) error {
@ -237,3 +247,31 @@ func GetOperatorNamespace() string {
} }
return operatorNamespace return operatorNamespace
} }
type Duration time.Duration
func (d *Duration) UnmarshalJSON(b []byte) error {
var (
v interface{}
err error
)
if err = json.Unmarshal(b, &v); err != nil {
return err
}
switch val := v.(type) {
case string:
t, err := time.ParseDuration(val);
if err != nil {
return err
}
*d = Duration(t)
return nil
case float64:
t := time.Duration(val)
*d = Duration(t)
return nil
default:
return fmt.Errorf("could not recognize type %T as a valid type to unmarshal to Duration", val)
}
return nil
}

View File

@ -2,8 +2,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/zalando-incubator/postgres-operator/pkg/spec" "github.com/zalando-incubator/postgres-operator/pkg/spec"
@ -33,7 +31,7 @@ type KubernetesMetaConfiguration struct {
PodServiceAccountName string `json:"pod_service_account_name,omitempty"` PodServiceAccountName string `json:"pod_service_account_name,omitempty"`
// TODO: change it to the proper json // TODO: change it to the proper json
PodServiceAccountDefinition string `json:"pod_service_account_definition,omitempty"` PodServiceAccountDefinition string `json:"pod_service_account_definition,omitempty"`
PodTerminateGracePeriod time.Duration `json:"pod_terminate_grace_period,omitempty"` PodTerminateGracePeriod spec.Duration `json:"pod_terminate_grace_period,omitempty"`
WatchedNamespace string `json:"watched_namespace,omitempty"` WatchedNamespace string `json:"watched_namespace,omitempty"`
PDBNameFormat StringTemplate `json:"pdb_name_format,omitempty"` PDBNameFormat StringTemplate `json:"pdb_name_format,omitempty"`
SecretNameTemplate StringTemplate `json:"secret_name_template,omitempty"` SecretNameTemplate StringTemplate `json:"secret_name_template,omitempty"`
@ -57,12 +55,12 @@ type PostgresPodResourcesDefaults struct {
} }
type OperatorTimeouts struct { type OperatorTimeouts struct {
ResourceCheckInterval time.Duration `json:"resource_check_interval,omitempty"` ResourceCheckInterval spec.Duration `json:"resource_check_interval,omitempty"`
ResourceCheckTimeout time.Duration `json:"resource_check_timeout,omitempty"` ResourceCheckTimeout spec.Duration `json:"resource_check_timeout,omitempty"`
PodLabelWaitTimeout time.Duration `json:"pod_label_wait_timeout,omitempty"` PodLabelWaitTimeout spec.Duration `json:"pod_label_wait_timeout,omitempty"`
PodDeletionWaitTimeout time.Duration `json:"pod_deletion_wait_timeout,omitempty"` PodDeletionWaitTimeout spec.Duration `json:"pod_deletion_wait_timeout,omitempty"`
ReadyWaitInterval time.Duration `json:"ready_wait_interval,omitempty"` ReadyWaitInterval spec.Duration `json:"ready_wait_interval,omitempty"`
ReadyWaitTimeout time.Duration `json:"ready_wait_timeout,omitempty"` ReadyWaitTimeout spec.Duration `json:"ready_wait_timeout,omitempty"`
} }
type LoadBalancerConfiguration struct { type LoadBalancerConfiguration struct {
@ -117,7 +115,7 @@ type OperatorConfigurationData struct {
Workers uint32 `json:"workers,omitempty"` Workers uint32 `json:"workers,omitempty"`
MinInstances int32 `json:"min_instances,omitempty"` MinInstances int32 `json:"min_instances,omitempty"`
MaxInstances int32 `json:"max_instances,omitempty"` MaxInstances int32 `json:"max_instances,omitempty"`
ResyncPeriod time.Duration `json:"resync_period,omitempty"` ResyncPeriod spec.Duration `json:"resync_period,omitempty"`
PostgresUsersConfiguration PostgresUsersConfiguration `json:"users"` PostgresUsersConfiguration PostgresUsersConfiguration `json:"users"`
Kubernetes KubernetesMetaConfiguration `json:"kubernetes"` Kubernetes KubernetesMetaConfiguration `json:"kubernetes"`
PostgresPodResources PostgresPodResourcesDefaults `json:"postgres_pod_resources"` PostgresPodResources PostgresPodResourcesDefaults `json:"postgres_pod_resources"`