Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(knative): func to check when either serving or eventing are insta… #5026

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/knative/knative_platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
func TestKnativePlatformDetection(t *testing.T) {
RegisterTestingT(t)

installed, err := knative.IsInstalled(TestClient())
installed, err := knative.IsServingInstalled(TestClient())
Expect(err).NotTo(HaveOccurred())
if !installed {
t.Error("Knative not installed in the cluster")
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/integration/platform_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func determineBestProfile(c client.Client, integration *v1.Integration, p *v1.In
// Use platform spec profile if set
return p.Spec.Profile, nil
}
if ok, err := knative.IsInstalled(c); err != nil {
if ok, err := knative.IsServingInstalled(c); err != nil {
return "", err
} else if ok {
return v1.TraitProfileKnative, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/kameletbinding/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func determineProfile(ctx context.Context, c client.Client, binding *v1alpha1.Ka
return pl.Spec.Profile, nil
}
}
if ok, err := knative.IsInstalled(c); err != nil {
if ok, err := knative.IsServingInstalled(c); err != nil {
return "", err
} else if ok {
return v1.TraitProfileKnative, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/pipe/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func determineProfile(ctx context.Context, c client.Client, binding *v1.Pipe) (v
return pl.Spec.Profile, nil
}
}
if ok, err := knative.IsInstalled(c); err != nil {
if ok, err := knative.IsServingInstalled(c); err != nil {
return "", err
} else if ok {
return v1.TraitProfileKnative, 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 @@ -121,7 +121,7 @@ func installClusterRoles(

// === For Knative ===
// ClusterRole: camel-k-operator-bind-addressable-resolver
isKnative, err := knative.IsInstalled(c)
isKnative, err := knative.IsEventingInstalled(c)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/trait/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (t *containerTrait) Configure(e *Environment) (bool, *TraitCondition, error
return false, nil, nil
}

knativeInstalled, _ := knative.IsInstalled(e.Client)
knativeInstalled, _ := knative.IsEventingInstalled(e.Client)
if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) && !knativeInstalled {
hasKnativeEndpoint, err := containsEndpoint("knative", e, t.Client)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/trait/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ func TestRunNonKnativeEndpointWithKnativeNotInstalled(t *testing.T) {
func createEnvironment() *Environment {

client, _ := test.NewFakeClient()
// disable the knative service api
// disable the knative eventing api
fakeClient := client.(*test.FakeClient) //nolint
fakeClient.DisableAPIGroupDiscovery("serving.knative.dev/v1")
fakeClient.DisableAPIGroupDiscovery("eventing.knative.dev/v1")

replicas := int32(3)
catalog, _ := camel.QuarkusCatalog()
Expand Down
8 changes: 4 additions & 4 deletions pkg/util/bindings/knative_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func (k KnativeRefBindingProvider) Translate(ctx BindingContext, endpointCtx End
return nil, err
}

if knativeInstalled, _ := knative.IsInstalled(ctx.Client); !knativeInstalled {
// works only when Knative is installed
if refInstalled, _ := knative.IsRefKindInstalled(ctx.Client, *e.Ref); !refInstalled {
// works only when Knative specific API Kind is installed
return nil, fmt.Errorf("integration referencing Knative endpoint '%s' that cannot run, "+
"because Knative is not installed on the cluster", e.Ref.Name)
}
Expand Down Expand Up @@ -155,8 +155,8 @@ func (k V1alpha1KnativeRefBindingProvider) Translate(ctx V1alpha1BindingContext,
return nil, err
}

if knativeInstalled, _ := knative.IsInstalled(ctx.Client); !knativeInstalled {
// works only when Knative is installed
if refInstalled, _ := knative.IsRefKindInstalled(ctx.Client, *e.Ref); !refInstalled {
// works only when Knative specific API Kind is installed
return nil, fmt.Errorf("integration referencing Knative endpoint '%s' that cannot run, "+
"because Knative is not installed on the cluster", e.Ref.Name)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/bindings/knative_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ func TestKnativeNotInstalled(t *testing.T) {
client, err := test.NewFakeClient()
assert.NoError(t, err)

// disable the knative service api
// disable the knative eventing api
fakeClient := client.(*test.FakeClient) //nolint
fakeClient.DisableAPIGroupDiscovery("serving.knative.dev/v1")
fakeClient.DisableAPIGroupDiscovery("eventing.knative.dev/v1")

bindingContext := BindingContext{
Ctx: ctx,
Expand Down
13 changes: 0 additions & 13 deletions pkg/util/knative/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,6 @@ var (
Resource: "brokers",
},
}

// RequiredKinds are Knative kinds used by Camel K for materializing integrations.
// They must be present on the cluster.
RequiredKinds = []GroupVersionKindResource{
{
GroupVersionKind: schema.GroupVersionKind{
Kind: "Service",
Group: "serving.knative.dev",
Version: "v1",
},
Resource: "services",
},
}
)

// GroupVersionKindResource --.
Expand Down
43 changes: 23 additions & 20 deletions pkg/util/knative/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,40 @@ limitations under the License.
package knative

import (
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"

util "github.com/apache/camel-k/v2/pkg/util/kubernetes"
)

// 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
} else if installed {
return true, nil
}
// IsRefKindInstalled returns true if the cluster has the referenced Kind installed.
func IsRefKindInstalled(c kubernetes.Interface, ref corev1.ObjectReference) (bool, error) {
if installed, err := isInstalled(c, ref.GroupVersionKind().GroupVersion()); err != nil {
return false, err
} else if installed {
return true, nil
}
return false, nil
}

// IsServingInstalled returns true if we are connected to a cluster with Knative Serving installed.
func IsServingInstalled(c kubernetes.Interface) (bool, error) {
return IsRefKindInstalled(c, corev1.ObjectReference{
Kind: "Service",
APIVersion: "serving.knative.dev/v1",
})
}

// IsEventingInstalled returns true if we are connected to a cluster with Knative Eventing installed.
func IsEventingInstalled(c kubernetes.Interface) (bool, error) {
return IsRefKindInstalled(c, corev1.ObjectReference{
Kind: "Broker",
APIVersion: "eventing.knative.dev/v1",
})
}

func isInstalled(c kubernetes.Interface, api schema.GroupVersion) (bool, error) {
_, err := c.Discovery().ServerResourcesForGroupVersion(api.String())
if err != nil && (k8serrors.IsNotFound(err) || util.IsUnknownAPIError(err)) {
Expand All @@ -46,15 +61,3 @@ func isInstalled(c kubernetes.Interface, api schema.GroupVersion) (bool, error)
}
return true, nil
}

func getRequiredKnativeGroupVersions() []schema.GroupVersion {
apis := make(map[schema.GroupVersion]bool)
res := make([]schema.GroupVersion, 0)
for _, gvk := range RequiredKinds {
if !apis[gvk.GroupVersion()] {
apis[gvk.GroupVersion()] = true
res = append(res, gvk.GroupVersion())
}
}
return res
}
17 changes: 16 additions & 1 deletion pkg/util/test/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,26 @@ func (f *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*me
}
}

// used in util/knative/enabled.go to verify if knative is installed
// used to verify if knative is installed
if groupVersion == "serving.knative.dev/v1" && !util.StringSliceExists(f.disabledGroups, groupVersion) {
return &metav1.APIResourceList{
GroupVersion: "serving.knative.dev/v1",
}, nil
}
if groupVersion == "eventing.knative.dev/v1" && !util.StringSliceExists(f.disabledGroups, groupVersion) {
return &metav1.APIResourceList{
GroupVersion: "eventing.knative.dev/v1",
}, nil
}
if groupVersion == "messaging.knative.dev/v1" && !util.StringSliceExists(f.disabledGroups, groupVersion) {
return &metav1.APIResourceList{
GroupVersion: "messaging.knative.dev/v1",
}, nil
}
if groupVersion == "messaging.knative.dev/v1beta1" && !util.StringSliceExists(f.disabledGroups, groupVersion) {
return &metav1.APIResourceList{
GroupVersion: "messaging.knative.dev/v1beta1",
}, nil
}
return f.DiscoveryInterface.ServerResourcesForGroupVersion(groupVersion)
}
Loading