Skip to content

Commit

Permalink
chore: Use global client discovery API to check Knative install
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Sep 29, 2022
1 parent 3615f0a commit 3652ee7
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 52 deletions.
4 changes: 3 additions & 1 deletion e2e/global/knative/knative_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ import (

func TestKnativePlatform(t *testing.T) {
WithNewTestNamespace(t, func(ns string) {
if !knative.IsEnabledInNamespace(TestContext, TestClient(), ns) {
installed, err := knative.IsInstalled(TestClient())
Expect(err).NotTo(HaveOccurred())
if !installed {
t.Error("Knative not installed in the cluster")
t.FailNow()
}
Expand Down
24 changes: 15 additions & 9 deletions pkg/controller/integration/platform_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func (action *platformSetupAction) Handle(ctx context.Context, integration *v1.I
if err != nil && !k8serrors.IsNotFound(err) {
return nil, err
} else if pl != nil {
integration.Status.Profile = determineBestProfile(ctx, action.client, integration, pl)
profile, err := determineBestProfile(action.client, integration, pl)
if err != nil {
return nil, err
}
integration.Status.Profile = profile
}

// Change the integration phase to Initialization after traits have been applied
Expand All @@ -73,24 +77,26 @@ func (action *platformSetupAction) Handle(ctx context.Context, integration *v1.I
}

// DetermineBestProfile tries to detect the best trait profile for the integration.
func determineBestProfile(ctx context.Context, c client.Client, integration *v1.Integration, p *v1.IntegrationPlatform) v1.TraitProfile {
func determineBestProfile(c client.Client, integration *v1.Integration, p *v1.IntegrationPlatform) (v1.TraitProfile, error) {
if integration.Spec.Profile != "" {
return integration.Spec.Profile
return integration.Spec.Profile, nil
}
if integration.Status.Profile != "" {
// Integration already has a profile
return integration.Status.Profile
return integration.Status.Profile, nil
}
if p.Status.Profile != "" {
// Use platform profile if set
return p.Status.Profile
return p.Status.Profile, nil
}
if p.Spec.Profile != "" {
// Use platform spec profile if set
return p.Spec.Profile
return p.Spec.Profile, nil
}
if knative.IsEnabledInNamespace(ctx, c, integration.Namespace) {
return v1.TraitProfileKnative
if ok, err := knative.IsInstalled(c); err != nil {
return "", err
} else if ok {
return v1.TraitProfileKnative, nil
}
return platform.GetProfile(p)
return platform.GetProfile(p), nil
}
4 changes: 3 additions & 1 deletion pkg/controller/kameletbinding/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ func determineProfile(ctx context.Context, c client.Client, binding *v1alpha1.Ka
return pl.Spec.Profile, nil
}
}
if knative.IsEnabledInNamespace(ctx, c, binding.Namespace) {
if ok, err := knative.IsInstalled(c); err != nil {
return "", err
} else if ok {
return v1.TraitProfileKnative, nil
}
if pl != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/install/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func SetupClusterWideResourcesOrCollect(ctx context.Context, clientProvider clie
}
}

isKnative, err := knative.IsInstalled(ctx, c)
isKnative, err := knative.IsInstalled(c)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/install/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const knativeAddressableResolverClusterRoleName = "addressable-resolver"
// BindKnativeAddressableResolverClusterRole binds the Knative addressable resolver aggregated ClusterRole
// to the operator ServiceAccount.
func BindKnativeAddressableResolverClusterRole(ctx context.Context, c kubernetes.Interface, namespace string, operatorNamespace string) error {
if isKnative, err := knative.IsInstalled(ctx, c); err != nil {
if isKnative, err := knative.IsInstalled(c); err != nil {
return err
} else if !isKnative {
return nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/install/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func OperatorOrCollect(ctx context.Context, cmd *cobra.Command, c client.Client,
}

// Additionally, install Knative resources (roles and bindings)
isKnative, err := knative.IsInstalled(ctx, c)
isKnative, err := knative.IsInstalled(c)
if err != nil {
return err
}
Expand Down
43 changes: 5 additions & 38 deletions pkg/util/knative/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,15 @@ limitations under the License.
package knative

import (
"context"

"github.com/apache/camel-k/pkg/client"
kubernetesutils "github.com/apache/camel-k/pkg/util/kubernetes"
"github.com/apache/camel-k/pkg/util/log"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)

// IsEnabledInNamespace returns true if we can list some basic knative objects in the given namespace.
//
// This method can be used at operator level to check if knative resources can be accessed.
func IsEnabledInNamespace(ctx context.Context, c client.Client, namespace string) bool {
dyn, err := dynamic.NewForConfig(c.GetConfig())
if err != nil {
log.Infof("could not create dynamic client to check knative installation in namespace %s, got error: %v", namespace, err)
return false
}
for _, kgv := range RequiredKinds {
_, err = dyn.Resource(schema.GroupVersionResource{
Group: kgv.Group,
Version: kgv.Version,
Resource: kgv.Resource,
}).Namespace(namespace).List(ctx, metav1.ListOptions{})

if err == nil {
return true
}
}

log.Infof("could not find any knative type in namespace %s, last error was: %v", namespace, err)
return false
}
util "github.com/apache/camel-k/pkg/util/kubernetes"
)

// IsInstalled returns true if we are connected to a cluster with Knative installed
//
// This method should not be called from the operator, as it might require permissions that are not available.
func IsInstalled(ctx context.Context, c kubernetes.Interface) (bool, error) {
// check some Knative APIs
// IsInstalled returns true if we are connected to a cluster with Knative installed.
func IsInstalled(c kubernetes.Interface) (bool, error) {
for _, api := range getRequiredKnativeGroupVersions() {
if installed, err := isInstalled(c, api); err != nil {
return false, err
Expand All @@ -72,7 +39,7 @@ func IsInstalled(ctx context.Context, c kubernetes.Interface) (bool, error) {

func isInstalled(c kubernetes.Interface, api schema.GroupVersion) (bool, error) {
_, err := c.Discovery().ServerResourcesForGroupVersion(api.String())
if err != nil && (k8serrors.IsNotFound(err) || kubernetesutils.IsUnknownAPIError(err)) {
if err != nil && (k8serrors.IsNotFound(err) || util.IsUnknownAPIError(err)) {
return false, nil
} else if err != nil {
return false, err
Expand Down

0 comments on commit 3652ee7

Please sign in to comment.