forked from kosmos-io/kosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: wangqi <[email protected]> Signed-off-by: dongweiguo <[email protected]>
- Loading branch information
1 parent
ff38dce
commit 93c7d54
Showing
47 changed files
with
2,450 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
Oops, something went wrong.