Skip to content

Commit 236b74f

Browse files
author
Mengqi Yu
authored
make wrapper server work with distroless-based images (#3070)
1 parent f454d9a commit 236b74f

File tree

5 files changed

+59
-31
lines changed

5 files changed

+59
-31
lines changed

porch/config/deploy/2-function-runner.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ spec:
4040
image: gcr.io/example-google-project-id/porch-function-runner:latest
4141
imagePullPolicy: IfNotPresent
4242
command:
43-
- sh
44-
- -c
45-
- /server --config=/config.yaml --functions=/functions --pod-namespace=porch-fn-system --wrapper-server-image=gcr.io/example-google-project-id/porch-wrapper-server:latest
43+
- /server
44+
- --config=/config.yaml
45+
- --functions=/functions
46+
- --pod-namespace=porch-fn-system
47+
env:
48+
- name: WRAPPER_SERVER_IMAGE
49+
value: gcr.io/example-google-project-id/porch-wrapper-server:latest
4650
ports:
4751
- containerPort: 9445
4852
# Add grpc readiness probe to ensure the cache is ready

porch/func/Dockerfile-wrapperserver

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ FROM golang:1.17.8-alpine3.15 as builder
22

33
WORKDIR /go/src/github.com/GoogleContainerTools/kpt
44

5+
# Ensure the wrapper server and grpc-health-probe is statically linked so that they works in distroless-based images.
6+
ENV CGO_ENABLED=0
7+
58
COPY go.mod go.sum ./
69
COPY porch/go.mod porch/go.sum porch/
710
COPY porch/api/go.mod porch/api/go.sum porch/api/

porch/func/internal/podevaluator.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,12 @@ func (pcm *podCacheManager) podCacheManager() {
266266
channels := pcm.waitlists[resp.image]
267267
delete(pcm.waitlists, resp.image)
268268
for i := range channels {
269-
// The channel has one buffer size, nothing will be blocking.
270-
channels[i] <- &clientConnAndError{
271-
grpcClient: resp.grpcClient,
272-
err: resp.err,
269+
cce := &clientConnAndError{err: resp.err}
270+
if resp.podAndGRPCClient != nil {
271+
cce.grpcClient = resp.podAndGRPCClient.grpcClient
273272
}
273+
// The channel has one buffer size, nothing will be blocking.
274+
channels[i] <- cce
274275
}
275276
case <-tick:
276277
// synchronous GC
@@ -497,8 +498,10 @@ func (pm *podManager) retrieveOrCreatePod(ctx context.Context, image string, ttl
497498
Name: "copy-wrapper-server",
498499
Image: pm.wrapperServerImage,
499500
Command: []string{
500-
"sh", "-c",
501-
fmt.Sprintf("cp /wrapper-server/* %v", volumeMountPath),
501+
"cp",
502+
"-a",
503+
"/wrapper-server/.",
504+
volumeMountPath,
502505
},
503506
VolumeMounts: []corev1.VolumeMount{
504507
{
@@ -510,12 +513,9 @@ func (pm *podManager) retrieveOrCreatePod(ctx context.Context, image string, ttl
510513
},
511514
Containers: []corev1.Container{
512515
{
513-
Name: "function",
514-
Image: image,
515-
Command: []string{
516-
"sh", "-c",
517-
strings.Join(cmd, " "),
518-
},
516+
Name: "function",
517+
Image: image,
518+
Command: cmd,
519519
ReadinessProbe: &corev1.Probe{
520520
ProbeHandler: corev1.ProbeHandler{
521521
Exec: &corev1.ExecAction{

porch/func/server/server.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,19 @@ import (
3333
const (
3434
execRuntime = "exec"
3535
podRuntime = "pod"
36+
37+
wrapperServerImageEnv = "WRAPPER_SERVER_IMAGE"
3638
)
3739

3840
var (
39-
port = flag.Int("port", 9445, "The server port")
40-
functions = flag.String("functions", "./functions", "Path to cached functions.")
41-
config = flag.String("config", "./config.yaml", "Path to the config file.")
42-
podCacheConfig = flag.String("pod-cache-config", "/pod-cache-config/pod-cache-config.yaml", "Path to the pod cache config file. The file is map of function name to TTL.")
43-
podNamespace = flag.String("pod-namespace", "porch-fn-system", "Namespace to run KRM functions pods.")
44-
podTTL = flag.Duration("pod-ttl", 30*time.Minute, "TTL for pods before GC.")
45-
scanInterval = flag.Duration("scan-interval", time.Minute, "The interval of GC between scans.")
46-
wrapperServerImage = flag.String("wrapper-server-image", "", "Image name of the wrapper server.")
47-
disableRuntimes = flag.String("disable-runtimes", "", fmt.Sprintf("The runtime(s) to disable. Multiple runtimes should separated by `,`. Available runtimes: `%v`, `%v`.", execRuntime, podRuntime))
41+
port = flag.Int("port", 9445, "The server port")
42+
functions = flag.String("functions", "./functions", "Path to cached functions.")
43+
config = flag.String("config", "./config.yaml", "Path to the config file.")
44+
podCacheConfig = flag.String("pod-cache-config", "/pod-cache-config/pod-cache-config.yaml", "Path to the pod cache config file. The file is map of function name to TTL.")
45+
podNamespace = flag.String("pod-namespace", "porch-fn-system", "Namespace to run KRM functions pods.")
46+
podTTL = flag.Duration("pod-ttl", 30*time.Minute, "TTL for pods before GC.")
47+
scanInterval = flag.Duration("scan-interval", time.Minute, "The interval of GC between scans.")
48+
disableRuntimes = flag.String("disable-runtimes", "", fmt.Sprintf("The runtime(s) to disable. Multiple runtimes should separated by `,`. Available runtimes: `%v`, `%v`.", execRuntime, podRuntime))
4849
)
4950

5051
func main() {
@@ -83,7 +84,11 @@ func run() error {
8384
}
8485
runtimes = append(runtimes, execEval)
8586
case podRuntime:
86-
podEval, err := internal.NewPodEvaluator(*podNamespace, *wrapperServerImage, *scanInterval, *podTTL, *podCacheConfig)
87+
wrapperServerImage := os.Getenv(wrapperServerImageEnv)
88+
if wrapperServerImage == "" {
89+
return fmt.Errorf("environment variable %v must be set to use pod function evaluator runtime", wrapperServerImageEnv)
90+
}
91+
podEval, err := internal.NewPodEvaluator(*podNamespace, wrapperServerImage, *scanInterval, *podTTL, *podCacheConfig)
8792
if err != nil {
8893
return fmt.Errorf("failed to initialize pod evaluator: %w", err)
8994
}

porch/hack/create-deployment-blueprint.sh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,30 @@ function customize-image {
126126
"newTag=${TAG}"
127127
}
128128

129-
function customize-image-in-command {
129+
function customize-image-in-env {
130130
local OLD="${1}"
131131
local NEW="${2}"
132+
local TAG="${NEW##*:}"
133+
local IMG="${NEW%:*}"
134+
135+
cat > set-image-config.yaml << EOF
136+
apiVersion: fn.kpt.dev/v1alpha1
137+
kind: SetImage
138+
metadata:
139+
name: my-func-config
140+
image:
141+
name: ${OLD}
142+
newName: ${IMG}
143+
newTag: ${TAG}
144+
additionalImageFields:
145+
- group: apps
146+
version: v1
147+
kind: Deployment
148+
path: spec/template/spec/containers[]/env[]/value
149+
EOF
132150

133-
kpt fn eval "${DESTINATION}" --image search-replace:v0.2.0 -- \
134-
"by-path=spec.template.spec.containers[*].command[*]" \
135-
"by-value-regex=(.*)${OLD}" \
136-
"put-value=\${1}${NEW}"
151+
kpt fn eval "${DESTINATION}" --image set-image:v0.1.0 --fn-config set-image-config.yaml
152+
rm set-image-config.yaml
137153
}
138154

139155
function customize-sa {
@@ -170,7 +186,7 @@ function main() {
170186
customize-image \
171187
"gcr.io/example-google-project-id/porch-controllers:latest" \
172188
"${CONTROLLERS_IMAGE}"
173-
customize-image-in-command \
189+
customize-image-in-env \
174190
"gcr.io/example-google-project-id/porch-wrapper-server:latest" \
175191
"${WRAPPER_SERVER_IMAGE}"
176192

0 commit comments

Comments
 (0)