Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
  • Loading branch information
pranavgaikwad committed Jun 12, 2024
1 parent 4a12c6a commit b829552
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
24 changes: 24 additions & 0 deletions demo-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion docs/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://<group-id>.<artifact-id>:<version>:<classifier>:<path>`. The field `<path>` 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://<group-id>:<artifact-id>:<version>:<classifier>@<path>`. The field `<path>` 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`:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <group>:<artifact>:<version>:<classifier>:<path>
// URI format is <group>:<artifact>:<version>:<classifier>@<path>
// <path> 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://<group>:<artifact>:<version>:<classifier>@<path>", 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))
Expand Down

0 comments on commit b829552

Please sign in to comment.