You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/07/26 01:24:29 UTC

[GitHub] [apisix-ingress-controller] tokers commented on a change in pull request #606: feat: add support for Knative

tokers commented on a change in pull request #606:
URL: https://github.com/apache/apisix-ingress-controller/pull/606#discussion_r676233793



##########
File path: pkg/ingress/knative_ingress.go
##########
@@ -0,0 +1,299 @@
+// 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 ingress
+
+import (
+	"context"
+	"fmt"
+	"time"
+
+	"go.uber.org/zap"
+	k8serrors "k8s.io/apimachinery/pkg/api/errors"
+	"k8s.io/client-go/tools/cache"
+	"k8s.io/client-go/util/workqueue"
+
+	"github.com/apache/apisix-ingress-controller/pkg/kube"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+	"github.com/apache/apisix-ingress-controller/pkg/types"
+)
+
+//const knativeIngressClassKey = "networking.knative.dev/ingress.class"
+
+type knativeIngressController struct {
+	controller *Controller
+	workqueue  workqueue.RateLimitingInterface
+	workers    int
+}
+
+func (c *Controller) newKnativeIngressController() *knativeIngressController {
+	ctl := &knativeIngressController{
+		controller: c,
+		workqueue:  workqueue.NewNamedRateLimitingQueue(workqueue.NewItemFastSlowRateLimiter(1*time.Second, 60*time.Second, 5), "KnativeIngress"),
+		workers:    1,
+	}
+
+	c.knativeIngressInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
+		AddFunc:    ctl.onAdd,
+		UpdateFunc: ctl.onUpdate,
+		DeleteFunc: ctl.OnDelete,
+	})
+	return ctl
+}
+
+func (c *knativeIngressController) run(ctx context.Context) {
+	log.Info("knative ingress controller started")
+	defer log.Infof("knative ingress controller exited")
+	defer c.workqueue.ShutDown()
+
+	if !cache.WaitForCacheSync(ctx.Done(), c.controller.knativeIngressInformer.HasSynced) {
+		log.Errorf("cache sync failed")
+		return
+	}
+	for i := 0; i < c.workers; i++ {
+		go c.runWorker(ctx)
+	}
+	<-ctx.Done()
+}
+
+func (c *knativeIngressController) runWorker(ctx context.Context) {
+	for {
+		obj, quit := c.workqueue.Get()
+		if quit {
+			return
+		}
+		err := c.sync(ctx, obj.(*types.Event))
+		c.workqueue.Done(obj)
+		c.handleSyncErr(obj, err)
+	}
+}
+
+func (c *knativeIngressController) sync(ctx context.Context, ev *types.Event) error {
+	ingEv := ev.Object.(kube.KnativeIngressEvent)
+	namespace, name, err := cache.SplitMetaNamespaceKey(ingEv.Key)
+	if err != nil {
+		log.Errorf("found knative ingress resource with invalid meta namespace key %s: %s", ingEv.Key, err)
+		return err
+	}
+
+	var ing kube.KnativeIngress
+	switch ingEv.GroupVersion {

Review comment:
       Version judgement can be made in the `onAdd`, `onUpdate` and `onDelete` callbacks.

##########
File path: pkg/kube/translation/knative_ingress.go
##########
@@ -0,0 +1,149 @@
+// 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 translation
+
+import (
+	"fmt"
+	"strings"
+
+	"go.uber.org/zap"
+	"k8s.io/apimachinery/pkg/util/intstr"
+	knativev1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1"
+
+	"github.com/apache/apisix-ingress-controller/pkg/id"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+	apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
+)
+
+func (t *translator) translateKnativeIngressV1alpha1(ing *knativev1alpha1.Ingress) (*TranslateContext, error) {
+	ctx := &TranslateContext{
+		upstreamMap: make(map[string]struct{}),
+	}
+	plugins := t.translateAnnotations(ing.Annotations)
+
+	for i, rule := range ing.Spec.Rules {
+		hosts := rule.Hosts
+		if rule.HTTP == nil {
+			continue
+		}
+		for j, httpPath := range rule.HTTP.Paths {
+			var (
+				ups *apisixv1.Upstream
+				err error
+			)
+			knativeBackend := knativeSelectSplit(httpPath.Splits)
+			servicePort := knativeBackend.ServicePort
+			serviceName := fmt.Sprintf("%s.%s.%s", knativeBackend.ServiceNamespace, knativeBackend.ServiceName,

Review comment:
       I am confused by the service name. Why here we should concatenate the namespace and name and port, but it was used just as the Kubernetes Service Name. I was wondering whether it really works.

##########
File path: pkg/kube/translation/knative_ingress.go
##########
@@ -0,0 +1,149 @@
+// 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 translation
+
+import (
+	"fmt"
+	"strings"
+
+	"go.uber.org/zap"
+	"k8s.io/apimachinery/pkg/util/intstr"
+	knativev1alpha1 "knative.dev/networking/pkg/apis/networking/v1alpha1"
+
+	"github.com/apache/apisix-ingress-controller/pkg/id"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+	apisixv1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
+)
+
+func (t *translator) translateKnativeIngressV1alpha1(ing *knativev1alpha1.Ingress) (*TranslateContext, error) {
+	ctx := &TranslateContext{
+		upstreamMap: make(map[string]struct{}),
+	}
+	plugins := t.translateAnnotations(ing.Annotations)
+
+	for i, rule := range ing.Spec.Rules {
+		hosts := rule.Hosts
+		if rule.HTTP == nil {
+			continue
+		}
+		for j, httpPath := range rule.HTTP.Paths {
+			var (
+				ups *apisixv1.Upstream
+				err error
+			)
+			knativeBackend := knativeSelectSplit(httpPath.Splits)

Review comment:
       Why just select one? Maybe others should be put to the traffic-split plugin.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org