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 <hello@hidde.co>
This commit is contained in:
Hidde Beydals 2020-12-11 13:19:45 +01:00
parent 5843c52526
commit fa71f3269b
2 changed files with 36 additions and 25 deletions

View File

@ -41,6 +41,7 @@ spec:
- --log-json - --log-json
- --enable-leader-election - --enable-leader-election
- --storage-path=/data - --storage-path=/data
- --storage-adv-addr=source-controller.$(RUNTIME_NAMESPACE).svc.cluster.local.
livenessProbe: livenessProbe:
httpGet: httpGet:
port: http port: http

60
main.go
View File

@ -68,19 +68,26 @@ func main() {
enableLeaderElection bool enableLeaderElection bool
storagePath string storagePath string
storageAddr string storageAddr string
storageAdvAddr string
concurrent int concurrent int
logLevel string logLevel string
logJSON bool logJSON bool
watchAllNamespaces bool watchAllNamespaces bool
) )
flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"), "The address the metric endpoint binds to.") flag.StringVar(&metricsAddr, "metrics-addr", envOrDefault("METRICS_ADDR", ":8080"),
flag.StringVar(&eventsAddr, "events-addr", envOrDefault("EVENTS_ADDR", ""), "The address of the events receiver.") "The address the metric endpoint binds to.")
flag.StringVar(&eventsAddr, "events-addr", envOrDefault("EVENTS_ADDR", ""),
"The address of the events receiver.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+ "Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.") "Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""), "The local storage path.") flag.StringVar(&storagePath, "storage-path", envOrDefault("STORAGE_PATH", ""),
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"), "The address the static file server binds to.") "The local storage path.")
flag.StringVar(&storageAddr, "storage-addr", envOrDefault("STORAGE_ADDR", ":9090"),
"The address the static file server binds to.")
flag.StringVar(&storageAdvAddr, "storage-adv-addr", envOrDefault("STORAGE_ADV_ADDR", ""),
"The advertised address of the static file server.")
flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.") flag.IntVar(&concurrent, "concurrent", 2, "The number of concurrent reconciles per controller.")
flag.StringVar(&logLevel, "log-level", "info", "Set logging level. Can be debug, info or error.") flag.StringVar(&logLevel, "log-level", "info", "Set logging level. Can be debug, info or error.")
flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.") flag.BoolVar(&logJSON, "log-json", false, "Set logging to JSON format.")
@ -122,8 +129,10 @@ func main() {
os.Exit(1) os.Exit(1)
} }
storage := mustInitStorage(storagePath, storageAddr, setupLog) if storageAdvAddr == "" {
storageAdvAddr = determineAdvStorageAddr(storageAddr, setupLog)
}
storage := mustInitStorage(storagePath, storageAdvAddr, setupLog)
go startFileServer(storage.BasePath, storageAddr, setupLog) go startFileServer(storage.BasePath, storageAddr, setupLog)
if err = (&controllers.GitRepositoryReconciler{ if err = (&controllers.GitRepositoryReconciler{
@ -202,19 +211,36 @@ func startFileServer(path string, address string, l logr.Logger) {
} }
} }
func mustInitStorage(path string, storageAddr string, l logr.Logger) *controllers.Storage { func mustInitStorage(path string, storageAdvAddr string, l logr.Logger) *controllers.Storage {
if path == "" { if path == "" {
p, _ := os.Getwd() p, _ := os.Getwd()
path = filepath.Join(p, "bin") path = filepath.Join(p, "bin")
os.MkdirAll(path, 0777) os.MkdirAll(path, 0777)
} }
storage, err := controllers.NewStorage(path, storageAdvAddr, 5*time.Minute)
if err != nil {
l.Error(err, "unable to initialise storage")
os.Exit(1)
}
return storage
}
func determineAdvStorageAddr(storageAddr string, l logr.Logger) string {
// TODO(hidde): remove next MINOR prerelease as it can be passed in using
// Kubernetes' substitution.
if os.Getenv("RUNTIME_NAMESPACE") != "" {
svcParts := strings.Split(os.Getenv("HOSTNAME"), "-")
return fmt.Sprintf("%s.%s",
strings.Join(svcParts[:len(svcParts)-2], "-"), os.Getenv("RUNTIME_NAMESPACE"))
}
host, port, err := net.SplitHostPort(storageAddr) host, port, err := net.SplitHostPort(storageAddr)
if err != nil { if err != nil {
l.Error(err, "unable to parse storage address") l.Error(err, "unable to parse storage address")
os.Exit(1) os.Exit(1)
} }
switch host { switch host {
case "": case "":
host = "localhost" host = "localhost"
@ -226,26 +252,10 @@ func mustInitStorage(path string, storageAddr string, l logr.Logger) *controller
l.Error(err, "0.0.0.0 specified in storage addr but hostname is invalid") l.Error(err, "0.0.0.0 specified in storage addr but hostname is invalid")
os.Exit(1) os.Exit(1)
} }
host = hn host = hn
} }
} }
return net.JoinHostPort(host, port)
hostname := net.JoinHostPort(host, port)
if os.Getenv("RUNTIME_NAMESPACE") != "" {
svcParts := strings.Split(os.Getenv("HOSTNAME"), "-")
hostname = fmt.Sprintf("%s.%s",
strings.Join(svcParts[:len(svcParts)-2], "-"), os.Getenv("RUNTIME_NAMESPACE"))
}
storage, err := controllers.NewStorage(path, hostname, 5*time.Minute)
if err != nil {
l.Error(err, "unable to initialise storage")
os.Exit(1)
}
return storage
} }
func envOrDefault(envName, defaultValue string) string { func envOrDefault(envName, defaultValue string) string {