Skip to content

Commit fdf748a

Browse files
committed
Remove HAProxy installation from the router image
HAProxy now runs on a sidecar container, using a distinct image from the router one. This update is removing the HAProxy installation from the router image. The reload script is also being removed, along with any configuration pointing to it, like envvar in the router image and dev documentation. The --reload command-line option is still supported, so this does not change the expectation of the nginx plugin. Providing a script however is optional if the HAProxy admin unix socket is provided. Hacking documentation for running the router locally is also updated. Jira: https://redhat.atlassian.net/browse/NE-2816
1 parent 3381229 commit fdf748a

10 files changed

Lines changed: 47 additions & 119 deletions

File tree

‎HACKING.md‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ sudo cp haproxy /usr/sbin/
8282

8383
### Run OpenShift router locally
8484

85-
Prepare the local environment and start router with default options:
85+
Prepare the local environment:
86+
87+
```bash
88+
make local-prepare
89+
```
90+
91+
On another terminal, start router with default options:
8692

8793
```bash
8894
make local-run
@@ -94,7 +100,7 @@ Prepare means create and clean `/var/lib/haproxy` and sub-directories, which rou
94100

95101
Here is the VSCode launch configuration that starts OpenShift router in debug mode.
96102

97-
> Run `make local-prepare` before the very first run, and whenever a clean environment is needed.
103+
> Run `make local-prepare` before starting router.
98104
99105
`.vscode/launch.json` content, merge its content with any previous one:
100106

@@ -119,7 +125,7 @@ Here is the VSCode launch configuration that starts OpenShift router in debug mo
119125
},
120126
"args": [
121127
"--template=images/router/haproxy/conf/haproxy-config.template",
122-
"--reload=images/router/haproxy/reload-haproxy",
128+
"--haproxy-admin-unix-socket=/var/lib/haproxy/run/admin.sock",
123129
],
124130
}
125131
]
@@ -129,4 +135,3 @@ Here is the VSCode launch configuration that starts OpenShift router in debug mo
129135
Caveats:
130136

131137
1. On debug mode, the binary name is not `openshift-router`, making the bootstrap code to fail. Temporarily patch `cmd/openshift-router/main.go` and hardcode `openshift-router` in the `CommandFor()` call.
132-
1. A haproxy instance should be left behind depending on how the router is stopped. Run `killall haproxy` in case the router complains when trying to listen to sockets.

‎hack/Dockerfile.debug‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
FROM registry.access.redhat.com/ubi9/ubi
2-
RUN yum install -y https://github.com/frobware/haproxy-builds/raw/master/rhaos-4.16-rhel-9/haproxy28-2.8.5-1.rhaos4.16.el9.x86_64.rpm
3-
RUN haproxy -vv
42
RUN INSTALL_PKGS="rsyslog procps-ng util-linux socat" && \
53
yum install -y --setopt=install_weak_deps=0 $INSTALL_PKGS && \
64
rpm -V $INSTALL_PKGS && \
@@ -17,6 +15,5 @@ USER 1001
1715
EXPOSE 80 443 7000
1816
WORKDIR /var/lib/haproxy/conf
1917
ENV XDG_CONFIG_HOME=/tmp \
20-
TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template \
21-
RELOAD_SCRIPT=/var/lib/haproxy/reload-haproxy
18+
TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template
2219
ENTRYPOINT ["/usr/bin/openshift-router", "--v=4"]

‎hack/local-router.sh‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ help() {
1818
}
1919

2020
router_prepare() {
21-
# dropping any leaked haproxy instance that router did not have a chance to kill
21+
# dropping any previously running haproxy instance
2222
killall haproxy 2>/dev/null || :
2323

2424
# copying mandatory files to HAProxy's configuration dir
@@ -28,19 +28,24 @@ router_prepare() {
2828
sudo mkdir -p "$haproxydir"
2929
sudo chown "${USER}:${USER}" "$haproxydir"
3030
fi
31-
rm -rf "${haproxydir}/{conf,run}"
32-
mkdir -p "${haproxydir}/{conf,run}"
31+
rm -rf "${haproxydir}/conf" "${haproxydir}/run"
32+
mkdir -p "${haproxydir}/conf" "${haproxydir}/run" "${haproxydir}/router/certs" "${haproxydir}/router/cacerts" "${haproxydir}/router/allowlists"
3333
cp images/router/haproxy/conf/* "${haproxydir}/conf/"
34+
touch "${haproxydir}/conf/haproxy.config"
3435

35-
echo "$haproxydir prepared"
36+
echo
37+
echo "$haproxydir prepared, starting HAProxy in foreground -- ^C to stop"
38+
39+
# starting the "sidecar" haproxy
40+
haproxy -W -db -S "${haproxydir}/run/admin.sock,mode,600" -f "${haproxydir}/conf/haproxy.config"
3641
}
3742

3843
router_run() {
3944
# changing listening ports, we usually don't have permission to bind to the default ones 80/443
40-
ROUTER_SERVICE_HTTP_PORT=9090 ROUTER_SERVICE_HTTPS_PORT=9443 STATS_USERNAME=admin STATS_PASSWORD=admin STATS_PORT=1936 \
45+
ROUTER_SERVICE_HTTP_PORT=9090 ROUTER_SERVICE_HTTPS_PORT=9443 STATS_USERNAME=admin STATS_PASSWORD=admin STATS_PORT=1936 ROUTER_GRACEFUL_SHUTDOWN_DELAY=2s \
4146
go run -v ./cmd/openshift-router \
4247
--template images/router/haproxy/conf/haproxy-config.template \
43-
--reload images/router/haproxy/reload-haproxy
48+
--haproxy-admin-unix-socket /var/lib/haproxy/run/admin.sock
4449
}
4550

4651
[ $# -ne 1 ] && help
@@ -50,7 +55,7 @@ cd "$(dirname $0)/.."
5055

5156
case "$1" in
5257
prepare) router_prepare;;
53-
run) router_prepare; router_run;;
58+
run) router_run;;
5459
help) help;;
5560
*) die "invalid command: $1";;
5661
esac

‎images/router/haproxy/Dockerfile‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM registry.svc.ci.openshift.org/openshift/origin-v4.0:base-router
2-
RUN INSTALL_PKGS="socat haproxy28 rsyslog sysvinit-tools" && \
2+
RUN INSTALL_PKGS="socat rsyslog sysvinit-tools" && \
33
yum install -y --setopt=install_weak_deps=0 $INSTALL_PKGS && \
44
rpm -V $INSTALL_PKGS && \
55
yum clean all && \
@@ -16,6 +16,5 @@ LABEL io.k8s.display-name="OpenShift HAProxy Router" \
1616
USER 1001
1717
EXPOSE 80 443
1818
WORKDIR /var/lib/haproxy/conf
19-
ENV TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template \
20-
RELOAD_SCRIPT=/var/lib/haproxy/reload-haproxy
19+
ENV TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template
2120
ENTRYPOINT ["/usr/bin/openshift-router", "--v=2"]

‎images/router/haproxy/Dockerfile.ocp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM registry.ci.openshift.org/ocp/5.0:haproxy-router-base
2-
RUN INSTALL_PKGS="socat haproxy32 rsyslog procps-ng util-linux" && \
2+
RUN INSTALL_PKGS="socat rsyslog procps-ng util-linux" && \
33
yum install -y --setopt=install_weak_deps=0 $INSTALL_PKGS && \
44
rpm -V $INSTALL_PKGS && \
55
yum clean all && \
@@ -17,6 +17,5 @@ LABEL io.k8s.display-name="OpenShift HAProxy Router" \
1717
USER 1001
1818
EXPOSE 80 443
1919
WORKDIR /var/lib/haproxy/conf
20-
ENV TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template \
21-
RELOAD_SCRIPT=/var/lib/haproxy/reload-haproxy
20+
ENV TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template
2221
ENTRYPOINT ["/usr/bin/openshift-router", "--v=2"]

‎images/router/haproxy/Dockerfile.rhel‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
FROM registry.svc.ci.openshift.org/ocp/4.0:base-router
2-
RUN INSTALL_PKGS="socat haproxy28 rsyslog sysvinit-tools" && \
2+
RUN INSTALL_PKGS="socat rsyslog sysvinit-tools" && \
33
yum install -y --setopt=install_weak_deps=0 $INSTALL_PKGS && \
44
rpm -V $INSTALL_PKGS && \
55
yum clean all && \
@@ -16,6 +16,5 @@ LABEL io.k8s.display-name="OpenShift HAProxy Router" \
1616
USER 1001
1717
EXPOSE 80 443
1818
WORKDIR /var/lib/haproxy/conf
19-
ENV TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template \
20-
RELOAD_SCRIPT=/var/lib/haproxy/reload-haproxy
19+
ENV TEMPLATE_FILE=/var/lib/haproxy/conf/haproxy-config.template
2120
ENTRYPOINT ["/usr/bin/openshift-router", "--v=2"]

‎images/router/haproxy/reload-haproxy‎

Lines changed: 0 additions & 86 deletions
This file was deleted.

‎pkg/cmd/infra/router/template.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type TemplateRouter struct {
111111
WorkingDir string
112112
TemplateFile string
113113
ReloadScript string
114+
HAProxyAdminUnixSocket string
114115
ReloadInterval time.Duration
115116
DefaultCertificate string
116117
DefaultCertificatePath string
@@ -172,6 +173,7 @@ func (o *TemplateRouter) Bind(flag *pflag.FlagSet) {
172173
flag.StringVar(&o.DefaultDestinationCAPath, "default-destination-ca-path", env("DEFAULT_DESTINATION_CA_PATH", ""), "A path to a PEM file containing the default CA bundle to use with re-encrypt routes. This CA should sign for certificates in the Kubernetes DNS space (service.namespace.svc).")
173174
flag.StringVar(&o.TemplateFile, "template", env("TEMPLATE_FILE", ""), "The path to the template file to use")
174175
flag.StringVar(&o.ReloadScript, "reload", env("RELOAD_SCRIPT", ""), "The path to the reload script to use")
176+
flag.StringVar(&o.HAProxyAdminUnixSocket, "haproxy-admin-unix-socket", env("ROUTER_HAPROXY_ADMIN_UNIX_SOCKET", ""), "The path to the HAProxy admin unix socket")
175177
flag.DurationVar(&o.ReloadInterval, "interval", getIntervalFromEnv("RELOAD_INTERVAL", defaultReloadInterval), "Controls how often router reloads are invoked. Mutiple router reload requests are coalesced for the duration of this interval since the last reload time.")
176178
flag.BoolVar(&o.BindPortsAfterSync, "bind-ports-after-sync", env("ROUTER_BIND_PORTS_AFTER_SYNC", "") == "true", "Bind ports only after route state has been synchronized")
177179
flag.StringVar(&o.MaxConnections, "max-connections", env("ROUTER_MAX_CONNECTIONS", ""), "Specifies the maximum number of concurrent connections.")
@@ -552,8 +554,8 @@ func (o *TemplateRouterOptions) Validate() error {
552554
return fmt.Errorf("unable to load default destination CA certificate: %v", err)
553555
}
554556
}
555-
if len(o.ReloadScript) == 0 {
556-
return errors.New("reload script must be specified")
557+
if len(o.ReloadScript) == 0 && len(o.HAProxyAdminUnixSocket) == 0 {
558+
return errors.New("either the reload script or the HAProxy admin unix socket must be specified")
557559
}
558560
return nil
559561
}
@@ -611,12 +613,11 @@ func (o *TemplateRouterOptions) Run(stopCh <-chan struct{}) error {
611613
}
612614
}
613615

614-
adminUnixSocket := os.Getenv("ROUTER_HAPROXY_ADMIN_UNIX_SOCKET")
615-
hasHAProxySidecar := len(adminUnixSocket) > 0
616+
hasHAProxySidecar := len(o.HAProxyAdminUnixSocket) > 0
616617

617618
pidFn := haproxyPidEmbedded
618619
if hasHAProxySidecar {
619-
pidFn = haproxyPidSidecar(ctx, adminUnixSocket)
620+
pidFn = haproxyPidSidecar(ctx, o.HAProxyAdminUnixSocket)
620621
}
621622
collector, err := haproxy.NewPrometheusCollector(haproxy.PrometheusOptions{
622623
// Only template router customizers who alter the image should need this
@@ -777,6 +778,7 @@ func (o *TemplateRouterOptions) Run(stopCh <-chan struct{}) error {
777778
WorkingDir: o.WorkingDir,
778779
TemplatePath: o.TemplateFile,
779780
ReloadScriptPath: o.ReloadScript,
781+
HAProxyAdminUnixSocketPath: o.HAProxyAdminUnixSocket,
780782
ReloadInterval: o.ReloadInterval,
781783
ReloadCallbacks: reloadCallbacks,
782784
DefaultCertificate: o.DefaultCertificate,

‎pkg/router/template/plugin.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type TemplatePluginConfig struct {
4747
WorkingDir string
4848
TemplatePath string
4949
ReloadScriptPath string
50+
HAProxyAdminUnixSocketPath string
5051
ReloadFn func(shutdown bool) error
5152
ReloadInterval time.Duration
5253
ReloadCallbacks []func()
@@ -152,6 +153,7 @@ func NewTemplatePlugin(cfg TemplatePluginConfig, lookupSvc ServiceLookup) (*Temp
152153
dir: cfg.WorkingDir,
153154
templates: templates,
154155
reloadScriptPath: cfg.ReloadScriptPath,
156+
haproxyAdminUnixSocketPath: cfg.HAProxyAdminUnixSocketPath,
155157
reloadFn: cfg.ReloadFn,
156158
reloadInterval: cfg.ReloadInterval,
157159
reloadCallbacks: cfg.ReloadCallbacks,

‎pkg/router/template/router.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ type templateRouter struct {
6868
state map[ServiceAliasConfigKey]ServiceAliasConfig
6969
serviceUnits map[ServiceUnitKey]ServiceUnit
7070
certManager certificateManager
71+
// haproxyAdminUnixSocketPath is the path to the unix socket of the HAProxy master process.
72+
// The HAProxy master process has the ability to reach any running worker, as well as
73+
// launch a new one to apply changes in the configuration file. If provided, it has priority
74+
// over the reload script when the router needs to reload the proxy.
75+
haproxyAdminUnixSocketPath string
7176
// defaultCertificate is a concatenated certificate(s), their keys, and their CAs that should be used by the underlying
7277
// implementation as the default certificate if no certificate is resolved by the normal matching mechanisms. This is
7378
// usually a wildcard certificate for a cloud domain such as *.mypaas.com to allow applications to create app.mypaas.com
@@ -140,6 +145,7 @@ type templateRouterCfg struct {
140145
dir string
141146
templates map[string]*template.Template
142147
reloadScriptPath string
148+
haproxyAdminUnixSocketPath string
143149
reloadFn func(shutdown bool) error
144150
reloadInterval time.Duration
145151
reloadCallbacks []func()
@@ -253,6 +259,7 @@ func newTemplateRouter(cfg templateRouterCfg) (*templateRouter, error) {
253259
dir: dir,
254260
templates: cfg.templates,
255261
reloadScriptPath: cfg.reloadScriptPath,
262+
haproxyAdminUnixSocketPath: cfg.haproxyAdminUnixSocketPath,
256263
reloadInterval: cfg.reloadInterval,
257264
reloadCallbacks: cfg.reloadCallbacks,
258265
reloadFn: cfg.reloadFn,
@@ -675,14 +682,13 @@ func (r *templateRouter) writeCertificates(cfg *ServiceAliasConfig) error {
675682

676683
// reloadRouter reloads haproxy.
677684
func (r *templateRouter) reloadRouter(shutdown bool) error {
678-
adminSocket := os.Getenv("ROUTER_HAPROXY_ADMIN_UNIX_SOCKET")
679-
if adminSocket != "" {
685+
if r.haproxyAdminUnixSocketPath != "" {
680686
if shutdown {
681687
// We are in HAProxy's master/worker mode, currently implemented as a sidecar,
682-
// so there is no local process to handle and the sidecar one already received SIGTERM.
688+
// so there is no local process to handle and the sidecar one will receive its own SIGTERM from kubelet.
683689
return nil
684690
}
685-
return reloadRouterSidecar(r.appCtx, "unix://"+adminSocket)
691+
return reloadRouterSidecar(r.appCtx, "unix://"+r.haproxyAdminUnixSocketPath)
686692
}
687693
return r.reloadRouterEmbedded(shutdown)
688694
}

0 commit comments

Comments
 (0)