Repository: kumahq/kuma
Pull request #3191 (2021-11-18 15:04:01)

fix(kuma-dp) parse Envoy version (backport #3188)
This is an automatic backport of pull request #3188 done by [Mergify](https://mergify.com).


---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- `@Mergifyio refresh` will re-evaluate the rules
- `@Mergifyio rebase` will rebase this PR on its base branch
- `@Mergifyio update` will merge the base branch into this PR
- `@Mergifyio backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>



Patch:
diff --git a/app/kuma-dp/pkg/dataplane/envoy/envoy.go b/app/kuma-dp/pkg/dataplane/envoy/envoy.go
index 1dd29c402..e34414c58 100644
--- a/app/kuma-dp/pkg/dataplane/envoy/envoy.go
+++ b/app/kuma-dp/pkg/dataplane/envoy/envoy.go
@@ -226,10 +226,13 @@ func (e *Envoy) version() (*EnvoyVersion, error) {
 	build = regexp.MustCompile(`version:(.*)`).FindString(build)
 	build = strings.Trim(build, "version:")
 	build = strings.Trim(build, " ")
-	version := regexp.MustCompile(`/([0-9.]+)/`).FindString(build)
-	version = strings.Trim(version, "/")
+
+	parts := strings.Split(build, "/")
+	if len(parts) != 5 { // revision/build_version_number/revision_status/build_type/ssl_version
+		return nil, errors.Errorf("wrong Envoy build format: %s", build)
+	}
 	return &EnvoyVersion{
 		Build:   build,
-		Version: version,
+		Version: parts[1],
 	}, nil
 }
diff --git a/tools/envoy/fetch.sh b/tools/envoy/fetch.sh
index f51f7ad87..50d2b7237 100755
--- a/tools/envoy/fetch.sh
+++ b/tools/envoy/fetch.sh
@@ -45,11 +45,6 @@ if [ "${ENVOY_DISTRO}" == "centos7" ]; then
   ENVOY_DISTRO="centos"
 fi
 
-FILE_EXTENSION=""
-if [ "${ENVOY_DISTRO}" == "windows" ]; then
-  FILE_EXTENSION=".exe"
-fi
-
 if [[ -n "${ENVOY_COMMIT_HASH}" ]]; then
   ENVOY_SHORT_HASH=${ENVOY_COMMIT_HASH:0:8}
 
@@ -66,7 +61,7 @@ fi
 
 if [[ -n "${ENVOY_TAG}" ]]; then
   ENVOY_VERSION=${ENVOY_TAG:1}
-  BINARY_NAME="envoy-${ENVOY_VERSION}-${ENVOY_DISTRO}${FILE_EXTENSION}"
+  BINARY_NAME="envoy-${ENVOY_VERSION}-${ENVOY_DISTRO}"
 
   download_envoy "${BINARY_NAME}"
   exit 0


Test patch:
diff --git a/app/kuma-dp/cmd/testdata/envoy-mock.sleep.sh b/app/kuma-dp/cmd/testdata/envoy-mock.sleep.sh
index 208bf00b7..a11aa7248 100755
--- a/app/kuma-dp/cmd/testdata/envoy-mock.sleep.sh
+++ b/app/kuma-dp/cmd/testdata/envoy-mock.sleep.sh
@@ -7,7 +7,7 @@ fi
 if [ "$1" = "--version" ];
 then
     echo $1 >> ${ENVOY_MOCK_CMDLINE_FILE}
-    printf "\n50ef0945fa2c5da4bff7627c3abf41fdd3b7cffd/1.15.0/clean-getenvoy-2aa564b-envoy/RELEASE/BoringSSL\n"
+    printf "\nversion: 50ef0945fa2c5da4bff7627c3abf41fdd3b7cffd/1.15.0/clean-getenvoy-2aa564b-envoy/RELEASE/BoringSSL\n"
     exit 0
 else
     for arg in "$@"; do
diff --git a/app/kuma-dp/pkg/dataplane/envoy/envoy_test.go b/app/kuma-dp/pkg/dataplane/envoy/envoy_test.go
index d2be8b4a5..0da9e15ca 100644
--- a/app/kuma-dp/pkg/dataplane/envoy/envoy_test.go
+++ b/app/kuma-dp/pkg/dataplane/envoy/envoy_test.go
@@ -279,6 +279,30 @@ var _ = Describe("Envoy", func() {
 			Expect(version.Build).To(Equal("50ef0945fa2c5da4bff7627c3abf41fdd3b7cffd/1.15.0/clean-getenvoy-2aa564b-envoy/RELEASE/BoringSSL"))
 		})
 
+		It("should properly read envoy version with label for unix-based systems", func() {
+			// given
+			cfg := kuma_dp.Config{
+				DataplaneRuntime: kuma_dp.DataplaneRuntime{
+					BinaryPath: filepath.Join("testdata", "envoy-mock.with-label.sh"),
+					ConfigDir:  configDir,
+				},
+			}
+
+			// when
+			dataplane, err := New(Opts{
+				Config: cfg,
+				Stdout: &bytes.Buffer{},
+				Stderr: &bytes.Buffer{},
+			})
+			Expect(err).ToNot(HaveOccurred())
+			version, err := dataplane.version()
+
+			// then
+			Expect(err).ToNot(HaveOccurred())
+			Expect(version.Version).To(Equal("1.20.0-dev"))
+			Expect(version.Build).To(Equal("50ef0945fa2c5da4bff7627c3abf41fdd3b7cffd/1.20.0-dev/clean-getenvoy-2aa564b-envoy/RELEASE/BoringSSL"))
+		})
+
 		It("should properly read envoy version for windows", func() {
 			// given
 			cfg := kuma_dp.Config{
diff --git a/app/kuma-dp/pkg/dataplane/envoy/testdata/envoy-mock.with-label.sh b/app/kuma-dp/pkg/dataplane/envoy/testdata/envoy-mock.with-label.sh
new file mode 100755
index 000000000..7299e9974
--- /dev/null
+++ b/app/kuma-dp/pkg/dataplane/envoy/testdata/envoy-mock.with-label.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+if [ "$1" = "--version" ];
+then
+  printf "\nenvoy  version: 50ef0945fa2c5da4bff7627c3abf41fdd3b7cffd/1.20.0-dev/clean-getenvoy-2aa564b-envoy/RELEASE/BoringSSL\n\n"
+  exit 0
+fi
+
+# print arguments to verify in the test
+echo $@