Skip to content

Commit

Permalink
Merge pull request #5 from hanweisen/main
Browse files Browse the repository at this point in the history
refactor controller-manager and fix some bugs
  • Loading branch information
wuyingjun-lucky authored Aug 11, 2023
2 parents 29de700 + 417f33a commit 22a2750
Show file tree
Hide file tree
Showing 139 changed files with 1,232 additions and 601 deletions.
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ linters:

linters-settings:
goimports:
local-prefixes: cnp.io/clusterlink
local-prefixes: github.com/kosmos.io/clusterlink
gocyclo:
# minimal cyclomatic complexity to report
min-complexity: 15
gci:
sections:
- Standard
- Default
- Prefix(cnp.io/clusterlink)
- Prefix(github.com/kosmos.io/clusterlink)

issues:
# The list of ids of default excludes to include or disable. By default it's empty.
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TARGETS := clusterlink-controller-manager \
clusterlink-elector \
clusterlink-floater \
clusterlink-network-manager \
clusterlink-proxy \

CTL_TARGETS := linkctl

Expand Down
23 changes: 12 additions & 11 deletions cmd/agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"

"cnp.io/clusterlink/cmd/agent/app/options"
"cnp.io/clusterlink/pkg/agent"
clusterlinkclientset "cnp.io/clusterlink/pkg/generated/clientset/versioned"
clusterlinkinformer "cnp.io/clusterlink/pkg/generated/informers/externalversions"
"cnp.io/clusterlink/pkg/network"
"cnp.io/clusterlink/pkg/scheme"
"cnp.io/clusterlink/pkg/sharedcli"
"cnp.io/clusterlink/pkg/sharedcli/klogflag"
"github.com/kosmos.io/clusterlink/cmd/agent/app/options"
"github.com/kosmos.io/clusterlink/pkg/agent"
clusterlinkclientset "github.com/kosmos.io/clusterlink/pkg/generated/clientset/versioned"
clusterlinkinformer "github.com/kosmos.io/clusterlink/pkg/generated/informers/externalversions"
"github.com/kosmos.io/clusterlink/pkg/network"
"github.com/kosmos.io/clusterlink/pkg/scheme"
"github.com/kosmos.io/clusterlink/pkg/sharedcli"
"github.com/kosmos.io/clusterlink/pkg/sharedcli/klogflag"
"github.com/kosmos.io/clusterlink/pkg/utils"
)

// NewAgentCommand creates a *cobra.Command object with default parameters
Expand Down Expand Up @@ -101,7 +102,7 @@ func run(ctx context.Context, opts *options.Options) error {
HealthProbeBindAddress: "0",
})
if err != nil {
klog.Errorf("failed to build controller manager: %v", err)
klog.Fatalf("failed to build controller manager123: %v", err)
return err
}

Expand All @@ -117,8 +118,8 @@ func run(ctx context.Context, opts *options.Options) error {
clusterNodeController := agent.Reconciler{
Scheme: mgr.GetScheme(),
NodeConfigLister: nodeConfigLister,
NodeName: os.Getenv("NODE_NAME"),
ClusterName: os.Getenv("CLUSTER_NAME"),
NodeName: os.Getenv(utils.EnvNodeName),
ClusterName: os.Getenv(utils.EnvClusterName),
NetworkManager: agent.NetworkManager(),
DebounceFunc: Debounce(5),
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
apiserver "k8s.io/apiserver/pkg/server"
"k8s.io/component-base/cli"

"cnp.io/clusterlink/cmd/agent/app"
"github.com/kosmos.io/clusterlink/cmd/agent/app"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterlink-proxy/app/clusterlink-proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/term"

"cnp.io/clusterlink/cmd/clusterlink-proxy/app/options"
"github.com/kosmos.io/clusterlink/cmd/clusterlink-proxy/app/options"
)

// NewClusterLinkProxyCommand creates a *cobra.Command object with default parameters
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterlink-proxy/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"

"cnp.io/clusterlink/pkg/proxy"
"github.com/kosmos.io/clusterlink/pkg/proxy"
)

// Options contains command line parameters for clusterlink-proxy
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterlink-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
apiserver "k8s.io/apiserver/pkg/server"
"k8s.io/component-base/cli"

"cnp.io/clusterlink/cmd/clusterlink-proxy/app"
"github.com/kosmos.io/clusterlink/cmd/clusterlink-proxy/app"
)

func main() {
Expand Down
88 changes: 41 additions & 47 deletions cmd/controller-manager/app/controller-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@ import (
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"

"cnp.io/clusterlink/cmd/controller-manager/app/options"
ctrlcontext "cnp.io/clusterlink/pkg/controllers/context"
"cnp.io/clusterlink/pkg/controllers/nodecidr"
"cnp.io/clusterlink/pkg/generated/clientset/versioned"
"cnp.io/clusterlink/pkg/scheme"
"cnp.io/clusterlink/pkg/sharedcli/klogflag"
"github.com/kosmos.io/clusterlink/cmd/controller-manager/app/options"
ctrlcontext "github.com/kosmos.io/clusterlink/pkg/controllers/context"
"github.com/kosmos.io/clusterlink/pkg/generated/clientset/versioned"
"github.com/kosmos.io/clusterlink/pkg/scheme"
"github.com/kosmos.io/clusterlink/pkg/sharedcli/klogflag"
)

var (
controllers = make(ctrlcontext.Initializers)
Controllers = make(ctrlcontext.Initializers)

// controllersDisabledByDefault is the set of controllers which is disabled by default
controllersDisabledByDefault = sets.New[string]()
// ControllersDisabledByDefault is the set of Controllers which is disabled by default
ControllersDisabledByDefault = sets.New[string]()

stopOnce sync.Once
)

func init() {
controllers["cluster"] = startClusterController
controllers["node"] = startNodeController
controllers["calicoIPPool"] = startCalicoPoolController
Controllers["cluster"] = startClusterController
Controllers["node"] = startNodeController
Controllers["calicoIPPool"] = startCalicoPoolController
Controllers["nodecidr"] = startNodeCIDRController
}

// NewControllerManagerCommand creates a *cobra.Command object with default parameters
Expand All @@ -59,7 +59,7 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
// and update the flag usage.
genericFlagSet.AddGoFlagSet(flag.CommandLine)
genericFlagSet.Lookup("kubeconfig").Usage = "Path to clusterlink control plane kubeconfig file."
opts.AddFlags(genericFlagSet, controllers.ControllerNames(), sets.List(controllersDisabledByDefault))
opts.AddFlags(genericFlagSet, Controllers.ControllerNames(), sets.List(ControllersDisabledByDefault))

logsFlagSet := fss.FlagSet("logs")
klogflag.Add(logsFlagSet)
Expand All @@ -83,7 +83,6 @@ func Run(ctx context.Context, opts *options.ControllerManagerOptions) error {
panic(err)
}

// ToDo 创建Manager时配置好自定义资源Schema、选举配置、存活检查和指标监控
controllerManager, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme.NewSchema(),
})
Expand All @@ -92,63 +91,58 @@ func Run(ctx context.Context, opts *options.ControllerManagerOptions) error {
return err
}

setupControllers(controllerManager, opts, ctx.Done())
//TODO 整理这块
controlPanelConfig, err := clientcmd.BuildConfigFromFlags("", opts.ControlPanelConfig)
if err != nil {
klog.Fatalf("build controlpanel config err: %v", err)
panic(err)
}

if err := controllerManager.Start(ctx); err != nil {
klog.Errorf("controller manager exits unexpectedly: %v", err)
clusterLinkClient, err := versioned.NewForConfig(controlPanelConfig)
if err != nil {
klog.Fatalf("Unable to create clusterlinkClient: %v", err)
panic(err)
}

controller := NewController(clusterLinkClient, controllerManager, opts)
err = controller.Start(ctx)
if err != nil {
return err
}

return nil
}

func setupControllers(mgr ctrl.Manager, opts *options.ControllerManagerOptions, stopChan <-chan struct{}) {
func setupControllers(mgr ctrl.Manager, opts *options.ControllerManagerOptions, ctx context.Context) []ctrlcontext.CleanFunc {
controlPanelConfig, err := clientcmd.BuildConfigFromFlags("", opts.ControlPanelConfig)
if err != nil {
klog.Fatalf("build controlpanel config err: %v", err)
panic(err)
}

clusterLinkClient, err := versioned.NewForConfig(controlPanelConfig)
if err != nil {
klog.Fatalf("Unable to create clusterlinkClient: %v", err)
panic(err)
}

controllerContext := ctrlcontext.Context{
Mgr: mgr,
Opts: ctrlcontext.Options{
Controllers: opts.Controllers,
ControlPanelConfig: controlPanelConfig,
ClusterName: opts.ClusterName,
RateLimiterOpts: opts.RateLimiterOpts,
},
StopChan: stopChan,
}

if err := controllers.StartControllers(controllerContext, controllersDisabledByDefault); err != nil {
klog.Fatalf("error starting controllers: %v", err)
panic(err)
Ctx: ctx,
ClusterLinkClient: clusterLinkClient,
}

config, err := clientcmd.BuildConfigFromFlags("", opts.ControlPanelConfig)
if err != nil {
klog.Fatalf("Unable to create controlPanelConfig: %v", err)
panic(err)
}
clusterLinkClient, err := versioned.NewForConfig(config)
cleanFuns, err := Controllers.StartControllers(controllerContext, ControllersDisabledByDefault)
if err != nil {
klog.Fatalf("Unable to create clusterLinkClient: %v", err)
panic(err)
}
nodeCIDRCtl := nodecidr.NewNodeCIDRController(mgr.GetConfig(), opts.ClusterName, clusterLinkClient, opts.RateLimiterOpts, stopChan)
if err := mgr.Add(nodeCIDRCtl); err != nil {
klog.Fatalf("Failed to setup node CIDR Controller: %v", err)
klog.Fatalf("error starting Controllers: %v", err)
panic(err)
}

// Ensure the InformerManager stops when the stop channel closes
go func() {
<-stopChan
StopInstance()
}()
}

func StopInstance() {
stopOnce.Do(func() {
close(make(chan struct{}))
})
return cleanFuns
}
Loading

0 comments on commit 22a2750

Please sign in to comment.