Skip to content

Commit

Permalink
assistant for kubeedge and raven
Browse files Browse the repository at this point in the history
  • Loading branch information
TianTianBigWang committed Oct 28, 2022
1 parent 154f47c commit 6fa152b
Show file tree
Hide file tree
Showing 60 changed files with 6,114 additions and 1,131 deletions.
64 changes: 21 additions & 43 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ package app
import (
"fmt"

"github.com/99nil/diplomat/core/assistant"
"github.com/99nil/diplomat/pkg/common"

mgtAgent "github.com/99nil/diplomat/core/mgt/agent"
mgtServer "github.com/99nil/diplomat/core/mgt/server"
"github.com/99nil/diplomat/pkg/k8s"
Expand All @@ -32,57 +33,34 @@ import (
)

func NewAssistant() *cobra.Command {
// TODO 二进制安装待测试
use := "assistant"
opt := NewOption(use)
opt := common.NewGlobalOption(use)
cmd := &cobra.Command{
Use: opt.Module,
Short: "Assistant",
SilenceUsage: true,
RunE: func(_ *cobra.Command, _ []string) error {
cfg := assistant.Environ(opt.EnvPrefix)
if err := server.ParseConfigWithEnv(opt.Config, cfg, opt.EnvPrefix); err != nil {
return fmt.Errorf("parse config failed: %v", err)
}
cfg.Complete()
if err := cfg.Validate(); err != nil {
return fmt.Errorf("validate config failed: %v", err)
}
loggerIns := logr.NewLogrusInstance(&cfg.Logger)
logr.SetDefault(loggerIns)

restConfig, err := k8s.NewRestConfig(cfg.Kubernetes)
if err != nil {
return fmt.Errorf("init kubernetes rest config failed: %v", err)
}
restConfig.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(1000, 1000)
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("init kubernetes client failed: %v", err)
}
dynamicClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("init dynamic client failed: %v", err)
}

logr.Infof("logger level: %v", logr.Level())
logr.Debugf("%#v", cfg)

//dockerClient, err := client.NewClientWithOpts(client.FromEnv)
//if err != nil {
// return err
//}

return assistant.Run(cfg, nil, kubeClient, dynamicClient)
},
Use: opt.Module,
Short: "Assistant",
}
cmd.AddCommand(
NewAssistantInitCommand(opt),
NewAssistantJoinCommand(opt),
// TODO 优化,合并reset
NewAssistantCloudResetCommand(opt),
NewAssistantEdgeResetCommand(opt),
NewAssistantGetTokenCommand(opt),
NewVersionCommand(opt),
)

opt.CompleteFlags(cmd)
cmd.PersistentFlags().StringVar(&opt.KubeConfig, "kubeconfig", "/root/.kube/config",
"Use this key to set the Kubernetes config path")
cmd.PersistentFlags().BoolVarP(&opt.Verbose, "verbose", "v", true,
"verbose output")
return cmd
}

func NewMgtServer() *cobra.Command {
use := "mgt-server"
opt := NewOption(use)
opt := common.NewGlobalOption(use)
cmd := &cobra.Command{
Use: opt.Module,
Short: "Management Server",
Expand Down Expand Up @@ -126,7 +104,7 @@ func NewMgtServer() *cobra.Command {

func NewMgtAgent() *cobra.Command {
use := "mgt-agent"
opt := NewOption(use)
opt := common.NewGlobalOption(use)
cmd := &cobra.Command{
Use: opt.Module,
Short: "Management Agent",
Expand Down
58 changes: 58 additions & 0 deletions cmd/app/gettoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright © 2022 99nil.
//
// 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 app

import (
"context"
"fmt"

"github.com/99nil/diplomat/global/constants"
"github.com/99nil/diplomat/pkg/common"

"github.com/spf13/cobra"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/flowcontrol"
)

// NewAssistantGetTokenCommand Get token to join edge node to cloud
func NewAssistantGetTokenCommand(globalOpt *common.GlobalOption) *cobra.Command {
cmd := &cobra.Command{
Use: "gettoken",
Short: "Get token to join edge node to cloud",
RunE: func(cmd *cobra.Command, args []string) error {
// init client
restConfig, err := clientcmd.BuildConfigFromFlags("", globalOpt.KubeConfig)
if err != nil {
return err
}
restConfig.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(1000, 1000)
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("init kubernetes client failed: %v", err)
}

secret, err := kubeClient.CoreV1().Secrets(constants.SystemNamespace).
Get(context.Background(), constants.TokenSecretName, metaV1.GetOptions{})
if err != nil {
return fmt.Errorf("failed to get token, err: %v", err)
}
fmt.Println(string(secret.Data[constants.TokenDataName]))
return nil
},
}
return cmd
}
132 changes: 132 additions & 0 deletions cmd/app/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright © 2022 99nil.
//
// 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 app

import (
"context"
"fmt"
"strings"

"github.com/99nil/diplomat/core/assistant"
"github.com/99nil/diplomat/pkg/common"
"github.com/99nil/diplomat/pkg/k8s"
"github.com/99nil/diplomat/pkg/logr"
"github.com/99nil/diplomat/static"
"github.com/99nil/gopkg/server"

"github.com/spf13/cobra"
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/flowcontrol"
)

func NewAssistantInitCommand(globalOpt *common.GlobalOption) *cobra.Command {
opt := &common.InitOption{}
cmd := &cobra.Command{
Use: "init",
Short: "Initial installation",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// parse config
cfg := assistant.Environ(globalOpt.EnvPrefix)
if err := server.ParseConfigWithEnv(globalOpt.Config, cfg, globalOpt.EnvPrefix); err != nil {
return fmt.Errorf("parse config failed: %v", err)
}
cfg.Complete()
if err := cfg.Validate(); err != nil {
return fmt.Errorf("validate config failed: %v", err)
}
loggerIns := logr.NewLogrusInstance(&cfg.Logger)
logr.SetDefault(loggerIns)

// init client
restConfig, err := clientcmd.BuildConfigFromFlags("", globalOpt.KubeConfig)
if err != nil {
return err
}
restConfig.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(1000, 1000)
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("init kubernetes client failed: %v", err)
}
dynamicClient, err := dynamic.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("init dynamic client failed: %v", err)
}
//dockerClient, err := client.NewClientWithOpts(client.FromEnv)
//if err != nil {
// return err
//}

kubeEdgeResources, err := k8s.ParseAllYamlToObject(static.KubeEdgeYaml)
if err != nil {
return err
}
ravenResources, err := k8s.ParseAllYamlToObject(static.RavenYaml)
if err != nil {
return err
}

cfg.KubeEdgeResources = kubeEdgeResources
cfg.RavenResources = ravenResources

advAddrs := strings.Split(opt.AdvertiseAddress, ",")
advAddr := advAddrs[0]
if len(advAddr) > 0 {
opt.CurrentIP = advAddr
} else {
// 获取IP地址
hostInter, err := utilnet.ChooseHostInterface()
if err != nil {
return fmt.Errorf("get host interface failed: %v", err)
}
opt.CurrentIP = hostInter.String()
if opt.AdvertiseAddress == "" {
opt.AdvertiseAddress = opt.CurrentIP
}
}

logr.Infof("logger level: %v", logr.Level())
logr.Debugf("%#v", cfg)

inst := assistant.NewInitInstance(globalOpt, opt, cfg, kubeClient, dynamicClient)
return inst.Run(context.Background())
},
}
initAssistantFlags(cmd, globalOpt, opt)
return cmd
}

func initAssistantFlags(cmd *cobra.Command, globalOpt *common.GlobalOption, opt *common.InitOption) {
cmd.Flags().StringVar(&opt.Type, "type", common.TypeContainer, "Install KubeEdge Type: binary、container")
cmd.Flags().StringVar(&opt.Env, "env", common.EnvDev, "Install Env: dev、prod")
cmd.Flags().StringVar(&opt.Domain, "domain", "", "set domain names,eg: www.99nil.com,www.baidu.com")

cmd.Flags().StringVar(&opt.AdvertiseAddress, "advertise-address", "",
"set IPs in cloudcore's certificate SubAltNames field,eg: 10.10.102.78,10.10.102.79")

cmd.Flags().StringArrayVar(&opt.Disable, "disable", nil,
"Components that need to be disabled, if there are multiple components separated by commas. eg: CloudStream")

cmd.Flags().StringVar(&opt.K8sCertPath, "k8s-cert-path", "/etc/kubernetes/pki",
"Use this key to set the Kubernetes certificate path, eg: /etc/kubernetes/pki; If not provide, will not generate certificate")

cmd.Flags().StringVar(&opt.ImageRepository, "image-repository", "",
"Choose a container registry to pull control plane images from (default \"docker.io/99nil\")")
cmd.Flags().StringVar(&opt.ImageRepositoryUsername, "image-repository-username", "", "Choose a container registry username")
cmd.Flags().StringVar(&opt.ImageRepositoryPassword, "image-repository-password", "", "Choose a container registry password")
}
Loading

0 comments on commit 6fa152b

Please sign in to comment.