Use named arguments in the DNS name format

This commit is contained in:
Murat Kabilov 2017-05-18 17:23:59 +02:00 committed by GitHub
parent 3b6454c2dc
commit 95a57d1e4f
4 changed files with 41 additions and 19 deletions

View File

@ -9,7 +9,7 @@ data:
pod_role_label: spilo-role pod_role_label: spilo-role
db_hosted_zone: db.example.com db_hosted_zone: db.example.com
debug_logging: "true" debug_logging: "true"
dns_name_format: '%s.%s.staging.%s' dns_name_format: '{cluster}.{team}.staging.{hostedzone}'
docker_image: registry.opensource.zalan.do/acid/spiloprivate-9.6:1.2-p4 docker_image: registry.opensource.zalan.do/acid/spiloprivate-9.6:1.2-p4
etcd_host: etcd-client.default.svc.cluster.local:2379 etcd_host: etcd-client.default.svc.cluster.local:2379
infrastructure_roles_secret_name: postgresql-infrastructure-roles infrastructure_roles_secret_name: postgresql-infrastructure-roles

View File

@ -245,11 +245,10 @@ func (c *Cluster) labelsSet() labels.Set {
} }
func (c *Cluster) dnsName() string { func (c *Cluster) dnsName() string {
return strings.ToLower(fmt.Sprintf( return strings.ToLower(c.OpConfig.DNSNameFormat.Format(
c.OpConfig.DNSNameFormat, "cluster", c.Spec.ClusterName,
c.Spec.ClusterName, "team", c.teamName(),
c.teamName(), "hostedzone", c.OpConfig.DbHostedZone))
c.OpConfig.DbHostedZone))
} }
func (c *Cluster) credentialSecretName(username string) string { func (c *Cluster) credentialSecretName(username string) string {

View File

@ -43,19 +43,19 @@ type Config struct {
TPR TPR
Resources Resources
Auth Auth
Namespace string `name:"namespace"` Namespace string `name:"namespace"`
EtcdHost string `name:"etcd_host" default:"etcd-client.default.svc.cluster.local:2379"` EtcdHost string `name:"etcd_host" default:"etcd-client.default.svc.cluster.local:2379"`
DockerImage string `name:"docker_image" default:"registry.opensource.zalan.do/acid/spiloprivate-9.6:1.2-p4"` DockerImage string `name:"docker_image" default:"registry.opensource.zalan.do/acid/spiloprivate-9.6:1.2-p4"`
ServiceAccountName string `name:"service_account_name" default:"operator"` ServiceAccountName string `name:"service_account_name" default:"operator"`
DbHostedZone string `name:"db_hosted_zone" default:"db.example.com"` DbHostedZone string `name:"db_hosted_zone" default:"db.example.com"`
EtcdScope string `name:"etcd_scope" default:"service"` EtcdScope string `name:"etcd_scope" default:"service"`
WALES3Bucket string `name:"wal_s3_bucket"` WALES3Bucket string `name:"wal_s3_bucket"`
KubeIAMRole string `name:"kube_iam_role"` KubeIAMRole string `name:"kube_iam_role"`
DebugLogging bool `name:"debug_logging" default:"false"` DebugLogging bool `name:"debug_logging" default:"true"`
EnableDBAccess bool `name:"enable_database_access" default:"true"` EnableDBAccess bool `name:"enable_database_access" default:"true"`
EnableTeamsAPI bool `name:"enable_teams_api" default:"true"` EnableTeamsAPI bool `name:"enable_teams_api" default:"true"`
DNSNameFormat string `name:"dns_name_format" default:"%s.%s.%s"` DNSNameFormat stringTemplate `name:"dns_name_format" default:"{cluster}.{team}.{hostedzone}"`
Workers uint32 `name:"workers" default:"4"` Workers uint32 `name:"workers" default:"4"`
} }
func (c Config) MustMarshal() string { func (c Config) MustMarshal() string {

View File

@ -1,6 +1,7 @@
package config package config
import ( import (
"encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"strconv" "strconv"
@ -18,6 +19,8 @@ type fieldInfo struct {
Field reflect.Value Field reflect.Value
} }
type stringTemplate string
func decoderFrom(field reflect.Value) (d Decoder) { func decoderFrom(field reflect.Value) (d Decoder) {
// it may be impossible for a struct field to fail this check // it may be impossible for a struct field to fail this check
if !field.CanInterface() { if !field.CanInterface() {
@ -162,3 +165,23 @@ func processField(value string, field reflect.Value) error {
return nil return nil
} }
func (f *stringTemplate) Decode(value string) error {
*f = stringTemplate(value)
return nil
}
func (f *stringTemplate) Format(a ...string) string {
res := string(*f)
for i := 0; i < len(a); i += 2 {
res = strings.Replace(res, "{"+a[i]+"}", a[i+1], -1)
}
return res
}
func (f stringTemplate) MarshalJSON() ([]byte, error) {
return json.Marshal(string(f))
}