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
db_hosted_zone: db.example.com
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
etcd_host: etcd-client.default.svc.cluster.local:2379
infrastructure_roles_secret_name: postgresql-infrastructure-roles

View File

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

View File

@ -51,10 +51,10 @@ type Config struct {
EtcdScope string `name:"etcd_scope" default:"service"`
WALES3Bucket string `name:"wal_s3_bucket"`
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"`
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"`
}

View File

@ -1,6 +1,7 @@
package config
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
@ -18,6 +19,8 @@ type fieldInfo struct {
Field reflect.Value
}
type stringTemplate string
func decoderFrom(field reflect.Value) (d Decoder) {
// it may be impossible for a struct field to fail this check
if !field.CanInterface() {
@ -162,3 +165,23 @@ func processField(value string, field reflect.Value) error {
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))
}