diff --git a/manifests/postgresql-operator-default-configuration.yaml b/manifests/postgresql-operator-default-configuration.yaml index 7278544c0..cce5763bd 100644 --- a/manifests/postgresql-operator-default-configuration.yaml +++ b/manifests/postgresql-operator-default-configuration.yaml @@ -70,7 +70,7 @@ configuration: scalyr: scalyr_cpu_request: 100m scalyr_memory_request: 50Mi - scalyr_cpu_limit: 1 + scalyr_cpu_limit: "1" scalyr_memory_limit: 1Gi # scalyr_api_key: "" # scalyr_image: "" diff --git a/pkg/controller/operator_config.go b/pkg/controller/operator_config.go index f6bf5cc59..384b71946 100644 --- a/pkg/controller/operator_config.go +++ b/pkg/controller/operator_config.go @@ -7,6 +7,7 @@ import ( "github.com/zalando-incubator/postgres-operator/pkg/util/config" "github.com/zalando-incubator/postgres-operator/pkg/util/constants" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "time" ) 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.MinInstances = fromCRD.MinInstances result.MaxInstances = fromCRD.MaxInstances - result.ResyncPeriod = fromCRD.ResyncPeriod + result.ResyncPeriod = time.Duration(fromCRD.ResyncPeriod) result.SuperUsername = fromCRD.PostgresUsersConfiguration.SuperUsername result.ReplicationUsername = fromCRD.PostgresUsersConfiguration.ReplicationUsername result.PodServiceAccountName = fromCRD.Kubernetes.PodServiceAccountName result.PodServiceAccountDefinition = fromCRD.Kubernetes.PodServiceAccountDefinition - result.PodTerminateGracePeriod = fromCRD.Kubernetes.PodTerminateGracePeriod + result.PodTerminateGracePeriod = time.Duration(fromCRD.Kubernetes.PodTerminateGracePeriod) result.WatchedNamespace = fromCRD.Kubernetes.WatchedNamespace result.PDBNameFormat = fromCRD.Kubernetes.PDBNameFormat result.SecretNameTemplate = fromCRD.Kubernetes.SecretNameTemplate @@ -63,12 +64,12 @@ func (c *Controller) importConfigurationFromCRD(fromCRD *config.OperatorConfigur result.DefaultCPULimit = fromCRD.PostgresPodResources.DefaultCPULimit result.DefaultMemoryLimit = fromCRD.PostgresPodResources.DefaultMemoryLimit - result.ResourceCheckInterval = fromCRD.Timeouts.ResourceCheckInterval - result.ResourceCheckTimeout = fromCRD.Timeouts.ResourceCheckTimeout - result.PodLabelWaitTimeout = fromCRD.Timeouts.PodLabelWaitTimeout - result.PodDeletionWaitTimeout = fromCRD.Timeouts.PodDeletionWaitTimeout - result.ReadyWaitInterval = fromCRD.Timeouts.ReadyWaitInterval - result.ReadyWaitTimeout = fromCRD.Timeouts.ReadyWaitTimeout + result.ResourceCheckInterval = time.Duration(fromCRD.Timeouts.ResourceCheckInterval) + result.ResourceCheckTimeout = time.Duration(fromCRD.Timeouts.ResourceCheckTimeout) + result.PodLabelWaitTimeout = time.Duration(fromCRD.Timeouts.PodLabelWaitTimeout) + result.PodDeletionWaitTimeout = time.Duration(fromCRD.Timeouts.PodDeletionWaitTimeout) + result.ReadyWaitInterval = time.Duration(fromCRD.Timeouts.ReadyWaitInterval) + result.ReadyWaitTimeout = time.Duration(fromCRD.Timeouts.ReadyWaitTimeout) result.DbHostedZone = fromCRD.LoadBalancer.DbHostedZone result.EnableMasterLoadBalancer = fromCRD.LoadBalancer.EnableMasterLoadBalancer diff --git a/pkg/spec/postgresql.go b/pkg/spec/postgresql.go index 5b7a108d8..5b47c2523 100644 --- a/pkg/spec/postgresql.go +++ b/pkg/spec/postgresql.go @@ -190,7 +190,7 @@ func (m *MaintenanceWindow) MarshalJSON() ([]byte, error) { 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 { var ( got MaintenanceWindow diff --git a/pkg/spec/types.go b/pkg/spec/types.go index ec870251a..5a5e54799 100644 --- a/pkg/spec/types.go +++ b/pkg/spec/types.go @@ -15,6 +15,7 @@ import ( "k8s.io/client-go/pkg/apis/apps/v1beta1" policyv1beta1 "k8s.io/client-go/pkg/apis/policy/v1beta1" "k8s.io/client-go/rest" + "encoding/json" ) // 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()) } +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 // from obtaining the operator namespace that depends on k8s mounting files at runtime func (n *NamespacedName) DecodeWorker(value, operatorNamespace string) error { @@ -237,3 +247,31 @@ func GetOperatorNamespace() string { } 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 +} \ No newline at end of file diff --git a/pkg/util/config/crd_config.go b/pkg/util/config/crd_config.go index a957164e0..88d3c69ce 100644 --- a/pkg/util/config/crd_config.go +++ b/pkg/util/config/crd_config.go @@ -2,8 +2,6 @@ package config import ( "encoding/json" - "time" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/zalando-incubator/postgres-operator/pkg/spec" @@ -33,7 +31,7 @@ type KubernetesMetaConfiguration struct { PodServiceAccountName string `json:"pod_service_account_name,omitempty"` // TODO: change it to the proper json 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"` PDBNameFormat StringTemplate `json:"pdb_name_format,omitempty"` SecretNameTemplate StringTemplate `json:"secret_name_template,omitempty"` @@ -57,12 +55,12 @@ type PostgresPodResourcesDefaults struct { } type OperatorTimeouts struct { - ResourceCheckInterval time.Duration `json:"resource_check_interval,omitempty"` - ResourceCheckTimeout time.Duration `json:"resource_check_timeout,omitempty"` - PodLabelWaitTimeout time.Duration `json:"pod_label_wait_timeout,omitempty"` - PodDeletionWaitTimeout time.Duration `json:"pod_deletion_wait_timeout,omitempty"` - ReadyWaitInterval time.Duration `json:"ready_wait_interval,omitempty"` - ReadyWaitTimeout time.Duration `json:"ready_wait_timeout,omitempty"` + ResourceCheckInterval spec.Duration `json:"resource_check_interval,omitempty"` + ResourceCheckTimeout spec.Duration `json:"resource_check_timeout,omitempty"` + PodLabelWaitTimeout spec.Duration `json:"pod_label_wait_timeout,omitempty"` + PodDeletionWaitTimeout spec.Duration `json:"pod_deletion_wait_timeout,omitempty"` + ReadyWaitInterval spec.Duration `json:"ready_wait_interval,omitempty"` + ReadyWaitTimeout spec.Duration `json:"ready_wait_timeout,omitempty"` } type LoadBalancerConfiguration struct { @@ -117,7 +115,7 @@ type OperatorConfigurationData struct { Workers uint32 `json:"workers,omitempty"` MinInstances int32 `json:"min_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"` Kubernetes KubernetesMetaConfiguration `json:"kubernetes"` PostgresPodResources PostgresPodResourcesDefaults `json:"postgres_pod_resources"`