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

Improve error reporting in case of knative is required but not installed #4237

Merged
merged 1 commit 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
4 changes: 4 additions & 0 deletions pkg/apis/camel/v1/integration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ const (
IntegrationConditionServiceAvailable IntegrationConditionType = "ServiceAvailable"
// IntegrationConditionKnativeServiceAvailable --
IntegrationConditionKnativeServiceAvailable IntegrationConditionType = "KnativeServiceAvailable"
// IntegrationConditionKnativeAvailable --
IntegrationConditionKnativeAvailable IntegrationConditionType = "KnativeAvailable"
// IntegrationConditionCronJobAvailable --
IntegrationConditionCronJobAvailable IntegrationConditionType = "CronJobAvailable"
// IntegrationConditionExposureAvailable --
Expand Down Expand Up @@ -203,6 +205,8 @@ const (
IntegrationConditionKnativeServiceAvailableReason string = "KnativeServiceAvailable"
// IntegrationConditionKnativeServiceNotAvailableReason --
IntegrationConditionKnativeServiceNotAvailableReason string = "KnativeServiceNotAvailable"
// IntegrationConditionKnativeNotInstalledReason --
IntegrationConditionKnativeNotInstalledReason string = "KnativeNotInstalled"
// IntegrationConditionCronJobAvailableReason --
IntegrationConditionCronJobAvailableReason string = "CronJobAvailableReason"
// IntegrationConditionCronJobNotAvailableReason --
Expand Down
4 changes: 3 additions & 1 deletion pkg/trait/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/defaults"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"
)

func TestBuilderTraitNotAppliedBecauseOfNilKit(t *testing.T) {
Expand Down Expand Up @@ -106,11 +107,12 @@ func createBuilderTestEnv(cluster v1.IntegrationPlatformCluster, strategy v1.Int
if err != nil {
panic(err)
}

client, _ := test.NewFakeClient()
res := &Environment{
Ctx: context.TODO(),
CamelCatalog: c,
Catalog: NewCatalog(nil),
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand Down
23 changes: 23 additions & 0 deletions pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
package trait

import (
"errors"
"fmt"
"path/filepath"

Expand All @@ -37,6 +38,7 @@ import (
"github.com/apache/camel-k/v2/pkg/util/defaults"
"github.com/apache/camel-k/v2/pkg/util/digest"
"github.com/apache/camel-k/v2/pkg/util/envvar"
"github.com/apache/camel-k/v2/pkg/util/knative"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
)

Expand Down Expand Up @@ -74,6 +76,27 @@ func (t *containerTrait) Configure(e *Environment) (bool, error) {
return false, nil
}

knativeInstalled, _ := knative.IsInstalled(e.Client)
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !knativeInstalled {
hasKnativeEndpoint, err := containsEndpoint("knative", e, t.Client)
if err != nil {
return false, err
}

if hasKnativeEndpoint {
// fail fast the integration as there is no knative installed in the cluster
t.L.ForIntegration(e.Integration).Infof("Integration %s/%s contains knative endpoint that cannot run, as knative is not installed in the cluster.", e.Integration.Namespace, e.Integration.Name)
err := errors.New("integration cannot run, as knative is not installed in the cluster")
e.Integration.Status.SetCondition(
v1.IntegrationConditionKnativeAvailable,
corev1.ConditionFalse,
v1.IntegrationConditionKnativeNotInstalledReason,
err.Error())
e.Integration.Status.Phase = v1.IntegrationPhaseError
return false, err
}
}

if pointer.BoolDeref(t.Auto, true) {
if t.Expose == nil {
e := e.Resources.GetServiceForIntegration(e.Integration) != nil
Expand Down
3 changes: 3 additions & 0 deletions pkg/trait/container_probes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"
)

func newTestProbesEnv(t *testing.T, integration *v1.Integration) Environment {
Expand All @@ -38,11 +39,13 @@ func newTestProbesEnv(t *testing.T, integration *v1.Integration) Environment {
assert.Nil(t, err)
assert.NotNil(t, catalog)

client, _ := test.NewFakeClient()
traitCatalog := NewCatalog(nil)

return Environment{
Catalog: traitCatalog,
CamelCatalog: catalog,
Client: client,
Platform: &v1.IntegrationPlatform{
Status: v1.IntegrationPlatformStatus{
Phase: v1.IntegrationPlatformPhaseReady,
Expand Down
98 changes: 98 additions & 0 deletions pkg/trait/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"

ctrl "sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -41,11 +42,13 @@ func TestContainerWithDefaults(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

client, _ := test.NewFakeClient()
traitCatalog := NewCatalog(nil)

environment := Environment{
CamelCatalog: catalog,
Catalog: traitCatalog,
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: ServiceTestName,
Expand Down Expand Up @@ -100,11 +103,13 @@ func TestContainerWithCustomName(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

client, _ := test.NewFakeClient()
traitCatalog := NewCatalog(nil)

environment := Environment{
CamelCatalog: catalog,
Catalog: traitCatalog,
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: ServiceTestName,
Expand Down Expand Up @@ -339,3 +344,96 @@ func TestContainerWithImagePullPolicy(t *testing.T) {

assert.Equal(t, corev1.PullAlways, container.ImagePullPolicy)
}

func TestRunKnativeEndpointWithKnativeNotInstalled(t *testing.T) {

environment := createEnvironment()

trait, _ := newContainerTrait().(*containerTrait)
trait.Enabled = pointer.Bool(true)

environment.Integration.Spec.Sources = []v1.SourceSpec{
{
DataSpec: v1.DataSpec{
Name: "test.java",
Content: `
from("knative:channel/test").to("log:${body};
`,
},
Language: v1.LanguageJavaSource,
},
}
configured, err := trait.Configure(environment)
assert.NotNil(t, err)
assert.False(t, configured)
conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 1)
assert.Equal(t, v1.IntegrationConditionKnativeNotInstalledReason, conditions[0].Reason)
}

func TestRunNonKnativeEndpointWithKnativeNotInstalled(t *testing.T) {

environment := createEnvironment()
trait, _ := newContainerTrait().(*containerTrait)
trait.Enabled = pointer.Bool(true)
environment.Integration.Spec.Sources = []v1.SourceSpec{
{
DataSpec: v1.DataSpec{
Name: "test.java",
Content: `
from("platform-http://my-site").to("log:${body}");
`,
},
Language: v1.LanguageJavaSource,
},
}

configured, err := trait.Configure(environment)
assert.Nil(t, err)
assert.True(t, configured)
conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 0)
}

func createEnvironment() *Environment {

client, _ := test.NewFakeClient()
// disable the knative service api
fakeClient := client.(*test.FakeClient) //nolint
fakeClient.DisableAPIGroupDiscovery("serving.knative.dev/v1")

replicas := int32(3)
catalog, _ := camel.QuarkusCatalog()

environment := &Environment{
CamelCatalog: catalog,
Catalog: NewCatalog(nil),
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "integration-name",
},
Spec: v1.IntegrationSpec{
Replicas: &replicas,
Traits: v1.Traits{},
},
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseInitialization,
},
},
Platform: &v1.IntegrationPlatform{
ObjectMeta: metav1.ObjectMeta{
Namespace: "namespace",
},
Spec: v1.IntegrationPlatformSpec{
Cluster: v1.IntegrationPlatformClusterKubernetes,
Profile: v1.TraitProfileKubernetes,
},
},
Resources: kubernetes.NewCollection(),
ApplicationProperties: make(map[string]string),
}
environment.Platform.ResyncStatusFullConfig()

return environment
}
9 changes: 9 additions & 0 deletions pkg/trait/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"
)

func TestCronFromURI(t *testing.T) {
Expand Down Expand Up @@ -225,11 +226,13 @@ func TestCronDeps(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

client, _ := test.NewFakeClient()
traitCatalog := NewCatalog(nil)

environment := Environment{
CamelCatalog: catalog,
Catalog: traitCatalog,
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand Down Expand Up @@ -298,11 +301,13 @@ func TestCronDepsFallback(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

client, _ := test.NewFakeClient()
traitCatalog := NewCatalog(nil)

environment := Environment{
CamelCatalog: catalog,
Catalog: traitCatalog,
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand Down Expand Up @@ -376,11 +381,13 @@ func TestCronWithActiveDeadline(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

client, _ := test.NewFakeClient()
traitCatalog := NewCatalog(nil)

environment := Environment{
CamelCatalog: catalog,
Catalog: traitCatalog,
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand Down Expand Up @@ -457,11 +464,13 @@ func TestCronWithBackoffLimit(t *testing.T) {
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

client, _ := test.NewFakeClient()
traitCatalog := NewCatalog(nil)

environment := Environment{
CamelCatalog: catalog,
Catalog: traitCatalog,
Client: client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Expand Down
1 change: 1 addition & 0 deletions pkg/trait/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func newDeploymentTrait() Trait {
}

func (t *deploymentTrait) Configure(e *Environment) (bool, error) {

if !e.IntegrationInRunningPhases() {
return false, nil
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/trait/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/utils/pointer"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"
)
Expand Down Expand Up @@ -265,10 +266,17 @@ func createNominalDeploymentTest() (*deploymentTrait, *Environment) {
},
})

// disable the knative service api
fakeClient := trait.Client.(*test.FakeClient) //nolint
fakeClient.DisableAPIGroupDiscovery("serving.knative.dev/v1")

replicas := int32(3)
catalog, _ := camel.QuarkusCatalog()

environment := &Environment{
Catalog: NewCatalog(nil),
CamelCatalog: catalog,
Catalog: NewCatalog(nil),
Client: trait.Client,
Integration: &v1.Integration{
ObjectMeta: metav1.ObjectMeta{
Name: "integration-name",
Expand Down
3 changes: 3 additions & 0 deletions pkg/trait/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"
)

func TestDefaultEnvironment(t *testing.T) {
Expand Down Expand Up @@ -195,9 +196,11 @@ func NewEnvironmentTestCatalog() *Catalog {
}

func mockEnvironment(catalog *camel.RuntimeCatalog) Environment {
fakeClient, _ := test.NewFakeClient()
return Environment{
CamelCatalog: catalog,
Catalog: NewCatalog(nil),
Client: fakeClient,
Integration: &v1.Integration{
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseDeploying,
Expand Down
4 changes: 3 additions & 1 deletion pkg/trait/istio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ import (
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/util/camel"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/test"
)

func NewIstioTestEnv(t *testing.T, d *appsv1.Deployment, s *serving.Service, enabled bool) Environment {
t.Helper()

client, _ := test.NewFakeClient()
catalog, err := camel.DefaultCatalog()
assert.Nil(t, err)

env := Environment{
Catalog: NewEnvironmentTestCatalog(),
CamelCatalog: catalog,
Client: client,
Integration: &v1.Integration{
Status: v1.IntegrationStatus{
Phase: v1.IntegrationPhaseDeploying,
Expand Down
Loading