Skip to content

Commit

Permalink
Improve error reporting in case of knative is required but not installed
Browse files Browse the repository at this point in the history
Fix #3803
  • Loading branch information
claudio4j committed Apr 6, 2023
1 parent 92077dd commit 489858c
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 5 deletions.
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
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
4 changes: 4 additions & 0 deletions pkg/trait/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,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 +102,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
23 changes: 23 additions & 0 deletions pkg/trait/cron.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"
"regexp"
"strconv"
Expand All @@ -32,6 +33,7 @@ import (
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/metadata"
"github.com/apache/camel-k/v2/pkg/util"
"github.com/apache/camel-k/v2/pkg/util/knative"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/uri"
)
Expand Down Expand Up @@ -179,6 +181,27 @@ func (t *cronTrait) 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
}
}

return t.Schedule != "", nil
}

Expand Down
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
24 changes: 24 additions & 0 deletions pkg/trait/deployment.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"

"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -29,6 +30,7 @@ import (

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/v2/pkg/util/knative"
)

type deploymentTrait struct {
Expand All @@ -45,6 +47,28 @@ func newDeploymentTrait() Trait {
}

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

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 !e.IntegrationInRunningPhases() {
return false, nil
}
Expand Down
79 changes: 78 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 Expand Up @@ -302,3 +310,72 @@ func createNominalDeploymentTest() (*deploymentTrait, *Environment) {

return trait, environment
}

func TestRunKnativeEndpointWithKnativeNotInstalled2(t *testing.T) {

deploymentTrait, environment := createNominalDeploymentTest()

environment.Client = deploymentTrait.Client
environment.Platform.Spec.Cluster = v1.IntegrationPlatformClusterKubernetes
environment.Platform.Spec.Profile = v1.TraitProfileKubernetes

environment.Integration.Spec.Sources = []v1.SourceSpec{
{
DataSpec: v1.DataSpec{
Name: "test.java",
Content: `
from("knative:channel/test").to("log:${body};
`,
},
Language: v1.LanguageJavaSource,
},
}
environment.Integration.Status.Phase = v1.IntegrationPhaseInitialization

environment.Integration.Status.SetCondition(
v1.IntegrationConditionDeploymentAvailable,
corev1.ConditionTrue,
v1.IntegrationConditionDeploymentAvailableReason,
"deployment-name",
)
configured, err := deploymentTrait.Configure(environment)
assert.NotNil(t, err)
assert.False(t, configured)
conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 2)
assert.Equal(t, v1.IntegrationConditionKnativeNotInstalledReason, conditions[1].Reason)
}

func TestRunNonKnativeEndpointWithKnativeNotInstalled(t *testing.T) {

deploymentTrait, environment := createNominalDeploymentTest()

environment.Client = deploymentTrait.Client
environment.Platform.Spec.Cluster = v1.IntegrationPlatformClusterKubernetes
environment.Platform.Spec.Profile = v1.TraitProfileKubernetes

environment.Integration.Spec.Sources = []v1.SourceSpec{
{
DataSpec: v1.DataSpec{
Name: "test.java",
Content: `
from("platform-http://my-site").to("log:${body}");
`,
},
Language: v1.LanguageJavaSource,
},
}
environment.Integration.Status.Phase = v1.IntegrationPhaseInitialization

environment.Integration.Status.SetCondition(
v1.IntegrationConditionDeploymentAvailable,
corev1.ConditionTrue,
v1.IntegrationConditionDeploymentAvailableReason,
"deployment-name",
)
configured, err := deploymentTrait.Configure(environment)
assert.Nil(t, err)
assert.False(t, configured)
conditions := environment.Integration.Status.Conditions
assert.Len(t, conditions, 1)
}
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

0 comments on commit 489858c

Please sign in to comment.