diff --git a/.github/workflows/unit-test-ci.yml b/.github/workflows/unit-test-ci.yml new file mode 100644 index 0000000000..4da2ecee01 --- /dev/null +++ b/.github/workflows/unit-test-ci.yml @@ -0,0 +1,23 @@ +name: unit-test-ci + +on: + push: + branches: + - master + - kv/ci + pull_request: + branches: + - master +jobs: + run-test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: setup go + uses: actions/setup-go@v1 + with: + go-version: '1.13' + - name: run unit test + working-directory: ./ + run: | + go test ./... diff --git a/conf/init.go b/conf/init.go index d3914b8a20..36d10b409a 100644 --- a/conf/init.go +++ b/conf/init.go @@ -81,7 +81,7 @@ var Syslog syslog var config *restclient.Config -func init() { +func Init() { // 获取当前环境 setEnvironment() // 获取配置文件路径 diff --git a/main.go b/main.go index 06ef9a3147..6c97417ba7 100644 --- a/main.go +++ b/main.go @@ -19,9 +19,11 @@ import ( "github.com/api7/ingress-controller/cmd" "github.com/api7/ingress-controller/log" + "github.com/api7/ingress-controller/conf" ) func main() { + conf.Init() root := cmd.NewAPISIXIngressControllerCommand() if err := root.Execute(); err != nil { log.GetLogger().Error(err.Error()) diff --git a/pkg/ingress/apisix/upstream_test.go b/pkg/ingress/apisix/upstream_test.go index 6453b0b755..7edf23421a 100644 --- a/pkg/ingress/apisix/upstream_test.go +++ b/pkg/ingress/apisix/upstream_test.go @@ -1,17 +1,17 @@ - // 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. +// 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 apisix import ( @@ -73,18 +73,21 @@ func (epr *EndpointRequestTest) BuildEps(ns, name string, port int) []*v1.Node { } func buildExpectUpstream() *v1.Upstream { - fullName := "default_httpserver_8080" + fullName := "cloud_httpserver_8080" LBType := "chash" HashOn := "header" Key := "hello_key" fromKind := "ApisixUpstream" + group := "" upstreamExpect := &v1.Upstream{ - FullName: &fullName, - Name: &fullName, - Type: &LBType, - HashOn: &HashOn, - Key: &Key, - FromKind: &fromKind, + Group: &group, + ResourceVersion: &group, + FullName: &fullName, + Name: &fullName, + Type: &LBType, + HashOn: &HashOn, + Key: &Key, + FromKind: &fromKind, } return upstreamExpect } @@ -94,6 +97,7 @@ kind: ApisixUpstream apiVersion: apisix.apache.org/v1 metadata: name: httpserver + namespace: cloud spec: ports: - loadbalancer: diff --git a/pkg/ingress/controller/apisix_route.go b/pkg/ingress/controller/apisix_route.go index 310167e045..65decd23bb 100644 --- a/pkg/ingress/controller/apisix_route.go +++ b/pkg/ingress/controller/apisix_route.go @@ -1,86 +1,86 @@ - // 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. +// 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 controller import ( - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/cache" - "k8s.io/client-go/util/workqueue" - "github.com/gxthrj/apisix-ingress-types/pkg/client/listers/config/v1" + "fmt" + "github.com/api7/ingress-controller/pkg/ingress/apisix" + api6V1 "github.com/gxthrj/apisix-ingress-types/pkg/apis/config/v1" clientSet "github.com/gxthrj/apisix-ingress-types/pkg/client/clientset/versioned" - api6Informers "github.com/gxthrj/apisix-ingress-types/pkg/client/informers/externalversions/config/v1" api6Scheme "github.com/gxthrj/apisix-ingress-types/pkg/client/clientset/versioned/scheme" - api6V1 "github.com/gxthrj/apisix-ingress-types/pkg/apis/config/v1" + api6Informers "github.com/gxthrj/apisix-ingress-types/pkg/client/informers/externalversions/config/v1" + "github.com/gxthrj/apisix-ingress-types/pkg/client/listers/config/v1" + "github.com/gxthrj/seven/state" + "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/util/runtime" - "k8s.io/client-go/kubernetes/scheme" - "fmt" "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/tools/cache" + "k8s.io/client-go/util/workqueue" "time" - "k8s.io/apimachinery/pkg/api/errors" - "github.com/api7/ingress-controller/pkg/ingress/apisix" - "github.com/gxthrj/seven/state" ) -type ApisixRouteController struct{ - kubeclientset kubernetes.Interface +type ApisixRouteController struct { + kubeclientset kubernetes.Interface apisixRouteClientset clientSet.Interface - apisixRouteList v1.ApisixRouteLister - apisixRouteSynced cache.InformerSynced - workqueue workqueue.RateLimitingInterface + apisixRouteList v1.ApisixRouteLister + apisixRouteSynced cache.InformerSynced + workqueue workqueue.RateLimitingInterface } type RouteQueueObj struct { - Key string `json:"key"` + Key string `json:"key"` OldObj *api6V1.ApisixRoute `json:"old_obj"` - Ope string `json:"ope"` // add / update / delete + Ope string `json:"ope"` // add / update / delete } func BuildApisixRouteController( kubeclientset kubernetes.Interface, api6RouteClientset clientSet.Interface, - api6RouteInformer api6Informers.ApisixRouteInformer) *ApisixRouteController{ + api6RouteInformer api6Informers.ApisixRouteInformer) *ApisixRouteController { runtime.Must(api6Scheme.AddToScheme(scheme.Scheme)) controller := &ApisixRouteController{ - kubeclientset: kubeclientset, + kubeclientset: kubeclientset, apisixRouteClientset: api6RouteClientset, - apisixRouteList: api6RouteInformer.Lister(), - apisixRouteSynced: api6RouteInformer.Informer().HasSynced, - workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ApisixRoutes"), + apisixRouteList: api6RouteInformer.Lister(), + apisixRouteSynced: api6RouteInformer.Informer().HasSynced, + workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ApisixRoutes"), } api6RouteInformer.Informer().AddEventHandler( cache.ResourceEventHandlerFuncs{ - AddFunc: controller.addFunc, + AddFunc: controller.addFunc, UpdateFunc: controller.updateFunc, DeleteFunc: controller.deleteFunc, }) return controller } -func (c *ApisixRouteController) addFunc(obj interface{}){ +func (c *ApisixRouteController) addFunc(obj interface{}) { var key string var err error if key, err = cache.MetaNamespaceKeyFunc(obj); err != nil { runtime.HandleError(err) return } - rqo := &RouteQueueObj{Key: key, OldObj: nil, Ope:ADD} + rqo := &RouteQueueObj{Key: key, OldObj: nil, Ope: ADD} c.workqueue.AddRateLimited(rqo) } -func (c *ApisixRouteController) updateFunc(oldObj, newObj interface{}){ +func (c *ApisixRouteController) updateFunc(oldObj, newObj interface{}) { oldRoute := oldObj.(*api6V1.ApisixRoute) newRoute := newObj.(*api6V1.ApisixRoute) if oldRoute.ResourceVersion == newRoute.ResourceVersion { @@ -97,7 +97,7 @@ func (c *ApisixRouteController) updateFunc(oldObj, newObj interface{}){ c.workqueue.AddRateLimited(rqo) } -func (c *ApisixRouteController) deleteFunc(obj interface{}){ +func (c *ApisixRouteController) deleteFunc(obj interface{}) { var key string var err error key, err = cache.DeletionHandlingMetaNamespaceKeyFunc(obj) @@ -120,8 +120,9 @@ func (c *ApisixRouteController) Run(stop <-chan struct{}) error { return nil } -func (c *ApisixRouteController) runWorker(){ - for c.processNextWorkItem() {} +func (c *ApisixRouteController) runWorker() { + for c.processNextWorkItem() { + } } func (c *ApisixRouteController) processNextWorkItem() bool { @@ -175,17 +176,17 @@ func (c *ApisixRouteController) syncHandler(rqo *RouteQueueObj) error { } } -func (c *ApisixRouteController) add(key string) error{ +func (c *ApisixRouteController) add(key string) error { namespace, name, err := cache.SplitMetaNamespaceKey(key) if err != nil { - logger.Error("invalid resource key: %s", key) + logger.Errorf("invalid resource key: %s", key) return fmt.Errorf("invalid resource key: %s", key) } apisixIngressRoute, err := c.apisixRouteList.ApisixRoutes(namespace).Get(name) if err != nil { - if errors.IsNotFound(err){ - logger.Info("apisixRoute %s is removed", key) + if errors.IsNotFound(err) { + logger.Infof("apisixRoute %s is removed", key) return nil } runtime.HandleError(fmt.Errorf("failed to list apisixRoute %s/%s", key, err.Error())) @@ -197,28 +198,29 @@ func (c *ApisixRouteController) add(key string) error{ _, err = comb.Solver() return err } + // sync // 1.diff routes between old and new objects // 2.delete routes not exist -func (c *ApisixRouteController) sync(rqo *RouteQueueObj) error{ +func (c *ApisixRouteController) sync(rqo *RouteQueueObj) error { key := rqo.Key namespace, name, err := cache.SplitMetaNamespaceKey(key) if err != nil { - logger.Error("invalid resource key: %s", key) + logger.Errorf("invalid resource key: %s", key) return fmt.Errorf("invalid resource key: %s", key) } apisixIngressRoute, err := c.apisixRouteList.ApisixRoutes(namespace).Get(name) if err != nil { - if errors.IsNotFound(err){ - logger.Info("apisixRoute %s is removed", key) + if errors.IsNotFound(err) { + logger.Infof("apisixRoute %s is removed", key) return nil } runtime.HandleError(fmt.Errorf("failed to list apisixRoute %s/%s", key, err.Error())) return err } switch { - case rqo.Ope == UPDATE : + case rqo.Ope == UPDATE: oldApisixRoute := apisix.ApisixRoute(*rqo.OldObj) oldRoutes, _, _, _ := oldApisixRoute.Convert() @@ -227,7 +229,7 @@ func (c *ApisixRouteController) sync(rqo *RouteQueueObj) error{ rc := &state.RouteCompare{OldRoutes: oldRoutes, NewRoutes: newRoutes} return rc.Sync() - case rqo.Ope == DELETE : + case rqo.Ope == DELETE: apisixRoute := apisix.ApisixRoute(*apisixIngressRoute) routes, _, _, _ := apisixRoute.Convert() rc := &state.RouteCompare{OldRoutes: routes, NewRoutes: nil} @@ -235,4 +237,4 @@ func (c *ApisixRouteController) sync(rqo *RouteQueueObj) error{ default: return fmt.Errorf("not expected in (ApisixRouteController) sync") } -} \ No newline at end of file +} diff --git a/pkg/ingress/controller/apisix_service.go b/pkg/ingress/controller/apisix_service.go index d54146cfda..cdd1cc6a37 100644 --- a/pkg/ingress/controller/apisix_service.go +++ b/pkg/ingress/controller/apisix_service.go @@ -1,45 +1,45 @@ - // 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. +// 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 controller import ( + "fmt" + "github.com/api7/ingress-controller/pkg/ingress/apisix" + "github.com/golang/glog" + apisixV1 "github.com/gxthrj/apisix-ingress-types/pkg/apis/config/v1" clientSet "github.com/gxthrj/apisix-ingress-types/pkg/client/clientset/versioned" - informers "github.com/gxthrj/apisix-ingress-types/pkg/client/informers/externalversions/config/v1" apisixScheme "github.com/gxthrj/apisix-ingress-types/pkg/client/clientset/versioned/scheme" - apisixV1 "github.com/gxthrj/apisix-ingress-types/pkg/apis/config/v1" + informers "github.com/gxthrj/apisix-ingress-types/pkg/client/informers/externalversions/config/v1" "github.com/gxthrj/apisix-ingress-types/pkg/client/listers/config/v1" + "github.com/gxthrj/seven/state" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/util/runtime" + "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" - "k8s.io/apimachinery/pkg/util/runtime" - "k8s.io/client-go/kubernetes/scheme" - "fmt" - "github.com/golang/glog" - "k8s.io/apimachinery/pkg/util/wait" "time" - "k8s.io/apimachinery/pkg/api/errors" - "github.com/api7/ingress-controller/pkg/ingress/apisix" - "github.com/gxthrj/seven/state" ) type ApisixServiceController struct { - kubeclientset kubernetes.Interface - apisixClientset clientSet.Interface - apisixServiceList v1.ApisixServiceLister - apisixServiceSynced cache.InformerSynced - workqueue workqueue.RateLimitingInterface + kubeclientset kubernetes.Interface + apisixClientset clientSet.Interface + apisixServiceList v1.ApisixServiceLister + apisixServiceSynced cache.InformerSynced + workqueue workqueue.RateLimitingInterface } func BuildApisixServiceController( @@ -49,11 +49,11 @@ func BuildApisixServiceController( runtime.Must(apisixScheme.AddToScheme(scheme.Scheme)) controller := &ApisixServiceController{ - kubeclientset: kubeclientset, - apisixClientset: apisixServiceClientset, + kubeclientset: kubeclientset, + apisixClientset: apisixServiceClientset, apisixServiceList: apisixServiceInformer.Lister(), apisixServiceSynced: apisixServiceInformer.Informer().HasSynced, - workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ApisixServices"), + workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "ApisixServices"), } apisixServiceInformer.Informer().AddEventHandler( cache.ResourceEventHandlerFuncs{ @@ -111,14 +111,14 @@ func (c *ApisixServiceController) processNextWorkItem() bool { func (c *ApisixServiceController) syncHandler(key string) error { namespace, name, err := cache.SplitMetaNamespaceKey(key) if err != nil { - logger.Error("invalid resource key: %s", key) + logger.Errorf("invalid resource key: %s", key) return fmt.Errorf("invalid resource key: %s", key) } apisixServiceYaml, err := c.apisixServiceList.ApisixServices(namespace).Get(name) if err != nil { if errors.IsNotFound(err) { - logger.Info("apisixUpstream %s is removed", key) + logger.Infof("apisixUpstream %s is removed", key) return nil } runtime.HandleError(fmt.Errorf("failed to list apisixUpstream %s/%s", key, err.Error())) diff --git a/pkg/ingress/controller/apisix_upstream.go b/pkg/ingress/controller/apisix_upstream.go index c5b680c492..63deac01de 100644 --- a/pkg/ingress/controller/apisix_upstream.go +++ b/pkg/ingress/controller/apisix_upstream.go @@ -1,21 +1,23 @@ - // 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. +// 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 controller import ( "fmt" + "github.com/api7/ingress-controller/pkg/ingress/apisix" + "github.com/api7/ingress-controller/pkg/ingress/endpoint" "github.com/golang/glog" apisixV1 "github.com/gxthrj/apisix-ingress-types/pkg/apis/config/v1" clientSet "github.com/gxthrj/apisix-ingress-types/pkg/client/clientset/versioned" @@ -23,7 +25,6 @@ import ( informers "github.com/gxthrj/apisix-ingress-types/pkg/client/informers/externalversions/config/v1" "github.com/gxthrj/apisix-ingress-types/pkg/client/listers/config/v1" "github.com/gxthrj/seven/state" - "github.com/api7/ingress-controller/pkg/ingress/apisix" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/wait" @@ -32,7 +33,6 @@ import ( "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" "time" - "github.com/api7/ingress-controller/pkg/ingress/endpoint" ) type ApisixUpstreamController struct { @@ -112,14 +112,14 @@ func (c *ApisixUpstreamController) processNextWorkItem() bool { func (c *ApisixUpstreamController) syncHandler(key string) error { namespace, name, err := cache.SplitMetaNamespaceKey(key) if err != nil { - logger.Error("invalid resource key: %s", key) + logger.Errorf("invalid resource key: %s", key) return fmt.Errorf("invalid resource key: %s", key) } apisixUpstreamYaml, err := c.apisixUpstreamList.ApisixUpstreams(namespace).Get(name) if err != nil { if errors.IsNotFound(err) { - logger.Info("apisixUpstream %s is removed", key) + logger.Infof("apisixUpstream %s is removed", key) return nil } runtime.HandleError(fmt.Errorf("failed to list apisixUpstream %s/%s", key, err.Error())) diff --git a/pkg/ingress/controller/endpoint.go b/pkg/ingress/controller/endpoint.go index c4345c2fdd..ff96da29dc 100644 --- a/pkg/ingress/controller/endpoint.go +++ b/pkg/ingress/controller/endpoint.go @@ -1,22 +1,26 @@ - // 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. +// 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 controller import ( "fmt" + "github.com/api7/ingress-controller/conf" "github.com/golang/glog" + apisixType "github.com/gxthrj/apisix-types/pkg/apis/apisix/v1" + "github.com/gxthrj/seven/apisix" + sevenConf "github.com/gxthrj/seven/conf" "github.com/gxthrj/seven/state" CoreV1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -26,27 +30,23 @@ import ( CoreListerV1 "k8s.io/client-go/listers/core/v1" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" - "time" "strconv" - "github.com/gxthrj/seven/apisix" - sevenConf "github.com/gxthrj/seven/conf" - apisixType "github.com/gxthrj/apisix-types/pkg/apis/apisix/v1" - "github.com/api7/ingress-controller/conf" + "time" ) type EndpointController struct { - kubeclientset kubernetes.Interface - endpointList CoreListerV1.EndpointsLister + kubeclientset kubernetes.Interface + endpointList CoreListerV1.EndpointsLister endpointSynced cache.InformerSynced - workqueue workqueue.RateLimitingInterface + workqueue workqueue.RateLimitingInterface } func BuildEndpointController(kubeclientset kubernetes.Interface) *EndpointController { controller := &EndpointController{ - kubeclientset: kubeclientset, - endpointList: conf.EndpointsInformer.Lister(), + kubeclientset: kubeclientset, + endpointList: conf.EndpointsInformer.Lister(), endpointSynced: conf.EndpointsInformer.Informer().HasSynced, - workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "endpoints"), + workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "endpoints"), } conf.EndpointsInformer.Informer().AddEventHandler( cache.ResourceEventHandlerFuncs{ @@ -68,7 +68,8 @@ func (c *EndpointController) Run(stop <-chan struct{}) error { } func (c *EndpointController) runWorker() { - for c.processNextWorkItem() {} + for c.processNextWorkItem() { + } } func (c *EndpointController) processNextWorkItem() bool { @@ -107,14 +108,14 @@ func (c *EndpointController) syncHandler(key string) error { glog.V(2).Infof("find endpoint %s/%s", namespace, name) } if err != nil { - logger.Error("invalid resource key: %s", key) + logger.Errorf("invalid resource key: %s", key) return fmt.Errorf("invalid resource key: %s", key) } endpointYaml, err := c.endpointList.Endpoints(namespace).Get(name) if err != nil { if errors.IsNotFound(err) { - logger.Info("endpoint %s is removed", key) + logger.Infof("endpoint %s is removed", key) return nil } runtime.HandleError(fmt.Errorf("failed to list endpoint %s/%s", key, err.Error())) @@ -126,16 +127,16 @@ func (c *EndpointController) syncHandler(key string) error { } func (c *EndpointController) process(ep *CoreV1.Endpoints) { - if ep.Namespace != "kube-system"{ // todo here is some ignore namespaces - for _, s := range ep.Subsets{ + if ep.Namespace != "kube-system" { // todo here is some ignore namespaces + for _, s := range ep.Subsets { // if upstream need to watch // ips ips := make([]string, 0) - for _, address := range s.Addresses{ + for _, address := range s.Addresses { ips = append(ips, address.IP) } // ports - for _, port := range s.Ports{ + for _, port := range s.Ports { upstreamName := ep.Namespace + "_" + ep.Name + "_" + strconv.Itoa(int(port.Port)) // find upstreamName is in apisix // default diff --git a/pkg/ingress/endpoint/ep.go b/pkg/ingress/endpoint/ep.go index 6138885972..e3dac75d02 100644 --- a/pkg/ingress/endpoint/ep.go +++ b/pkg/ingress/endpoint/ep.go @@ -1,17 +1,17 @@ - // 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. +// 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 endpoint import ( @@ -20,20 +20,20 @@ import ( "github.com/gxthrj/apisix-types/pkg/apis/apisix/v1" ) -type Endpoint interface{ +type Endpoint interface { BuildEps(ns, name string, port int) []*v1.Node } -type EndpointRequest struct {} +type EndpointRequest struct{} func (epr *EndpointRequest) BuildEps(ns, name string, port int) []*v1.Node { nodes := make([]*v1.Node, 0) epInformers := conf.EndpointsInformer if ep, err := epInformers.Lister().Endpoints(ns).Get(name); err != nil { - glog.Errorf("find endpoint %s/%s err", ns, name, err.Error()) + glog.Errorf("find endpoint %s/%s err: %s", ns, name, err.Error()) } else { for _, s := range ep.Subsets { - for _, ip := range s.Addresses{ + for _, ip := range s.Addresses { p := ip.IP weight := 100 node := &v1.Node{IP: &p, Port: &port, Weight: &weight} @@ -45,14 +45,14 @@ func (epr *EndpointRequest) BuildEps(ns, name string, port int) []*v1.Node { } // BuildEps build nodes from endpoints for upstream -func BuildEps(ns, name string, port int) []*v1.Node{ +func BuildEps(ns, name string, port int) []*v1.Node { nodes := make([]*v1.Node, 0) epInformers := conf.EndpointsInformer if ep, err := epInformers.Lister().Endpoints(ns).Get(name); err != nil { - glog.Errorf("find endpoint %s/%s err", ns, name, err.Error()) + glog.Errorf("find endpoint %s/%s err: %s", ns, name, err.Error()) } else { for _, s := range ep.Subsets { - for _, ip := range s.Addresses{ + for _, ip := range s.Addresses { p := ip.IP weight := 100 node := &v1.Node{IP: &p, Port: &port, Weight: &weight}