From b829552b03f6f2eae109679e55b7ac170be6d01c Mon Sep 17 00:00:00 2001 From: Pranav Gaikwad Date: Wed, 12 Jun 2024 15:50:35 -0400 Subject: [PATCH] address feedback Signed-off-by: Pranav Gaikwad --- demo-output.yaml | 24 ++++++++++++++ docs/providers.md | 2 +- .../pkg/java_external_provider/provider.go | 32 ++++++++++++------- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/demo-output.yaml b/demo-output.yaml index b344e935..1a8090dc 100644 --- a/demo-output.yaml +++ b/demo-output.yaml @@ -899,6 +899,30 @@ matchingXML: "" effort: 1 insights: + java-downloaded-maven-artifact: + description: | + This rule tests the application downloaded from maven artifact + labels: + - tag=Java Operator SDK + incidents: + - uri: file:///java-project/src/main/java/io/javaoperatorsdk/operator/sample/QuarkusOperator.java + message: "" + codeSnip: " 1 package io.javaoperatorsdk.operator.sample;\n 2 \n 3 import io.fabric8.kubernetes.client.KubernetesClient;\n 4 import io.javaoperatorsdk.operator.Operator;\n 5 import io.javaoperatorsdk.operator.api.config.ConfigurationService;\n 6 import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;\n 7 import io.quarkus.runtime.Quarkus;\n 8 import io.quarkus.runtime.QuarkusApplication;\n 9 import io.quarkus.runtime.annotations.QuarkusMain;\n10 import javax.inject.Inject;\n11 \n12 @QuarkusMain\n13 public class QuarkusOperator implements QuarkusApplication {\n14 @Inject" + lineNumber: 4 + variables: + file: file:///java-project/src/main/java/io/javaoperatorsdk/operator/sample/QuarkusOperator.java + kind: Module + name: io.javaoperatorsdk.operator.Operator + package: io.javaoperatorsdk.operator.sample + - uri: file:///java-project/src/main/java/io/javaoperatorsdk/operator/sample/QuarkusOperator.java + message: "" + codeSnip: " 7 import io.quarkus.runtime.Quarkus;\n 8 import io.quarkus.runtime.QuarkusApplication;\n 9 import io.quarkus.runtime.annotations.QuarkusMain;\n10 import javax.inject.Inject;\n11 \n12 @QuarkusMain\n13 public class QuarkusOperator implements QuarkusApplication {\n14 @Inject\n15 KubernetesClient client;\n16 @Inject\n17 Operator operator;\n18 @Inject\n19 ConfigurationService configuration;\n20 @Inject\n21 CustomServiceController controller;\n22 \n23 public static void main(String... args) {\n24 Quarkus.run(QuarkusOperator.class, args);\n25 }\n26 \n27 public int run(String... args) throws Exception {" + lineNumber: 17 + variables: + file: file:///java-project/src/main/java/io/javaoperatorsdk/operator/sample/QuarkusOperator.java + kind: Field + name: operator + package: io.javaoperatorsdk.operator.sample multiple-actions-001: description: "" labels: diff --git a/docs/providers.md b/docs/providers.md index 3aa3eb8e..b58d7d0e 100644 --- a/docs/providers.md +++ b/docs/providers.md @@ -92,7 +92,7 @@ Here's an example config for `java` provider that is currently in-tree and does } ``` -The `location` can be a path to the application's source code or to a binary JAR, WAR, or EAR file. Optionally, coordinates to a maven artifact can be provided as input in the format `mvn://.:::`. The field `` is optional, it specifies a local path where the artifact will be downloaded. If not specified, provider will use a temporary directory to download it. +The `location` can be a path to the application's source code or to a binary JAR, WAR, or EAR file. Optionally, coordinates to a maven artifact can be provided as input in the format `mvn://:::@`. The field `` is optional, it specifies a local path where the artifact will be downloaded. If not specified, provider will use the current working directory to download it. The `java` provider also takes following options in `providerSpecificConfig`: diff --git a/external-providers/java-external-provider/pkg/java_external_provider/provider.go b/external-providers/java-external-provider/pkg/java_external_provider/provider.go index 520e77e9..e329a63a 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/provider.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/provider.go @@ -224,37 +224,47 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide // location can be a coordinate to a remote mvn artifact if strings.HasPrefix(config.Location, MvnURIPrefix) { mvnUri := strings.Replace(config.Location, MvnURIPrefix, "", 1) - // URI format is :::: + // URI format is :::@ // is optional & points to a local path where it will be downloaded - mvnUriParts := strings.Split(mvnUri, ":") - if len(mvnUriParts) < 4 || len(mvnUriParts) > 6 { + mvnCoordinates, destPath, _ := strings.Cut(mvnUri, "@") + mvnCoordinatesParts := strings.Split(mvnCoordinates, ":") + if mvnCoordinates == "" || len(mvnCoordinatesParts) < 3 { cancelFunc() - return nil, fmt.Errorf("invalid maven coordinates in location %s", config.Location) + return nil, fmt.Errorf("invalid maven coordinates in location %s, must be in format mvn://:::@", config.Location) } outputDir := "." - if len(mvnUriParts) == 5 { - if stat, err := os.Stat(mvnUriParts[len(mvnUriParts)-1]); err != nil || !stat.IsDir() { + if destPath != "" { + if stat, err := os.Stat(destPath); err != nil || !stat.IsDir() { cancelFunc() return nil, fmt.Errorf("output path does not exist or not a directory") } - outputDir = mvnUriParts[len(mvnUriParts)-1] + outputDir = destPath } mvnOptions := []string{ "dependency:copy", - fmt.Sprintf("-Dartifact=%s", strings.Join(mvnUriParts[:4], ":")), + fmt.Sprintf("-Dartifact=%s", mvnCoordinates), fmt.Sprintf("-DoutputDirectory=%s", outputDir), } if mavenSettingsFile != "" { mvnOptions = append(mvnOptions, "-s", mavenSettingsFile) } + log.Info("downloading maven artifact", "artifact", mvnCoordinates, "options", mvnOptions) cmd := exec.CommandContext(ctx, "mvn", mvnOptions...) cmd.Dir = outputDir if err := cmd.Run(); err != nil { cancelFunc() - return nil, fmt.Errorf("error downloading java artifact - %s", mvnUri) + return nil, fmt.Errorf("error downloading java artifact %s - %w", mvnUri, err) } - config.Location = filepath.Join(outputDir, - fmt.Sprintf("%s.%s", strings.Join(mvnUriParts[1:3], "-"), strings.ToLower(mvnUriParts[3]))) + downloadedPath := filepath.Join(outputDir, + fmt.Sprintf("%s.jar", strings.Join(mvnCoordinatesParts[1:3], "-"))) + if len(mvnCoordinatesParts) == 4 { + downloadedPath = filepath.Join(outputDir, + fmt.Sprintf("%s.%s", strings.Join(mvnCoordinatesParts[1:3], "-"), strings.ToLower(mvnCoordinatesParts[3]))) + } + if _, err := os.Stat(downloadedPath); err != nil { + return nil, fmt.Errorf("failed to download maven artifact to path %s - %w", downloadedPath, err) + } + config.Location = downloadedPath } extension := strings.ToLower(path.Ext(config.Location))