Skip to content

Commit

Permalink
Use regex to check whether runner image has pulsarctl (#682)
Browse files Browse the repository at this point in the history
* Update methods for checking whether runner image has pulsarctl

* Fix style

* Fix unittest
  • Loading branch information
jiangpengcheng authored Aug 24, 2023
1 parent 7358f44 commit de8f46c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
13 changes: 8 additions & 5 deletions controllers/spec/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package spec
import (
"bytes"
"context"
"regexp"

autoscalingv2beta2 "k8s.io/api/autoscaling/v2beta2"

Expand Down Expand Up @@ -65,11 +66,9 @@ const (
PulsarAdminExecutableFile = "/pulsar/bin/pulsar-admin"
WorkDir = "/pulsar/"

JavaRunnerImageHasPulsarctl = "pulsar-functions-pulsarctl-java-runner"
PythonRunnerImageHasPulsarctl = "pulsar-functions-pulsarctl-python-runner"
GolangRunnerImageHasPulsarctl = "pulsar-functions-pulsarctl-go-runner"
RunnerImageHasPulsarctl = "pulsar-functions-(pulsarctl|sn)-(java|python|go)-runner"

PulsarctlExecutableFile = "/usr/local/bin/pulsarctl"
PulsarctlExecutableFile = "pulsarctl"
DownloaderName = "downloader"
DownloaderVolume = "downloader-volume"
DownloaderImage = DefaultRunnerPrefix + "pulsarctl:2.10.2.3"
Expand Down Expand Up @@ -379,7 +378,7 @@ func MakeGoFunctionCommand(downloadPath, goExecFilePath string, function *v1alph
// prepend download command if the downPath is provided
hasPulsarctl := function.Spec.ImageHasPulsarctl
hasWget := function.Spec.ImageHasWget
if strings.Contains(function.Spec.Image, GolangRunnerImageHasPulsarctl) {
if match, _ := regexp.MatchString(RunnerImageHasPulsarctl, function.Spec.Image); match {
hasPulsarctl = true
hasWget = true
}
Expand Down Expand Up @@ -533,6 +532,7 @@ func getPulsarAdminCommand(authProvided, tlsProvided bool, tlsConfig TLSConfig,
func getPulsarctlCommand(authProvided, tlsProvided bool, tlsConfig TLSConfig,
authConfig *v1alpha1.AuthConfig) []string {
args := []string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url",
"$webServiceURL",
Expand All @@ -541,6 +541,7 @@ func getPulsarctlCommand(authProvided, tlsProvided bool, tlsConfig TLSConfig,
if authConfig != nil {
if authConfig.OAuth2Config != nil {
args = []string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"context",
"set",
Expand Down Expand Up @@ -568,6 +569,7 @@ func getPulsarctlCommand(authProvided, tlsProvided bool, tlsConfig TLSConfig,
}...)
} else if authConfig.GenericAuth != nil {
args = []string{
"export PATH=$PATH:/pulsar/bin && ",
"( " + PulsarctlExecutableFile,
"oauth2",
"activate",
Expand All @@ -585,6 +587,7 @@ func getPulsarctlCommand(authProvided, tlsProvided bool, tlsConfig TLSConfig,
}
} else if authProvided {
args = []string{
"export PATH=$PATH:/pulsar/bin && ",
"( " + PulsarctlExecutableFile,
"oauth2",
"activate",
Expand Down
11 changes: 11 additions & 0 deletions controllers/spec/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestGetDownloadCommand(t *testing.T) {
// test get the download command with package name
{"function://public/default/test@v1", "function-package.jar", nil, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"packages", "download", "function://public/default/test@v1", "--path", "function-package.jar",
Expand All @@ -57,6 +58,7 @@ func TestGetDownloadCommand(t *testing.T) {
},
{"sink://public/default/test@v1", "sink-package.jar", nil, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"packages", "download", "sink://public/default/test@v1", "--path", "sink-package.jar",
Expand All @@ -65,6 +67,7 @@ func TestGetDownloadCommand(t *testing.T) {
},
{"source://public/default/test@v1", "source-package.jar", nil, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"packages", "download", "source://public/default/test@v1", "--path", "source-package.jar",
Expand All @@ -74,6 +77,7 @@ func TestGetDownloadCommand(t *testing.T) {
// test get the download command with normal name
{"/test", "test.jar", nil, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"functions", "download", "--path", "/test", "--destination-file", "test.jar",
Expand All @@ -83,6 +87,7 @@ func TestGetDownloadCommand(t *testing.T) {
// test get the download command with a wrong package name
{"source/public/default/test@v1", "source-package.jar", nil, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"functions", "download", "--path", "source/public/default/test@v1", "--destination-file", "source-package.jar",
Expand All @@ -91,6 +96,7 @@ func TestGetDownloadCommand(t *testing.T) {
},
{"source:/public/default/test@v1", "source-package.jar", nil, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"functions", "download", "--path", "source:/public/default/test@v1", "--destination-file", "source-package.jar",
Expand All @@ -106,6 +112,7 @@ func TestGetDownloadCommand(t *testing.T) {
KeySecretKey: "auth.json",
}, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"context",
"set",
Expand Down Expand Up @@ -138,6 +145,7 @@ func TestGetDownloadCommand(t *testing.T) {
},
}, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"--tls-allow-insecure=false",
Expand All @@ -158,6 +166,7 @@ func TestGetDownloadCommand(t *testing.T) {
},
}, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"packages", "download", "function://public/default/test@v1", "--path", "function-package.jar",
Expand All @@ -175,6 +184,7 @@ func TestGetDownloadCommand(t *testing.T) {
},
}, nil, nil,
[]string{
"export PATH=$PATH:/pulsar/bin && ",
PulsarctlExecutableFile,
"--admin-service-url", "$webServiceURL",
"--tls-allow-insecure=true",
Expand All @@ -198,6 +208,7 @@ func TestGetDownloadCommand(t *testing.T) {
ClientAuthenticationPlugin: "auth-plugin",
},
[]string{
"export PATH=$PATH:/pulsar/bin && ",
"( " + PulsarctlExecutableFile,
"oauth2",
"activate",
Expand Down
20 changes: 7 additions & 13 deletions controllers/spec/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package spec

import (
"strings"
"regexp"

"github.com/streamnative/function-mesh/utils"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -112,9 +112,7 @@ func MakeFunctionCleanUpJob(function *v1alpha1.Function) *v1.Job {
}
}
hasPulsarctl := function.Spec.ImageHasPulsarctl
if strings.Contains(function.Spec.Image, JavaRunnerImageHasPulsarctl) ||
strings.Contains(function.Spec.Image, PythonRunnerImageHasPulsarctl) ||
strings.Contains(function.Spec.Image, GolangRunnerImageHasPulsarctl) {
if match, _ := regexp.MatchString(RunnerImageHasPulsarctl, function.Spec.Image); match {
hasPulsarctl = true
}
command := getCleanUpCommand(hasPulsarctl,
Expand Down Expand Up @@ -210,12 +208,12 @@ func makeFunctionCommand(function *v1alpha1.Function) []string {

hasPulsarctl := function.Spec.ImageHasPulsarctl
hasWget := function.Spec.ImageHasWget
if match, _ := regexp.MatchString(RunnerImageHasPulsarctl, function.Spec.Image); match {
hasPulsarctl = true
hasWget = true
}
if spec.Java != nil {
if spec.Java.Jar != "" {
if strings.Contains(function.Spec.Image, JavaRunnerImageHasPulsarctl) {
hasPulsarctl = true
hasWget = true
}
return MakeJavaFunctionCommand(spec.Java.JarLocation, spec.Java.Jar,
spec.Name, spec.ClusterName,
generateJavaLogConfigCommand(function.Spec.Java, function.Spec.LogTopicAgent),
Expand All @@ -231,13 +229,9 @@ func makeFunctionCommand(function *v1alpha1.Function) []string {
}
} else if spec.Python != nil {
if spec.Python.Py != "" {
if strings.Contains(function.Spec.Image, PythonRunnerImageHasPulsarctl) {
hasPulsarctl = true
hasWget = true
}
return MakePythonFunctionCommand(spec.Python.PyLocation, spec.Python.Py,
spec.Name, spec.ClusterName,
generatePythonLogConfigCommand(function.Name, function.Spec.Python, function.Spec.LogTopicAgent),
generatePythonLogConfigCommand(function.Name, function.Spec.Python, function.Spec.LogTopicAgent),
generateFunctionDetailsInJSON(function), string(function.UID), hasPulsarctl, hasWget,
spec.Pulsar.AuthSecret != "", spec.Pulsar.TLSSecret != "", function.Spec.SecretsMap,
function.Spec.StateConfig, function.Spec.Pulsar.TLSConfig, function.Spec.Pulsar.AuthConfig)
Expand Down
6 changes: 3 additions & 3 deletions controllers/spec/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package spec

import (
"strings"
"regexp"

"github.com/streamnative/function-mesh/utils"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -159,7 +159,7 @@ func MakeSinkCleanUpJob(sink *v1alpha1.Sink) *v1.Job {
}
}
imageHasPulsarctl := sink.Spec.ImageHasPulsarctl
if strings.Contains(sink.Spec.Image, JavaRunnerImageHasPulsarctl) {
if match, _ := regexp.MatchString(RunnerImageHasPulsarctl, sink.Spec.Image); match {
imageHasPulsarctl = true
}
command := getCleanUpCommand(imageHasPulsarctl,
Expand Down Expand Up @@ -203,7 +203,7 @@ func MakeSinkCommand(sink *v1alpha1.Sink) []string {
spec := sink.Spec
hasPulsarctl := sink.Spec.ImageHasPulsarctl
hasWget := sink.Spec.ImageHasWget
if strings.Contains(spec.Image, JavaRunnerImageHasPulsarctl) {
if match, _ := regexp.MatchString(RunnerImageHasPulsarctl, sink.Spec.Image); match {
hasPulsarctl = true
hasWget = true
}
Expand Down
6 changes: 3 additions & 3 deletions controllers/spec/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package spec

import (
"fmt"
"strings"
"regexp"

"github.com/streamnative/function-mesh/utils"
"google.golang.org/protobuf/encoding/protojson"
Expand Down Expand Up @@ -150,7 +150,7 @@ func makeSourceCommand(source *v1alpha1.Source) []string {
spec := source.Spec
hasPulsarctl := source.Spec.ImageHasPulsarctl
hasWget := source.Spec.ImageHasWget
if strings.Contains(spec.Image, JavaRunnerImageHasPulsarctl) {
if match, _ := regexp.MatchString(RunnerImageHasPulsarctl, source.Spec.Image); match {
hasPulsarctl = true
hasWget = true
}
Expand Down Expand Up @@ -201,7 +201,7 @@ func MakeSourceCleanUpJob(source *v1alpha1.Source) *v1.Job {
}

hasPulsarctl := source.Spec.ImageHasPulsarctl
if strings.Contains(source.Spec.Image, JavaRunnerImageHasPulsarctl) {
if match, _ := regexp.MatchString(RunnerImageHasPulsarctl, source.Spec.Image); match {
hasPulsarctl = true
}
command := getCleanUpCommand(hasPulsarctl,
Expand Down

0 comments on commit de8f46c

Please sign in to comment.