Skip to content
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

feat(cli/run): Support dependencies defined using HTTP URLs #3644

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions e2e/namespace/install/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,24 @@ func TestKamelCLIRun(t *testing.T) {
// Clean up
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
t.Run("Run with http dependency", func(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Elegant way to reuse the test we already have but with a different scope. Kudos!

Expect(KamelRunWithID(operatorID, ns, "../../../global/common/traits/files/jvm/Classpath.java",
"-d", "https://github.com/apache/camel-k/raw/main/e2e/global/common/traits/files/jvm/sample-1.0.jar",
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, "classpath"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationConditionStatus(ns, "classpath", v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, "classpath"), TestTimeoutShort).Should(ContainSubstring("Hello World!"))
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
t.Run("Run with http dependency using options", func(t *testing.T) {
Expect(KamelRunWithID(operatorID, ns, "../../../global/common/traits/files/jvm/Classpath.java",
"-d", "https://github.com/apache/camel-k/raw/main/e2e/global/common/traits/files/jvm/sample-1.0.jar",
"-d", "https://raw.githubusercontent.com/apache/camel-k/main/e2e/namespace/install/cli/files/Java.java|targetPath=/tmp/foo",
).Execute()).To(Succeed())
Eventually(IntegrationPodPhase(ns, "classpath"), TestTimeoutLong).Should(Equal(corev1.PodRunning))
Eventually(IntegrationConditionStatus(ns, "classpath", v1.IntegrationConditionReady), TestTimeoutShort).Should(Equal(corev1.ConditionTrue))
Eventually(IntegrationLogs(ns, "classpath"), TestTimeoutShort).Should(ContainSubstring("Hello World!"))
Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})
})
}
43 changes: 35 additions & 8 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ import (
"github.com/apache/camel-k/pkg/util/watch"
)

const usageDependency = `A dependency that should be included, e.g., "-d camel:mail" for a Camel component, "-d mvn:org.my:app:1.0" for a Maven dependency or "file://localPath[?targetPath=<path>&registry=<registry_URL>&skipChecksums=<true>&skipPOM=<true>]" for local files (experimental)`
const usageDependency = `A dependency that should be included, e.g., "-d camel:mail" for a Camel component, "-d mvn:org.my:app:1.0" for a Maven dependency, "-d http(s)://my-repo/my-dependency.jar|targetPath=<path>&registry=<registry_URL>&skipChecksums=<true>&skipPOM=<true>" for custom dependencies located on an http server or "file://localPath[?targetPath=<path>&registry=<registry_URL>&skipChecksums=<true>&skipPOM=<true>]" for local files (experimental)`

func newCmdRun(rootCmdOptions *RootCmdOptions) (*cobra.Command, *runCmdOptions) {
options := runCmdOptions{
Expand Down Expand Up @@ -798,16 +798,15 @@ func (o *runCmdOptions) convertToTraitParameter(c client.Client, value, traitPar
func (o *runCmdOptions) applyDependencies(cmd *cobra.Command, c client.Client, it *v1.Integration, name string) error {
var platform *v1.IntegrationPlatform
for _, item := range o.Dependencies {
// TODO: accept URLs
if strings.HasPrefix(item, "file://") {
if strings.HasPrefix(item, "file://") || strings.HasPrefix(item, "http://") || strings.HasPrefix(item, "https://") {
if platform == nil {
var err error
platform, err = o.getPlatform(cmd, c, it)
if err != nil {
return err
}
}
if err := o.uploadFileOrDirectory(platform, item, name, cmd, it); err != nil {
if err := o.uploadDependency(platform, item, name, cmd, it); err != nil {
return errors.Wrap(err, fmt.Sprintf("Error trying to upload %s to the Image Registry.", item))
}
} else {
Expand Down Expand Up @@ -966,10 +965,38 @@ func (o *runCmdOptions) getTargetPath() string {
return o.RegistryOptions.Get("targetPath")
}

func (o *runCmdOptions) uploadFileOrDirectory(platform *v1.IntegrationPlatform, item string, integrationName string, cmd *cobra.Command, integration *v1.Integration) error {
uri := parseFileURI(item)
o.RegistryOptions = uri.Query()
localPath, targetPath := uri.Path, o.getTargetPath()
func (o *runCmdOptions) uploadDependency(platform *v1.IntegrationPlatform, item string, integrationName string, cmd *cobra.Command, integration *v1.Integration) error {
var localPath string
if strings.HasPrefix(item, "http://") || strings.HasPrefix(item, "https://") {
idx := strings.LastIndex(item, "|")
var depURL string
if idx == -1 {
depURL = item
o.RegistryOptions = make(url.Values)
} else {
query := item[idx+1:]
options, err := url.ParseQuery(query)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("invalid http dependency options %s", query))
}
o.RegistryOptions = options
depURL = item[:idx]
}

uri, err := url.Parse(depURL)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("invalid http dependency url %s", depURL))
} else if localPath, err = downloadDependency(o.Context, *uri); err != nil {
return errors.Wrap(err, fmt.Sprintf("could not download http dependency %s", depURL))
}
// Remove the temporary file
defer os.Remove(localPath)
essobedo marked this conversation as resolved.
Show resolved Hide resolved
} else {
uri := parseFileURI(item)
o.RegistryOptions = uri.Query()
localPath = uri.Path
}
targetPath := o.getTargetPath()
options := o.getSpectrumOptions(platform, cmd)
dirName, err := getDirName(localPath)
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions pkg/cmd/run_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ package cmd
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"strings"

Expand Down Expand Up @@ -161,3 +166,33 @@ func fromMapToProperties(data interface{}, toString func(reflect.Value) string,
}
return result, nil
}

// downloadDependency downloads the file located at the given URL into a temporary folder and returns the local path to the generated temporary file.
func downloadDependency(ctx context.Context, url url.URL) (string, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return "", err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer res.Body.Close()
base := path.Base(url.Path)
if base == "." || base == "/" || filepath.Ext(base) == "" {
base = path.Base(url.String())
if base == "." || base == "/" {
base = "tmp"
}
}
out, err := os.CreateTemp("", fmt.Sprintf("*.%s", base))
if err != nil {
return "", err
}
defer out.Close()
_, err = io.Copy(out, res.Body)
if err != nil {
return "", err
}
return out.Name(), nil
}
39 changes: 39 additions & 0 deletions pkg/cmd/run_help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ limitations under the License.
package cmd

import (
"context"
"net/url"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -39,3 +43,38 @@ func TestFilterFileLocation(t *testing.T) {
assert.Equal(t, "app.properties", filteredOptions[1])
assert.Equal(t, "/validfile", filteredOptions[2])
}

func TestDownloadDependencyWithBadURL(t *testing.T) {
u, _ := url.Parse("http://foo")
_, err := downloadDependency(context.Background(), *u)
assert.NotNil(t, err)
}

func TestDownloadDependencyWithFileNameInURL(t *testing.T) {
u, _ := url.Parse("https://repo1.maven.org/maven2/org/apache/camel/camel-core/3.18.2/camel-core-3.18.2.jar")
path, err := downloadDependency(context.Background(), *u)
t.Cleanup(func() { os.Remove(path) })
assert.Nil(t, err)
assert.True(t, strings.HasSuffix(path, "camel-core-3.18.2.jar"), "The name of the jar file is expected")
_, err = os.Stat(path)
assert.Nil(t, err)
}

func TestDownloadDependencyWithFileNameInQuery(t *testing.T) {
u, _ := url.Parse("https://search.maven.org/remotecontent?filepath=org/apache/camel/quarkus/camel-quarkus-file/2.12.0/camel-quarkus-file-2.12.0.jar")
path, err := downloadDependency(context.Background(), *u)
t.Cleanup(func() { os.Remove(path) })
assert.Nil(t, err)
assert.True(t, strings.HasSuffix(path, "camel-quarkus-file-2.12.0.jar"), "The name of the jar file is expected")
_, err = os.Stat(path)
assert.Nil(t, err)
}

func TestDownloadDependencyWithoutFileName(t *testing.T) {
u, _ := url.Parse("https://search.maven.org")
path, err := downloadDependency(context.Background(), *u)
t.Cleanup(func() { os.Remove(path) })
assert.Nil(t, err)
_, err = os.Stat(path)
assert.Nil(t, err)
}