Skip to content

Commit

Permalink
integrate envtest
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyasdf committed May 24, 2022
1 parent cc3ac8e commit caf0d26
Show file tree
Hide file tree
Showing 12 changed files with 705 additions and 83 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ jobs:
${{ runner.os }}-go-
- name: Test and update codecov
run: |
go test -race -coverprofile=coverage.txt -covermode=atomic ./...
make bin/envtest
export KUBEBUILDER_ASSETS=$(pwd)/bin/envtest && go test -race -coverprofile=coverage.txt -covermode=atomic ./...
- uses: codecov/[email protected]
with:
file: ./coverage.txt
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ jobs:
${{ runner.os }}-go-
- name: Test
run: |
make bin/envtest
export KUBEBUILDER_ASSETS=$(pwd)/bin/envtest
go test -v ./...
go test -race -v ./...
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# IDE and editor files
.idea
.vscode
bin
/bin
/kubebuilder

# Binaries for programs and plugins
*.exe
Expand Down
20 changes: 15 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CRD_YAML_DIR=charts/hybridnet/crds
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"

.PHONY: build-dev-images release
.PHONY: build-dev-images release code-gen generate crd-yamls test

build-dev-images:
@for arch in ${ARCHS} ; do \
Expand All @@ -25,21 +25,31 @@ release:
code-gen:
cd hack && chmod u+x ./update-codegen.sh && ./update-codegen.sh

crd-yamls: controller-gen ## Generate CustomResourceDefinition objects.
crd-yamls: bin/controller-gen ## Generate CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=hybridnet webhook paths="./pkg/apis/..." output:crd:artifacts:config=${CRD_YAML_DIR} && rm -rf ./config

generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: bin/controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./pkg/apis/..."

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
controller-gen: ## Download controller-gen locally if necessary.
bin/controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])

# use command of "./bin/kubebuilder create api --group multicluster --kind RemoteXXX --version v1 --namespaced=false" to generate crd types
KUBEBUILDER_BIN = $(shell pwd)/bin/kubebuilder
kubebuilder: ## Download kubebuilder binary locally if necessary.
bin/kubebuilder: ## Download kubebuilder binary locally if necessary.
$(call curl-get-tool,$(KUBEBUILDER_BIN),https://github.com/kubernetes-sigs/kubebuilder/releases/download/v3.1.0/kubebuilder_${GOOS}_${GOARCH})

ENVTEST_BINS = $(shell pwd)/bin/envtest
bin/envtest: ## download envtest binaries
curl -sSLo envtest-bins.tar.gz "https://go.kubebuilder.io/test-tools/1.20.2/$(GOOS)/$(GOARCH)"
if [ ! -d bin ]; then mkdir bin; fi
tar -zvxf envtest-bins.tar.gz && cp -r kubebuilder/bin $(ENVTEST_BINS)
rm -rf kubebuilder && rm envtest-bins.tar.gz

test: bin/envtest
export KUBEBUILDER_ASSETS=$(shell pwd)/bin/envtest && go test -v ./...

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
Expand Down
78 changes: 4 additions & 74 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"time"

"k8s.io/apimachinery/pkg/util/errors"

globalutils "github.com/alibaba/hybridnet/pkg/utils"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/errors"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -40,12 +33,11 @@ import (
networkingv1 "github.com/alibaba/hybridnet/pkg/apis/networking/v1"
"github.com/alibaba/hybridnet/pkg/controllers/concurrency"
"github.com/alibaba/hybridnet/pkg/controllers/multicluster"
"github.com/alibaba/hybridnet/pkg/controllers/multicluster/clusterchecker"
"github.com/alibaba/hybridnet/pkg/controllers/networking"
"github.com/alibaba/hybridnet/pkg/controllers/utils"
"github.com/alibaba/hybridnet/pkg/feature"
"github.com/alibaba/hybridnet/pkg/managerruntime"
zapinit "github.com/alibaba/hybridnet/pkg/zap"
"github.com/spf13/pflag"
)

var (
Expand Down Expand Up @@ -112,7 +104,7 @@ func main() {
})

// indexers need to be injected be for informer is running
if err = initIndexers(mgr); err != nil {
if err = networking.InitIndexers(mgr); err != nil {
entryLog.Error(err, "unable to init indexers")
os.Exit(1)
}
Expand Down Expand Up @@ -229,7 +221,7 @@ func main() {

daemonHub := managerruntime.NewDaemonHub(signalContext)

clusterStatusChecker, err := initClusterStatusChecker(mgr)
clusterStatusChecker, err := multicluster.InitClusterStatusChecker(mgr)
if err != nil {
entryLog.Error(err, "unable to init cluster status checker")
os.Exit(1)
Expand Down Expand Up @@ -274,65 +266,3 @@ func main() {

<-signalContext.Done()
}

func initClusterStatusChecker(mgr ctrl.Manager) (clusterchecker.Checker, error) {
clusterUUID, err := utils.GetClusterUUID(mgr.GetClient())
if err != nil {
return nil, fmt.Errorf("unable to get cluster UUID: %v", err)
}

checker := clusterchecker.NewChecker()

if err = checker.Register(clusterchecker.HealthzCheckName, &clusterchecker.Healthz{}); err != nil {
return nil, err
}
if err = checker.Register(clusterchecker.BidirectionCheckName, &clusterchecker.Bidirection{LocalUUID: clusterUUID}); err != nil {
return nil, err
}
if err = checker.Register(clusterchecker.OverlayNetIDCheckName, &clusterchecker.OverlayNetID{LocalClient: mgr.GetClient()}); err != nil {
return nil, err
}
if err = checker.Register(clusterchecker.SubnetCheckName, &clusterchecker.Subnet{LocalClient: mgr.GetClient()}); err != nil {
return nil, err
}
return checker, nil
}

func initIndexers(mgr ctrl.Manager) (err error) {
// init node indexer for networks
if err = mgr.GetFieldIndexer().IndexField(context.TODO(), &networkingv1.Network{},
networking.IndexerFieldNode, func(obj client.Object) []string {
network, ok := obj.(*networkingv1.Network)
if !ok {
return nil
}

switch networkingv1.GetNetworkType(network) {
case networkingv1.NetworkTypeUnderlay:
return globalutils.DeepCopyStringSlice(network.Status.NodeList)
case networkingv1.NetworkTypeOverlay:
return []string{networking.OverlayNodeName}
case networkingv1.NetworkTypeGlobalBGP:
return []string{networking.GlobalBGPNodeName}
default:
return nil
}
}); err != nil {
return err
}

// init network indexer for Subnets
return mgr.GetFieldIndexer().IndexField(context.TODO(), &networkingv1.Subnet{},
networking.IndexerFieldNetwork, func(obj client.Object) []string {
subnet, ok := obj.(*networkingv1.Subnet)
if !ok {
return nil
}

networkName := subnet.Spec.Network
if len(networkName) > 0 {
return []string{networkName}
}
return nil
})
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ require (
github.com/mdlayher/ndp v0.0.0-20200602162440-17ab9e3e5567
github.com/mdlayher/raw v0.0.0-20190606142536-fef19f00fc18
github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.17.0 // indirect
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
github.com/osrg/gobgp/v3 v3.0.0-rc4
github.com/parnurzeal/gorequest v0.2.16
github.com/prometheus/client_golang v1.11.0
Expand Down
49 changes: 49 additions & 0 deletions pkg/controllers/multicluster/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2021 The Hybridnet Authors.
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 multicluster

import (
"fmt"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/alibaba/hybridnet/pkg/controllers/multicluster/clusterchecker"
"github.com/alibaba/hybridnet/pkg/controllers/utils"
)

func InitClusterStatusChecker(mgr ctrl.Manager) (clusterchecker.Checker, error) {
clusterUUID, err := utils.GetClusterUUID(mgr.GetClient())
if err != nil {
return nil, fmt.Errorf("unable to get cluster UUID: %v", err)
}

checker := clusterchecker.NewChecker()

if err = checker.Register(clusterchecker.HealthzCheckName, &clusterchecker.Healthz{}); err != nil {
return nil, err
}
if err = checker.Register(clusterchecker.BidirectionCheckName, &clusterchecker.Bidirection{LocalUUID: clusterUUID}); err != nil {
return nil, err
}
if err = checker.Register(clusterchecker.OverlayNetIDCheckName, &clusterchecker.OverlayNetID{LocalClient: mgr.GetClient()}); err != nil {
return nil, err
}
if err = checker.Register(clusterchecker.SubnetCheckName, &clusterchecker.Subnet{LocalClient: mgr.GetClient()}); err != nil {
return nil, err
}
return checker, nil
}
Loading

0 comments on commit caf0d26

Please sign in to comment.