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

fix(controller): camelcatalog default timeout #4239

Merged
merged 2 commits into from
Apr 11, 2023
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: 8 additions & 0 deletions config/crd/bases/camel.apache.org_integrationplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ spec:
images. It can be useful if you want to provide some custom
base image with further utility softwares
type: string
buildCatalogToolTimeout:
description: the timeout (in seconds) to use when creating the
build tools container image
type: string
buildStrategy:
description: the strategy to adopt for building an Integration
base image
Expand Down Expand Up @@ -1626,6 +1630,10 @@ spec:
images. It can be useful if you want to provide some custom
base image with further utility softwares
type: string
buildCatalogToolTimeout:
description: the timeout (in seconds) to use when creating the
build tools container image
type: string
buildStrategy:
description: the strategy to adopt for building an Integration
base image
Expand Down
7 changes: 7 additions & 0 deletions docs/modules/ROOT/partials/apis/camel-k-crds.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,13 @@ It can be useful if you want to provide some custom base image with further util

the image registry used to push/pull Integration images

|`buildCatalogToolTimeout` +
*https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#duration-v1-meta[Kubernetes meta/v1.Duration]*
|


the timeout (in seconds) to use when creating the build tools container image

|`timeout` +
*https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#duration-v1-meta[Kubernetes meta/v1.Duration]*
|
Expand Down
31 changes: 31 additions & 0 deletions e2e/commonwithcustominstall/catalog_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ import (
"fmt"
"strings"
"testing"
"time"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

. "github.com/apache/camel-k/v2/e2e/support"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/defaults"
)

func TestCamelCatalogBuilder(t *testing.T) {

WithNewTestNamespace(t, func(ns string) {
operatorID := fmt.Sprintf("camel-k-%s", ns)
Expect(KamelInstallWithID(operatorID, ns).Execute()).To(Succeed())
Expand Down Expand Up @@ -144,4 +147,32 @@ func TestCamelCatalogBuilder(t *testing.T) {

Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed())
})

WithNewTestNamespace(t, func(ns string) {
operatorID := fmt.Sprintf("camel-k-%s", ns)
Expect(KamelInstallWithID(operatorID, ns).Execute()).To(Succeed())
Eventually(OperatorPod(ns)).ShouldNot(BeNil())
Eventually(Platform(ns)).ShouldNot(BeNil())

pl := Platform(ns)()
// set a very short timeout to simulate the timeout
pl.Spec.Build.BuildCatalogToolTimeout = &metav1.Duration{
Duration: 1 * time.Second,
}
TestClient().Update(TestContext, pl)
Eventually(Platform(ns)).ShouldNot(BeNil())
Eventually(PlatformBuildCatalogToolTimeout(ns)).Should(Equal(
&metav1.Duration{
Duration: 1 * time.Second,
},
))

Eventually(PlatformConditionStatus(ns, v1.IntegrationPlatformConditionReady), TestTimeoutShort).
Should(Equal(corev1.ConditionTrue))
catalogName := fmt.Sprintf("camel-catalog-%s", strings.ToLower(defaults.DefaultRuntimeVersion))

Eventually(CamelCatalog(ns, catalogName)).ShouldNot(BeNil())
Eventually(CamelCatalogPhase(ns, catalogName)).Should(Equal(v1.CamelCatalogPhaseError))
Eventually(CamelCatalogCondition(ns, catalogName, v1.CamelCatalogConditionReady)().Message).Should(ContainSubstring("build timeout"))
})
}
10 changes: 10 additions & 0 deletions e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,16 @@ func PlatformProfile(ns string) func() v1.TraitProfile {
}
}

func PlatformBuildCatalogToolTimeout(ns string) func() *metav1.Duration {
return func() *metav1.Duration {
p := Platform(ns)()
if p == nil {
return &metav1.Duration{}
}
return p.Status.Build.BuildCatalogToolTimeout
}
}

func AssignPlatformToOperator(ns, operator string) error {
pl := Platform(ns)()
if pl == nil {
Expand Down
8 changes: 8 additions & 0 deletions helm/camel-k/crds/crd-integration-platform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ spec:
images. It can be useful if you want to provide some custom
base image with further utility softwares
type: string
buildCatalogToolTimeout:
description: the timeout (in seconds) to use when creating the
build tools container image
type: string
buildStrategy:
description: the strategy to adopt for building an Integration
base image
Expand Down Expand Up @@ -1626,6 +1630,10 @@ spec:
images. It can be useful if you want to provide some custom
base image with further utility softwares
type: string
buildCatalogToolTimeout:
description: the timeout (in seconds) to use when creating the
build tools container image
type: string
buildStrategy:
description: the strategy to adopt for building an Integration
base image
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/camel/v1/integrationplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ type IntegrationPlatformBuildSpec struct {
BaseImage string `json:"baseImage,omitempty"`
// the image registry used to push/pull Integration images
Registry RegistrySpec `json:"registry,omitempty"`
// the timeout (in seconds) to use when creating the build tools container image
BuildCatalogToolTimeout *metav1.Duration `json:"buildCatalogToolTimeout,omitempty"`
// how much time to wait before time out the build process
Timeout *metav1.Duration `json:"timeout,omitempty"`
// Maven configuration used to build the Camel/Camel-Quarkus applications
Expand Down
10 changes: 9 additions & 1 deletion pkg/apis/camel/v1/integrationplatform_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (b IntegrationPlatformBuildSpec) IsOptionEnabled(option string) bool {
return false
}

// Add a publish strategy option
// AddOption add a publish strategy option
func (b *IntegrationPlatformBuildSpec) AddOption(option string, value string) {
options := b.PublishStrategyOptions
if options == nil {
Expand All @@ -204,6 +204,14 @@ func (b IntegrationPlatformBuildSpec) GetTimeout() metav1.Duration {
return *b.Timeout
}

// GetBuildCatalogToolTimeout returns the specified duration or a default one
func (b IntegrationPlatformBuildSpec) GetBuildCatalogToolTimeout() metav1.Duration {
if b.BuildCatalogToolTimeout == nil {
return metav1.Duration{}
}
return *b.BuildCatalogToolTimeout
}

var _ ResourceCondition = IntegrationPlatformCondition{}

// GetConditions --
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/camel/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 22 additions & 4 deletions pkg/controller/catalog/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"runtime"
"strings"
"time"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/builder"
Expand Down Expand Up @@ -71,14 +72,14 @@ func (action *initializeAction) Handle(ctx context.Context, catalog *v1.CamelCat
return catalog, err
}

return initialize(options, platform.Spec.Build.Registry.Address, catalog)
return initialize(options, platform, catalog)
}

func initialize(options spectrum.Options, registryAddress string, catalog *v1.CamelCatalog) (*v1.CamelCatalog, error) {
func initialize(options spectrum.Options, ip *v1.IntegrationPlatform, catalog *v1.CamelCatalog) (*v1.CamelCatalog, error) {
target := catalog.DeepCopy()
imageName := fmt.Sprintf(
"%s/camel-k-runtime-%s-builder:%s",
registryAddress,
ip.Status.Build.Registry.Address,
catalog.Spec.Runtime.Provider,
strings.ToLower(catalog.Spec.Runtime.Version),
)
Expand Down Expand Up @@ -115,7 +116,7 @@ func initialize(options spectrum.Options, registryAddress string, catalog *v1.Ca
options.Base = catalog.Spec.GetQuarkusToolingImage()
options.Target = imageName

err := buildRuntimeBuilderImage(options)
err := buildRuntimeBuilderWithTimeout(options, ip.Status.Build.GetBuildCatalogToolTimeout().Duration)

if err != nil {
target.Status.Phase = v1.CamelCatalogPhaseError
Expand Down Expand Up @@ -159,6 +160,23 @@ func imageSnapshot(options spectrum.Options) bool {
return strings.HasSuffix(options.Base, "snapshot")
}

func buildRuntimeBuilderWithTimeout(options spectrum.Options, timeout time.Duration) error {
// Backward compatibility with IP which had not a timeout field
if timeout == 0 {
return buildRuntimeBuilderImage(options)
}
result := make(chan error, 1)
go func() {
result <- buildRuntimeBuilderImage(options)
}()
select {
case <-time.After(timeout):
return fmt.Errorf("build timeout: %s", timeout.String())
case result := <-result:
return result
}
}

// This func will take care to dynamically build an image that will contain the tools required
// by the catalog build plus kamel binary and a maven wrapper required for the build.
func buildRuntimeBuilderImage(options spectrum.Options) error {
Expand Down
28 changes: 24 additions & 4 deletions pkg/platform/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ func setPlatformDefaults(p *v1.IntegrationPlatform, verbose bool) error {
p.Status.Build.PublishStrategyOptions[builder.KanikoPVCName] = p.Name
}

if p.Status.Build.GetTimeout().Duration != 0 {
// Build timeout
if p.Status.Build.GetTimeout().Duration == 0 {
p.Status.Build.Timeout = &metav1.Duration{
Duration: 5 * time.Minute,
}
} else {
d := p.Status.Build.GetTimeout().Duration.Truncate(time.Second)

if verbose && p.Status.Build.GetTimeout().Duration != d {
Expand All @@ -221,11 +226,26 @@ func setPlatformDefaults(p *v1.IntegrationPlatform, verbose bool) error {
Duration: d,
}
}
if p.Status.Build.GetTimeout().Duration == 0 {
p.Status.Build.Timeout = &metav1.Duration{
Duration: 5 * time.Minute,

// Catalog tools build timeout
if p.Status.Build.GetBuildCatalogToolTimeout().Duration == 0 {
log.Debugf("Integration Platform [%s]: setting default build camel catalog tool timeout (1 minute)", p.Namespace)
p.Status.Build.BuildCatalogToolTimeout = &metav1.Duration{
Duration: 1 * time.Minute,
}
} else {
d := p.Status.Build.GetBuildCatalogToolTimeout().Duration.Truncate(time.Second)

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

log.Debugf("Integration Platform [%s]: setting build catalog tools timeout", p.Namespace)
p.Status.Build.BuildCatalogToolTimeout = &metav1.Duration{
Duration: d,
}
}

_, cacheEnabled := p.Status.Build.PublishStrategyOptions[builder.KanikoBuildCacheEnabled]
if p.Status.Build.PublishStrategy == v1.IntegrationPlatformBuildPublishStrategyKaniko && !cacheEnabled {
// Default to disabling Kaniko cache warmer
Expand Down