Skip to content

Commit

Permalink
Fix apache#2834: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpoth committed Feb 2, 2022
1 parent 875c872 commit ab4a3f5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 66 deletions.
106 changes: 61 additions & 45 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,58 +594,23 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C
return nil, err
}

list := v1.NewIntegrationPlatformList()
if err := c.List(o.Context, &list, ctrl.InNamespace(integration.Namespace)); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("could not retrieve integration platform from namespace %s", o.Namespace))
}
if len(list.Items) > 1 {
return nil, fmt.Errorf("expected 1 integration platform in the namespace, found: %d", len(list.Items))
} else if len(list.Items) == 0 {
return nil, errors.New("no integration platforms found in the namespace: run \"kamel install\" to install the platform")
}
platform := list.Items[0]
registry := platform.Spec.Build.Registry.Address
insecure := platform.Spec.Build.Registry.Insecure
var platform *v1.IntegrationPlatform

for _, item := range o.Dependencies {
// TODO: accept URLs
// TODO: accept other resources through Maven types (i.e not just JARs)
if strings.HasPrefix(item, "file://") && strings.HasSuffix(item, ".jar") {
newStdR, newStdW, _ := os.Pipe()
defer newStdW.Close()

fileInfo, err := os.Stat(item[6:])
if err != nil {
return nil, err
}
mapping := item[6:] + ":" + "."

version := defaults.Version
artifactId := name + "-" + strings.TrimSuffix(fileInfo.Name(), ".jar")
artifactPath := "/org/apache/camel/k/external/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar"
artifactPath = strings.ToLower(artifactPath)
target := registry + artifactPath + ":" + version
options := spectrum.Options{
PullInsecure: true,
PushInsecure: insecure,
PullConfigDir: "",
PushConfigDir: "",
Base: "",
Target: target,
Stdout: cmd.OutOrStdout(),
Stderr: cmd.OutOrStderr(),
Recursive: false,
if platform == nil {
// let's also enable the registry trait
o.Traits = append(o.Traits, "registry.enabled=true")
platform, err = getPlatform(c, o.Context, integration.Namespace)
if err != nil {
return nil, err
}
}

go readSpectrumLogs(newStdR)
_, err = spectrum.Build(options, mapping)
if err != nil {
return nil, err
if err := uploadDependency(platform, item, name, cmd, integration); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Error trying to upload %s to the Image Registry.", item))
}

// let's enable the registry trait
o.Traits = append(o.Traits, "registry.enabled=true")
integration.Spec.AddDependency("mvn:org.apache.camel.k.external:" + artifactId + ":" + version)
} else {
integration.Spec.AddDependency(item)
}
Expand Down Expand Up @@ -942,6 +907,57 @@ func configureTrait(config map[string]interface{}, trait interface{}) error {
return decoder.Decode(config)
}

func getPlatform(c client.Client, context context.Context, ns string) (*v1.IntegrationPlatform, error) {
list := v1.NewIntegrationPlatformList()
if err := c.List(context, &list, ctrl.InNamespace(ns)); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("could not retrieve integration platform from namespace %s", ns))
}
if len(list.Items) > 1 {
return nil, fmt.Errorf("expected 1 integration platform in the namespace, found: %d", len(list.Items))
} else if len(list.Items) == 0 {
return nil, errors.New("no integration platforms found in the namespace: run \"kamel install\" to install the platform")
}
return &list.Items[0], nil
}

func uploadDependency(platform *v1.IntegrationPlatform, item string, name string, cmd *cobra.Command, integration *v1.Integration) error {
registry := platform.Spec.Build.Registry.Address
insecure := platform.Spec.Build.Registry.Insecure
newStdR, newStdW, _ := os.Pipe()
defer newStdW.Close()

fileInfo, err := os.Stat(item[6:])
if err != nil {
return err
}
mapping := item[6:] + ":" + "."

version := defaults.Version
artifactId := name + "-" + strings.TrimSuffix(fileInfo.Name(), ".jar")
artifactPath := "/org/apache/camel/k/external/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar"
artifactPath = strings.ToLower(artifactPath)
target := registry + artifactPath + ":" + version
options := spectrum.Options{
PullInsecure: true,
PushInsecure: insecure,
PullConfigDir: "",
PushConfigDir: "",
Base: "",
Target: target,
Stdout: cmd.OutOrStdout(),
Stderr: cmd.OutOrStderr(),
Recursive: false,
}

go readSpectrumLogs(newStdR)
_, err = spectrum.Build(options, mapping)
if err != nil {
return err
}
integration.Spec.AddDependency("mvn:org.apache.camel.k.external:" + artifactId + ":" + version)
return nil
}

func readSpectrumLogs(newStdOut *os.File) {
scanner := bufio.NewScanner(newStdOut)

Expand Down
44 changes: 23 additions & 21 deletions pkg/trait/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ func newRegistryTrait() Trait {
}
}

// InfluencesKit overrides base class method
func (t *registryTrait) InfluencesKit() bool {
return true
}

func (t *registryTrait) Configure(e *Environment) (bool, error) {
//by default disable
// disabled by default
if IsNilOrFalse(t.Enabled) {
return false, nil
}
Expand All @@ -45,26 +50,23 @@ func (t *registryTrait) Configure(e *Environment) (bool, error) {
}

func (t *registryTrait) Apply(e *Environment) error {
if e.IntegrationKitInPhase(v1.IntegrationKitPhaseBuildSubmitted) {
build := getBuilderTask(e.BuildTasks)
ext := v1.MavenArtifact{
GroupID: "com.github.johnpoth",
ArtifactID: "wagon-oci-distribution",
Version: "1.0-SNAPSHOT",
}
policy := v1.RepositoryPolicy{
Enabled: true,
}
repo := v1.Repository{
ID: "image-registry",
URL: "oci://" + e.Platform.Spec.Build.Registry.Address,
Snapshots: policy,
Releases: policy,
}
// configure Maven to lookup dependencies in the Image registry
build.Maven.Repositories = append(build.Maven.Repositories, repo)
build.Maven.Extension = append(build.Maven.Extension, ext)
build := getBuilderTask(e.BuildTasks)
ext := v1.MavenArtifact{
GroupID: "com.github.johnpoth",
ArtifactID: "wagon-oci-distribution",
Version: "1.0-SNAPSHOT",
}

policy := v1.RepositoryPolicy{
Enabled: true,
}
repo := v1.Repository{
ID: "image-registry",
URL: "oci://" + e.Platform.Spec.Build.Registry.Address,
Snapshots: policy,
Releases: policy,
}
// configure Maven to lookup dependencies in the Image registry
build.Maven.Repositories = append(build.Maven.Repositories, repo)
build.Maven.Extension = append(build.Maven.Extension, ext)
return nil
}

0 comments on commit ab4a3f5

Please sign in to comment.