Skip to content

Commit 3024d6a

Browse files
ivanmatmatioktalz
authored andcommitted
MINOR: add pebble supervisor
1 parent 3861163 commit 3024d6a

File tree

10 files changed

+303
-3
lines changed

10 files changed

+303
-3
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ example-remove:
4949
build:
5050
docker build -t haproxytech/kubernetes-ingress --build-arg TARGETPLATFORM=$(TARGETPLATFORM) -f build/Dockerfile .
5151

52+
.PHONY: build-pebble
53+
build-pebble:
54+
docker build -t haproxytech/kubernetes-ingress --build-arg TARGETPLATFORM=$(TARGETPLATFORM) -f build/Dockerfile.pebble .
55+
5256
.PHONY: publish
5357
publish:
5458
goreleaser release --rm-dist

build/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
6868
chown haproxy:haproxy /var/run/s6 && \
6969
chmod ug+rwx /var/run/s6 && \
7070
sed -i 's/ root / haproxy /g' /etc/s6/init/init-stage2-fixattrs.txt && \
71-
chmod ugo+x /etc/services.d/*/run /etc/cont-init.d/*
71+
chmod ugo+x /etc/services.d/*/run /etc/cont-init.d/* && \
72+
rm -rf /var/lib/pebble
7273

7374
COPY --from=builder /src/fs/haproxy-ingress-controller .
7475

build/Dockerfile.pebble

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2019 HAProxy Technologies LLC
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+
FROM golang:1.20-alpine AS builder
16+
17+
RUN apk --no-cache add git openssh
18+
19+
COPY /go.mod /src/go.mod
20+
COPY /go.sum /src/go.sum
21+
RUN cd /src && go mod download
22+
23+
COPY / /src
24+
25+
RUN go install github.com/canonical/pebble/cmd/[email protected]
26+
27+
RUN mkdir -p /var/run/vars && \
28+
cd /src && \
29+
git config --get remote.origin.url > /var/run/vars/GIT_REPO && \
30+
git rev-parse --short HEAD > /var/run/vars/GIT_HEAD_COMMIT && \
31+
git log -1 --date=format:"%Y/%m/%d %T" --format="%ad" > /var/run/vars/GIT_DATE_LAST_COMMIT && \
32+
git describe --abbrev=0 --tags > /var/run/vars/GIT_LAST_TAG && \
33+
git rev-parse --short $(cat /var/run/vars/GIT_LAST_TAG) > /var/run/vars/GIT_TAG_COMMIT && \
34+
git diff $(cat /var/run/vars/GIT_HEAD_COMMIT) $(cat /var/run/vars/GIT_TAG_COMMIT) --quiet > /var/run/vars/GIT_MODIFIED1 || echo '.dev' > /var/run/vars/GIT_MODIFIED1 && \
35+
git diff --quiet > /var/run/vars/GIT_MODIFIED2 || echo '.dirty' > /var/run/vars/GIT_MODIFIED2 && \
36+
cat /var/run/vars/GIT_MODIFIED1 /var/run/vars/GIT_MODIFIED2 | tr -d '\n' > /var/run/vars/GIT_MODIFIED && \
37+
CGO_ENABLED=0 go build \
38+
-ldflags "-X main.GitRepo=$(cat /var/run/vars/GIT_REPO) -X main.GitTag=$(cat /var/run/vars/GIT_LAST_TAG) -X main.GitCommit=$(cat /var/run/vars/GIT_HEAD_COMMIT) -X main.GitDirty=$(cat /var/run/vars/GIT_MODIFIED) -X \"main.GitCommitDate=$(cat /var/run/vars/GIT_DATE_LAST_COMMIT)\"" \
39+
-o fs/haproxy-ingress-controller .
40+
41+
FROM haproxytech/haproxy-alpine:2.7
42+
43+
ARG TARGETPLATFORM
44+
45+
COPY /fs /
46+
47+
RUN apk --no-cache add socat openssl util-linux htop tzdata curl libcap && \
48+
rm -f /usr/local/bin/dataplaneapi /usr/bin/dataplaneapi && \
49+
chgrp -R haproxy /usr/local/etc/haproxy /run /var && \
50+
chmod -R ug+rwx /usr/local/etc/haproxy /run /var && \
51+
setcap 'cap_net_bind_service=+ep' /usr/local/sbin/haproxy && \
52+
chown -R haproxy:haproxy /var/lib/pebble/default && \
53+
chmod ugo+rwx /var/lib/pebble/default/* && \
54+
rm -rf /etc/services.d/haproxy && \
55+
rm -rf /etc/services.d/ingress-controller && \
56+
rm -rf /etc/cont-init.d
57+
58+
59+
COPY --from=builder /go/bin/pebble /usr/local/bin
60+
COPY --from=builder /src/fs/haproxy-ingress-controller .
61+
62+
ENTRYPOINT ["/start-pebble.sh"]

fs/start-pebble.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# Copyright 2017 The Kubernetes Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
if [ ! -e /etc/haproxy/haproxy-aux.cfg ]; then
18+
touch /etc/haproxy/haproxy-aux.cfg
19+
chgrp haproxy /etc/haproxy/haproxy-aux.cfg
20+
chmod g+w /etc/haproxy/haproxy-aux.cfg
21+
fi
22+
23+
export EXTRA_OPTIONS="$@"
24+
pebble run --verbose
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# (Optional) A short one line summary of the layer
2+
summary: Manage the HAPRoxy process
3+
4+
# (Optional) A list of services managed by this configuration layer
5+
services:
6+
7+
haproxy:
8+
9+
# (Required) Control how this service definition is combined with any
10+
# other pre-existing definition with the same name in the Pebble plan.
11+
#
12+
# The value 'merge' will ensure that values in this layer specification
13+
# are merged over existing definitions, whereas 'replace' will entirely
14+
# override the existing service spec in the plan with the same name.
15+
override: replace
16+
17+
# (Required in combined layer) The command to run the service. It is executed
18+
# directly, not interpreted by a shell, and may be optionally suffixed by default
19+
# arguments within "[" and "]" which may be overriden via --args.
20+
# Example: /usr/bin/somedaemon --db=/db/path [ --port 8080 ]
21+
command: /var/lib/pebble/default/run-haproxy
22+
23+
24+
# (Optional) Control whether the service is started automatically when
25+
# Pebble starts. Default is "disabled".
26+
startup: enabled
27+
28+
# (Optional) Username for starting service as a different user. It is
29+
# an error if the user doesn't exist.
30+
# user: haproxy
31+
32+
# (Optional) Group name for starting service as a different user. It is
33+
# an error if the group doesn't exist.
34+
# group: haproxy
35+
36+
# (Optional) Defines what happens when the service exits with a zero
37+
# exit code. Possible values are: "restart" (default) which restarts
38+
# the service after the backoff delay, "shutdown" which shuts down and
39+
# exits the Pebble server, and "ignore" which does nothing further.
40+
on-success: restart
41+
42+
# (Optional) Defines what happens when the service exits with a nonzero
43+
# exit code. Possible values are: "restart" (default) which restarts
44+
# the service after the backoff delay, "shutdown" which shuts down and
45+
# exits the Pebble server, and "ignore" which does nothing further.
46+
on-failure: restart
47+
48+
controller:
49+
50+
# (Required) Control how this service definition is combined with any
51+
# other pre-existing definition with the same name in the Pebble plan.
52+
#
53+
# The value 'merge' will ensure that values in this layer specification
54+
# are merged over existing definitions, whereas 'replace' will entirely
55+
# override the existing service spec in the plan with the same name.
56+
override: replace
57+
58+
# (Required in combined layer) The command to run the service. It is executed
59+
# directly, not interpreted by a shell, and may be optionally suffixed by default
60+
# arguments within "[" and "]" which may be overriden via --args.
61+
# Example: /usr/bin/somedaemon --db=/db/path [ --port 8080 ]
62+
command: /var/lib/pebble/default/run-controller
63+
64+
65+
# (Optional) Control whether the service is started automatically when
66+
# Pebble starts. Default is "disabled".
67+
startup: enabled
68+
69+
# (Optional) Username for starting service as a different user. It is
70+
# an error if the user doesn't exist.
71+
#user: haproxy
72+
73+
# (Optional) Group name for starting service as a different user. It is
74+
# an error if the group doesn't exist.
75+
#group: haproxy
76+
77+
# (Optional) Defines what happens when the service exits with a zero
78+
# exit code. Possible values are: "restart" (default) which restarts
79+
# the service after the backoff delay, "shutdown" which shuts down and
80+
# exits the Pebble server, and "ignore" which does nothing further.
81+
on-success: restart
82+
83+
# (Optional) Defines what happens when the service exits with a nonzero
84+
# exit code. Possible values are: "restart" (default) which restarts
85+
# the service after the backoff delay, "shutdown" which shuts down and
86+
# exits the Pebble server, and "ignore" which does nothing further.
87+
on-failure: restart
88+
89+
after:
90+
- haproxy
91+
92+
checks:
93+
check-haproxy:
94+
# (Required) Control how this check definition is combined with any
95+
# other pre-existing definition with the same name in the Pebble plan.
96+
#
97+
# The value 'merge' will ensure that values in this layer specification
98+
# are merged over existing definitions, whereas 'replace' will entirely
99+
# override the existing check spec in the plan with the same name.
100+
override: replace
101+
http:
102+
# (Required) URL to fetch, for example "https://example.com/foo".
103+
url: http://127.0.0.1:1042/healthz
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
3+
MEMLIMIT=$(free -m | awk '/Mem:/ {print int($2 / 3)}')
4+
5+
CG_LIMIT_FILE="/sys/fs/cgroup/memory/memory.limit_in_bytes"
6+
if [ -f "/sys/fs/cgroup/cgroup.controllers" ]; then
7+
CG_LIMIT_FILE="/sys/fs/cgroup/memory.max"
8+
fi
9+
10+
if [ -r "${CG_LIMIT_FILE}" ]; then
11+
MEMLIMIT_CG=$(awk '{print int($1 / 1024 / 1024 / 3)}' "${CG_LIMIT_FILE}")
12+
13+
if [ "${MEMLIMIT_CG}" -gt 0 ]; then
14+
if [ "${MEMLIMIT_CG}" -lt "${MEMLIMIT}" ]; then
15+
MEMLIMIT="${MEMLIMIT_CG}"
16+
fi
17+
fi
18+
fi
19+
20+
export GOMEMLIMIT="${MEMLIMIT}MiB"
21+
22+
echo "Memory limit for Ingress Controller: ${GOMEMLIMIT}"
23+
24+
exec /haproxy-ingress-controller --with-pebble ${EXTRA_OPTIONS}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
MEMLIMIT=$(free -m | awk '/Mem:/ {print int($2 * 2 / 3)}')
4+
5+
CG_LIMIT_FILE="/sys/fs/cgroup/memory/memory.limit_in_bytes"
6+
if [ -f "/sys/fs/cgroup/cgroup.controllers" ]; then
7+
CG_LIMIT_FILE="/sys/fs/cgroup/memory.max"
8+
fi
9+
10+
if [ -r "${CG_LIMIT_FILE}" ]; then
11+
MEMLIMIT_CG=$(awk '{print int($1 / 1024 / 1024 * 2 / 3)}' "${CG_LIMIT_FILE}")
12+
13+
if [ "${MEMLIMIT_CG}" -gt 0 ]; then
14+
if [ "${MEMLIMIT_CG}" -lt "${MEMLIMIT}" ]; then
15+
MEMLIMIT="${MEMLIMIT_CG}"
16+
fi
17+
fi
18+
fi
19+
20+
echo "Memory limit for HAProxy: ${MEMLIMIT}MiB"
21+
22+
exec /usr/local/sbin/haproxy -W -db -m "${MEMLIMIT}" -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/haproxy-aux.cfg

pkg/haproxy/process/interface.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ type Process interface {
2020
}
2121

2222
func New(env env.Env, osArgs utils.OSArgs, auxCfgFile string, api api.HAProxyClient) (p Process) { //nolint:ireturn
23-
if osArgs.UseWiths6Overlay {
23+
switch {
24+
case osArgs.UseWiths6Overlay:
2425
p = &s6Control{
2526
Env: env,
2627
OSArgs: osArgs,
2728
API: api,
2829
}
29-
} else {
30+
case osArgs.UseWithPebble:
31+
p = &pebbleControl{
32+
Env: env,
33+
OSArgs: osArgs,
34+
}
35+
default:
3036
p = &directControl{
3137
Env: env,
3238
OSArgs: osArgs,

pkg/haproxy/process/pebble.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package process
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
8+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
9+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/env"
10+
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
11+
)
12+
13+
type pebbleControl struct {
14+
Env env.Env
15+
OSArgs utils.OSArgs
16+
}
17+
18+
func (d *pebbleControl) Service(action string) error {
19+
if d.OSArgs.Test {
20+
logger.Infof("HAProxy would be %sed now", action)
21+
return nil
22+
}
23+
var cmd *exec.Cmd
24+
25+
switch action {
26+
case "start":
27+
// no need to start it is up already (pebble)
28+
return nil
29+
case "stop":
30+
// no need to stop it (pebble)
31+
return nil
32+
case "reload":
33+
cmd = exec.Command("pebble", "signal", "SIGUSR2", "haproxy")
34+
cmd.Stdout = os.Stdout
35+
cmd.Stderr = os.Stderr
36+
return cmd.Run()
37+
case "restart":
38+
cmd = exec.Command("pebble", "restart", "haproxy")
39+
cmd.Stdout = os.Stdout
40+
cmd.Stderr = os.Stderr
41+
return cmd.Run()
42+
default:
43+
return fmt.Errorf("unknown command '%s'", action)
44+
}
45+
}
46+
47+
func (d *pebbleControl) UseAuxFile(useAuxFile bool) {
48+
// do nothing we always have it
49+
}
50+
51+
func (d *pebbleControl) SetAPI(api api.HAProxyClient) {
52+
// unused
53+
}

pkg/utils/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,5 @@ type OSArgs struct {
108108
DisableHTTP bool `long:"disable-http" description:"toggle to disable the HTTP frontend"`
109109
DisableIPV6 bool `long:"disable-ipv6" description:"toggle to disable the IPv6 protocol from all frontends"`
110110
DisableConfigSnippets string `long:"disable-config-snippets" description:"Allow to disable config snippets. List of comma separated values (possible values: all/global/backend/frontend)"`
111+
UseWithPebble bool `long:"with-pebble" description:"use pebble to start/stop/reload HAProxy"`
111112
}

0 commit comments

Comments
 (0)