Skip to content

Commit

Permalink
Fix apache#2834: support local dependencies through the Image repository
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpoth committed Dec 15, 2021
1 parent 4eda7fc commit d575e66
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 4 deletions.
74 changes: 70 additions & 4 deletions pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package cmd

import (
"bufio"
"context"
"encoding/json"
"fmt"
Expand All @@ -29,6 +30,7 @@ import (
"strings"
"syscall"

spectrum "github.com/container-tools/spectrum/pkg/builder"
"github.com/magiconair/properties"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
Expand All @@ -46,9 +48,11 @@ import (
"github.com/apache/camel-k/pkg/client"
"github.com/apache/camel-k/pkg/trait"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/defaults"
"github.com/apache/camel-k/pkg/util/dsl"
"github.com/apache/camel-k/pkg/util/kubernetes"
k8slog "github.com/apache/camel-k/pkg/util/kubernetes/log"
"github.com/apache/camel-k/pkg/util/log"
"github.com/apache/camel-k/pkg/util/property"
"github.com/apache/camel-k/pkg/util/sync"
"github.com/apache/camel-k/pkg/util/watch"
Expand Down Expand Up @@ -225,13 +229,13 @@ func (o *runCmdOptions) validate() error {
}

// Deprecation warning
if o.PropertyFiles != nil {
if o.PropertyFiles != nil && len(o.PropertyFiles) != 0 {
fmt.Println("Warn: --property-file has been deprecated. You should use --property file:/path/to/conf.properties instead.")
}
if o.ConfigMaps != nil {
if o.ConfigMaps != nil && len(o.ConfigMaps) != 0 {
fmt.Println("Warn: --configmap has been deprecated. You should use --config configmap:my-configmap instead.")
}
if o.Secrets != nil {
if o.Secrets != nil && len(o.Secrets) != 0 {
fmt.Println("Warn: --secret has been deprecated. You should use --config secret:my-secret instead.")
}

Expand Down Expand Up @@ -584,8 +588,61 @@ func (o *runCmdOptions) createOrUpdateIntegration(cmd *cobra.Command, c client.C
}
}

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

for _, item := range o.Dependencies {
integration.Spec.AddDependency(item)
// 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,
}

go readSpectrumLogs(newStdR)
_, err = spectrum.Build(options, mapping)
if err != nil {
return nil, err
}

// 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)
}
}
for _, item := range o.PropertyFiles {
// Deprecated: making it compatible with newer mechanism
Expand Down Expand Up @@ -867,3 +924,12 @@ func configureTrait(config map[string]interface{}, trait interface{}) error {

return decoder.Decode(config)
}

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

for scanner.Scan() {
line := scanner.Text()
log.Infof(line)
}
}
70 changes: 70 additions & 0 deletions pkg/trait/registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package trait

import (
v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
)

// The Registry trait sets up Maven to use the Image registry
// as a Maven repository
//
// +camel-k:trait=registry
type registryTrait struct {
BaseTrait `property:",squash"`
}

func newRegistryTrait() Trait {
return &registryTrait{
BaseTrait: NewBaseTrait("registry", 1650),
}
}

func (t *registryTrait) Configure(e *Environment) (bool, error) {
//by default disable
if IsNilOrFalse(t.Enabled) {
return false, nil
}

return e.IntegrationKitInPhase(v1.IntegrationKitPhaseBuildSubmitted), nil
}

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)
}

return nil
}
1 change: 1 addition & 0 deletions pkg/trait/trait_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func init() {
AddToTraits(newPrometheusTrait)
AddToTraits(newPullSecretTrait)
AddToTraits(newQuarkusTrait)
AddToTraits(newRegistryTrait)
AddToTraits(newRouteTrait)
AddToTraits(newServiceTrait)
AddToTraits(newServiceBindingTrait)
Expand Down

0 comments on commit d575e66

Please sign in to comment.