diff --git a/go.mod b/go.mod index 295aacbf1..9ecd84928 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ replace github.com/docker/docker => github.com/moby/moby v17.12.0-ce-rc1.0.20200 replace github.com/imdario/mergo => github.com/imdario/mergo v0.3.5 require ( + github.com/agiledragon/gomonkey/v2 v2.2.0 github.com/dubbogo/go-zookeeper v1.0.4-0.20211212162352-f9d2183d89d5 github.com/dubbogo/gost v1.13.1 github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1 diff --git a/go.sum b/go.sum index efb323eaf..89010dc37 100644 --- a/go.sum +++ b/go.sum @@ -156,6 +156,8 @@ github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrU github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= +github.com/agiledragon/gomonkey/v2 v2.2.0 h1:QJWqpdEhGV/JJy70sZ/LDnhbSlMrqHAWHcNOjz1kyuI= +github.com/agiledragon/gomonkey/v2 v2.2.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/ahmetb/gen-crd-api-reference-docs v0.3.0/go.mod h1:TdjdkYhlOifCQWPs1UdTma97kQQMozf5h26hTuG70u8= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/pkg/bootstrap/server.go b/pkg/bootstrap/server.go index 92d9f32cc..f705b0607 100644 --- a/pkg/bootstrap/server.go +++ b/pkg/bootstrap/server.go @@ -229,7 +229,9 @@ func (s *Server) initConfigController() error { // Defer starting the controller until after the service is created. s.server.RunComponent(func(stop <-chan struct{}) error { - ingressConfig.InitializeCluster(ingressController, stop) + if err := ingressConfig.InitializeCluster(ingressController, stop); err != nil { + return err + } go s.configController.Run(stop) return nil }) @@ -322,8 +324,7 @@ func (s *Server) initXdsServer() error { s.xdsServer.Start(stop) return nil }) - s.initGrpcServer() - return nil + return s.initGrpcServer() } func (s *Server) initGrpcServer() error { @@ -381,6 +382,7 @@ func (s *Server) initHttpServer() error { return nil } +// readyHandler checks whether the http server is ready func (s *Server) readyHandler(w http.ResponseWriter, _ *http.Request) { for name, fn := range s.readinessProbes { if ready, err := fn(); !ready { @@ -394,10 +396,7 @@ func (s *Server) readyHandler(w http.ResponseWriter, _ *http.Request) { // cachesSynced checks whether caches have been synced. func (s *Server) cachesSynced() bool { - if !s.configController.HasSynced() { - return false - } - return true + return s.configController.HasSynced() } func (s *Server) waitForCacheSync(stop <-chan struct{}) bool { diff --git a/pkg/bootstrap/server_test.go b/pkg/bootstrap/server_test.go new file mode 100644 index 000000000..60e1f2a2a --- /dev/null +++ b/pkg/bootstrap/server_test.go @@ -0,0 +1,66 @@ +// Copyright (c) 2022 Alibaba Group Holding Ltd. +// +// Licensed 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 bootstrap + +import ( + "context" + "testing" + + "github.com/agiledragon/gomonkey/v2" + "istio.io/istio/pilot/pkg/features" + "istio.io/istio/pkg/keepalive" + + higresskube "github.com/alibaba/higress/pkg/kube" +) + +func TestStartWithNoError(t *testing.T) { + var ( + s *Server + err error + ) + + mockFn := func(s *Server) error { + s.kubeClient = higresskube.NewFakeClient() + return nil + } + + gomonkey.ApplyFunc((*Server).initKubeClient, mockFn) + + if s, err = NewServer(newServerArgs()); err != nil { + t.Errorf("failed to create server: %v", err) + } + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + if err = s.Start(ctx.Done()); err != nil { + t.Errorf("failed to start the server: %v", err) + } +} + +func newServerArgs() *ServerArgs { + return &ServerArgs{ + Debug: true, + NativeIstio: true, + HttpAddress: ":8888", + GrpcAddress: ":15051", + GrpcKeepAliveOptions: keepalive.DefaultOption(), + XdsOptions: XdsOptions{ + DebounceAfter: features.DebounceAfter, + DebounceMax: features.DebounceMax, + EnableEDSDebounce: features.EnableEDSDebounce, + }, + } +} diff --git a/pkg/kube/client.go b/pkg/kube/client.go index b14c75c0a..e6d463998 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -61,6 +61,7 @@ func NewFakeClient(objects ...runtime.Object) Client { } c.higress = higressfake.NewSimpleClientset() c.higressInformer = higressinformer.NewSharedInformerFactoryWithOptions(c.higress, resyncInterval) + c.informerWatchesPending = atomic.NewInt32(0) // https://github.com/kubernetes/kubernetes/issues/95372 // There is a race condition in the client fakes, where events that happen between the List and Watch