tokers commented on a change in pull request #573:
URL:
https://github.com/apache/apisix-ingress-controller/pull/573#discussion_r698117176
##########
File path: pkg/api/server.go
##########
@@ -12,14 +12,21 @@
// 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 api
import (
+ "context"
+ "crypto/tls"
"net"
"net/http"
"net/http/pprof"
+ "time"
Review comment:
Sort the import.
##########
File path: pkg/api/server.go
##########
@@ -67,13 +99,51 @@ func NewServer(cfg *config.Config) (*Server, error) {
func (srv *Server) Run(stopCh <-chan struct{}) error {
go func() {
<-stopCh
- if err := srv.httpListener.Close(); err != nil {
- log.Errorf("failed to close http listener: %s", err)
+ ctx, cancel := context.WithTimeout(context.Background(),
5*time.Second)
+ defer cancel()
+
+ closeHttp := make(chan struct{})
+ go srv.closeHttpServer(closeHttp)
Review comment:
The codes here are weird, we may both add a timeout for these two
servers, and the closing can be parallel.
##########
File path: pkg/api/router/webhook.go
##########
@@ -0,0 +1,29 @@
+// 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 router
+
+import (
+ "github.com/gin-gonic/gin"
+
+ "github.com/apache/apisix-ingress-controller/pkg/api/validation"
+ "github.com/apache/apisix-ingress-controller/pkg/apisix"
+)
+
Review comment:
Please add some comments for this function.
##########
File path: pkg/api/server.go
##########
@@ -12,14 +12,21 @@
// 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 api
import (
+ "context"
+ "crypto/tls"
"net"
"net/http"
"net/http/pprof"
+ "time"
+
+ "github.com/apache/apisix-ingress-controller/pkg/apisix"
Review comment:
Sort the import.
##########
File path: pkg/api/router/webhook.go
##########
@@ -0,0 +1,29 @@
+// 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 router
+
+import (
+ "github.com/gin-gonic/gin"
+
+ "github.com/apache/apisix-ingress-controller/pkg/api/validation"
+ "github.com/apache/apisix-ingress-controller/pkg/apisix"
+)
+
+func MountWebhooks(r *gin.Engine, co *apisix.ClusterOptions) {
+ // init the schema client
+ _, _ = validation.GetSchemaClient(co)
+ r.POST("/validation/apisixroute/plugin",
gin.WrapH(validation.NewPluginValidatorHandler()))
Review comment:
Maybe `/validation/apisixroutes/plugin`?
##########
File path: test/e2e/scaffold/scaffold.go
##########
@@ -340,12 +350,12 @@ func (s *Scaffold) afterEach() {
}
}
- err := k8s.DeleteNamespaceE(s.t, s.kubectlOptions, s.namespace)
- assert.Nilf(ginkgo.GinkgoT(), err, "deleting namespace %s", s.namespace)
-
- for _, f := range s.finializers {
- f()
- }
+ //err := k8s.DeleteNamespaceE(s.t, s.kubectlOptions, s.namespace)
Review comment:
Why comment these codes?
##########
File path: pkg/api/server.go
##########
@@ -41,23 +46,43 @@ func NewServer(cfg *config.Config) (*Server, error) {
return nil, err
}
gin.SetMode(gin.ReleaseMode)
- router := gin.New()
- router.Use(gin.Recovery(), gin.Logger())
- apirouter.Mount(router)
+ httpServer := gin.New()
+ httpServer.Use(gin.Recovery(), gin.Logger())
+ apirouter.Mount(httpServer)
srv := &Server{
- router: router,
+ httpServer: httpServer,
httpListener: httpListener,
}
-
if cfg.EnableProfiling {
srv.pprofMu = new(http.ServeMux)
srv.pprofMu.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
srv.pprofMu.HandleFunc("/debug/pprof/profile", pprof.Profile)
srv.pprofMu.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
srv.pprofMu.HandleFunc("/debug/pprof/trace", pprof.Trace)
srv.pprofMu.HandleFunc("/debug/pprof/", pprof.Index)
- router.GET("/debug/pprof/*profile",
gin.WrapF(srv.pprofMu.ServeHTTP))
+ httpServer.GET("/debug/pprof/*profile",
gin.WrapF(srv.pprofMu.ServeHTTP))
+ }
+
+ cert, err := tls.LoadX509KeyPair(cfg.CertFilePath, cfg.KeyFilePath)
Review comment:
Then you should prepare the default certificate and private key, or it's
difficult to start the server in the local environment.
##########
File path: pkg/api/validation/apisix_route.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 validation
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "net/http"
+ "strings"
+
+ "github.com/hashicorp/go-multierror"
+ kwhhttp "github.com/slok/kubewebhook/v2/pkg/http"
+ kwhmodel "github.com/slok/kubewebhook/v2/pkg/model"
+ kwhvalidating "github.com/slok/kubewebhook/v2/pkg/webhook/validating"
+ 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"
+)
+
+// NewPluginValidatorHandler returns a new http.Handler ready to handle
admission reviews using the pluginValidator.
+func NewPluginValidatorHandler() http.Handler {
+ // Create a validating webhook.
+ wh, err := kwhvalidating.NewWebhook(kwhvalidating.WebhookConfig{
+ ID: "apisixRoute-plugin",
+ Validator: pluginValidator,
+ })
+ if err != nil {
+ log.Errorf("failed to create webhook: %s", err)
+ }
+
+ h, err := kwhhttp.HandlerFor(kwhhttp.HandlerConfig{Webhook: wh})
+ if err != nil {
+ log.Errorf("failed to create webhook handle: %s", err)
+ }
+
+ return h
+}
+
+// ErrNotApisixRoute will be used when the validating object is not
ApisixRoute.
+var ErrNotApisixRoute = errors.New("object is not ApisixRoute")
+
+type apisixRoutePlugin struct {
+ Name string
+ Config interface{}
+}
+
+// pluginValidator validates plugins in ApisixRoute.
+// When the validation of one plugin fails, it will continue to validate the
rest of plugins.
+var pluginValidator = kwhvalidating.ValidatorFunc(
+ func(ctx context.Context, review *kwhmodel.AdmissionReview, object
metav1.Object) (result *kwhvalidating.ValidatorResult, err error) {
+ log.Debug("arrive plugin validator webhook")
+
+ valid := true
+ var plugins []apisixRoutePlugin
+
+ switch ar := object.(type) {
+ case *v2beta1.ApisixRoute:
+ for _, h := range ar.Spec.HTTP {
+ for _, p := range h.Plugins {
+ // only check plugins that are enabled.
+ if p.Enable {
+ plugins = append(plugins,
apisixRoutePlugin{
+ p.Name, p.Config,
+ })
+ }
+ }
+ }
+ case *v2alpha1.ApisixRoute:
+ for _, h := range ar.Spec.HTTP {
+ for _, p := range h.Plugins {
+ if p.Enable {
+ plugins = append(plugins,
apisixRoutePlugin{
+ p.Name, p.Config,
+ })
+ }
+ }
+ }
+ case *v1.ApisixRoute:
+ for _, r := range ar.Spec.Rules {
+ for _, path := range r.Http.Paths {
+ for _, p := range path.Plugins {
+ if p.Enable {
+ plugins =
append(plugins, apisixRoutePlugin{
+ p.Name,
p.Config,
+ })
+ }
+ }
+ }
+ }
+ default:
+ return &kwhvalidating.ValidatorResult{Valid: false,
Message: ErrNotApisixRoute.Error()}, ErrNotApisixRoute
+ }
+
+ client, err := GetSchemaClient(&apisix.ClusterOptions{})
+ if err != nil {
+ log.Errorf("failed to get the schema client: %s", err)
+ return &kwhvalidating.ValidatorResult{Valid: false,
Message: "failed to get the schema client"}, err
+ }
+
+ var msg []string
+ for _, p := range plugins {
+ if v, m, err := validatePlugin(client, p.Name,
p.Config); !v {
+ valid = false
+ msg = append(msg, m)
+ log.Warnf("failed to validate plugin %s: %s",
p.Name, err)
+ }
+ }
+
+ return &kwhvalidating.ValidatorResult{Valid: valid, Message:
strings.Join(msg, "\n")}, nil
+ })
Review comment:
```suggestion
}
)
```
##########
File path: test/e2e/scaffold/scaffold.go
##########
@@ -407,3 +417,16 @@ func waitExponentialBackoff(condFunc func() (bool, error))
error {
}
return wait.ExponentialBackoff(backoff, condFunc)
}
+
+// generateWebhookCert generates signed certs of webhook and create the
corresponding secret by running a script.
+func generateWebhookCert(ns string) error {
+ commandTemplate := `../../utils/webhook-create-signed-cert.sh`
Review comment:
Why not prepare static files and put them into `testdata` dir?
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]