Skip to content

Make the container image smaller#753

Merged
k8s-ci-robot merged 1 commit intokubernetes:masterfrom
thockin:v4_smaller_image
Jun 12, 2023
Merged

Make the container image smaller#753
k8s-ci-robot merged 1 commit intokubernetes:masterfrom
thockin:v4_smaller_image

Conversation

@thockin
Copy link
Copy Markdown
Member

@thockin thockin commented Jun 9, 2023

For each package and binary we need, this pulls in all the files and deps (shared libs, mostly). The build is slower but the final image is 85 MB (versus 157 MB before). e2e passes.

This is based on scripts used in kubernetes and KinD.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 9, 2023
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested review from nan-yu and stp-ip June 9, 2023 23:37
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 9, 2023
Copy link
Copy Markdown
Member

@BenTheElder BenTheElder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one nit :-)

Comment thread stage_binaries.sh Outdated
dpkg -s "${package}" > "${staging}/var/lib/dpkg/status.d/${package}"
}

# helper because bash sometimes barfs on embedded comments
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I can't really tell from this comment and the usage context why this is needed. At the call site it's also not obvious what's happening without going back and reading this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grep treats "no match" as an error, which trips up pipefail and errexit

$ echo foo | grep bar; echo $?
1

So we want to do:

$ echo foo | { grep bar || true; }; echo $?
0

Adding back the comments in the style of binary_to_libraries():

$ echo foo \
    `# comment1` \
    | { grep "bar" || true; } \
    `# comment2`; echo $?
bash: syntax error near unexpected token ``# comment2`'

Parens fails the same way.

It works without the braces and || true, but returns error status

$ echo foo     `# comment1`     | grep "bar"     `# comment2`; echo $?
1

Once bash parses {} or () it can't handle subsequent #comment blocks. happy() hides that. I could rename it :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grep treats "no match" as an error, which trips up pipefail and errexit

instead of swallowing all errors, you can actually detect the no-match case and ignore that

https://github.com/kubernetes-sigs/kind/blob/2e6414694724a44803caff801eeee2b62a5b6765/images/base/files/usr/local/bin/entrypoint#L41-L44

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grep_allow_nomatch() {
  # grep exits 0 on match, 1 on no match, 2 on error
  grep "$@" || [ $? = 1 ]
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cute, I guess I should actually read the man page :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL. I want to run a scanner against it to be sure it works, when I get a moment

@BenTheElder
Copy link
Copy Markdown
Member

For each package and binary we need, this pulls in all the files and deps (shared libs, mostly). The build is slower but the final image is 85 MB (versus 157 MB before). e2e passes.

And hopefully less CVEs.

@thockin thockin force-pushed the v4_smaller_image branch from 1f52e3b to d360a68 Compare June 12, 2023 18:49
For each package and binary we need, this pulls in all the files and
deps (shared libs, mostly).  The build is slower but the final image is
85 MB (versus 157 MB before).  e2e passes.  Hopefully less CVE surface.

This is based on scripts used in kubernetes and KinD.
@thockin thockin force-pushed the v4_smaller_image branch from d360a68 to f037087 Compare June 12, 2023 19:22
Copy link
Copy Markdown
Member

@BenTheElder BenTheElder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jun 12, 2023
@k8s-ci-robot k8s-ci-robot merged commit b60eef6 into kubernetes:master Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants