Skip to content

chore(cni-plugin): make script stylistically more consistent #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 55 commits into from
Jun 18, 2025

Conversation

sdickhoven
Copy link
Contributor

@sdickhoven sdickhoven commented Jun 9, 2025

this pr makes no functional changes but makes the cni-plugin/deployment/scripts/install-cni.sh script stylistically more consistent.

feel free to do with this pr what you will.

my goal was simply to do a bit of cleanup to make it more "enjoyable" to work on this script. 🧹

but feel free to close this pr if you don't mind the inconsistencies.

@sdickhoven sdickhoven requested a review from a team as a code owner June 9, 2025 23:20
sdickhoven and others added 28 commits June 11, 2025 08:57
Signed-off-by: Simon Dickhoven <[email protected]>
Signed-off-by: Simon Dickhoven <[email protected]>
Signed-off-by: Simon Dickhoven <[email protected]>
Signed-off-by: Simon Dickhoven <[email protected]>
Signed-off-by: Simon Dickhoven <[email protected]>
Signed-off-by: Simon Dickhoven <[email protected]>
Bumps [k8s.io/client-go](https://github.com/kubernetes/client-go) from 0.32.0 to 0.32.2.
- [Changelog](https://github.com/kubernetes/client-go/blob/master/CHANGELOG.md)
- [Commits](kubernetes/client-go@v0.32.0...v0.32.2)

---
updated-dependencies:
- dependency-name: k8s.io/client-go
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [docker/setup-qemu-action](https://github.com/docker/setup-qemu-action) from 3.2.0 to 3.4.0.
- [Release notes](https://github.com/docker/setup-qemu-action/releases)
- [Commits](docker/setup-qemu-action@49b3bc8...4574d27)

---
updated-dependencies:
- dependency-name: docker/setup-qemu-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [sigstore/cosign-installer](https://github.com/sigstore/cosign-installer) from 3.7.0 to 3.8.0.
- [Release notes](https://github.com/sigstore/cosign-installer/releases)
- [Commits](sigstore/cosign-installer@dc72c7d...c56c2d3)

---
updated-dependencies:
- dependency-name: sigstore/cosign-installer
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.30.0 to 0.33.0.
- [Commits](golang/net@v0.30.0...v0.33.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps golang from 1.23-alpine to 1.24-alpine.

---
updated-dependencies:
- dependency-name: golang
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…rd#468)

Bumps [DavidAnson/markdownlint-cli2-action](https://github.com/davidanson/markdownlint-cli2-action) from 18.0.0 to 19.1.0.
- [Release notes](https://github.com/davidanson/markdownlint-cli2-action/releases)
- [Commits](DavidAnson/markdownlint-cli2-action@eb5ca3a...05f3221)

---
updated-dependencies:
- dependency-name: DavidAnson/markdownlint-cli2-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [softprops/action-gh-release](https://github.com/softprops/action-gh-release) from 2.2.0 to 2.2.1.
- [Release notes](https://github.com/softprops/action-gh-release/releases)
- [Changelog](https://github.com/softprops/action-gh-release/blob/master/CHANGELOG.md)
- [Commits](softprops/action-gh-release@7b4da11...c95fe14)

---
updated-dependencies:
- dependency-name: softprops/action-gh-release
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [linkerd/dev](https://github.com/linkerd/dev) from 44 to 45.
- [Commits](linkerd/dev@v44...v45)

---
updated-dependencies:
- dependency-name: linkerd/dev
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…linkerd#480)

Required to unblock CI

```console
$ cargo update ring
    Updating crates.io index
     Locking 4 packages to latest compatible versions
    Updating cc v1.0.83 -> v1.2.14
    Updating libc v0.2.151 -> v0.2.169
    Updating ring v0.17.7 -> v0.17.9
      Adding shlex v1.3.0
    Removing spin v0.9.8

$ cargo update openssl
    Updating crates.io index
     Locking 2 packages to latest compatible versions
    Updating openssl v0.10.66 -> v0.10.71
    Updating openssl-sys v0.9.103 -> v0.9.106
```
…#481)

Bumps [github.com/spf13/cobra](https://github.com/spf13/cobra) from 1.8.1 to 1.9.1.
- [Release notes](https://github.com/spf13/cobra/releases)
- [Commits](spf13/cobra@v1.8.1...v1.9.1)

---
updated-dependencies:
- dependency-name: github.com/spf13/cobra
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [actions/cache](https://github.com/actions/cache) from 4.2.0 to 4.2.1.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](actions/cache@1bd1e32...0c907a7)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
linkerd#484)

The test was failing with:

```
  cargo:warning=Compiler family detection failed due to error: ToolNotFound: Failed to find tool. Is `clang-19` installed?
```

Using linkerd/dev/actions/setup-rust ensures the proper dependencies are set up.

Also, pinned to ubuntu-22.04 for now as ubuntu-latest is lacking everything this build requires.
* fix(linkerd-cni): improve SA token rotation detection

This makes the logic introduced in linkerd#440 more robust, by watching over
the proper file change to trigger the kubeconfig file re-creation.
See linkerd/linkerd2#12573 (comment)
…linkerd#488)

Bumps [EmbarkStudios/cargo-deny-action](https://github.com/embarkstudios/cargo-deny-action) from 2.0.4 to 2.0.5.
- [Release notes](https://github.com/embarkstudios/cargo-deny-action/releases)
- [Commits](EmbarkStudios/cargo-deny-action@e2f4ede...13fd9ef)

---
updated-dependencies:
- dependency-name: EmbarkStudios/cargo-deny-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…erd#487)

Bumps [sigstore/cosign-installer](https://github.com/sigstore/cosign-installer) from 3.8.0 to 3.8.1.
- [Release notes](https://github.com/sigstore/cosign-installer/releases)
- [Commits](sigstore/cosign-installer@c56c2d3...d7d6bc7)

---
updated-dependencies:
- dependency-name: sigstore/cosign-installer
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps alpine from 3.21.0 to 3.21.3.

---
updated-dependencies:
- dependency-name: alpine
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps golang from 1.23-alpine to 1.24-alpine.

---
updated-dependencies:
- dependency-name: golang
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…linkerd#489)

Bumps [EmbarkStudios/cargo-deny-action](https://github.com/embarkstudios/cargo-deny-action) from 2.0.5 to 2.0.6.
- [Release notes](https://github.com/embarkstudios/cargo-deny-action/releases)
- [Commits](EmbarkStudios/cargo-deny-action@13fd9ef...0484eed)

---
updated-dependencies:
- dependency-name: EmbarkStudios/cargo-deny-action
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…erd#491)

Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 4.1.8 to 4.1.9.
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](actions/download-artifact@fa0a91b...cc20338)

---
updated-dependencies:
- dependency-name: actions/download-artifact
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
olix0r and others added 17 commits June 11, 2025 08:58
cargo-deny-action is broken: EmbarkStudios/cargo-deny-action#91

This change replaces the action with a manual invocation via the dev
container image.
Bumps [tokio](https://github.com/tokio-rs/tokio) from 1.35.1 to 1.38.2.
- [Release notes](https://github.com/tokio-rs/tokio/releases)
- [Commits](tokio-rs/tokio@tokio-1.35.1...tokio-1.38.2)

---
updated-dependencies:
- dependency-name: tokio
  dependency-version: 1.38.2
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…erd#518)

Fixes linkerd/linkerd2#13976

When the Linkerd CNI-plugin acts on a pod with a `config.linkerd.io/skip-inbound-ports` annotation, it replaces the inbound skip ports that the CNI-plugin was configured with instead of adding to them.  The CNI-plugin is configured with the Linkerd proxy's admin and tap ports as skip inbound ports.  Therefore, on any pod with the `config.linkerd.io/skip-inbound-ports` annotation, the admin and tap ports will no longer be skipped by the iptables redirect rules, making these proxy ports inaccessible and causing tap and promethus scraping to no longer function.

Instead of replacing, we append the ports specified in `config.linkerd.io/skip-inbound-ports` to the inbound skip ports.  This allows the admin and tap ports to continue to skip iptables redirection and reach the proxy as desired.

This matches the behavior of the linkerd-init container: https://github.com/linkerd/linkerd2/blob/main/charts/partials/templates/_proxy-init.tpl#L25

Tested manually on k3d with Calico and validating that tap works as expected on workloads with `config.linkerd.io/skip-inbound-ports` configured.  Due to the architecture of the cni-plugin executable, it is not well set up to be tested automatically without further refactoring so we omit automated tests here.

Signed-off-by: Alex Leong <[email protected]>
…erd#517)

Bumps [actions/download-artifact](https://github.com/actions/download-artifact) from 4.1.9 to 4.3.0.
- [Release notes](https://github.com/actions/download-artifact/releases)
- [Commits](actions/download-artifact@cc20338...d3f86a1)

---
updated-dependencies:
- dependency-name: actions/download-artifact
  dependency-version: 4.3.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…erd#510)

Bumps [sigstore/cosign-installer](https://github.com/sigstore/cosign-installer) from 3.8.1 to 3.8.2.
- [Release notes](https://github.com/sigstore/cosign-installer/releases)
- [Commits](sigstore/cosign-installer@d7d6bc7...3454372)

---
updated-dependencies:
- dependency-name: sigstore/cosign-installer
  dependency-version: 3.8.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [ring](https://github.com/briansmith/ring) from 0.17.9 to 0.17.14.
- [Changelog](https://github.com/briansmith/ring/blob/main/RELEASES.md)
- [Commits](https://github.com/briansmith/ring/commits)

---
updated-dependencies:
- dependency-name: ring
  dependency-version: 0.17.14
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [golang.org/x/net](https://github.com/golang/net) from 0.33.0 to 0.38.0.
- [Commits](golang/net@v0.33.0...v0.38.0)

---
updated-dependencies:
- dependency-name: golang.org/x/net
  dependency-version: 0.38.0
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…nkerd#509)

Bumps [softprops/action-gh-release](https://github.com/softprops/action-gh-release) from 2.2.1 to 2.2.2.
- [Release notes](https://github.com/softprops/action-gh-release/releases)
- [Changelog](https://github.com/softprops/action-gh-release/blob/master/CHANGELOG.md)
- [Commits](softprops/action-gh-release@c95fe14...da05d55)

---
updated-dependencies:
- dependency-name: softprops/action-gh-release
  dependency-version: 2.2.2
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…3.0 (linkerd#506)

Bumps [github.com/containernetworking/cni](https://github.com/containernetworking/cni) from 1.2.3 to 1.3.0.
- [Release notes](https://github.com/containernetworking/cni/releases)
- [Commits](containernetworking/cni@v1.2.3...v1.3.0)

---
updated-dependencies:
- dependency-name: github.com/containernetworking/cni
  dependency-version: 1.3.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [actions/cache](https://github.com/actions/cache) from 4.2.2 to 4.2.3.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](actions/cache@d4323d4...5a3ec84)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [docker/login-action](https://github.com/docker/login-action) from 3.3.0 to 3.4.0.
- [Release notes](https://github.com/docker/login-action/releases)
- [Commits](docker/login-action@9780b0c...74a5d14)

---
updated-dependencies:
- dependency-name: docker/login-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps [DavidAnson/markdownlint-cli2-action](https://github.com/davidanson/markdownlint-cli2-action) from 19.1.0 to 20.0.0.
- [Release notes](https://github.com/davidanson/markdownlint-cli2-action/releases)
- [Commits](DavidAnson/markdownlint-cli2-action@05f3221...992badc)

---
updated-dependencies:
- dependency-name: DavidAnson/markdownlint-cli2-action
  dependency-version: 20.0.0
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Bumps alpine from 3.21.3 to 3.22.0.

---
updated-dependencies:
- dependency-name: alpine
  dependency-version: 3.22.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
…inkerd#527)

Bumps [actions/checkout](https://github.com/actions/checkout) from 3df4ab11eba7bda6032a0b82a6bb43b11571feac to 09d2acae674a48949e3602304ab46fd20ae0c42f.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](actions/checkout@3df4ab1...09d2aca)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '09d2acae674a48949e3602304ab46fd20ae0c42f'
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@sdickhoven sdickhoven force-pushed the CHORE-fix-inconsistencies branch from 3ff3825 to 834ed72 Compare June 11, 2025 14:17
Signed-off-by: Simon Dickhoven <[email protected]>
SERVICEACCOUNT_TOKEN=$(cat ${SERVICEACCOUNT_PATH}/token)
SERVICEACCOUNT_TOKEN=$(cat "$SERVICEACCOUNT_PATH/token")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"dangerous" not to quote command arguments. what if $SERVICEACCOUNT_PATH has a space char in it?

Comment on lines 193 to 195
sed -i s~__KUBECONFIG_FILEPATH__~"${DEST_CNI_NET_DIR}/${KUBECONFIG_FILE_NAME}"~g ${TMP_CONF}
sed -i s~__KUBECONFIG_FILEPATH__~"$DEST_CNI_NET_DIR/$KUBECONFIG_FILE_NAME"~g "$TMP_CONF"

log "CNI config: $(cat ${TMP_CONF})"
log "CNI config: $(cat "$TMP_CONF")"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"dangerous" not to quote command arguments. what if $TMP_CONF has a space char in it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...and, yes, bash understands how to correctly interpret "$(... "...")"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'm surprised shellcheck doesn't complain about this. it's a common rookie mistake. 🙂

$ file="a b"
$ touch "$file"
$ ls "$file"
a b
$ ls $file
ls: a: No such file or directory
ls: b: No such file or directory

Comment on lines 322 to 349
config_files=$(find "${HOST_CNI_NET}" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \))
config_files=$(find "$HOST_CNI_NET" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \) | grep -v linkerd || true)
if [ -z "$config_files" ]; then
log "No active CNI configuration files found"
log "No active CNI configuration files found"
else
config_file_count=$(echo "$config_files" | grep -v linkerd | sort | wc -l)
if [ "$config_file_count" -eq 0 ]; then
log "No active CNI configuration files found"
else
Copy link
Contributor Author

Choose a reason for hiding this comment

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

combine the two ifs into one.

Copy link

@cratelyn cratelyn left a comment

Choose a reason for hiding this comment

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

i left a few questions below, noting that there are some semantic changes in this proposal.

i'm not sure about the utility of removing the braces from parameter expansion, personally. do we stand to benefit at all by using $FOO rather than ${FOO}?

i'll defer to @alpeb's judgement about that, but my inclination is that refraining from changing that would make the other more interesting changes related to e.g. conditional branches more reviewable.

Comment on lines 34 to 35
log "${1}"
log "$1"

Choose a reason for hiding this comment

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

may i ask, why are we opting to remove the surrounding braces throughout this script?

Copy link
Contributor Author

@sdickhoven sdickhoven Jun 11, 2025

Choose a reason for hiding this comment

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

yes. good question. i'm just going for consistency.

"$foo" == ${foo}

i'm happy going with either but the script is using both. i'm just trying to add consistency.

this is entirely a stylistic nit. 100% non-functional.

the braces are typically used for two reasons:

  1. to do string manipulation - e.g. ${foo:-default_value} or ${foo//a/b}
  2. to remove ambiguity - e.g. $foobar != ${foo}bar

but there's nothing wrong with always using braces.

however, the script currently uses them sometimes (when they are not actually required for any functional reasons) and sometimes not. that's all.

Choose a reason for hiding this comment

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

got it. my personal vote would be to use braces, rather than remove them. as you mention, these are often used to remove ambiguity or to otherwise avoid accidentally expanding a different variable than intended.

i'll cede to @alpeb's opinion on that, however.

Copy link
Contributor Author

@sdickhoven sdickhoven Jun 16, 2025

Choose a reason for hiding this comment

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

happy to update the pr to switch all variables to using braces.

i have no strong preference one way or the other. i just like consistency. that's all.

...but i realize that most people probably couldn't care less about the consistent use of braces here. 🙂

rest assured that it won't hurt my feelings if y'all decide to simply close this pr. 😌

...though i do think that we should (double)quote all variables that are used as command line arguments:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my personal vote would be to use braces, rather than remove them.

i have just pushed a commit that flips all variables to using braces.

Comment on lines 342 to 349
config_files=$(find "${HOST_CNI_NET}" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \))
config_files=$(find "$HOST_CNI_NET" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \) | grep -v linkerd || true)

Choose a reason for hiding this comment

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

can you explain what this is doing? this seems like a change to the logic of this script, versus the cosmetic/stylistic changes happening elsewhere in this diff.

Copy link
Contributor Author

@sdickhoven sdickhoven Jun 11, 2025

Choose a reason for hiding this comment

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

this is a simplification.

right now, the script checks if there are any files in /host/etc/cni/net.d and if not it prints "No active CNI configuration files found":

config_files=$(find "${HOST_CNI_NET}" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \))
if [ -z "$config_files" ]; then
  log "No active CNI configuration files found"
else

in the else case, the script then removes any files from the $config_files variable that have the (sub)string linkerd in them and counts how many files are left (the sort is completely nonsensical here!).

and if there are no files left (i.e. wc -l == number of lines == 0) then it again prints "No active CNI configuration files found":

  config_file_count=$(echo "$config_files" | grep -v linkerd | sort | wc -l)
  if [ "$config_file_count" -eq 0 ]; then
    log "No active CNI configuration files found"
  else

here are a couple of examples:


/host/etc/cni/net.d has no files:

[ -z "" ] # true

-> first if statement is true .: "No active CNI configuration files found"


/host/etc/cni/net.d has the following file:

  • something-linkerd-something
[ -z "something-linkerd-something" ] # false
echo "something-linkerd-something" | grep -v linkerd | wc -l # 0
[ "0" -eq 0 ] # true

-> first if statement is false, second if statement is true .: "No active CNI configuration files found"


/host/etc/cni/net.d has the following files:

  • something-linkerd-something
  • something-else
[ -z "something-linkerd-something
something-else" ] # false
echo "something-linkerd-something
something-else" | grep -v linkerd | wc -l # 1
[ "1" -eq 0 ] # false

-> find files and patch them


anyway, my logic just finds all files in /host/etc/cni/net.d and immediately removes any files that have the (sub)string linkerd in them and then checks whether any files are left:

config_files=$(find "$HOST_CNI_NET" -maxdepth 1 -type f \( -iname '*conflist' -o -iname '*conf' \) | grep -v linkerd || true)
if [ -z "$config_files" ]; then
...

this is much more straight forward and easier to understand:

  1. find all files that don't have the (sub)string linkerd in them
  2. if what was found is an empty string ... else ...

you may be wondering about the || true at the end.

this is due to the fact that the script configures bash with set -e (== exit immediately when a command exits with a non-zero exit status) and that grep foo or grep -v foo will exit with an exit status of 1 (which would then cause the entire script to exit) when no output is produced.

$ grep foo <<< foobarbaz
foobarbaz
$ echo $?
0

$ grep foo <<< noobarbaz
$ echo $?
1

$ grep -v foo <<< foobarbaz
$ echo $?
1

$ grep -v foo <<< noobarbaz
noobarbaz
$ echo $?
0

the || construct simply means "if exit status is non-zero do this instead" and the command true simply does nothing and exits with a zero exit status.

$ true
$ echo $?
0

$ false
$ echo $?
1

so the || true simply means: if the exit status is non-zero (which is entirely expected and totally fine), change it to zero so that the script doesn't exit (due to set -e).

by the way, the same thing can be accomplished with

config_files=$(find "$HOST_CNI_NET" -maxdepth 1 -type f ! -name '*linkerd*' \( -iname '*conflist' -o -iname '*conf' \))

actually... i like that better. i'll update... because then we don't need that somewhat mysterious || true.

Copy link
Contributor Author

@sdickhoven sdickhoven Jun 11, 2025

Choose a reason for hiding this comment

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

ok. so now we are trading

  config_file_count=$(echo "$config_files" | grep -v linkerd | sort | wc -l)
  if [ "$config_file_count" -eq 0 ]; then
    log "No active CNI configuration files found"
  else

for

! -name '*linkerd*' in the find command.

to get the same exact behavior.

by the way, it's a bit incongruent that files with linkerd in them are not excluded from the subsequent find that drives the actual patching.

i would personally get rid of that second find as well and just use $config_files but that would actually be a slightly different behavior and this pr is all about not making any functional changes.

@alpeb
Copy link
Member

alpeb commented Jun 12, 2025

Would you please sync again with main so your fix is picked up?

@alpeb alpeb changed the title CHORE: make script stylistically more consistent chore(cni-plugin): make script stylistically more consistent Jun 18, 2025
Copy link
Member

@alpeb alpeb left a comment

Choose a reason for hiding this comment

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

Thanks @sdickhoven for the polishments and @cratelyn for helping getting this through 👍
On its final state this looks good to me 👍

@alpeb alpeb merged commit c4ca3c5 into linkerd:main Jun 18, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants