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

chore(build): Cancellable Maven operations #2348

Merged
merged 2 commits into from
Jun 2, 2021
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
8 changes: 0 additions & 8 deletions pkg/apis/camel/v1/integrationplatform_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,6 @@ func (b IntegrationPlatformBuildSpec) GetTimeout() metav1.Duration {
return *b.Timeout
}

// GetTimeout returns the specified duration or a default one
func (m MavenSpec) GetTimeout() metav1.Duration {
if m.Timeout == nil {
return metav1.Duration{}
}
return *m.Timeout
}

var _ ResourceCondition = IntegrationPlatformCondition{}

// GetConditions --
Expand Down
15 changes: 7 additions & 8 deletions pkg/builder/quarkus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package builder

import (
"context"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -145,10 +146,9 @@ func GenerateQuarkusProjectCommon(camelQuarkusVersion string, runtimeVersion str
}

func buildQuarkusRunner(ctx *builderContext) error {
mc := maven.NewContext(path.Join(ctx.Path, "maven"), ctx.Maven.Project)
mc := maven.NewContext(path.Join(ctx.Path, "maven"))
mc.SettingsContent = ctx.Maven.SettingsData
mc.LocalRepository = ctx.Build.Maven.LocalRepository
mc.Timeout = ctx.Build.Maven.GetTimeout().Duration

if ctx.Maven.TrustStoreName != "" {
mc.ExtraMavenOpts = append(mc.ExtraMavenOpts,
Expand All @@ -157,15 +157,15 @@ func buildQuarkusRunner(ctx *builderContext) error {
)
}

err := BuildQuarkusRunnerCommon(mc)
err := BuildQuarkusRunnerCommon(ctx.C, mc, ctx.Maven.Project)
if err != nil {
return err
}

return nil
}

func BuildQuarkusRunnerCommon(mc maven.Context) error {
func BuildQuarkusRunnerCommon(ctx context.Context, mc maven.Context, project maven.Project) error {
resourcesPath := path.Join(mc.Path, "src", "main", "resources")
if err := os.MkdirAll(resourcesPath, os.ModePerm); err != nil {
return errors.Wrap(err, "failure while creating resource folder")
Expand All @@ -182,19 +182,18 @@ func BuildQuarkusRunnerCommon(mc maven.Context) error {

mc.AddArgument("package")

// Build the project
if err := maven.Run(mc); err != nil {
// Run the Maven goal
if err := project.Command(mc).Do(ctx); err != nil {
return errors.Wrap(err, "failure while building project")
}

return nil
}

func computeQuarkusDependencies(ctx *builderContext) error {
mc := maven.NewContext(path.Join(ctx.Path, "maven"), ctx.Maven.Project)
mc := maven.NewContext(path.Join(ctx.Path, "maven"))
mc.SettingsContent = ctx.Maven.SettingsData
mc.LocalRepository = ctx.Build.Maven.LocalRepository
mc.Timeout = ctx.Build.Maven.GetTimeout().Duration

// Process artifacts list and add it to existing artifacts.
artifacts, err := ProcessQuarkusTransitiveDependencies(mc)
Expand Down
5 changes: 2 additions & 3 deletions pkg/cmd/local_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,14 @@ func (command *localBuildCmdOptions) run(cmd *cobra.Command, args []string) erro
var dependenciesList, propertyFilesList []string
routeFiles := args
if !command.BaseImage {
// Fetch dependencies.
dependencies, err := getDependencies(args, command.AdditionalDependencies, command.MavenRepositories, true)
dependencies, err := getDependencies(command.Context, args, command.AdditionalDependencies, command.MavenRepositories, true)
if err != nil {
return err
}

var propertyFiles []string
if !command.DependenciesOnly {
// Manage integration properties which may come from files or CLI.
// Manage integration properties which may come from files or CLI
propertyFiles, err = updateIntegrationProperties(command.Properties, command.PropertyFiles, false)
if err != nil {
return err
Expand Down
6 changes: 1 addition & 5 deletions pkg/cmd/local_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ type localInspectCmdOptions struct {
}

func (command *localInspectCmdOptions) validate(args []string) error {
// Validate integration files.
err := validateIntegrationFiles(args)
if err != nil {
return err
}

// Validate additional dependencies specified by the user.
err = validateAdditionalDependencies(command.AdditionalDependencies)
if err != nil {
return err
Expand All @@ -93,13 +91,11 @@ func (command *localInspectCmdOptions) init() error {
}

func (command *localInspectCmdOptions) run(args []string) error {
// Fetch dependencies.
dependencies, err := getDependencies(args, command.AdditionalDependencies, command.MavenRepositories, command.AllDependencies)
dependencies, err := getDependencies(command.Context, args, command.AdditionalDependencies, command.MavenRepositories, command.AllDependencies)
if err != nil {
return err
}

// Print dependencies.
err = outputDependencies(dependencies, command.OutputFormat)
if err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/local_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ func (command *localRunCmdOptions) run(cmd *cobra.Command, args []string) error
}
dependencies = localBuildDependencies
} else {
// Fetch dependencies.
computedDependencies, err := getDependencies(args, command.AdditionalDependencies, command.MavenRepositories, true)
computedDependencies, err := getDependencies(command.Context, args, command.AdditionalDependencies, command.MavenRepositories, true)
if err != nil {
return err
}
Expand Down
38 changes: 15 additions & 23 deletions pkg/cmd/util_dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package cmd

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand All @@ -42,9 +43,9 @@ var additionalDependencyUsageMessage = `Additional top-level dependencies are sp
<type>:<dependency-name>
where <type> is one of {` + strings.Join(acceptedDependencyTypes, "|") + `}.`

func getDependencies(args []string, additionalDependencies []string, repositories []string, allDependencies bool) ([]string, error) {
func getDependencies(ctx context.Context, args []string, additionalDependencies []string, repositories []string, allDependencies bool) ([]string, error) {
// Fetch existing catalog or create new one if one does not already exist
catalog, err := createCamelCatalog()
catalog, err := createCamelCatalog(ctx)

// Get top-level dependencies
dependencies, err := getTopLevelDependencies(catalog, args)
Expand All @@ -67,7 +68,7 @@ func getDependencies(args []string, additionalDependencies []string, repositorie
util.StringSliceUniqueAdd(&dependencies, runtimeDep.GetDependencyID())
}

dependencies, err = getTransitiveDependencies(catalog, dependencies, repositories)
dependencies, err = getTransitiveDependencies(ctx, catalog, dependencies, repositories)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -101,29 +102,20 @@ func getTopLevelDependencies(catalog *camel.RuntimeCatalog, args []string) ([]st
return dependencies.List(), nil
}

func getTransitiveDependencies(
catalog *camel.RuntimeCatalog,
dependencies []string, repositories []string) ([]string, error) {

mvn := v1.MavenSpec{
LocalRepository: "",
}

// Create Maven project
func getTransitiveDependencies(ctx context.Context, catalog *camel.RuntimeCatalog, dependencies []string, repositories []string) ([]string, error) {
project := builder.GenerateQuarkusProjectCommon(
catalog.CamelCatalogSpec.Runtime.Metadata["camel-quarkus.version"],
defaults.DefaultRuntimeVersion, catalog.CamelCatalogSpec.Runtime.Metadata["quarkus.version"])
defaults.DefaultRuntimeVersion,
catalog.CamelCatalogSpec.Runtime.Metadata["quarkus.version"],
)

// Inject dependencies into Maven project
err := camel.ManageIntegrationDependencies(&project, dependencies, catalog)
if err != nil {
return nil, err
}

// Maven local context to be used for generating the transitive dependencies
mc := maven.NewContext(util.MavenWorkingDirectory, project)
mc.LocalRepository = mvn.LocalRepository
mc.Timeout = mvn.GetTimeout().Duration
mc := maven.NewContext(util.MavenWorkingDirectory)
mc.LocalRepository = ""

if len(repositories) > 0 {
var repoList []v1.Repository
Expand Down Expand Up @@ -155,7 +147,7 @@ func getTransitiveDependencies(
// Make maven command less verbose
mc.AdditionalArguments = append(mc.AdditionalArguments, "-q")

err = builder.BuildQuarkusRunnerCommon(mc)
err = builder.BuildQuarkusRunnerCommon(ctx, mc, project)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -217,7 +209,7 @@ func getLocalBuildRoutes(integrationDirectory string) ([]string, error) {
return locallyBuiltRoutes, nil
}

func generateCatalog() (*camel.RuntimeCatalog, error) {
func generateCatalog(ctx context.Context) (*camel.RuntimeCatalog, error) {
// A Camel catalog is required for this operation
settings := ""
mvn := v1.MavenSpec{
Expand All @@ -229,15 +221,15 @@ func generateCatalog() (*camel.RuntimeCatalog, error) {
}
var providerDependencies []maven.Dependency
var caCert []byte
catalog, err := camel.GenerateCatalogCommon(settings, caCert, mvn, runtime, providerDependencies)
catalog, err := camel.GenerateCatalogCommon(ctx, settings, caCert, mvn, runtime, providerDependencies)
if err != nil {
return nil, err
}

return catalog, nil
}

func createCamelCatalog() (*camel.RuntimeCatalog, error) {
func createCamelCatalog(ctx context.Context) (*camel.RuntimeCatalog, error) {
// Attempt to reuse existing Camel catalog if one is present
catalog, err := camel.DefaultCatalog()
if err != nil {
Expand All @@ -246,7 +238,7 @@ func createCamelCatalog() (*camel.RuntimeCatalog, error) {

// Generate catalog if one was not found
if catalog == nil {
catalog, err = generateCatalog()
catalog, err = generateCatalog(ctx)
if err != nil {
return nil, err
}
Expand Down
23 changes: 3 additions & 20 deletions pkg/controller/integrationplatform/initialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ import (
"testing"
"time"

"github.com/apache/camel-k/pkg/platform"
"github.com/rs/xid"

"github.com/stretchr/testify/assert"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/platform"
"github.com/apache/camel-k/pkg/util/log"
"github.com/apache/camel-k/pkg/util/test"

"github.com/stretchr/testify/assert"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -54,10 +53,6 @@ func TestTimeouts_Default(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, answer)

n := answer.Status.Build.GetTimeout().Duration.Seconds() * 0.75
d := (time.Duration(n) * time.Second).Truncate(time.Second)

assert.Equal(t, d, answer.Status.Build.Maven.GetTimeout().Duration)
assert.Equal(t, 5*time.Minute, answer.Status.Build.GetTimeout().Duration)
}

Expand Down Expand Up @@ -88,10 +83,6 @@ func TestTimeouts_MavenComputedFromBuild(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, answer)

n := answer.Status.Build.GetTimeout().Duration.Seconds() * 0.75
d := (time.Duration(n) * time.Second).Truncate(time.Second)

assert.Equal(t, d, answer.Status.Build.Maven.GetTimeout().Duration)
assert.Equal(t, 1*time.Minute, answer.Status.Build.GetTimeout().Duration)
}

Expand All @@ -109,13 +100,6 @@ func TestTimeouts_Truncated(t *testing.T) {
Duration: bt,
}

mt, err := time.ParseDuration("2m1ms")
assert.Nil(t, err)

ip.Spec.Build.Maven.Timeout = &metav1.Duration{
Duration: mt,
}

c, err := test.NewFakeClient(&ip)
assert.Nil(t, err)

Expand All @@ -129,7 +113,6 @@ func TestTimeouts_Truncated(t *testing.T) {
assert.Nil(t, err)
assert.NotNil(t, answer)

assert.Equal(t, 2*time.Minute, answer.Status.Build.Maven.GetTimeout().Duration)
assert.Equal(t, 5*time.Minute, answer.Status.Build.GetTimeout().Duration)
}

Expand Down
31 changes: 6 additions & 25 deletions pkg/platform/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func ConfigureDefaults(ctx context.Context, c client.Client, p *v1.IntegrationPl
log.Log.Info("No registry specified for publishing images")
}

if verbose && p.Status.Build.Maven.GetTimeout().Duration != 0 {
log.Log.Infof("Maven Timeout set to %s", p.Status.Build.Maven.GetTimeout().Duration)
if verbose && p.Status.Build.GetTimeout().Duration != 0 {
log.Log.Infof("Maven Timeout set to %s", p.Status.Build.GetTimeout().Duration)
}

return nil
Expand Down Expand Up @@ -184,24 +184,6 @@ func setPlatformDefaults(ctx context.Context, c client.Client, p *v1.Integration
}
}

if p.Status.Build.Maven.GetTimeout().Duration != 0 {
d := p.Status.Build.Maven.GetTimeout().Duration.Truncate(time.Second)

if verbose && p.Status.Build.Maven.GetTimeout().Duration != d {
log.Log.Infof("Maven timeout minimum unit is sec (configured: %s, truncated: %s)", p.Status.Build.Maven.GetTimeout().Duration, d)
}

p.Status.Build.Maven.Timeout = &metav1.Duration{
Duration: d,
}
}
if p.Status.Build.Maven.GetTimeout().Duration == 0 {
n := p.Status.Build.GetTimeout().Duration.Seconds() * 0.75
p.Status.Build.Maven.Timeout = &metav1.Duration{
Duration: (time.Duration(n) * time.Second).Truncate(time.Second),
}
}

if p.Status.Build.Maven.Settings.ConfigMapKeyRef == nil && p.Status.Build.Maven.Settings.SecretKeyRef == nil {
var repositories []v1.Repository
var mirrors []maven.Mirror
Expand All @@ -214,11 +196,11 @@ func setPlatformDefaults(ctx context.Context, c client.Client, p *v1.Integration
}
mirrors = append(mirrors, mirror)
} else {
repository := maven.NewRepository(c.Value)
if repository.ID == "" {
repository.ID = fmt.Sprintf("repository-%03d", i)
repo := maven.NewRepository(c.Value)
if repo.ID == "" {
repo.ID = fmt.Sprintf("repository-%03d", i)
}
repositories = append(repositories, repository)
repositories = append(repositories, repo)
}
}
}
Expand Down Expand Up @@ -260,7 +242,6 @@ func setPlatformDefaults(ctx context.Context, c client.Client, p *v1.Integration
log.Log.Infof("BaseImage set to %s", p.Status.Build.BaseImage)
log.Log.Infof("LocalRepository set to %s", p.Status.Build.Maven.LocalRepository)
log.Log.Infof("Timeout set to %s", p.Status.Build.GetTimeout())
log.Log.Infof("Maven Timeout set to %s", p.Status.Build.Maven.GetTimeout().Duration)
}

return nil
Expand Down
8 changes: 0 additions & 8 deletions pkg/resources/resources.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion pkg/trait/camel.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package trait

import (
"context"
"fmt"
"strings"

Expand Down Expand Up @@ -96,7 +97,9 @@ func (t *camelTrait) loadOrCreateCatalog(e *Environment, runtimeVersion string)
// the required versions (camel and runtime) are not expressed as
// semver constraints
if exactVersionRegexp.MatchString(runtimeVersion) {
catalog, err = camel.GenerateCatalog(e.C, e.Client, ns, e.Platform.Status.Build.Maven, runtime, []maven.Dependency{})
ctx, cancel := context.WithTimeout(e.C, e.Platform.Status.Build.GetTimeout().Duration)
defer cancel()
catalog, err = camel.GenerateCatalog(ctx, e.Client, ns, e.Platform.Status.Build.Maven, runtime, []maven.Dependency{})
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/trait/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ limitations under the License.
package trait

import (
"strconv"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util"
"github.com/apache/camel-k/pkg/util/envvar"
"strconv"
)

const (
Expand Down
Loading