Skip to content

Commit fca8c79

Browse files
committed
REORG/MEDIUM: config: fixing code-generation for configuration files
1 parent de441b7 commit fca8c79

File tree

13 files changed

+252
-95
lines changed

13 files changed

+252
-95
lines changed

configuration/cluster_sync.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"os"
3333
"path"
3434
"runtime"
35+
"strconv"
3536
"strings"
3637
"time"
3738

@@ -213,7 +214,10 @@ func (c *ClusterSync) monitorBootstrapKey() {
213214
}
214215
url := fmt.Sprintf("%s://%s", data["schema"], data["address"])
215216
c.cfg.Cluster.URL.Store(url)
216-
c.cfg.Cluster.Port.Store(data["port"])
217+
c.cfg.Cluster.Port.Store(func() int {
218+
i, _ := strconv.Atoi(data["port"])
219+
return i
220+
}())
217221
c.cfg.Cluster.APIBasePath.Store(data["api-base-path"])
218222
registerPath, ok := data["register-path"]
219223
if !ok {
@@ -480,7 +484,7 @@ func (c *ClusterSync) fetchCert() {
480484
apiBasePath := c.cfg.Cluster.APIBasePath.Load()
481485
apiNodesPath := c.cfg.Cluster.APINodesPath.Load()
482486
id := c.cfg.Cluster.ID.Load()
483-
url = fmt.Sprintf("%s:%s/%s", url, port, strings.TrimLeft(path.Join(apiBasePath, apiNodesPath, id), "/"))
487+
url = fmt.Sprintf("%s:%d/%s/%s/%s", url, port, apiBasePath, apiNodesPath, id)
484488
req, err := http.NewRequest("GET", url, nil)
485489
if err != nil {
486490
c.activateFetchCert(err)

configuration/configuration.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ type ClusterConfiguration struct {
101101
ActiveBootstrapKey AtomicString `yaml:"active_bootstrap_key,omitempty" group:"cluster" save:"true"`
102102
Token AtomicString `yaml:"token,omitempty" group:"cluster" save:"true"`
103103
URL AtomicString `yaml:"url,omitempty" group:"cluster" save:"true"`
104-
Port AtomicString `yaml:"port,omitempty" group:"cluster" save:"true"`
104+
Port AtomicInt `yaml:"port,omitempty" group:"cluster" save:"true"`
105105
APIBasePath AtomicString `yaml:"api_base_path,omitempty" group:"cluster" save:"true"`
106106
APINodesPath AtomicString `yaml:"api_nodes_path,omitempty" group:"cluster" save:"true"`
107107
APIRegisterPath AtomicString `yaml:"api_register_path,omitempty" group:"cluster" save:"true"`
108108
StorageDir AtomicString `yaml:"storage-dir,omitempty" group:"cluster" save:"true"`
109109
CertificateDir AtomicString `yaml:"cert-path,omitempty" group:"cluster" save:"true"`
110-
CertificateFetched AtomicBool `yaml:"cert-fetched,omitempty" group:"cluster" save:"true"`
110+
CertificateFetched AtomicBool `yaml:"cert-fetched,omitempty" group:"cluster" save:"true" example:"false"`
111111
Name AtomicString `yaml:"name,omitempty" group:"cluster" save:"true"`
112112
Description AtomicString `yaml:"description,omitempty" group:"cluster" save:"true"`
113113
}
@@ -116,7 +116,7 @@ func (c *ClusterConfiguration) Clear() {
116116
c.ID.Store("")
117117
c.ActiveBootstrapKey.Store("")
118118
c.Token.Store("")
119-
c.Port.Store("")
119+
c.Port.Store(0)
120120
c.APIBasePath.Store("")
121121
c.APINodesPath.Store("")
122122
c.APIRegisterPath.Store("")

configuration/configuration_generated.go

Lines changed: 12 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configuration/examples/example-full.hcl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@ dataplaneapi {
1010
max-header-size = "1MiB"
1111
socket-path = "/var/run/data-plane.sock"
1212
host = "localhost"
13-
port = "80"
14-
listen-limit = "null"
13+
port = 80
14+
listen-limit = 1024
1515
keep-alive = "3m"
1616
read-timeout = "30s"
1717
write-timeout = "60s"
18-
show-system-info = "false"
19-
disable-inotify = "false"
18+
show-system-info = false
19+
disable-inotify = false
2020
pid-file = "/tmp/dataplane.pid"
21-
uid = "1000"
22-
gid = "1000"
21+
uid = 1000
22+
gid = 1000
2323

2424
tls {
2525
tls-host = "null"
26-
tls-port = "null"
26+
tls-port = 6443
2727
tls-certificate = "null"
2828
tls-key = "null"
2929
tls-ca = "null"
30-
tls-listen-limit = "null"
31-
tls-keep-alive = "null"
32-
tls-read-timeout = "null"
33-
tls-write-timeout = "null"
30+
tls-listen-limit = 10
31+
tls-keep-alive = "1m"
32+
tls-read-timeout = "10s"
33+
tls-write-timeout = "10s"
3434
}
3535

3636
user "admin" {
37-
insecure = "true"
37+
insecure = true
3838
password = "adminpwd"
3939
}
4040

@@ -45,23 +45,23 @@ dataplaneapi {
4545

4646
transaction {
4747
transaction-dir = "/tmp/haproxy"
48-
backups-number = "0"
48+
backups-number = 0
4949
backups-dir = "/tmp/backups"
50-
max-open-transactions = "20"
50+
max-open-transactions = 20
5151
}
5252

5353
resources {
5454
maps-dir = "/etc/haproxy/maps"
5555
ssl-certs-dir = "/etc/haproxy/ssl"
56-
update-map-files = "false"
57-
update-map-files-period = "10"
56+
update-map-files = false
57+
update-map-files-period = 10
5858
spoe-dir = "/etc/haproxy/spoe"
5959
spoe-transaction-dir = "/tmp/spoe-haproxy"
6060
}
6161

6262
advertised {
6363
api-address = "10.2.3.4"
64-
api-port = "80"
64+
api-port = 80
6565
}
6666
}
6767

@@ -70,13 +70,13 @@ haproxy {
7070
haproxy-bin = "haproxy"
7171
master-runtime = "null"
7272
fid = "null"
73-
master-worker-mode = "false"
73+
master-worker-mode = false
7474

7575
reload {
76-
reload-delay = "5"
76+
reload-delay = 5
7777
reload-cmd = "null"
7878
restart-cmd = "null"
79-
reload-retention = "1"
79+
reload-retention = 1
8080
validate-cmd = "null"
8181
}
8282
}
@@ -88,20 +88,20 @@ cluster {
8888
active_bootstrap_key = "null"
8989
token = "null"
9090
url = "null"
91-
port = "80"
91+
port = 80
9292
api_base_path = "null"
9393
api_nodes_path = "null"
9494
api_register_path = "null"
9595
storage-dir = "null"
9696
cert-path = "null"
97-
cert-fetched = "null"
97+
cert-fetched = false
9898
name = "null"
9999
description = "null"
100100
}
101101

102102
service_discovery {
103-
consuls = "null"
104-
aws-regions = "null"
103+
consuls = []
104+
aws-regions = []
105105
}
106106

107107
log {

configuration/examples/example-full.yaml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dataplaneapi:
1111
socket-path: "/var/run/data-plane.sock" # flags.Filename
1212
host: "localhost" # string
1313
port: 80 # int
14-
listen-limit: null # int
14+
listen-limit: 1024 # int
1515
keep-alive: "3m" # time.Duration
1616
read-timeout: "30s" # time.Duration
1717
write-timeout: "60s" # time.Duration
@@ -22,14 +22,14 @@ dataplaneapi:
2222
gid: 1000 # int
2323
tls:
2424
tls-host: null # string 2
25-
tls-port: null # int 2
25+
tls-port: 6443 # int 2
2626
tls-certificate: null # flags.Filename 2
2727
tls-key: null # flags.Filename 2
2828
tls-ca: null # flags.Filename 2
29-
tls-listen-limit: null # int 2
30-
tls-keep-alive: null # time.Duration 2
31-
tls-read-timeout: null # time.Duration 2
32-
tls-write-timeout: null # time.Duration 2
29+
tls-listen-limit: 10 # int 2
30+
tls-keep-alive: 1m # time.Duration 2
31+
tls-read-timeout: 10s # time.Duration 2
32+
tls-write-timeout: 10s # time.Duration 2
3333
user:
3434
- name: admin # string 2
3535
insecure: true # bool 2
@@ -39,14 +39,14 @@ dataplaneapi:
3939
userlist-file: null # string 2
4040
transaction:
4141
transaction-dir: "/tmp/haproxy" # string 2
42-
backups-number: "0" # int 2
42+
backups-number: 0 # int 2
4343
backups-dir: /tmp/backups # string 2
44-
max-open-transactions: "20" # int64 2
44+
max-open-transactions: 20 # int64 2
4545
resources:
4646
maps-dir: "/etc/haproxy/maps" # string 2
4747
ssl-certs-dir: "/etc/haproxy/ssl" # string 2
4848
update-map-files: false # bool 2
49-
update-map-files-period: "10" # int64 2
49+
update-map-files-period: 10 # int64 2
5050
spoe-dir: "/etc/haproxy/spoe" # string 2
5151
spoe-transaction-dir: "/tmp/spoe-haproxy" # string 2
5252
advertised:
@@ -59,10 +59,10 @@ haproxy:
5959
fid: null # string
6060
master-worker-mode: false # bool
6161
reload:
62-
reload-delay: "5" # int 2
62+
reload-delay: 5 # int 2
6363
reload-cmd: null # string 2
6464
restart-cmd: null # string 2
65-
reload-retention: "1" # int 2
65+
reload-retention: 1 # int 2
6666
validate-cmd: null # string 2
6767
cluster:
6868
cluster-tls-dir: null # string
@@ -71,18 +71,18 @@ cluster:
7171
active_bootstrap_key: null # string
7272
token: null # string
7373
url: null # string
74-
port: 80 # string
74+
port: 80 # int
7575
api_base_path: null # string
7676
api_nodes_path: null # string
7777
api_register_path: null # string
7878
storage-dir: null # string
7979
cert-path: null # string
80-
cert-fetched: null # bool
80+
cert-fetched: false # bool
8181
name: null # string
8282
description: null # string
8383
service_discovery:
84-
consuls: null # []*models.Consul
85-
aws-regions: null # []*models.AwsRegion
84+
consuls: [] # []*models.Consul
85+
aws-regions: [] # []*models.AwsRegion
8686
log:
8787
log-to: "stdout" # string
8888
log-file: "/var/log/dataplaneapi/dataplaneapi.log" # string

configuration/int.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2019 HAProxy Technologies
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
package configuration
17+
18+
import (
19+
"fmt"
20+
"sync/atomic"
21+
)
22+
23+
type AtomicInt struct {
24+
value atomic.Value
25+
}
26+
27+
func (s *AtomicInt) Load() int {
28+
v := s.value.Load()
29+
if v == nil {
30+
return 0
31+
}
32+
return v.(int)
33+
}
34+
35+
func (s *AtomicInt) Store(str int) {
36+
s.value.Store(str)
37+
}
38+
39+
func (s *AtomicInt) String() string {
40+
return fmt.Sprintf("%d", s.Load())
41+
}
42+
43+
func (s *AtomicInt) UnmarshalYAML(unmarshal func(interface{}) error) error {
44+
var buf int
45+
err := unmarshal(&buf)
46+
if err != nil {
47+
return err
48+
}
49+
50+
s.Store(buf)
51+
return nil
52+
}
53+
54+
func (s AtomicInt) MarshalYAML() (interface{}, error) {
55+
return s.Load(), nil
56+
}

0 commit comments

Comments
 (0)