Repository: apache/camel-k (go)

Issue:
Nil pointer on build failure recovery
I wanted to simulate a build failure so I did:

```
$ kamel run --dependency=camel-foobar examples/hello.xml
```

Which made the operator crash:

```
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x12c9b56]
goroutine 412 [running]:
k8s.io/apimachinery/pkg/util/runtime.HandleCrash(0x0, 0x0, 0x0)
	/home/james/go/src/github.com/apache/camel-k/vendor/k8s.io/apimachinery/pkg/util/runtime/runtime.go:58 +0x108
panic(0x1490540, 0x245c110)
	/usr/share/go/src/runtime/panic.go:513 +0x1b9
github.com/apache/camel-k/pkg/controller/build.(*errorRecoveryAction).Handle(0xc0005b8500, 0x182ae60, 0xc000042028, 0xc000638480, 0xc00089dc58, 0x1, 0x1)
	/home/james/go/src/github.com/apache/camel-k/pkg/controller/build/recovery.go:100 +0x346
github.com/apache/camel-k/pkg/controller/build.(*ReconcileBuild).Reconcile(0xc000100a80, 0xc00079aca0, 0x9, 0xc000ad4f40, 0x18, 0x24714a0, 0x0, 0x0, 0x0)
	/home/james/go/src/github.com/apache/camel-k/pkg/controller/build/build_controller.go:188 +0x888
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem(0xc000317900, 0x0)
	/home/james/go/src/github.com/apache/camel-k/vendor/sigs.k8s.io/controller-runtime/pkg/internal/controller/controller.go:215 +0x18f
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func1()
	/home/james/go/src/github.com/apache/camel-k/vendor/sigs.k8s.io/controller-runtime/pkg/internal/controller/controller.go:158 +0x36
k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1(0xc00034a0a0)
	/home/james/go/src/github.com/apache/camel-k/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:133 +0x54
k8s.io/apimachinery/pkg/util/wait.JitterUntil(0xc00034a0a0, 0x3b9aca00, 0x0, 0x1, 0xc0005a03c0)
	/home/james/go/src/github.com/apache/camel-k/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:134 +0xbe
k8s.io/apimachinery/pkg/util/wait.Until(0xc00034a0a0, 0x3b9aca00, 0xc0005a03c0)
	/home/james/go/src/github.com/apache/camel-k/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go:88 +0x4d
created by sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start
	/home/james/go/src/github.com/apache/camel-k/vendor/sigs.k8s.io/controller-runtime/pkg/internal/controller/controller.go:157 +0x32a
```

[This](https://github.com/apache/camel-k/blob/master/pkg/controller/build/recovery.go#L100) seems to be the problem. `build.Status.Failure` wont have been initialized at this point.

Pull request:
fix: Nil pointer on build failure recovery
fixes #804

Patch:
diff --git a/pkg/controller/build/recovery.go b/pkg/controller/build/recovery.go
index f7abc9726..1d8d7a97d 100644
--- a/pkg/controller/build/recovery.go
+++ b/pkg/controller/build/recovery.go
@@ -87,7 +87,6 @@ func (action *errorRecoveryAction) Handle(ctx context.Context, build *v1alpha1.B
 		return nil, nil
 	}
 
-	build.Status = v1alpha1.BuildStatus{}
 	build.Status.Phase = v1alpha1.BuildPhaseInitialization
 	build.Status.Failure.Recovery.Attempt++
 	build.Status.Failure.Recovery.AttemptTime = metav1.Now()
diff --git a/pkg/trait/dependencies.go b/pkg/trait/dependencies.go
index 5a0ad2588..d12d777d2 100644
--- a/pkg/trait/dependencies.go
+++ b/pkg/trait/dependencies.go
@@ -40,7 +40,7 @@ func (t *dependenciesTrait) Configure(e *Environment) (bool, error) {
 		return false, nil
 	}
 
-	return e.IntegrationInPhase(""), nil
+	return e.IntegrationInPhase(v1alpha1.IntegrationPhaseInitialization), nil
 }
 
 func (t *dependenciesTrait) Apply(e *Environment) error {
diff --git a/pkg/trait/rest-dsl.go b/pkg/trait/rest-dsl.go
index ab1789d27..ef6b22bf9 100644
--- a/pkg/trait/rest-dsl.go
+++ b/pkg/trait/rest-dsl.go
@@ -60,7 +60,7 @@ func (t *restDslTrait) Configure(e *Environment) (bool, error) {
 
 	for _, resource := range e.Integration.Spec.Resources {
 		if resource.Type == v1alpha1.ResourceTypeOpenAPI {
-			return e.IntegrationInPhase(""), nil
+			return e.IntegrationInPhase(v1alpha1.IntegrationPhaseInitialization), nil
 		}
 	}
 
diff --git a/pkg/util/kubernetes/util.go b/pkg/util/kubernetes/util.go
index b55b56d34..7f1df15af 100644
--- a/pkg/util/kubernetes/util.go
+++ b/pkg/util/kubernetes/util.go
@@ -286,7 +286,7 @@ func GetConfigMapRefValue(ctx context.Context, client client.Client, namespace s
 // ResolveValueSource --
 func ResolveValueSource(ctx context.Context, client client.Client, namespace string, valueSource *v1alpha1.ValueSource) (string, error) {
 	if valueSource.ConfigMapKeyRef != nil && valueSource.SecretKeyRef != nil {
-		return "", fmt.Errorf("value source has bot config map and secret configuired")
+		return "", fmt.Errorf("value source has bot config map and secret configured")
 	}
 	if valueSource.ConfigMapKeyRef != nil {
 		return GetConfigMapRefValue(ctx, client, namespace, valueSource.ConfigMapKeyRef)


Test patch:
diff --git a/pkg/trait/dependencies_test.go b/pkg/trait/dependencies_test.go
new file mode 100644
index 000000000..774c17b6e
--- /dev/null
+++ b/pkg/trait/dependencies_test.go
@@ -0,0 +1,121 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package trait
+
+import (
+	"testing"
+
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+	"github.com/apache/camel-k/pkg/util/test"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestDependenciesTraitApplicability(t *testing.T) {
+	e := &Environment{
+		Integration: &v1alpha1.Integration{},
+	}
+
+	trait := newDependenciesTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration.Status.Phase = v1alpha1.IntegrationPhaseNone
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration.Status.Phase = v1alpha1.IntegrationPhaseInitialization
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+}
+
+func TestIntegrationDefaultDeps(t *testing.T) {
+	catalog, err := test.DefaultCatalog()
+	assert.Nil(t, err)
+
+	e := &Environment{
+		CamelCatalog: catalog,
+		Integration: &v1alpha1.Integration{
+			Spec: v1alpha1.IntegrationSpec{
+				Sources: []v1alpha1.SourceSpec{
+					{
+						DataSpec: v1alpha1.DataSpec{
+							Name:    "Request.java",
+							Content: `from("direct:foo").to("log:bar");`,
+						},
+						Language: v1alpha1.LanguageJavaSource,
+					},
+				},
+			},
+			Status: v1alpha1.IntegrationStatus{
+				Phase: v1alpha1.IntegrationPhaseInitialization,
+			},
+		},
+	}
+
+	trait := newDependenciesTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+
+	err = trait.Apply(e)
+	assert.Nil(t, err)
+	assert.ElementsMatch(t, []string{"camel:core", "camel:direct", "camel:log", "runtime:jvm"}, e.Integration.Status.Dependencies)
+}
+
+func TestIntegrationCustomDeps(t *testing.T) {
+	catalog, err := test.DefaultCatalog()
+	assert.Nil(t, err)
+
+	e := &Environment{
+		CamelCatalog: catalog,
+		Integration: &v1alpha1.Integration{
+			Spec: v1alpha1.IntegrationSpec{
+				Dependencies: []string{
+					"camel:undertow",
+					"org.foo:bar",
+				},
+				Sources: []v1alpha1.SourceSpec{
+					{
+						DataSpec: v1alpha1.DataSpec{
+							Name:    "Request.java",
+							Content: `from("direct:foo").to("log:bar");`,
+						},
+						Language: v1alpha1.LanguageJavaSource,
+					},
+				},
+			},
+			Status: v1alpha1.IntegrationStatus{
+				Phase: v1alpha1.IntegrationPhaseInitialization,
+			},
+		},
+	}
+
+	trait := newDependenciesTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+
+	err = trait.Apply(e)
+	assert.Nil(t, err)
+	assert.ElementsMatch(t, []string{"camel:core", "camel:direct", "camel:log",
+		"camel:undertow", "org.foo:bar", "runtime:jvm"}, e.Integration.Status.Dependencies)
+}
diff --git a/pkg/trait/rest-dsl_test.go b/pkg/trait/rest-dsl_test.go
new file mode 100644
index 000000000..e63cd94e8
--- /dev/null
+++ b/pkg/trait/rest-dsl_test.go
@@ -0,0 +1,57 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package trait
+
+import (
+	"testing"
+
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestRestDslTraitApplicability(t *testing.T) {
+	e := &Environment{}
+
+	trait := newRestDslTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration = &v1alpha1.Integration{
+		Status: v1alpha1.IntegrationStatus{
+			Phase: v1alpha1.IntegrationPhaseNone,
+		},
+	}
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	resource := v1alpha1.ResourceSpec{
+		Type: v1alpha1.ResourceTypeOpenAPI,
+	}
+	e.Integration.Spec.Resources = append(e.Integration.Spec.Resources, resource)
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration.Status.Phase = v1alpha1.IntegrationPhaseInitialization
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+}