Skip to content

Commit

Permalink
init command and deployment
Browse files Browse the repository at this point in the history
Co-authored-by: wangqi <[email protected]>
Signed-off-by: dongweiguo <[email protected]>
  • Loading branch information
wuyingjun-lucky and ONE7live committed Aug 11, 2023
1 parent ff38dce commit 93c7d54
Show file tree
Hide file tree
Showing 47 changed files with 2,450 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cluster/images/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM alpine:3.17.1

ARG BINARY

RUN apk add --no-cache ca-certificates
RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl tcpdump busybox-extras

COPY ${BINARY} /bin/${BINARY}
10 changes: 10 additions & 0 deletions cluster/images/buildx.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM alpine:3.17.1

ARG BINARY
ARG TARGETPLATFORM

RUN apk add --no-cache ca-certificates
RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl tcpdump busybox-extras

COPY ${TARGETPLATFORM}/${BINARY} /bin/${BINARY}
19 changes: 19 additions & 0 deletions cluster/images/buildx.floater.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM alpine:3.17.1

ARG BINARY
ARG TARGETPLATFORM

RUN apk add --no-cache ca-certificates
RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl

COPY ${TARGETPLATFORM}/certificate /bin/certificate/

COPY ${TARGETPLATFORM}/${BINARY} /bin/${BINARY}


RUN adduser -D -g clusterlink -u 1002 clusterlink && \
chown -R clusterlink:clusterlink /bin/certificate && \
chown -R clusterlink:clusterlink /bin/${BINARY}

USER clusterlink
18 changes: 18 additions & 0 deletions cluster/images/floater.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM alpine:3.17.1

ARG BINARY

RUN apk add --no-cache ca-certificates
RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl

COPY certificate /bin/certificate/

COPY ${BINARY} /bin/${BINARY}


RUN adduser -D -g clusterlink -u 1002 clusterlink && \
chown -R clusterlink:clusterlink /bin/certificate && \
chown -R clusterlink:clusterlink /bin/${BINARY}

USER clusterlink
146 changes: 146 additions & 0 deletions cmd/agent/app/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package app

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

"github.com/spf13/cobra"
"k8s.io/client-go/tools/clientcmd"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/term"
"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"
)

// NewAgentCommand creates a *cobra.Command object with default parameters
func NewAgentCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()

cmd := &cobra.Command{
Use: "clusterlink-agent",
Long: `Configure the network based on clusternodes and clusters`,
RunE: func(cmd *cobra.Command, args []string) error {
// validate options
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := run(ctx, opts); err != nil {
return err
}
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
if len(arg) > 0 {
return fmt.Errorf("%q does not take any arguments, got %q", cmd.CommandPath(), args)
}
}
return nil
},
}

fss := cliflag.NamedFlagSets{}

genericFlagSet := fss.FlagSet("generic")
opts.AddFlags(genericFlagSet)

logsFlagSet := fss.FlagSet("logs")
klogflag.Add(logsFlagSet)

cmd.Flags().AddFlagSet(genericFlagSet)
cmd.Flags().AddFlagSet(logsFlagSet)

cols, h, err := term.TerminalSize(cmd.OutOrStdout())
if err != nil {
klog.Warning(err, h)
}
sharedcli.SetUsageAndHelpFunc(cmd, fss, cols)

return cmd
}

func Debounce(waits time.Duration) func(func()) {
var timer *time.Timer
return func(f func()) {
if timer != nil {
timer.Reset(time.Second * waits)
} else {
timer = time.NewTimer(time.Second * waits)
}
go func() {
<-timer.C
f()
}()
}
}

func run(ctx context.Context, opts *options.Options) error {
restConfig, err := clientcmd.BuildConfigFromFlags("", opts.KubeConfig)
if err != nil {
return fmt.Errorf("error building kubeconfig: %s", err.Error())
}

if err = network.CreateGlobalNetIptablesChains(); err != nil {
return fmt.Errorf("failed to create clusterlink iptables chains: %s", err.Error())
}

mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme.NewSchema(),
MetricsBindAddress: "0",
HealthProbeBindAddress: "0",
})
if err != nil {
klog.Errorf("failed to build controller manager: %v", err)
return err
}

clusterlinkClientset, err := clusterlinkclientset.NewForConfig(restConfig)
if err != nil {
klog.Fatalf("Unable to create clusterlink clientset: %v", err)
return err
}

factory := clusterlinkinformer.NewSharedInformerFactory(clusterlinkClientset, 0)
nodeConfigLister := factory.Clusterlink().V1alpha1().NodeConfigs().Lister()

clusterNodeController := agent.Reconciler{
Scheme: mgr.GetScheme(),
NodeConfigLister: nodeConfigLister,
NodeName: os.Getenv("NODE_NAME"),
ClusterName: os.Getenv("CLUSTER_NAME"),
NetworkManager: agent.NetworkManager(),
DebounceFunc: Debounce(5),
}
if err = clusterNodeController.SetupWithManager(mgr); err != nil {
klog.Fatalf("Unable to create cluster node controller: %v", err)
return err
}

factory.Start(ctx.Done())
factory.WaitForCacheSync(ctx.Done())

// go wait.UntilWithContext(ctx, func(ctx context.Context) {
// if err := clusterNodeController.Cleanup(); err != nil {
// klog.Warningf("An error was encountered while cleaning: %v", err)
// }
// }, opts.CleanPeriod)
go clusterNodeController.StartTimer(ctx)

if err := mgr.Start(ctx); err != nil {
klog.Errorf("controller manager exits unexpectedly: %v", err)
return err
}

return nil
}
31 changes: 31 additions & 0 deletions cmd/agent/app/options/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package options

import (
"time"

"github.com/spf13/pflag"
)

type Options struct {
KubeConfig string

// CleanPeriod represents clusterlink-agent cleanup period
CleanPeriod time.Duration
}

// NewOptions builds a default agent options.
func NewOptions() *Options {
return &Options{
KubeConfig: "",
}
}

// AddFlags adds flags of agent to the specified FlagSet
func (o *Options) AddFlags(fs *pflag.FlagSet) {
if o == nil {
return
}

fs.StringVar(&o.KubeConfig, "kubeconfig", o.KubeConfig, "Path to control plane kubeconfig file.")
fs.DurationVar(&o.CleanPeriod, "clean-period", 30*time.Second, "Specifies how often the agent cleans up routes and network interface.")
}
10 changes: 10 additions & 0 deletions cmd/agent/app/options/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package options

import "k8s.io/apimachinery/pkg/util/validation/field"

// Validate checks Options and return a slice of found errs.
func (o *Options) Validate() field.ErrorList {
errs := field.ErrorList{}

return errs
}
17 changes: 17 additions & 0 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

import (
"os"

apiserver "k8s.io/apiserver/pkg/server"
"k8s.io/component-base/cli"

"cnp.io/clusterlink/cmd/agent/app"
)

func main() {
ctx := apiserver.SetupSignalContext()
cmd := app.NewAgentCommand(ctx)
code := cli.Run(cmd)
os.Exit(code)
}
72 changes: 72 additions & 0 deletions cmd/clusterlink-proxy/app/clusterlink-proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app

import (
"context"
"fmt"
"k8s.io/klog/v2"

"github.com/spf13/cobra"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/term"

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

// NewClusterLinkProxyCommand creates a *cobra.Command object with default parameters
func NewClusterLinkProxyCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions()

cmd := &cobra.Command{
Use: "proxy",
Long: `The proxy starts a apiserver for agent access the backend proxy`,
RunE: func(cmd *cobra.Command, args []string) error {
// validate options
/*
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
*/
if err := run(ctx, opts); err != nil {
return err
}
return nil
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {
if len(arg) > 0 {
return fmt.Errorf("%q does not take any arguments, got %q", cmd.CommandPath(), args)
}
}
return nil
},
}
namedFlagSets := opts.Flags()

fs := cmd.Flags()
for _, f := range namedFlagSets.FlagSets {
fs.AddFlagSet(f)
}

cols, _, err := term.TerminalSize(cmd.OutOrStdout())
if err != nil {
klog.Warning("term.TerminalSize err: %v", err)
} else {
cliflag.SetUsageAndHelpFunc(cmd, namedFlagSets, cols)
}

return cmd
}

func run(ctx context.Context, opts *options.Options) error {
config, err := opts.Config()
if err != nil {
return err
}

server, err := config.Complete().New()
if err != nil {
return err
}

return server.GenericAPIServer.PrepareRun().Run(ctx.Done())
}
Loading

0 comments on commit 93c7d54

Please sign in to comment.