Skip to content

Commit 5f60f64

Browse files
committed
Changing build and sign to use Buildah instead of Kaniko
Because Kaniko is archived and thus not supported, it would be wise to change the build and sign feature to use buildah container instead of kaniko. That way we can also match the build and sign d/s to behave the same.
1 parent a5e13e4 commit 5f60f64

File tree

6 files changed

+225
-89
lines changed

6 files changed

+225
-89
lines changed

ci/kmm-kmod-dockerfile.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ data:
1515
gcc \
1616
linux-headers-${KERNEL_VERSION}
1717
18-
WORKDIR /usr/src
18+
RUN grep super-secret-value ci-build-secret
1919
20-
RUN grep super-secret-value /run/secrets/build-secret/ci-build-secret
20+
WORKDIR /usr/src
2121
2222
RUN git clone https://github.com/kubernetes-sigs/kernel-module-management.git
2323

config/manager-base/manager.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ spec:
4848
fieldRef:
4949
fieldPath: metadata.namespace
5050
- name: RELATED_IMAGE_BUILD
51-
value: gcr.io/kaniko-project/executor:latest
51+
value: quay.io/buildah/stable:latest
5252
- name: RELATED_IMAGE_SIGN
5353
value: signer
5454
securityContext:

internal/buildsign/resource/common.go

Lines changed: 108 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"strings"
1010
"text/template"
1111

12+
"k8s.io/utils/ptr"
13+
1214
kmmv1beta1 "github.com/kubernetes-sigs/kernel-module-management/api/v1beta1"
1315
"github.com/kubernetes-sigs/kernel-module-management/internal/api"
1416
"github.com/kubernetes-sigs/kernel-module-management/internal/constants"
@@ -34,11 +36,18 @@ var tmpl = template.Must(
3436
template.ParseFS(templateFS, "templates/Dockerfile.gotmpl"),
3537
)
3638

39+
func formatBuildArgs(buildArgs []kmmv1beta1.BuildArg) string {
40+
args := []string{}
41+
for _, ba := range buildArgs {
42+
args = append(args, "--build-arg", fmt.Sprintf("%s=%s", ba.Name, ba.Value))
43+
}
44+
return strings.Join(args, " ")
45+
}
46+
3747
func (rm *resourceManager) buildSpec(mld *api.ModuleLoaderData, destinationImg string, pushImage bool) v1.PodSpec {
3848

3949
buildConfig := mld.Build
4050

41-
args := containerArgs(mld, destinationImg, mld.Build.BaseImageRegistryTLS, pushImage)
4251
overrides := []kmmv1beta1.BuildArg{
4352
{Name: "KERNEL_VERSION", Value: mld.KernelVersion},
4453
{Name: "KERNEL_FULL_VERSION", Value: mld.KernelVersion},
@@ -49,19 +58,11 @@ func (rm *resourceManager) buildSpec(mld *api.ModuleLoaderData, destinationImg s
4958
buildConfig.BuildArgs,
5059
overrides...,
5160
)
52-
for _, ba := range buildArgs {
53-
args = append(args, "--build-arg", fmt.Sprintf("%s=%s", ba.Name, ba.Value))
54-
}
61+
buildArgsStr := formatBuildArgs(buildArgs)
5562

56-
kanikoImage := os.Getenv("RELATED_IMAGE_BUILD")
63+
args := buildContainerArgs(destinationImg, mld.Build.BaseImageRegistryTLS, pushImage, buildArgsStr)
5764

58-
if buildConfig.KanikoParams != nil && buildConfig.KanikoParams.Tag != "" {
59-
if idx := strings.IndexAny(kanikoImage, "@:"); idx != -1 {
60-
kanikoImage = kanikoImage[0:idx]
61-
}
62-
63-
kanikoImage += ":" + buildConfig.KanikoParams.Tag
64-
}
65+
buildahImage := os.Getenv("RELATED_IMAGE_BUILD")
6566

6667
selector := mld.Selector
6768
if len(mld.Build.Selector) != 0 {
@@ -74,9 +75,13 @@ func (rm *resourceManager) buildSpec(mld *api.ModuleLoaderData, destinationImg s
7475
Containers: []v1.Container{
7576
{
7677
Args: args,
77-
Name: "kaniko",
78-
Image: kanikoImage,
78+
Command: []string{"/bin/bash", "-c"},
79+
Name: "buildah-build",
80+
Image: buildahImage,
7981
VolumeMounts: volumeMounts,
82+
SecurityContext: &v1.SecurityContext{
83+
Privileged: ptr.To(true),
84+
},
8085
},
8186
},
8287
RestartPolicy: v1.RestartPolicyNever,
@@ -89,14 +94,15 @@ func (rm *resourceManager) buildSpec(mld *api.ModuleLoaderData, destinationImg s
8994
func signSpec(mld *api.ModuleLoaderData, destinationImg string, pushImage bool) v1.PodSpec {
9095

9196
signConfig := mld.Sign
92-
args := containerArgs(mld, destinationImg, signConfig.UnsignedImageRegistryTLS, pushImage)
97+
args := signContainerArgs(destinationImg, signConfig.UnsignedImageRegistryTLS, pushImage)
9398
volumes, volumeMounts := makeSignResourceVolumesAndVolumeMounts(signConfig, mld.ImageRepoSecret)
9499

95100
return v1.PodSpec{
96101
Containers: []v1.Container{
97102
{
98103
Args: args,
99-
Name: "kaniko",
104+
Command: []string{"/bin/bash", "-c"},
105+
Name: "buildah-sign",
100106
Image: os.Getenv("RELATED_IMAGE_BUILD"),
101107
VolumeMounts: volumeMounts,
102108
},
@@ -108,33 +114,98 @@ func signSpec(mld *api.ModuleLoaderData, destinationImg string, pushImage bool)
108114
}
109115
}
110116

111-
func containerArgs(mld *api.ModuleLoaderData, destinationImg string,
112-
tlsOptions kmmv1beta1.TLSOptions, pushImage bool) []string {
117+
type BuildOperation int
113118

114-
args := []string{}
119+
const (
120+
Build BuildOperation = iota
121+
Sign
122+
)
115123

116-
if pushImage {
117-
args = append(args, "--destination", destinationImg)
118-
if mld.RegistryTLS.Insecure {
119-
args = append(args, "--insecure")
120-
}
121-
if mld.RegistryTLS.InsecureSkipTLSVerify {
122-
args = append(args, "--skip-tls-verify")
123-
}
124-
} else {
125-
args = append(args, "--no-push")
126-
}
124+
// buildContainerArgs creates the script for building container images
125+
func buildContainerArgs(destinationImg string, tlsOptions kmmv1beta1.TLSOptions, pushImage bool, buildArgs string) []string {
126+
script := buildBuildahScript(destinationImg, tlsOptions, pushImage, buildArgs, Build)
127+
return []string{script}
128+
}
127129

128-
if tlsOptions.Insecure {
129-
args = append(args, "--insecure-pull")
130-
}
130+
// signContainerArgs creates the script for signing container images
131+
func signContainerArgs(destinationImg string, tlsOptions kmmv1beta1.TLSOptions, pushImage bool) []string {
132+
script := buildBuildahScript(destinationImg, tlsOptions, pushImage, "", Sign)
133+
return []string{script}
134+
}
131135

132-
if tlsOptions.InsecureSkipTLSVerify {
133-
args = append(args, "--skip-tls-verify-pull")
136+
// buildBuildahScript constructs the buildah script for build or sign operations
137+
func buildBuildahScript(destinationImg string, tlsOptions kmmv1beta1.TLSOptions, pushImage bool, buildArgs string, operation BuildOperation) string {
138+
tlsVerify := "true"
139+
if tlsOptions.InsecureSkipTLSVerify || tlsOptions.Insecure {
140+
tlsVerify = "false"
134141
}
135142

136-
return args
137-
143+
pushImageStr := "false"
144+
if pushImage {
145+
pushImageStr = "true"
146+
}
147+
148+
// Build the setup section based on operation type
149+
setupSection := `echo "setting up build context"
150+
mkdir -p /tmp/build-context
151+
cp /workspace/Dockerfile /tmp/build-context/
152+
# Copy build secrets into build context so they're accessible during build
153+
for secret_dir in /run/secrets/*/; do
154+
if [ -d "$secret_dir" ]; then
155+
echo "copying secrets from $secret_dir to build context"
156+
cp -r "$secret_dir"* /tmp/build-context/
157+
fi
158+
done`
159+
160+
// If we do sign instead
161+
if operation == Sign {
162+
setupSection = `echo "setting up build context with cert and key files"
163+
mkdir -p /tmp/build-context
164+
cp /workspace/Dockerfile /tmp/build-context/
165+
cp /run/secrets/cert/cert.pem /tmp/build-context/cert.pem
166+
cp /run/secrets/key/key.pem /tmp/build-context/key.pem`
167+
}
168+
169+
// Build command section
170+
buildCmd := "buildah bud"
171+
if buildArgs != "" && operation == Build {
172+
buildCmd = fmt.Sprintf("buildah bud %s", buildArgs)
173+
}
174+
175+
actionDescription := "build"
176+
pushDescription := "image"
177+
if operation == Sign {
178+
actionDescription = "build for signing"
179+
pushDescription = "signed image"
180+
}
181+
182+
script := fmt.Sprintf(`
183+
export IMAGE="%s"
184+
export PUSH_IMAGE="%s"
185+
186+
%s
187+
188+
echo "starting Buildah %s for $IMAGE"
189+
%s \
190+
--tls-verify=%s \
191+
--storage-driver=vfs \
192+
-f /tmp/build-context/Dockerfile \
193+
-t "$IMAGE" \
194+
/tmp/build-context
195+
196+
if [ "$PUSH_IMAGE" = "true" ]; then
197+
echo "pushing %s $IMAGE..."
198+
buildah push \
199+
--tls-verify=%s \
200+
--storage-driver=vfs \
201+
"$IMAGE" \
202+
"docker://$IMAGE"
203+
else
204+
echo "skipping push step (PUSH_IMAGE=$PUSH_IMAGE)"
205+
fi
206+
`, destinationImg, pushImageStr, setupSection, actionDescription, buildCmd, tlsVerify, pushDescription, tlsVerify)
207+
208+
return script
138209
}
139210

140211
func (rm *resourceManager) getBuildHashAnnotationValue(ctx context.Context, configMapName, namespace string,

0 commit comments

Comments
 (0)