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 apache#3803

(cherry picked from commit e1e74fc)
  • Loading branch information
claudio4j committed Apr 11, 2023
1 parent 263b3cb commit b4a9025
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 4 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 @@ -171,6 +171,8 @@ const (
IntegrationConditionServiceAvailable IntegrationConditionType = "ServiceAvailable"
// IntegrationConditionKnativeServiceAvailable --
IntegrationConditionKnativeServiceAvailable IntegrationConditionType = "KnativeServiceAvailable"
// IntegrationConditionKnativeAvailable --
IntegrationConditionKnativeAvailable IntegrationConditionType = "KnativeAvailable"
// IntegrationConditionCronJobAvailable --
IntegrationConditionCronJobAvailable IntegrationConditionType = "CronJobAvailable"
// IntegrationConditionExposureAvailable --
Expand Down Expand Up @@ -210,6 +212,8 @@ const (
IntegrationConditionKnativeServiceAvailableReason string = "KnativeServiceAvailable"
// IntegrationConditionKnativeServiceNotAvailableReason --
IntegrationConditionKnativeServiceNotAvailableReason string = "KnativeServiceNotAvailable"
// IntegrationConditionKnativeNotInstalledReason --
IntegrationConditionKnativeNotInstalledReason string = "KnativeNotInstalled"
// IntegrationConditionCronJobAvailableReason --
IntegrationConditionCronJobAvailableReason string = "CronJobAvailableReason"
// IntegrationConditionCronJobNotAvailableReason --
Expand Down
3 changes: 3 additions & 0 deletions 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/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/defaults"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/test"
)

func TestBuilderTraitNotAppliedBecauseOfNilKit(t *testing.T) {
Expand Down Expand Up @@ -108,10 +109,12 @@ func createBuilderTestEnv(cluster v1.IntegrationPlatformCluster, strategy v1.Int
}

kanikoCache := false
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"

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

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

knativeInstalled, _ := knative.IsInstalled(e.Ctx, 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/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/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{},
Integration: integration,
Resources: kubernetes.NewCollection(),
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 @@ -96,11 +99,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 @@ -314,3 +319,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/pkg/util"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/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 @@ -295,11 +298,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 @@ -370,11 +375,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 @@ -443,11 +450,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
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/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/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/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/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/pkg/apis/camel/v1/trait"
"github.com/apache/camel-k/pkg/util/camel"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/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 b4a9025

Please sign in to comment.