Skip to content

Commit

Permalink
fix(#3393): Make CronJob functionality optional
Browse files Browse the repository at this point in the history
- Explicitly check for CronJob batch/v1 API availability before using it
- CronJob batch/v1 API is not available on OpenShift 3.x
- Disable cron job e2e test on OpenShift 3.x
  • Loading branch information
christophd committed Jun 30, 2022
1 parent 56d3bc6 commit 9e6d3f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
6 changes: 3 additions & 3 deletions e2e/common/cli/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/apache/camel-k/pkg/util/defaults"
"github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/openshift"
console "github.com/openshift/api/console/v1"
consolev1 "github.com/openshift/api/console/v1"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -98,11 +98,11 @@ func TestConsoleCliDownload(t *testing.T) {
ocp, err := openshift.IsOpenShift(TestClient())
assert.Nil(t, err)

ok, err := kubernetes.IsAPIResourceInstalled(TestClient(), "console.openshift.io/v1", reflect.TypeOf(console.ConsoleCLIDownload{}).Name())
ok, err := kubernetes.IsAPIResourceInstalled(TestClient(), "console.openshift.io/v1", reflect.TypeOf(consolev1.ConsoleCLIDownload{}).Name())
assert.Nil(t, err)

if !ocp || !ok {
t.Skip("This test requires ConsoleCliDownload object which is available on OpenShift 4 only.")
t.Skip("This test requires ConsoleCliDownload object which is available on OpenShift 4+ only.")
return
}

Expand Down
12 changes: 12 additions & 0 deletions e2e/common/cron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,29 @@ limitations under the License.
package common

import (
"reflect"
"testing"

. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"

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

func TestRunCronExample(t *testing.T) {
ok, err := kubernetes.IsAPIResourceInstalled(TestClient(), batchv1.SchemeGroupVersion.Group, reflect.TypeOf(batchv1.CronJob{}).Name())
assert.Nil(t, err)

if !ok {
t.Skip("This test requires CronJob batch/v1 API installed.")
return
}

WithNewTestNamespace(t, func(ns string) {
Expect(Kamel("install", "-n", ns).Execute()).To(Succeed())

Expand Down
26 changes: 19 additions & 7 deletions pkg/cmd/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"math/rand"
"os"
"reflect"
"runtime"
"strconv"
"strings"
Expand All @@ -35,6 +36,7 @@ import (
coordination "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
Expand Down Expand Up @@ -192,6 +194,22 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
exitOnError(err, "cannot create Integration label selector")
selector := labels.NewSelector().Add(*hasIntegrationLabel)

selectors := cache.SelectorsByObject{
&corev1.Pod{}: {Label: selector},
&appsv1.Deployment{}: {Label: selector},
&batchv1.Job{}: {Label: selector},
&servingv1.Service{}: {Label: selector},
}

if ok, err := kubernetes.IsAPIResourceInstalled(c, batchv1.SchemeGroupVersion.String(), reflect.TypeOf(batchv1.CronJob{}).Name()); ok && err == nil {
selectors[&batchv1.CronJob{}] = struct {
Label labels.Selector
Field fields.Selector
}{
Label: selector,
}
}

mgr, err := manager.New(c.GetConfig(), manager.Options{
Namespace: watchNamespace,
EventBroadcaster: broadcaster,
Expand All @@ -204,13 +222,7 @@ func Run(healthPort, monitoringPort int32, leaderElection bool, leaderElectionID
MetricsBindAddress: ":" + strconv.Itoa(int(monitoringPort)),
NewCache: cache.BuilderWithOptions(
cache.Options{
SelectorsByObject: cache.SelectorsByObject{
&corev1.Pod{}: {Label: selector},
&appsv1.Deployment{}: {Label: selector},
&batchv1.CronJob{}: {Label: selector},
&batchv1.Job{}: {Label: selector},
&servingv1.Service{}: {Label: selector},
},
SelectorsByObject: selectors,
},
),
})
Expand Down
7 changes: 5 additions & 2 deletions pkg/controller/integration/integration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ func add(mgr manager.Manager, c client.Client, r reconcile.Reconciler) error {
})).
// Watch for the owned Deployments
Owns(&appsv1.Deployment{}, builder.WithPredicates(StatusChangedPredicate{})).
// Watch for the owned CronJobs
Owns(&batchv1.CronJob{}, builder.WithPredicates(StatusChangedPredicate{})).
// Watch for the Integration Pods
Watches(&source.Kind{Type: &corev1.Pod{}},
handler.EnqueueRequestsFromMapFunc(func(a ctrl.Object) []reconcile.Request {
Expand All @@ -225,6 +223,11 @@ func add(mgr manager.Manager, c client.Client, r reconcile.Reconciler) error {
}
}))

if ok, err := kubernetes.IsAPIResourceInstalled(c, batchv1.SchemeGroupVersion.String(), reflect.TypeOf(batchv1.CronJob{}).Name()); ok && err == nil {
// Watch for the owned CronJobs
b.Owns(&batchv1.CronJob{}, builder.WithPredicates(StatusChangedPredicate{}))
}

// Watch for the owned Knative Services conditionally
if ok, err := kubernetes.IsAPIResourceInstalled(c, servingv1.SchemeGroupVersion.String(), reflect.TypeOf(servingv1.Service{}).Name()); err != nil {
return err
Expand Down

0 comments on commit 9e6d3f4

Please sign in to comment.