Skip to content

Commit

Permalink
feat: Add single cluster network connectivity visual analysis
Browse files Browse the repository at this point in the history
Signed-off-by: ONE7live <[email protected]>
  • Loading branch information
ONE7live committed Nov 1, 2023
1 parent 106a93d commit 08fa4ce
Show file tree
Hide file tree
Showing 18 changed files with 665 additions and 179 deletions.
6 changes: 0 additions & 6 deletions cluster/images/buildx.floater.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@ RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl

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

RUN adduser -D -g clusterlink -u 1002 clusterlink && \
chown -R clusterlink:clusterlink /bin/${BINARY} && \
chmod u+s /bin/ping

USER clusterlink
6 changes: 0 additions & 6 deletions cluster/images/floater.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,3 @@ RUN apk update && apk upgrade
RUN apk add ip6tables iptables curl

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

RUN adduser -D -g clusterlink -u 1002 clusterlink && \
chown -R clusterlink:clusterlink /bin/${BINARY} && \
chmod u+s /bin/ping

USER clusterlink
54 changes: 50 additions & 4 deletions cmd/clusterlink/floater/app/floater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/json"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/term"
"k8s.io/klog/v2"

"github.com/kosmos.io/kosmos/cmd/clusterlink/floater/app/options"
networkmanager "github.com/kosmos.io/kosmos/pkg/clusterlink/agent/network-manager"
"github.com/kosmos.io/kosmos/pkg/clusterlink/network"
"github.com/kosmos.io/kosmos/pkg/sharedcli"
"github.com/kosmos.io/kosmos/pkg/sharedcli/klogflag"
)
Expand All @@ -28,7 +33,7 @@ func NewFloaterCommand(ctx context.Context) *cobra.Command {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := run(ctx, opts); err != nil {
if err := Run(ctx, opts); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -63,8 +68,20 @@ func NewFloaterCommand(ctx context.Context) *cobra.Command {
return cmd
}

func run(_ context.Context, _ *options.Options) error {
func Run(_ context.Context, _ *options.Options) error {
enableAnalysis, err := strconv.ParseBool(os.Getenv("ENABLE_ANALYSIS"))
if err != nil {
klog.Errorf("env variable read error: %s", err)
}

if enableAnalysis {
if err = collectNetworkConfig(); err != nil {
return err
}
}

port := os.Getenv("PORT")
klog.Infof("PORT: ", port)
if len(port) == 0 {
port = "8889"
}
Expand All @@ -80,11 +97,40 @@ func run(_ context.Context, _ *options.Options) error {
ReadHeaderTimeout: 3 * time.Second,
}

err := server.ListenAndServe()
err = server.ListenAndServe()
if err != nil {
klog.Errorf("lanch server error: %s", err)
klog.Errorf("launch server error: %s", err)
panic(err)
}

return nil
}

func collectNetworkConfig() error {
var nodeConfigSpecByte []byte

net := network.NewNetWork(false)
nManager := networkmanager.NewNetworkManager(net)
klog.Infof("Starting collect network config, create network manager...")

nodeConfigSpec, err := nManager.LoadSystemConfig()
if err != nil {
klog.Errorf("nodeConfigSpec query error: %s", err)
}
klog.Infof("load system config into nodeConfigSpec succeeded, nodeConfigSpec: [", nodeConfigSpec, "]")

nodeConfigSpecByte, err = json.Marshal(nodeConfigSpec)
if err != nil {
klog.Errorf("nodeConfigSpec marshal error: %s", err)
}
klog.Infof("marshal nodeConfigSpec into bytes succeeded, nodeConfigSpecByte: [", nodeConfigSpecByte, "]")

filePath := filepath.Join(os.Getenv("HOME"), "nodeconfig.json")
err = os.WriteFile(filePath, nodeConfigSpecByte, os.ModePerm)
if err != nil {
klog.Errorf("nodeconfig.json write error: %s", err)
}
klog.Infof("nodeconfig.json has been written, filePath: [", filePath, "]")

return nil
}
8 changes: 4 additions & 4 deletions cmd/clusterlink/operator/app/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewOperatorCommand(ctx context.Context) *cobra.Command {
if errs := opts.Validate(); len(errs) != 0 {
return errs.ToAggregate()
}
if err := run(ctx, opts); err != nil {
if err := Run(ctx, opts); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -70,12 +70,12 @@ func NewOperatorCommand(ctx context.Context) *cobra.Command {
return cmd
}

func run(ctx context.Context, opts *options.Options) error {
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())
}
clientset, err := kubernetes.NewForConfig(restConfig)
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("error get kubeclient: %v", err)
}
Expand All @@ -88,7 +88,7 @@ func run(ctx context.Context, opts *options.Options) error {
}
} else {
// try get kubeconfig from configmap
cm, err := clientset.CoreV1().ConfigMaps(utils.DefaultNamespace).Get(context.Background(), opts.ExternalKubeConfigName, metav1.GetOptions{})
cm, err := clientSet.CoreV1().ConfigMaps(utils.DefaultNamespace).Get(context.Background(), opts.ExternalKubeConfigName, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to configmap %s: %v", opts.ExternalKubeConfigName, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/clusterlink/agent/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Reconciler struct {
}

func NetworkManager() *networkmanager.NetworkManager {
net := network.NewNetWork()
net := network.NewNetWork(true)
return networkmanager.NewNetworkManager(net)
}

Expand Down
8 changes: 6 additions & 2 deletions pkg/clusterlink/network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ type NetWork interface {
UpdateCidrConfig(cluster *clusterlinkv1alpha1.Cluster)
}

func NewNetWork() NetWork {
func NewNetWork(enableInitSys bool) NetWork {
dn := &DefaultNetWork{}
dn.InitSys()

if enableInitSys {
dn.InitSys()
}

return dn
}
Loading

0 comments on commit 08fa4ce

Please sign in to comment.