Skip to content

Commit fa71f32

Browse files
committed
Allow overwrite of advertised storage addr
The added `--storage-adv-addr` flag allows overwriting the HTTP address advertised in the status objects of the sources. This allows for finer grain configuration in setups where a modified service is used, or where the DNS resolution differs from the Kubernetes defaults. When the flag is omitted, an attempt is made to determine the address based on the configured `--storage-addr` and the `HOSTNAME`. Signed-off-by: Hidde Beydals <[email protected]>
1 parent 5843c52 commit fa71f32

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

config/manager/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ spec:
4141
- --log-json
4242
- --enable-leader-election
4343
- --storage-path=/data
44+
- --storage-adv-addr=source-controller.$(RUNTIME_NAMESPACE).svc.cluster.local.
4445
livenessProbe:
4546
httpGet:
4647
port: http

main.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,26 @@ func main() {
6868
enableLeaderElection bool
6969
storagePath string
7070
storageAddr string
71+
storageAdvAddr string
7172
concurrent int
7273
logLevel string
7374
logJSON bool
7475
watchAllNamespaces bool
7576
)
7677

77-
flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"), "The address the metric endpoint binds to.")
78-
flag.StringVar(&eventsAddr, "events-addr", envOrDefault("EVENTS_ADDR", ""), "The address of the events receiver.")
78+
flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"),
79+
"The address the metric endpoint binds to.")
80+
flag.StringVar(&eventsAddr, "events-addr", envOrDefault("EVENTS_ADDR", ""),
81+
"The address of the events receiver.")
7982
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
8083
"Enable leader election for controller manager. "+
8184
"Enabling this will ensure there is only one active controller manager.")
82-
flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""), "The local storage path.")
83-
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"), "The address the static file server binds to.")
85+
flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""),
86+
"The local storage path.")
87+
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"),
88+
"The address the static file server binds to.")
89+
flag.StringVar(&storageAdvAddr, "storage-adv-addr", envOrDefault("STORAGE_ADV_ADDR", ""),
90+
"The advertised address of the static file server.")
8491
flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.")
8592
flag.StringVar(&logLevel, "log-level", "info", "Set logging level. Can be debug, info or error.")
8693
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
@@ -122,8 +129,10 @@ func main() {
122129
os.Exit(1)
123130
}
124131

125-
storage := mustInitStorage(storagePath, storageAddr, setupLog)
126-
132+
if storageAdvAddr == "" {
133+
storageAdvAddr = determineAdvStorageAddr(storageAddr, setupLog)
134+
}
135+
storage := mustInitStorage(storagePath, storageAdvAddr, setupLog)
127136
go startFileServer(storage.BasePath, storageAddr, setupLog)
128137

129138
if err = (&controllers.GitRepositoryReconciler{
@@ -202,19 +211,36 @@ func startFileServer(path string, address string, l logr.Logger) {
202211
}
203212
}
204213

205-
func mustInitStorage(path string, storageAddr string, l logr.Logger) *controllers.Storage {
214+
func mustInitStorage(path string, storageAdvAddr string, l logr.Logger) *controllers.Storage {
206215
if path == "" {
207216
p, _ := os.Getwd()
208217
path = filepath.Join(p, "bin")
209218
os.MkdirAll(path, 0777)
210219
}
211220

221+
storage, err := controllers.NewStorage(path, storageAdvAddr, 5*time.Minute)
222+
if err != nil {
223+
l.Error(err, "unable to initialise storage")
224+
os.Exit(1)
225+
}
226+
227+
return storage
228+
}
229+
230+
func determineAdvStorageAddr(storageAddr string, l logr.Logger) string {
231+
// TODO(hidde): remove next MINOR prerelease as it can be passed in using
232+
// Kubernetes' substitution.
233+
if os.Getenv("RUNTIME_NAMESPACE") != "" {
234+
svcParts := strings.Split(os.Getenv("HOSTNAME"), "-")
235+
return fmt.Sprintf("%s.%s",
236+
strings.Join(svcParts[:len(svcParts)-2], "-"), os.Getenv("RUNTIME_NAMESPACE"))
237+
}
238+
212239
host, port, err := net.SplitHostPort(storageAddr)
213240
if err != nil {
214241
l.Error(err, "unable to parse storage address")
215242
os.Exit(1)
216243
}
217-
218244
switch host {
219245
case "":
220246
host = "localhost"
@@ -226,26 +252,10 @@ func mustInitStorage(path string, storageAddr string, l logr.Logger) *controller
226252
l.Error(err, "0.0.0.0 specified in storage addr but hostname is invalid")
227253
os.Exit(1)
228254
}
229-
230255
host = hn
231256
}
232257
}
233-
234-
hostname := net.JoinHostPort(host, port)
235-
236-
if os.Getenv("RUNTIME_NAMESPACE") != "" {
237-
svcParts := strings.Split(os.Getenv("HOSTNAME"), "-")
238-
hostname = fmt.Sprintf("%s.%s",
239-
strings.Join(svcParts[:len(svcParts)-2], "-"), os.Getenv("RUNTIME_NAMESPACE"))
240-
}
241-
242-
storage, err := controllers.NewStorage(path, hostname, 5*time.Minute)
243-
if err != nil {
244-
l.Error(err, "unable to initialise storage")
245-
os.Exit(1)
246-
}
247-
248-
return storage
258+
return net.JoinHostPort(host, port)
249259
}
250260

251261
func envOrDefault(envName, defaultValue string) string {

0 commit comments

Comments
 (0)