Skip to content

Commit

Permalink
Merge pull request #284 from alibaba/feature/builtindualstack
Browse files Browse the repository at this point in the history
built-in dualstack and integration tests
  • Loading branch information
hhyasdf authored Jun 30, 2022
2 parents da3e41b + 47ad918 commit e78b24b
Show file tree
Hide file tree
Showing 55 changed files with 4,007 additions and 2,277 deletions.
157 changes: 7 additions & 150 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@ import (
"flag"
"fmt"
"os"
"time"

"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"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"

multiclusterv1 "github.com/alibaba/hybridnet/pkg/apis/multicluster/v1"
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/networking"
"github.com/alibaba/hybridnet/pkg/feature"
"github.com/alibaba/hybridnet/pkg/managerruntime"
zapinit "github.com/alibaba/hybridnet/pkg/zap"
)

Expand Down Expand Up @@ -96,13 +92,6 @@ func main() {
os.Exit(1)
}

// pre-start hooks registration
var preStartHooks []func() error
preStartHooks = append(preStartHooks, func() error {
// TODO: this conversion will be removed in next major version
return networkingv1.CanonicalizeIPInstance(mgr.GetClient())
})

// indexers need to be injected be for informer is running
if err = networking.InitIndexers(mgr); err != nil {
entryLog.Error(err, "unable to init indexers")
Expand All @@ -122,150 +111,18 @@ func main() {
// wait for manager cache client ready
mgr.GetCache().WaitForCacheSync(globalContext)

// run pre-start hooks
if err = errors.AggregateGoroutines(preStartHooks...); err != nil {
entryLog.Error(err, "unable to run start hooks")
os.Exit(1)
}

// init IPAM manager and start
ipamManager, err := networking.NewIPAMManager(globalContext, mgr.GetClient())
if err != nil {
entryLog.Error(err, "unable to create IPAM manager")
os.Exit(1)
}

podIPCache, err := networking.NewPodIPCache(globalContext, mgr.GetClient(), ctrllog.Log.WithName("pod-ip-cache"))
if err != nil {
entryLog.Error(err, "unable to create Pod IP cache")
os.Exit(1)
}

ipamStore := networking.NewIPAMStore(mgr.GetClient())

// setup controllers
if err = (&networking.IPAMReconciler{
Client: mgr.GetClient(),
Refresh: ipamManager,
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[networking.ControllerIPAM]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", networking.ControllerIPAM)
os.Exit(1)
}

if err = (&networking.IPInstanceReconciler{
Client: mgr.GetClient(),
PodIPCache: podIPCache,
IPAMManager: ipamManager,
IPAMStore: ipamStore,
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[networking.ControllerIPInstance]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", networking.ControllerIPInstance)
os.Exit(1)
}

if err = (&networking.NodeReconciler{
Context: globalContext,
Client: mgr.GetClient(),
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[networking.ControllerNode]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", networking.ControllerNode)
os.Exit(1)
}

if err = (&networking.PodReconciler{
APIReader: mgr.GetAPIReader(),
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor(networking.ControllerPod + "Controller"),
PodIPCache: podIPCache,
IPAMStore: ipamStore,
IPAMManager: ipamManager,
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[networking.ControllerPod]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", networking.ControllerPod)
os.Exit(1)
}

if err = (&networking.NetworkStatusReconciler{
Context: globalContext,
Client: mgr.GetClient(),
IPAMManager: ipamManager,
Recorder: mgr.GetEventRecorderFor(networking.ControllerNetworkStatus + "Controller"),
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[networking.ControllerNetworkStatus]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", networking.ControllerNetworkStatus)
os.Exit(1)
}

if err = (&networking.SubnetStatusReconciler{
Client: mgr.GetClient(),
IPAMManager: ipamManager,
Recorder: mgr.GetEventRecorderFor(networking.ControllerSubnetStatus + "Controller"),
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[networking.ControllerSubnetStatus]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", networking.ControllerSubnetStatus)
os.Exit(1)
}

if err = (&networking.QuotaReconciler{
Client: mgr.GetClient(),
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[networking.ControllerQuota]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", networking.ControllerQuota)
if err = networking.RegisterToManager(globalContext, mgr, networking.RegisterOptions{
ConcurrencyMap: controllerConcurrency,
}); err != nil {
entryLog.Error(err, "unable to register networking controllers")
os.Exit(1)
}

if feature.MultiClusterEnabled() {
clusterCheckEvent := make(chan multicluster.ClusterCheckEvent, 5)

uuidMutex, err := multicluster.NewUUIDMutexFromClient(globalContext, mgr.GetClient())
if err != nil {
entryLog.Error(err, "unable to create cluster UUID mutex")
os.Exit(1)
}

daemonHub := managerruntime.NewDaemonHub(globalContext)

clusterStatusChecker, err := multicluster.InitClusterStatusChecker(globalContext, mgr)
if err != nil {
entryLog.Error(err, "unable to init cluster status checker")
os.Exit(1)
}

if err = (&multicluster.RemoteClusterUUIDReconciler{
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor(multicluster.ControllerRemoteClusterUUID + "Controller"),
UUIDMutex: uuidMutex,
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[multicluster.ControllerRemoteClusterUUID]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", multicluster.ControllerRemoteClusterUUID)
os.Exit(1)
}

if err = (&multicluster.RemoteClusterReconciler{
Context: globalContext,
Client: mgr.GetClient(),
Recorder: mgr.GetEventRecorderFor(multicluster.ControllerRemoteCluster + "Controller"),
UUIDMutex: uuidMutex,
DaemonHub: daemonHub,
LocalManager: mgr,
Event: clusterCheckEvent,
ControllerConcurrency: concurrency.ControllerConcurrency(controllerConcurrency[multicluster.ControllerRemoteCluster]),
}).SetupWithManager(mgr); err != nil {
entryLog.Error(err, "unable to inject controller", "controller", multicluster.ControllerRemoteCluster)
os.Exit(1)
}

if err = mgr.Add(&multicluster.RemoteClusterStatusChecker{
Client: mgr.GetClient(),
Logger: mgr.GetLogger().WithName("checker").WithName(multicluster.CheckerRemoteClusterStatus),
CheckPeriod: 30 * time.Second,
DaemonHub: daemonHub,
Checker: clusterStatusChecker,
Event: clusterCheckEvent,
Recorder: mgr.GetEventRecorderFor(multicluster.CheckerRemoteClusterStatus + "Checker"),
if err = multicluster.RegisterToManager(globalContext, mgr, multicluster.RegisterOptions{
ConcurrencyMap: controllerConcurrency,
}); err != nil {
entryLog.Error(err, "unable to inject checker", "checker", multicluster.CheckerRemoteClusterStatus)
entryLog.Error(err, "unable to register multi-cluster controllers")
os.Exit(1)
}
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/apis/networking/v1/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
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 v1

import "sort"

type IPInstancePointerSlice []*IPInstance

func (I IPInstancePointerSlice) Len() int {
return len(I)
}

func (I IPInstancePointerSlice) Less(i, j int) bool {
if I[i] == nil || I[j] == nil {
return false
}

// sorting IPInstance by order rule, ipv4 after ipv6
// if i is ipv4 and j is ipv6, less, return true
// if i and j are both ipv4 or ipv6, equal, return false
// if i is ipv6 and j is ipv4, greater, return false
return !IsIPv6IPInstance(I[i]) && IsIPv6IPInstance(I[j])
}

func (I IPInstancePointerSlice) Swap(i, j int) {
I[i], I[j] = I[j], I[i]
}

func SortIPInstancePointerSlice(in []*IPInstance) {
sort.Stable(IPInstancePointerSlice(in))
}
Loading

0 comments on commit e78b24b

Please sign in to comment.