Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install assistant for raven and kubeedge #17

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 21 additions & 30 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,59 +17,50 @@ 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"
"github.com/99nil/diplomat/pkg/logr"
"github.com/99nil/gopkg/ctr"
"github.com/99nil/gopkg/server"

"github.com/docker/docker/client"
"github.com/spf13/cobra"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/flowcontrol"
)

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)

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, dockerClient)
},
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 @@ -113,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")
}
113 changes: 113 additions & 0 deletions cmd/app/join.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// 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/core/assistant"

"github.com/99nil/diplomat/pkg/common"
"github.com/spf13/cobra"
)

const (
edgeJoinLongDescription = `
"assistant join" command bootstraps Edge's worker node (at the edge) component.
It will also connect with cloud component to receive
further instructions and forward telemetry data from
devices to cloud
`
edgeJoinExample = `
assistant join --cloudcore-ipport=<ip:port address> --edgenode-name=<unique string as edge identifier>

- For this command --cloudcore-ipport flag is a required option
- This command will download and install the default version of pre-requisites and Edge

assistant join --cloudcore-ipport=10.20.30.40:10000 --edgenode-name=testing123
`
)

func NewAssistantJoinCommand(globalOpt *common.GlobalOption) *cobra.Command {
opt := newOption()
cmd := &cobra.Command{
Use: "join",
Short: "Bootstraps edge component. Checks and install (if required) the pre-requisites. Execute it on any edge node machine you wish to join",
Long: edgeJoinLongDescription,
Example: edgeJoinExample,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ins := assistant.NewJoinInstance(globalOpt, opt)
return ins.Run(context.Background())
},
}
addJoinOtherFlags(cmd, globalOpt, opt)
return cmd
}

func addJoinOtherFlags(cmd *cobra.Command, globalOpt *common.GlobalOption, opt *common.JoinOption) {
cmd.Flags().StringVarP(&opt.CloudCoreIPPort, constants.CloudCoreIPPort, "e", opt.CloudCoreIPPort,
"IP:Port address of Edge CloudCore")

if err := cmd.MarkFlagRequired(constants.CloudCoreIPPort); err != nil {
fmt.Printf("mark flag required failed with error: %v\n", err)
}

cmd.Flags().StringVarP(&opt.EdgeNodeName, constants.EdgeNodeName, "i", opt.EdgeNodeName,
"Edge Node unique identification string, If flag not used then the command will generate a unique id on its own")

cmd.Flags().StringVar(&opt.Namespace, "namespace", opt.Namespace,
"Set to the edge node of the specified namespace")

cmd.Flags().StringSliceVar(&opt.Labels, constants.Labels, opt.Labels,
"Set to the edge node of the specified labels")

cmd.Flags().StringVar(&opt.CGroupDriver, constants.CGroupDriver, opt.CGroupDriver,
"CGroupDriver that uses to manipulate cgroups on the host (cgroupfs or systemd), the default value is cgroupfs")

cmd.Flags().StringVar(&opt.CertPath, constants.CertPath, opt.CertPath,
fmt.Sprintf("The certPath used by edgecore, the default value is %s", constants.DefaultCertPath))

cmd.Flags().StringVarP(&opt.RuntimeType, constants.RuntimeType, "r", opt.RuntimeType,
"Container runtime type")

cmd.Flags().StringVarP(&opt.RemoteRuntimeEndpoint, constants.RemoteRuntimeEndpoint, "p", opt.RemoteRuntimeEndpoint,
"KubeEdge Edge Node RemoteRuntimeEndpoint string, If flag not set, it will use unix:///var/run/dockershim.sock")

cmd.Flags().StringVarP(&opt.Token, constants.Token, "t", opt.Token,
"Used for edge to apply for the certificate")

cmd.Flags().StringVarP(&opt.CertPort, constants.CertPort, "s", opt.CertPort,
"The port where to apply for the edge certificate")

cmd.Flags().BoolVar(&opt.WithMQTT, "with-mqtt", opt.WithMQTT,
`Use this key to set whether to install and start MQTT Broker by default`)

cmd.Flags().StringVar(&opt.ImageRepository, constants.ImageRepository, opt.ImageRepository,
`Use this key to decide which image repository to pull images from.`,
)
}

func newOption() *common.JoinOption {
joinOptions := &common.JoinOption{}
joinOptions.WithMQTT = true
joinOptions.CGroupDriver = constants.CGroupDriverCGroupFS
joinOptions.CertPath = constants.DefaultCertPath
joinOptions.RuntimeType = constants.DockerContainerRuntime
return joinOptions
}
Loading