Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: add test ci #80

Merged
merged 7 commits into from Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions .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 ./...
2 changes: 1 addition & 1 deletion conf/init.go
Expand Up @@ -81,7 +81,7 @@ var Syslog syslog

var config *restclient.Config

func init() {
func Init() {
// 获取当前环境
setEnvironment()
// 获取配置文件路径
Expand Down
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -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())
Expand Down
46 changes: 25 additions & 21 deletions 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 (
Expand Down Expand Up @@ -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
}
Expand All @@ -94,6 +97,7 @@ kind: ApisixUpstream
apiVersion: apisix.apache.org/v1
metadata:
name: httpserver
namespace: cloud
gxthrj marked this conversation as resolved.
Show resolved Hide resolved
spec:
ports:
- loadbalancer:
Expand Down
112 changes: 57 additions & 55 deletions 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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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()))
Expand All @@ -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()

Expand All @@ -227,12 +229,12 @@ 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}
return rc.Sync()
default:
return fmt.Errorf("not expected in (ApisixRouteController) sync")
}
}
}