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/09/07 02:03:47 UTC

[GitHub] [apisix-ingress-controller] tokers commented on a change in pull request #667: feat: add webhooks for consumer/tls/upstream

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



##########
File path: pkg/api/validation/apisix_consumer.go
##########
@@ -0,0 +1,80 @@
+// 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 validation
+
+import (
+	"context"
+	"errors"
+	"strings"
+
+	kwhmodel "github.com/slok/kubewebhook/v2/pkg/model"
+	kwhvalidating "github.com/slok/kubewebhook/v2/pkg/webhook/validating"
+	"github.com/xeipuuv/gojsonschema"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
+	"github.com/apache/apisix-ingress-controller/pkg/apisix"
+	v1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2beta1"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+)
+
+// ErrNotApisixConsumer will be used when the validating object is not ApisixConsumer.
+var ErrNotApisixConsumer = errors.New("object is not ApisixConsumer")
+
+// ApisixConsumerValidator validates ApisixConsumer's spec.
+var ApisixConsumerValidator = kwhvalidating.ValidatorFunc(
+	func(ctx context.Context, review *kwhmodel.AdmissionReview, object metav1.Object) (result *kwhvalidating.ValidatorResult, err error) {
+		log.Debug("arrive ApisixConsumer validator webhook")
+
+		valid := true
+		var spec interface{}
+
+		switch ac := object.(type) {
+		case *v2beta1.ApisixRoute:
+			spec = ac.Spec
+		case *v2alpha1.ApisixRoute:
+			spec = ac.Spec
+		case *v1.ApisixRoute:
+			spec = ac.Spec
+		default:
+			return &kwhvalidating.ValidatorResult{Valid: false, Message: ErrNotApisixConsumer.Error()}, ErrNotApisixConsumer
+		}
+
+		client, err := GetSchemaClient(&apisix.ClusterOptions{})
+		if err != nil {
+			msg := "failed to get the schema client"
+			log.Errorf("%s: %s", msg, err)
+			return &kwhvalidating.ValidatorResult{Valid: false, Message: msg}, err
+		}
+
+		cs, err := client.GetConsumerSchema(ctx)
+		if err != nil {
+			msg := "failed to get consumer's schema"
+			log.Errorf("%s: %s", msg, err)

Review comment:
       Ditto.

##########
File path: pkg/api/validation/apisix_consumer.go
##########
@@ -0,0 +1,80 @@
+// 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 validation
+
+import (
+	"context"
+	"errors"
+	"strings"
+
+	kwhmodel "github.com/slok/kubewebhook/v2/pkg/model"
+	kwhvalidating "github.com/slok/kubewebhook/v2/pkg/webhook/validating"
+	"github.com/xeipuuv/gojsonschema"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
+	"github.com/apache/apisix-ingress-controller/pkg/apisix"
+	v1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2beta1"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+)
+
+// ErrNotApisixConsumer will be used when the validating object is not ApisixConsumer.
+var ErrNotApisixConsumer = errors.New("object is not ApisixConsumer")
+
+// ApisixConsumerValidator validates ApisixConsumer's spec.
+var ApisixConsumerValidator = kwhvalidating.ValidatorFunc(
+	func(ctx context.Context, review *kwhmodel.AdmissionReview, object metav1.Object) (result *kwhvalidating.ValidatorResult, err error) {
+		log.Debug("arrive ApisixConsumer validator webhook")
+
+		valid := true
+		var spec interface{}
+
+		switch ac := object.(type) {
+		case *v2beta1.ApisixRoute:
+			spec = ac.Spec
+		case *v2alpha1.ApisixRoute:
+			spec = ac.Spec
+		case *v1.ApisixRoute:
+			spec = ac.Spec
+		default:
+			return &kwhvalidating.ValidatorResult{Valid: false, Message: ErrNotApisixConsumer.Error()}, ErrNotApisixConsumer
+		}
+
+		client, err := GetSchemaClient(&apisix.ClusterOptions{})
+		if err != nil {
+			msg := "failed to get the schema client"
+			log.Errorf("%s: %s", msg, err)

Review comment:
       ```suggestion
   			log.Errorf("failed to get schema client: %s", err)
   ```

##########
File path: pkg/api/validation/apisix_consumer.go
##########
@@ -0,0 +1,80 @@
+// 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 validation
+
+import (
+	"context"
+	"errors"
+	"strings"
+
+	kwhmodel "github.com/slok/kubewebhook/v2/pkg/model"
+	kwhvalidating "github.com/slok/kubewebhook/v2/pkg/webhook/validating"
+	"github.com/xeipuuv/gojsonschema"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
+	"github.com/apache/apisix-ingress-controller/pkg/apisix"
+	v1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2beta1"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+)
+
+// ErrNotApisixConsumer will be used when the validating object is not ApisixConsumer.
+var ErrNotApisixConsumer = errors.New("object is not ApisixConsumer")

Review comment:
       Is this object used in other places? Or we don't have to export it.

##########
File path: pkg/api/validation/apisix_consumer.go
##########
@@ -0,0 +1,80 @@
+// 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 validation
+
+import (
+	"context"
+	"errors"
+	"strings"
+
+	kwhmodel "github.com/slok/kubewebhook/v2/pkg/model"
+	kwhvalidating "github.com/slok/kubewebhook/v2/pkg/webhook/validating"
+	"github.com/xeipuuv/gojsonschema"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+
+	"github.com/apache/apisix-ingress-controller/pkg/apisix"
+	v1 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2alpha1"
+	"github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2beta1"
+	"github.com/apache/apisix-ingress-controller/pkg/log"
+)
+
+// ErrNotApisixConsumer will be used when the validating object is not ApisixConsumer.
+var ErrNotApisixConsumer = errors.New("object is not ApisixConsumer")
+
+// ApisixConsumerValidator validates ApisixConsumer's spec.
+var ApisixConsumerValidator = kwhvalidating.ValidatorFunc(
+	func(ctx context.Context, review *kwhmodel.AdmissionReview, object metav1.Object) (result *kwhvalidating.ValidatorResult, err error) {
+		log.Debug("arrive ApisixConsumer validator webhook")
+
+		valid := true
+		var spec interface{}
+
+		switch ac := object.(type) {
+		case *v2beta1.ApisixRoute:
+			spec = ac.Spec
+		case *v2alpha1.ApisixRoute:
+			spec = ac.Spec
+		case *v1.ApisixRoute:
+			spec = ac.Spec
+		default:
+			return &kwhvalidating.ValidatorResult{Valid: false, Message: ErrNotApisixConsumer.Error()}, ErrNotApisixConsumer
+		}
+
+		client, err := GetSchemaClient(&apisix.ClusterOptions{})
+		if err != nil {
+			msg := "failed to get the schema client"
+			log.Errorf("%s: %s", msg, err)
+			return &kwhvalidating.ValidatorResult{Valid: false, Message: msg}, err
+		}
+
+		cs, err := client.GetConsumerSchema(ctx)
+		if err != nil {
+			msg := "failed to get consumer's schema"
+			log.Errorf("%s: %s", msg, err)
+			return &kwhvalidating.ValidatorResult{Valid: false, Message: msg}, err
+		}
+		acSchemaLoader := gojsonschema.NewStringLoader(cs.Content)
+
+		var msg []string

Review comment:
       Why use slice? Only one place uses it.




-- 
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