diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5a596825920..9f25b515eae 100755 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -21,8 +21,12 @@ jobs: steps: - name: Checkout uses: actions/checkout@v2 + - name: Setup Go + uses: actions/setup-go@v2 + with: + go-version: ${{ env.GO_VERSION }} - name: Run Check License - run: + run: | wget https://github.com/google/addlicense/releases/download/v1.0.0/addlicense_1.0.0_Linux_x86_64.tar.gz tar -zxvf addlicense_1.0.0_Linux_x86_64.tar.gz -C $(go env GOPATH)/bin chmod a+x $(go env GOPATH)/bin/addlicense diff --git a/.golangci.yml b/.golangci.yml index 959980ccf77..ed78a75446c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,8 +9,6 @@ run: # vendor$, third_party$, testdata$, examples$, Godeps$, builtin$ skip-dirs-use-default: true - modules-download-mode: vendor - linters: # please, do not use `enable-all`: it's deprecated and will be removed soon. # inverted configuration with `enable-all` and `disable` is not scalable during updates of golangci-lint @@ -39,11 +37,14 @@ linters: - ifshort - unconvert -issues: - exclude-rules: - - linters: - - golint - text: "AccessKeyId" +linters-settings: + errcheck: + check-type-assertions: false + ignore: fmt:.*,io/ioutil:^Read.* + exclude-functions: + - io/ioutil.ReadFile + - io.Copy(*bytes.Buffer) + - io.Copy(os.Stdout) # golangci.com configuration # https://github.com/golangci/golangci/wiki/Configuration diff --git a/CHANGELOG.md b/CHANGELOG.md index 0757274aeea..54c374fb795 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,19 +1,3 @@ - - # Table of Contents - [3.3.9-rc.6](#671) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 28c23f0cac9..bafde43ab2f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -70,7 +70,7 @@ To put forward a PR, we assume you have registered a GitHub ID. Then you could f 1. **FORK** sealos to your repository. To make this work, you just need to click the button Fork in right-left of [fanux/sealos](https://github.com/fanux/sealos) main page. Then you will end up with your repository in `https://github.com//sealos`, in which `your-username` is your GitHub username. -1. **CLONE** your own repository to develop locally. Use `git clone https://github.com//sealos.git` to clone repository to your local machine. Then you can create new branches to finish the change you wish to make. +1. **CLONE** your own repository to master locally. Use `git clone https://github.com//sealos.git` to clone repository to your local machine. Then you can create new branches to finish the change you wish to make. 1. **Set Remote** upstream to be `https://github.com/fanux/sealos.git` using the following two commands: @@ -120,11 +120,11 @@ To put forward a PR, we assume you have registered a GitHub ID. Then you could f git push // push to your forked repository after rebase done ``` -1. **File a pull request** to fanux/sealos:develop +1. **File a pull request** to fanux/sealos:master ### Branch Definition -Right now we assume every contribution via pull request is for [branch develop](https://github.com/fanux/sealos/tree/develop) in sealos. Before contributing, be aware of branch definition would help a lot. +Right now we assume every contribution via pull request is for [branch master](https://github.com/fanux/sealos/tree/master) in sealos. Before contributing, be aware of branch definition would help a lot. As a contributor, keep in mind again that every contribution via pull request is for branch master. While in project sealos, there are several other branches, we generally call them rc branches, release branches and backport branches. diff --git a/Makefile b/Makefile index 6844de7b1c4..ad691360e95 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,7 @@ filelicense: SHELL:=/bin/bash filelicense: ## add license for file in ${Dirs} ; do \ if [[ $$file != '_output' && $$file != 'docs' && $$file != 'vendor' && $$file != 'logger' && $$file != 'applications' ]]; then \ - $(ADDLICENSE_BIN) -y $(shell date +"%Y") -c "Alibaba Group Holding Ltd." -f hack/template/LICENSE ./$$file ; \ + $(ADDLICENSE_BIN) -y $(shell date +"%Y") -c "sealos." -f hack/template/LICENSE ./$$file ; \ fi \ done @@ -74,7 +74,7 @@ install-ossutil: ## check ossutil if not exist install ossutil tools ifeq (, $(shell which ossutil)) @{ \ set -e ;\ - go install github.com/aliyun/ossutil@latest ;\ + curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(GOBIN) v2.2.0 ;\ } OSSUTIL_BIN=$(GOBIN)/ossutil else diff --git a/cert/cert.go b/cert/cert.go index 96096a09895..41922c49891 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 cert import ( diff --git a/cert/cert_cmd.go b/cert/cert_cmd.go index 72a3654aee0..77ea84acf2e 100755 --- a/cert/cert_cmd.go +++ b/cert/cert_cmd.go @@ -1,10 +1,24 @@ +// Copyright © 2021 sealos. +// +// 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 cert import ( "fmt" "os" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) // CMD return sealos cert command @@ -42,5 +56,5 @@ func GenerateCert(certPATH, certEtcdPATH string, altNames []string, hostIP, host logger.Error("generator cert config failed %s", err) os.Exit(-1) } - certConfig.GenerateAll() + _ = certConfig.GenerateAll() } diff --git a/cert/kube_certs.go b/cert/kube_certs.go index 673e980cd3e..b757d2dc7c5 100644 --- a/cert/kube_certs.go +++ b/cert/kube_certs.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 cert import ( @@ -8,7 +22,7 @@ import ( "os" "path" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) var ( @@ -62,7 +76,7 @@ func CaList(CertPath, CertEtcdPath string) []Config { } } -func CertList(CertPath, CertEtcdPath string) []Config { +func List(CertPath, CertEtcdPath string) []Config { return []Config{ { Path: CertPath, @@ -198,8 +212,7 @@ func NewSealosCertMetaData(certPATH, certEtcdPATH string, apiServerIPAndDomains } data.APIServer.DNSNames[altName] = altName } - ip := net.ParseIP(nodeIP) - if ip != nil { + if ip := net.ParseIP(nodeIP); ip != nil { data.APIServer.IPs[ip.String()] = ip } @@ -213,8 +226,8 @@ func (meta *SealosCertMetaData) apiServerAltName(certList *[]Config) { (*certList)[APIserverCert].AltNames.DNSNames[dns] = dns } - svcDns := fmt.Sprintf("kubernetes.default.svc.%s", meta.DNSDomain) - (*certList)[APIserverCert].AltNames.DNSNames[svcDns] = svcDns + svcDNS := fmt.Sprintf("kubernetes.default.svc.%s", meta.DNSDomain) + (*certList)[APIserverCert].AltNames.DNSNames[svcDNS] = svcDNS (*certList)[APIserverCert].AltNames.DNSNames[meta.NodeName] = meta.NodeName for _, ip := range meta.APIServer.IPs { @@ -268,10 +281,10 @@ func (meta *SealosCertMetaData) generatorServiceAccountKeyPaire() error { func (meta *SealosCertMetaData) GenerateAll() error { cas := CaList(meta.CertPath, meta.CertEtcdPath) - certs := CertList(meta.CertPath, meta.CertEtcdPath) + certs := List(meta.CertPath, meta.CertEtcdPath) meta.apiServerAltName(&certs) meta.etcdAltAndCommonName(&certs) - meta.generatorServiceAccountKeyPaire() + _ = meta.generatorServiceAccountKeyPaire() CACerts := map[string]*x509.Certificate{} CAKeys := map[string]crypto.Signer{} diff --git a/cert/kube_certs_test.go b/cert/kube_certs_test.go index ce3c5e4d181..84a10ce1088 100644 --- a/cert/kube_certs_test.go +++ b/cert/kube_certs_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 cert import ( diff --git a/cert/kubeconfig.go b/cert/kubeconfig.go index 8a05692eee4..ecb58976bc7 100644 --- a/cert/kubeconfig.go +++ b/cert/kubeconfig.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 cert import ( @@ -5,7 +19,6 @@ import ( "crypto" "crypto/x509" "fmt" - "io" "os" "path/filepath" @@ -14,7 +27,7 @@ import ( clientcmdapi "k8s.io/client-go/tools/clientcmd/api" "k8s.io/client-go/util/keyutil" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) // clientCertAuth struct holds info required to build a client certificate to provide authentication info in a kubeconfig object @@ -64,9 +77,7 @@ func CreateKubeConfigFile(kubeConfigFileName string, outDir string, cfg Config, // createKubeConfigFiles creates all the requested kubeconfig files. // If kubeconfig files already exists, they are used only if evaluated equal; otherwise an error is returned. -func createKubeConfigFiles(outDir string, cfg Config, nodeName, controlPlaneEndpoint, clusterName string, kubeConfigFileNames ...string) error { - - // gets the KubeConfigSpecs, actualized for the current InitConfiguration +func createKubeConfigFiles(outDir string, cfg Config, nodeName, controlPlaneEndpoint, clusterName string, kubeConfigFileNames ...string) error { // gets the KubeConfigSpecs, actualized for the current InitConfiguration specs, err := getKubeConfigSpecs(cfg, nodeName, controlPlaneEndpoint) if err != nil { return err @@ -97,7 +108,6 @@ func createKubeConfigFiles(outDir string, cfg Config, nodeName, controlPlaneEndp // getKubeConfigSpecs returns all KubeConfigSpecs actualized to the context of the current InitConfiguration // NB. this methods holds the information about how kubeadm creates kubeconfig files. func getKubeConfigSpecs(cfg Config, nodeName, controlPlaneEndpoint string) (map[string]*kubeConfigSpec, error) { - caCert, caKey, err := LoadCaCertAndKeyFromDisk(cfg) if err != nil { return nil, errors.Wrap(err, "couldn't create a kubeconfig; the CA files couldn't be loaded") @@ -153,7 +163,6 @@ func getKubeConfigSpecs(cfg Config, nodeName, controlPlaneEndpoint string) (map[ // buildKubeConfigFromSpec creates a kubeconfig object for the given kubeConfigSpec func buildKubeConfigFromSpec(spec *kubeConfigSpec, clustername string) (*clientcmdapi.Config, error) { - // If this kubeconfig should use token if spec.TokenAuth != nil { // create a kubeconfig with a token @@ -268,107 +277,6 @@ func createKubeConfigFileIfNotExists(outDir, filename string, config *clientcmda return nil } -// WriteKubeConfigWithClientCert writes a kubeconfig file - with a client certificate as authentication info - to the given writer. -func WriteKubeConfigWithClientCert(out io.Writer, cfg Config, clientName, controlPlaneEndpoint, clusterName string, organizations []string) error { - - // creates the KubeConfigSpecs, actualized for the current InitConfiguration - caCert, caKey, err := LoadCaCertAndKeyFromDisk(cfg) - if err != nil { - return errors.Wrap(err, "couldn't create a kubeconfig; the CA files couldn't be loaded") - } - - if len(controlPlaneEndpoint) == 0 { - return errors.New("controlPlaneEndpoint can not be empty") - } - - spec := &kubeConfigSpec{ - ClientName: clientName, - APIServer: controlPlaneEndpoint, - CACert: caCert, - ClientCertAuth: &clientCertAuth{ - CAKey: caKey, - Organizations: organizations, - }, - } - - return writeKubeConfigFromSpec(out, spec, clusterName) -} - -// WriteKubeConfigWithToken writes a kubeconfig file - with a token as client authentication info - to the given writer. -func WriteKubeConfigWithToken(out io.Writer, cfg Config, clientName, controlPlaneEndpoint, clusterName, token string) error { - - // creates the KubeConfigSpecs, actualized for the current InitConfiguration - caCert, _, err := LoadCaCertAndKeyFromDisk(cfg) - if err != nil { - return errors.Wrap(err, "couldn't create a kubeconfig; the CA files couldn't be loaded") - } - - if len(controlPlaneEndpoint) == 0 { - return errors.New("controlPlaneEndpoint can not be empty") - } - - spec := &kubeConfigSpec{ - ClientName: clientName, - APIServer: controlPlaneEndpoint, - CACert: caCert, - TokenAuth: &tokenAuth{ - Token: token, - }, - } - - return writeKubeConfigFromSpec(out, spec, clusterName) -} - -// writeKubeConfigFromSpec creates a kubeconfig object from a kubeConfigSpec and writes it to the given writer. -func writeKubeConfigFromSpec(out io.Writer, spec *kubeConfigSpec, clustername string) error { - - // builds the KubeConfig object - config, err := buildKubeConfigFromSpec(spec, clustername) - if err != nil { - return err - } - - // writes the kubeconfig to disk if it not exists - configBytes, err := clientcmd.Write(*config) - if err != nil { - return errors.Wrap(err, "failure while serializing admin kubeconfig") - } - - fmt.Fprintln(out, string(configBytes)) - return nil -} - -// ValidateKubeconfigsForExternalCA check if the kubeconfig file exist and has the expected CA and server URL using kubeadmapi.InitConfiguration. -func ValidateKubeconfigsForExternalCA(outDir string, cfg Config, controlPlaneEndpoint string) error { - kubeConfigFileNames := []string{ - "admin.conf", - "kubelet.conf", - "controller-manager.conf", - "scheduler.conf", - } - - // Creates a kubeconfig file with the target CA and server URL - // to be used as a input for validating user provided kubeconfig files - caCert, _, err := LoadCaCertAndKeyFromDisk(cfg) - if err != nil { - return err - } - - if len(controlPlaneEndpoint) == 0 { - return errors.New("controlPlaneEndpoint can not be empty") - } - - validationConfig := CreateBasic(controlPlaneEndpoint, "dummy", "dummy", EncodeCertPEM(caCert)) - - // validate user provided kubeconfig files - for _, kubeConfigFileName := range kubeConfigFileNames { - if err = validateKubeConfig(outDir, kubeConfigFileName, validationConfig); err != nil { - return errors.Wrapf(err, "the %s file does not exists or it is not valid", kubeConfigFileName) - } - } - return nil -} - // cmd/kubeadm/app/util/kubeconfig/kubeconfig.go // CreateBasic creates a basic, general KubeConfig object that then can be extended func CreateBasic(serverURL, clusterName, userName string, caCert []byte) *clientcmdapi.Config { diff --git a/cmd/cert.go b/cmd/cert.go index edd28c9fda6..1aa22d5985a 100755 --- a/cmd/cert.go +++ b/cmd/cert.go @@ -1,4 +1,4 @@ -// Copyright © 2020 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/cmd/clean.go b/cmd/clean.go index 7d01b4c4e3b..ee2ee60201c 100644 --- a/cmd/clean.go +++ b/cmd/clean.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,8 +18,8 @@ import ( "os" "github.com/fanux/sealos/install" + "github.com/fanux/sealos/pkg/logger" "github.com/spf13/cobra" - "github.com/wonderivan/logger" ) var exampleCleanCmd = ` @@ -71,27 +71,9 @@ func CleanCmdFunc(cmd *cobra.Command, args []string) { deleteNodes := install.ParseIPs(install.NodeIPs) deleteMasters := install.ParseIPs(install.MasterIPs) c := &install.SealConfig{} - err := c.Load(cfgFile) - if err != nil { - // comment: if cfgFile is not exist; do not use sealos clean something. - // its danger for sealos do clean nodes without `~/.sealos/config.yaml` - //// 判断错误是否为配置文件不存在 - //if errors.Is(err, os.ErrNotExist) { - // _, err = fmt.Fprint(os.Stdout, "Please enter the password to connect to the node:\n") - // if err != nil { - // logger.Error("fmt.Fprint err", err) - // os.Exit(-1) - // } - // passwordTmp, err := terminal.ReadPassword(int(os.Stdin.Fd())) - // if err != nil { - // logger.Error("read password err", err) - // os.Exit(-1) - // } - // install.SSHConfig.Password = string(passwordTmp) - //} else { + if err := c.Load(cfgFile); err != nil { logger.Error(err) os.Exit(-1) - //} } // 使用 sealos clean --node 不小心写了 masterip. @@ -107,7 +89,6 @@ func CleanCmdFunc(cmd *cobra.Command, args []string) { install.BuildClean(deleteNodes, deleteMasters) c.Dump(cfgFile) - } // IsExistNodes diff --git a/cmd/cloud.go b/cmd/cloud.go index cf1c4f4f4e9..ea93ca22949 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,9 +26,6 @@ import ( var p bool -func prompt() { -} - // cloudCmd represents the cloud command var cloudCmd = &cobra.Command{ Use: "cloud", diff --git a/cmd/cni.go b/cmd/cni.go index 529e1bc1bb1..4ce3f9e2b50 100644 --- a/cmd/cni.go +++ b/cmd/cni.go @@ -1,26 +1,25 @@ -/* -Copyright © 2021 NAME HERE +// Copyright © 2021 sealos. +// +// 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. -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 cmd import ( "fmt" "github.com/fanux/sealos/net" + "github.com/fanux/sealos/pkg/logger" "github.com/spf13/cobra" - "github.com/wonderivan/logger" ) var cniType string @@ -39,7 +38,7 @@ to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { if cniType != net.CALICO && cniType != net.FLANNEL && cniType != net.CILIUM { logger.Error("unsupport cni: ", cniType) - return + return } if version == "" { logger.Error("cni version should not nil ", version) @@ -49,7 +48,7 @@ to quickly create a Cobra application.`, Interface: "interface=eth.*|en.*|em.*", IPIP: true, MTU: "1440", - Version: version, + Version: version, }).Manifests("") fmt.Println(yaml) }, @@ -58,8 +57,8 @@ to quickly create a Cobra application.`, func init() { rootCmd.AddCommand(cniCmd) - cniCmd.Flags().StringVarP(&cniType,"cni-type", "t", net.CALICO, "print cni yaml, cni tpye just like, calico.flannel.cilium") - cniCmd.Flags().StringVarP(&version,"version", "v", "", "calico version") + cniCmd.Flags().StringVarP(&cniType, "cni-type", "t", net.CALICO, "print cni yaml, cni tpye just like, calico.flannel.cilium") + cniCmd.Flags().StringVarP(&version, "version", "v", "", "calico version") // Here you will define your flags and configuration settings. diff --git a/cmd/completion.go b/cmd/completion.go index 7bb5822abc7..27bffd053af 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -1,18 +1,3 @@ -/* -Copyright © 2020 NAME HERE - -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 cmd import ( @@ -23,7 +8,7 @@ import ( "github.com/spf13/cobra" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) const defaultCopyRight = ` @@ -132,7 +117,7 @@ func runCompletionBash(out io.Writer, copyRight string, sealos *cobra.Command) e func runCompletionZsh(out io.Writer, copyRight string, sealos *cobra.Command) error { zshHead := "#compdef sealos\n" - out.Write([]byte(zshHead)) + _, _ = out.Write([]byte(zshHead)) if len(copyRight) == 0 { copyRight = defaultCopyRight @@ -229,17 +214,17 @@ __sealos_convert_bash_to_zsh() { -e "s/\\\$(type${RWORD}/\$(__sealos_type/g" \ <<'BASH_COMPLETION_EOF' ` - out.Write([]byte(zshInitialization)) + _, _ = out.Write([]byte(zshInitialization)) buf := new(bytes.Buffer) - sealos.GenBashCompletion(buf) - out.Write(buf.Bytes()) + _ = sealos.GenBashCompletion(buf) + _, _ = out.Write(buf.Bytes()) zshTail := ` BASH_COMPLETION_EOF } __sealos_bash_source <(__sealos_convert_bash_to_zsh) ` - out.Write([]byte(zshTail)) + _, _ = out.Write([]byte(zshTail)) return nil } diff --git a/cmd/config.go b/cmd/config.go index 0ec108a9b71..94333939d58 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/cmd/delete.go b/cmd/delete.go index a5796642f68..6d25d9461af 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -1,18 +1,17 @@ -/* -Copyright © 2020 NAME HERE +// Copyright © 2021 sealos. +// +// 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. -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 cmd import ( @@ -46,11 +45,11 @@ var deleteCmd = &cobra.Command{ Example: deleteExamlpe, Run: func(cmd *cobra.Command, args []string) { cfg := appmanager.GetDeleteFlags(AppURL) - appmanager.DeleteApp(cfg, cfgFile) + _ = appmanager.DeleteApp(cfg, cfgFile) }, PreRun: func(cmd *cobra.Command, args []string) { if install.ExitDeleteCase(AppURL) { - cmd.Help() + _ = cmd.Help() os.Exit(install.ErrorExitOSCase) } }, diff --git a/cmd/etcd.go b/cmd/etcd.go index 063e66c6ca8..bc629edde0b 100644 --- a/cmd/etcd.go +++ b/cmd/etcd.go @@ -1,18 +1,17 @@ -/* -Copyright © 2020 NAME HERE +// Copyright © 2021 sealos. +// +// 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. -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 cmd import ( @@ -23,7 +22,7 @@ import ( "github.com/spf13/cobra" "github.com/fanux/sealos/install" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) var exampleCmd = ` @@ -86,7 +85,7 @@ func NewEtcdSaveCommand() *cobra.Command { cmd.Flags().StringVar(&install.SnapshotName, "name", install.ETCDSNAPSHOTDEFAULTNAME, "Specify snapshot name") cmd.Flags().StringVar(&install.EtcdBackDir, "backupPath", install.ETCDDEFAULTBACKUPDIR, "Specify snapshot backup dir") cmd.Flags().StringVar(&install.BucketName, "bucket", "", "oss bucketName to save snapshot") - cmd.Flags().StringVar(&install.AccessKeyId, "aliId", "", "aliyun accessKeyId to save snapshot") + cmd.Flags().StringVar(&install.AccessKeyID, "aliId", "", "aliyun accessKeyId to save snapshot") cmd.Flags().StringVar(&install.AccessKeySecrets, "aliKey", "", "aliyun accessKeySecrets to save snapshot") cmd.Flags().StringVar(&install.OssEndpoint, "ep", "", "aliyun endpoints to save snapshot") cmd.Flags().StringVar(&install.ObjectPath, "objectPath", "", "aliyun oss objectPath to save snapshot, like: /sealos/snapshots/") @@ -118,7 +117,7 @@ func NewEtcdHealthCommand() *cobra.Command { func EtcdSaveCmdFunc(cmd *cobra.Command, args []string) { e := install.GetEtcdBackFlags(cfgFile) err := e.Save(install.InDocker) - if err == nil && e.AccessKeyId != "" { + if err == nil && e.AccessKeyID != "" { e.Dump(cfgFile) } logger.Info("Finished saving/uploading snapshot [%s]", e.Name) @@ -144,8 +143,7 @@ func EtcdRestoreCmdFunc(cmd *cobra.Command, args []string) { time.Sleep(time.Second * 10) logger.Info("send restore file to etcd master node and start etcd") // send restore file to etcd master node to start etcd - err := e.AfterRestore() - if err != nil { + if err := e.AfterRestore(); err != nil { logger.Error(err) logger.Info("Start RecoveryKuBeCluster") e.RecoveryKuBeCluster(tmpdir) diff --git a/cmd/exec.go b/cmd/exec.go index 03fcae37a4c..54a3d968431 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -1,18 +1,17 @@ -/* -Copyright © 2020 NAME HERE +// Copyright © 2021 sealos. +// +// 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. -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 cmd import ( @@ -47,7 +46,6 @@ func init() { execCmd.Flags().StringVar(&install.ExecCommand, "cmd", "", "exec command string") execCmd.Flags().StringVar(&install.Label, "label", "", "kubernetes labels like node-role.kubernetes.io/master=") execCmd.Flags().StringSliceVar(&install.ExecNode, "node", []string{}, "node ip or hostname in kubernetes") - } func ExecCmdFunc(cmd *cobra.Command, args []string) { diff --git a/cmd/init.go b/cmd/init.go index 5609c97466d..a6e91fe1d92 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ import ( "github.com/fanux/sealos/cert" "github.com/fanux/sealos/install" "github.com/fanux/sealos/net" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) var contact = ` @@ -98,7 +98,7 @@ var initCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { // 使用了cfgFile 就不进行preRun了 if cfgFile == "" && install.ExitInitCase() { - cmd.Help() + _ = cmd.Help() os.Exit(install.ErrorExitOSCase) } }, @@ -116,13 +116,13 @@ func init() { initCmd.Flags().StringVar(&install.KubeadmFile, "kubeadm-config", "", "kubeadm-config.yaml template file") - initCmd.Flags().StringVar(&install.ApiServer, "apiserver", "apiserver.cluster.local", "apiserver domain name") + initCmd.Flags().StringVar(&install.APIServer, "apiserver", "apiserver.cluster.local", "apiserver domain name") initCmd.Flags().StringVar(&install.VIP, "vip", "10.103.97.2", "virtual ip") initCmd.Flags().StringSliceVar(&install.MasterIPs, "master", []string{}, "kubernetes multi-masters ex. 192.168.0.2-192.168.0.4") initCmd.Flags().StringSliceVar(&install.NodeIPs, "node", []string{}, "kubernetes multi-nodes ex. 192.168.0.5-192.168.0.5") initCmd.Flags().StringSliceVar(&install.CertSANS, "cert-sans", []string{}, "kubernetes apiServerCertSANs ex. 47.0.0.22 sealyun.com ") - initCmd.Flags().StringVar(&install.PkgUrl, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline package url, or file location ex. /root/kube1.14.1.tar.gz") + initCmd.Flags().StringVar(&install.PkgURL, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline package url, or file location ex. /root/kube1.14.1.tar.gz") initCmd.Flags().StringVar(&install.Version, "version", "", "version is kubernetes version") initCmd.Flags().StringVar(&install.Repo, "repo", "k8s.gcr.io", "choose a container registry to pull control plane images from") initCmd.Flags().StringVar(&install.PodCIDR, "podcidr", "100.64.0.0/10", "Specify range of IP addresses for the pod network") diff --git a/cmd/install.go b/cmd/install.go index b9d626c11a6..eced7d6d0cf 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -48,17 +48,16 @@ var installCmd = &cobra.Command{ Example: installExample, Run: func(cmd *cobra.Command, args []string) { cfg := appmanager.GetInstallFlags(AppURL) - appmanager.InstallApp(cfg, cfgFile) + _ = appmanager.InstallApp(cfg, cfgFile) }, PreRun: func(cmd *cobra.Command, args []string) { if install.ExitInstallCase(AppURL) { - cmd.Help() + _ = cmd.Help() os.Exit(install.ErrorExitOSCase) } }, } -var name string func init() { rootCmd.AddCommand(installCmd) diff --git a/cmd/ipvs.go b/cmd/ipvs.go index b2be75cfa17..79610255348 100644 --- a/cmd/ipvs.go +++ b/cmd/ipvs.go @@ -1,18 +1,17 @@ -/* -Copyright © 2020 NAME HERE +// Copyright © 2021 sealos. +// +// 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. -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 cmd import ( @@ -30,8 +29,6 @@ var ipvsCmd = &cobra.Command{ }, } -var clean bool - func init() { rootCmd.AddCommand(ipvsCmd) diff --git a/cmd/join.go b/cmd/join.go index 087f1f92572..68990f36b28 100644 --- a/cmd/join.go +++ b/cmd/join.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import ( "github.com/spf13/cobra" "github.com/fanux/sealos/install" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) // joinCmd represents the join command @@ -31,7 +31,7 @@ var joinCmd = &cobra.Command{ PreRun: func(cmd *cobra.Command, args []string) { if len(install.MasterIPs) == 0 && len(install.NodeIPs) == 0 { logger.Error("this command is join feature,master and node is empty at the same time.please check your args in command.") - cmd.Help() + _ = cmd.Help() os.Exit(0) } }, @@ -45,20 +45,19 @@ func init() { joinCmd.Flags().IntVar(&install.Vlog, "vlog", 0, "kubeadm log level") } -func JoinCmdFunc (cmd *cobra.Command, args []string) { +func JoinCmdFunc(cmd *cobra.Command, args []string) { beforeNodes := install.ParseIPs(install.NodeIPs) beforeMasters := install.ParseIPs(install.MasterIPs) c := &install.SealConfig{} - err := c.Load(cfgFile) - if err != nil { + if err := c.Load(cfgFile); err != nil { logger.Error(err) c.ShowDefaultConfig() os.Exit(0) } cfgNodes := append(c.Masters, c.Nodes...) - joinNodes := append(beforeNodes, beforeMasters...) + joinNodes := append(beforeNodes, beforeMasters...) if ok, node := deleteOrJoinNodeIsExistInCfgNodes(joinNodes, cfgNodes); ok { logger.Error(`[%s] has already exist in your cluster. please check.`, node) diff --git a/cmd/root.go b/cmd/root.go index d9d5bba7227..813922de927 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import ( "github.com/fanux/sealos/cert" "github.com/fanux/sealos/install" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" "github.com/spf13/cobra" ) @@ -58,7 +58,6 @@ func init() { cobra.OnInitialize(initConfig) rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.sealos/config.yaml)") rootCmd.PersistentFlags().BoolVar(&Info, "info", false, "logger ture for Info, false for Debug") - } // initConfig reads in config file and ENV variables if set. diff --git a/cmd/route.go b/cmd/route.go index 8869960bd9f..fafb0563b8a 100644 --- a/cmd/route.go +++ b/cmd/route.go @@ -1,18 +1,17 @@ -/* -Copyright © 2020 NAME HERE +// Copyright © 2021 sealos. +// +// 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. -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 cmd import ( @@ -23,7 +22,7 @@ import ( var ( host string - gatewayIp string + gatewayIP string ) func NewRouteCmd() *cobra.Command { @@ -52,7 +51,7 @@ func NewAddRouteCmd() *cobra.Command { } // manually to set host via gateway cmd.Flags().StringVar(&host, "host", "", "route host ,ex ip route add host via gateway") - cmd.Flags().StringVar(&gatewayIp, "gateway", "", "route gateway ,ex ip route add host via gateway") + cmd.Flags().StringVar(&gatewayIP, "gateway", "", "route gateway ,ex ip route add host via gateway") return cmd } @@ -64,21 +63,21 @@ func NewDelRouteCmd() *cobra.Command { } // manually to set host via gateway cmd.Flags().StringVar(&host, "host", "", "route host ,ex ip route del host via gateway") - cmd.Flags().StringVar(&gatewayIp, "gateway", "", "route gateway ,ex ip route del host via gateway") + cmd.Flags().StringVar(&gatewayIP, "gateway", "", "route gateway ,ex ip route del host via gateway") return cmd } func RouteCmdFunc(cmd *cobra.Command, args []string) { - r := install.GetRouteFlag(host, gatewayIp) + r := install.GetRouteFlag(host, gatewayIP) r.CheckRoute() } func RouteAddCmdFunc(cmd *cobra.Command, args []string) { - r := install.GetRouteFlag(host, gatewayIp) + r := install.GetRouteFlag(host, gatewayIP) r.SetRoute() } func RouteDelCmdFunc(cmd *cobra.Command, args []string) { - r := install.GetRouteFlag(host, gatewayIp) + r := install.GetRouteFlag(host, gatewayIP) r.DelRoute() } diff --git a/cmd/upgrade.go b/cmd/upgrade.go index d70342e66e3..051ad402a36 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -1,18 +1,17 @@ -/* -Copyright © 2020 NAME HERE +// Copyright © 2021 sealos. +// +// 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. -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 cmd import ( @@ -22,7 +21,7 @@ import ( "github.com/spf13/cobra" "github.com/fanux/sealos/install" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) func init() { @@ -31,7 +30,7 @@ func init() { var ( newVersion string - newPkgUrl string + newPkgURL string ) func NewUpgradeCmd() *cobra.Command { @@ -42,7 +41,7 @@ func NewUpgradeCmd() *cobra.Command { PreRun: PreRunUpgradeCmdFunc, } cmd.Flags().StringVar(&newVersion, "version", "", "upgrade version for kubernetes version") - cmd.Flags().StringVar(&newPkgUrl, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline package url, or file location ex. /root/kube1.14.1.tar.gz") + cmd.Flags().StringVar(&newPkgURL, "pkg-url", "", "http://store.lameleg.com/kube1.14.1.tar.gz download offline package url, or file location ex. /root/kube1.14.1.tar.gz") cmd.Flags().BoolVarP(&force, "force", "f", false, "upgrade need interactive to confirm") return cmd } @@ -57,13 +56,13 @@ func UpgradeCmdFunc(cmd *cobra.Command, args []string) { os.Exit(-1) } } - u := install.NewUpgrade(newVersion, newPkgUrl) + u := install.NewUpgrade(newVersion, newPkgURL) u.SetUP() u.Dump(cfgFile) } func PreRunUpgradeCmdFunc(cmd *cobra.Command, args []string) { - if err := install.ExitUpgradeCase(newVersion, newPkgUrl, cfgFile); err != nil { + if err := install.ExitUpgradeCase(newVersion, newPkgURL, cfgFile); err != nil { logger.Error("PreRun error: ", err) os.Exit(1) } diff --git a/cmd/version.go b/cmd/version.go index 8902c6dc99f..5f479d52d20 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -16,9 +16,10 @@ package cmd import ( "fmt" - extver "github.com/linuxsuren/cobra-extension/version" "runtime" "strings" + + extver "github.com/linuxsuren/cobra-extension/version" ) func init() { diff --git a/docs/execCmd.md b/docs/execCmd.md index 17dcb83b471..c97583124b1 100644 --- a/docs/execCmd.md +++ b/docs/execCmd.md @@ -105,7 +105,7 @@ func (e *ExecFlag) IsUseCmd() bool { ### 远程命令和复制实现 -实现`scp`复制, 则是通过复制单个文件, 然后递归复制即可. 查看具体的[源码](https://github.com/fanux/sealos/blob/develop/pkg/sshcmd/sshutil/scp.go) +实现`scp`复制, 则是通过复制单个文件, 然后递归复制即可. 查看具体的[源码](https://github.com/fanux/sealos/blob/master/pkg/sshcmd/sshutil/scp.go) 如果`--dst`在目标机器存在, 则不执行copy动作, 直接就跳过了. // todo diff --git a/go.mod b/go.mod index d6beee4a6ca..533540bfa27 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,9 @@ module github.com/fanux/sealos go 1.13 require ( - github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible + github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect github.com/dustin/go-humanize v1.0.0 // indirect - github.com/fanux/lvscare v0.0.0-00010101000000-000000000000 github.com/fanux/sealgate v0.0.5 github.com/google/uuid v1.1.2 // indirect github.com/linuxsuren/cobra-extension v0.0.8 @@ -14,9 +13,9 @@ require ( github.com/pkg/sftp v1.11.0 github.com/prometheus/common v0.13.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect + github.com/sealyun/lvscare v1.1.2-alpha.2 github.com/spf13/cobra v1.1.1 github.com/vishvananda/netlink v1.1.0 - github.com/wonderivan/logger v1.0.0 go.etcd.io/etcd v0.0.0-20200716221620-18dfb9cca345 go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.16.0 @@ -29,8 +28,3 @@ require ( k8s.io/client-go v0.18.0 sigs.k8s.io/yaml v1.2.0 ) - -replace ( - github.com/fanux/lvscare => github.com/fanux/lvscare v0.0.0-20201224091410-96651f6cbbad - github.com/wonderivan/logger => ./pkg/logger -) diff --git a/go.sum b/go.sum index 0c06501640c..8e92bfebd93 100644 --- a/go.sum +++ b/go.sum @@ -41,8 +41,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20191115143122-2863a2c99a71 h1:403lX+Je3enjqcePi8bSN6QWzhuzx71dBViv+PhqHUA= github.com/aliyun/alibaba-cloud-sdk-go v0.0.0-20191115143122-2863a2c99a71/go.mod h1:mNZkuqaeM5UCiAdkV4r+lrheu8Q5fe/487bRFrGYZ8A= -github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible h1:6t4QUhWVCcnilQy+d3aO0zsaO7wH6N3geMo4wgbC4MY= -github.com/aliyun/aliyun-oss-go-sdk v2.1.4+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= +github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible h1:ht2+VfbXtNLGhCsnTMc6/N26nSTBK6qdhktjYyjJQkk= +github.com/aliyun/aliyun-oss-go-sdk v2.2.0+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= @@ -116,8 +116,6 @@ github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4s github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= -github.com/fanux/lvscare v0.0.0-20201224091410-96651f6cbbad h1:ObOI3VUVmT7NK0s2Z3Ehl/rn01t299R8NeBIYj+FGho= -github.com/fanux/lvscare v0.0.0-20201224091410-96651f6cbbad/go.mod h1:mGT+7+yGJC3szumUqSBIplLvcAtVUWnZIEW37Gi/5Jc= github.com/fanux/sealgate v0.0.5 h1:KCBf+x3TdjCEVVNCjdkeMukDE0kZx9QE39Yy2IazkiM= github.com/fanux/sealgate v0.0.5/go.mod h1:y3141RLsCSgid4+H0O+7CoAqIHDGB0r0mcR8TYnqvps= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= @@ -442,6 +440,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= +github.com/sealyun/lvscare v1.1.2-alpha.2 h1:SlnEAXOPn5gC6l8tgvbn9fApyJaQ0ecQHSV3cEfvQYY= +github.com/sealyun/lvscare v1.1.2-alpha.2/go.mod h1:FtOEdsXuYtw9Jwd/Jct25K+PcpUFSDemvF4VgNygjj0= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= @@ -501,6 +501,8 @@ github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJ github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k= github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= +github.com/wonderivan/logger v1.0.0 h1:Z6Nz+3SNcizolx3ARH11axdD4DXjFpb2J+ziGUVlv/U= +github.com/wonderivan/logger v1.0.0/go.mod h1:NObMfQ3WOLKfYEZuGeZQfuQfSPE5+QNgRddVMzsAT/k= github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= diff --git a/hack/docker/Dockerfile b/hack/docker/Dockerfile index 1f0cebf9e54..5674b5b49e2 100755 --- a/hack/docker/Dockerfile +++ b/hack/docker/Dockerfile @@ -1,3 +1,17 @@ +# Copyright © 2021 sealos. +# +# 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. + # syntax=docker/dockerfile:1.0-experimental FROM alpine:latest MAINTAINER "louisehong " diff --git a/hack/lib/ssh-mutual-trust.sh b/hack/lib/ssh-mutual-trust.sh index e72ebd08347..48063e0a676 100755 --- a/hack/lib/ssh-mutual-trust.sh +++ b/hack/lib/ssh-mutual-trust.sh @@ -1,4 +1,18 @@ #!/bin/bash +# Copyright © 2021 sealos. +# +# 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. + #1、将IP写在一个文件里,比如文件名为hosts_file,一行一个IP地址。 #2、修改ssh-mutual-trust.sh里面的用户名及密码,默认为root用户及密码123。 diff --git a/install/check.go b/install/check.go index 15d702e09e2..3c25790e922 100644 --- a/install/check.go +++ b/install/check.go @@ -1,16 +1,32 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( "fmt" "os" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/net" + + "github.com/fanux/sealos/pkg/logger" ) // SetHosts set hosts. if can't access to hostName, set /etc/hosts func SetHosts(hostIP, hostName string) { - cmd := fmt.Sprintf("cat /etc/hosts |grep %s || echo '%s %s' >> /etc/hosts", hostName, IpFormat(hostIP), hostName) - SSHConfig.CmdAsync(hostIP, cmd) + cmd := fmt.Sprintf("cat /etc/hosts |grep %s || echo '%s %s' >> /etc/hosts", hostName, IPFormat(hostIP), hostName) + _ = SSHConfig.CmdAsync(hostIP, cmd) } //CheckValid is @@ -49,7 +65,7 @@ func (s *SealosInstaller) CheckValid() { } logger.Info("[%s] ------------ check ok", h) } - if s.Network == "cilium" { + if s.Network == net.CILIUM { if err := SSHConfig.CmdAsync(h, "uname -r | grep 5 | awk -F. '{if($2>3)print \"ok\"}' | grep ok && exit 0 || exit 1"); err != nil { logger.Error("[%s] ------------ check kernel version < 5.3", h) os.Exit(1) @@ -63,7 +79,7 @@ func (s *SealosInstaller) CheckValid() { // version >= 1.20 , Add prefight for containerd if For120(Version) { // for containerd. if docker exist ; exit frist. - + dockerExist := SSHConfig.CmdToString(h, "command -v dockerd &> /dev/null && echo yes || :", "") if dockerExist == "yes" { errList = append(errList, h) diff --git a/install/clean.go b/install/clean.go index 203c693e4b5..c1ba93c1951 100644 --- a/install/clean.go +++ b/install/clean.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -7,8 +21,8 @@ import ( "sync" "github.com/fanux/sealos/ipvs" + "github.com/fanux/sealos/pkg/logger" sshcmd "github.com/fanux/sealos/pkg/sshcmd/cmd" - "github.com/wonderivan/logger" ) type SealosClean struct { @@ -78,7 +92,6 @@ end: cfgPath := home + defaultConfigPath sshcmd.Cmd("/bin/sh", "-c", "rm -rf "+cfgPath) } - } //Clean clean cluster. @@ -108,7 +121,6 @@ func (s *SealosClean) Clean() { } wg.Wait() } - } func (s *SealosClean) cleanNode(node string) { @@ -169,7 +181,7 @@ func clean(host string) { _ = SSHConfig.CmdAsync(host, cmd) cmd = "rm -rf /var/lib/etcd && rm -rf /var/etcd" _ = SSHConfig.CmdAsync(host, cmd) - cmd = fmt.Sprintf("sed -i \"/%s/d\" /etc/hosts ", ApiServer) + cmd = fmt.Sprintf("sed -i \"/%s/d\" /etc/hosts ", APIServer) _ = SSHConfig.CmdAsync(host, cmd) cmd = "rm -rf ~/kube" _ = SSHConfig.CmdAsync(host, cmd) @@ -183,11 +195,11 @@ func clean(host string) { func cleanRoute(node string) { // clean route - cmdRoute := fmt.Sprintf("sealos route --host %s", IpFormat(node)) + cmdRoute := fmt.Sprintf("sealos route --host %s", IPFormat(node)) status := SSHConfig.CmdToString(node, cmdRoute, "") if status != "ok" { // 删除为 vip创建的路由。 - delRouteCmd := fmt.Sprintf("sealos route del --host %s --gateway %s", VIP, IpFormat(node)) + delRouteCmd := fmt.Sprintf("sealos route del --host %s --gateway %s", VIP, IPFormat(node)) SSHConfig.CmdToString(node, delRouteCmd, "") } } diff --git a/install/cloud.go b/install/cloud.go index 099e985896e..89cdda8c7d1 100644 --- a/install/cloud.go +++ b/install/cloud.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -6,8 +20,8 @@ import ( "strings" "github.com/fanux/sealgate/cloud" + "github.com/fanux/sealos/pkg/logger" extver "github.com/linuxsuren/cobra-extension/version" - "github.com/wonderivan/logger" ) //VersionURL is base64 encode k8s version and offline package url @@ -43,7 +57,6 @@ type Cluster struct { SecuretyGroupID string } -//Global config var C Cluster var ClusterDir = "/root/.sealos/clusters/" @@ -63,7 +76,7 @@ func CloudInstall(c *Cluster) { config := c.Config p := cloud.NewProvider(config) - Dump(fmt.Sprintf("%s%s.yaml", ClusterDir, c.Name), c) + _ = Dump(fmt.Sprintf("%s%s.yaml", ClusterDir, c.Name), c) //TODO concurrence create master and nodes vms, should not create two vpcs /* diff --git a/install/config.go b/install/config.go index 4a0e304e422..71526a5ebc8 100644 --- a/install/config.go +++ b/install/config.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -5,22 +19,25 @@ import ( "io/ioutil" "os" - "gopkg.in/yaml.v2" + v2 "gopkg.in/yaml.v2" "github.com/fanux/sealos/net" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) -const defaultConfigPath = "/.sealos" -const defaultConfigFile = "/config.yaml" +const ( + defaultConfigPath = "/.sealos" + defaultConfigFile = "/config.yaml" + defaultAPIServerDomain = "apiserver.cluster.local" +) // SealConfig for ~/.sealos/config.yaml type SealConfig struct { Masters []string Nodes []string //config from kubeadm.cfg. ex. cluster.local - DnsDomain string - ApiServerCertSANs []string + DNSDomain string + APIServerCertSANs []string //SSHConfig User string @@ -28,7 +45,7 @@ type SealConfig struct { PrivateKey string PkPassword string //ApiServer ex. apiserver.cluster.local - ApiServerDomain string + APIServerDomain string Network string VIP string PkgURL string @@ -59,29 +76,29 @@ func (c *SealConfig) Dump(path string) { c.Passwd = SSHConfig.Password c.PrivateKey = SSHConfig.PkFile c.PkPassword = SSHConfig.PkPassword - c.ApiServerDomain = ApiServer + c.APIServerDomain = APIServer c.Network = Network c.VIP = VIP - c.PkgURL = PkgUrl + c.PkgURL = PkgURL c.Version = Version c.Repo = Repo c.SvcCIDR = SvcCIDR c.PodCIDR = PodCIDR - c.DnsDomain = DnsDomain - c.ApiServerCertSANs = ApiServerCertSANs + c.DNSDomain = DNSDomain + c.APIServerCertSANs = APIServerCertSANs c.CertPath = CertPath c.CertEtcdPath = CertEtcdPath //lvscare c.LvscareName = LvscareImage.Image c.LvscareTag = LvscareImage.Tag // oss - c.AliOss.AccessKeyId = AccessKeyId + c.AliOss.AccessKeyID = AccessKeyID c.AliOss.AccessKeySecrets = AccessKeySecrets c.AliOss.OssEndpoint = OssEndpoint c.AliOss.BucketName = BucketName c.AliOss.ObjectPath = ObjectPath - y, err := yaml.Marshal(c) + y, err := v2.Marshal(c) if err != nil { logger.Error("dump config file failed: %s", err) } @@ -97,7 +114,7 @@ func (c *SealConfig) Dump(path string) { } func Dump(path string, content interface{}) error { - y, err := yaml.Marshal(content) + y, err := v2.Marshal(content) if err != nil { logger.Error("dump config file failed: %s", err) return err @@ -109,7 +126,7 @@ func Dump(path string, content interface{}) error { return err } - ioutil.WriteFile(path, y, 0644) + _ = ioutil.WriteFile(path, y, 0644) return nil } @@ -125,7 +142,7 @@ func (c *SealConfig) Load(path string) (err error) { return fmt.Errorf("read config file %s failed %w", path, err) } - err = yaml.Unmarshal(y, c) + err = v2.Unmarshal(y, c) if err != nil { return fmt.Errorf("unmarshal config file failed: %w", err) } @@ -136,16 +153,16 @@ func (c *SealConfig) Load(path string) (err error) { SSHConfig.Password = c.Passwd SSHConfig.PkFile = c.PrivateKey SSHConfig.PkPassword = c.PkPassword - ApiServer = c.ApiServerDomain + APIServer = c.APIServerDomain Network = c.Network VIP = c.VIP - PkgUrl = c.PkgURL + PkgURL = c.PkgURL Version = c.Version Repo = c.Repo PodCIDR = c.PodCIDR SvcCIDR = c.SvcCIDR - DnsDomain = c.DnsDomain - ApiServerCertSANs = c.ApiServerCertSANs + DNSDomain = c.DNSDomain + APIServerCertSANs = c.APIServerCertSANs CertPath = c.CertPath CertEtcdPath = c.CertEtcdPath //lvscare @@ -153,9 +170,9 @@ func (c *SealConfig) Load(path string) (err error) { LvscareImage.Tag = c.LvscareTag // 优先使用使用命令行, 再使用配置文件 - if AccessKeyId == "" || AccessKeySecrets == "" || + if AccessKeyID == "" || AccessKeySecrets == "" || OssEndpoint == "" || BucketName == "" || ObjectPath == "" { - AccessKeyId = c.AliOss.AccessKeyId + AccessKeyID = c.AliOss.AccessKeyID AccessKeySecrets = c.AliOss.AccessKeySecrets OssEndpoint = c.AliOss.OssEndpoint BucketName = c.AliOss.BucketName @@ -171,7 +188,7 @@ func Load(path string, content interface{}) error { os.Exit(0) } - err = yaml.Unmarshal(y, content) + err = v2.Unmarshal(y, content) if err != nil { logger.Error("unmarshal config file failed: %s", err) } @@ -185,7 +202,7 @@ func (c *SealConfig) ShowDefaultConfig() { c.User = "root" c.Passwd = "123456" c.PrivateKey = home + "/.ssh/id_rsa" - c.ApiServerDomain = "apiserver.cluster.local" + c.APIServerDomain = defaultAPIServerDomain c.Network = net.CALICO c.VIP = "10.103.97.2" c.PkgURL = home + "/kube1.17.13.tar.gz" @@ -193,13 +210,13 @@ func (c *SealConfig) ShowDefaultConfig() { c.Repo = "k8s.gcr.io" c.PodCIDR = "100.64.0.0/10" c.SvcCIDR = "10.96.0.0/12" - c.ApiServerCertSANs = []string{"apiserver.cluster.local", "127.0.0.1"} + c.APIServerCertSANs = []string{"apiserver.cluster.local", "127.0.0.1"} c.CertPath = home + "/.sealos/pki" c.CertEtcdPath = home + "/.sealos/pki/etcd" c.LvscareName = "fanux/lvscare" c.LvscareTag = "latest" - y, err := yaml.Marshal(c) + y, err := v2.Marshal(c) if err != nil { logger.Error("marshal config file failed: %s", err) } diff --git a/install/constants.go b/install/constants.go index 21ec66f3a5a..e8c25bd46b3 100644 --- a/install/constants.go +++ b/install/constants.go @@ -1,17 +1,26 @@ +// Copyright © 2021 sealos. +// +// 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 install const ( ErrorExitOSCase = -1 // 错误直接退出类型 - ErrorMasterEmpty = "your master is empty." // master节点ip为空 - ErrorVersionEmpty = "your kubernetes version is empty." // kubernetes 版本号为空 - ErrorFileNotExist = "your package file is not exist." // 离线安装包为空 - ErrorPkgUrlNotExist = "Your package url is incorrect." // 离线安装包为http路径不对 - ErrorPkgUrlSize = "Download file size is less then 200M " // 离线安装包为http路径不对 - //ErrorMessageSSHConfigEmpty = "your ssh password or private-key is empty." // ssh 密码/秘钥为空 - // ErrorMessageCommon // 其他错误消息 - - // MinDownloadFileSize int64 = 400 * 1024 * 1024 + ErrorMasterEmpty = "your master is empty." // master节点ip为空 + ErrorVersionEmpty = "your kubernetes version is empty." // kubernetes 版本号为空 + ErrorFileNotExist = "your package file is not exist." // 离线安装包为空 + ErrorPkgURLNotExist = "Your package url is incorrect." // 离线安装包为http路径不对 // etcd backup ETCDSNAPSHOTDEFAULTNAME = "snapshot" @@ -30,29 +39,29 @@ const ( DefaultCgroupDriver = "cgroupfs" DefaultSystemdCgroupDriver = "systemd" - KubeadmV1beta1 = "kubeadm.k8s.io/v1beta1" - KubeadmV1beta2 = "kubeadm.k8s.io/v1beta2" - KubeadmV1beta3 = "kubeadm.k8s.io/v1beta3" -/* -A list of changes since v1beta1: + KubeadmV1beta1 = "kubeadm.k8s.io/v1beta1" + KubeadmV1beta2 = "kubeadm.k8s.io/v1beta2" + KubeadmV1beta3 = "kubeadm.k8s.io/v1beta3" + /* + A list of changes since v1beta1: -`certificateKey" field is added to InitConfiguration and JoinConfiguration. -"ignorePreflightErrors" field is added to the NodeRegistrationOptions. -The JSON "omitempty" tag is used in a more places where appropriate. -The JSON "omitempty" tag of the "taints" field (inside NodeRegistrationOptions) is removed. See the Kubernetes 1.15 changelog for further details. + `certificateKey" field is added to InitConfiguration and JoinConfiguration. + "ignorePreflightErrors" field is added to the NodeRegistrationOptions. + The JSON "omitempty" tag is used in a more places where appropriate. + The JSON "omitempty" tag of the "taints" field (inside NodeRegistrationOptions) is removed. See the Kubernetes 1.15 changelog for further details. -A list of changes since v1beta2: + A list of changes since v1beta2: -The deprecated ClusterConfiguration.useHyperKubeImage field has been removed. Kubeadm no longer supports the hyperkube image. -The ClusterConfiguration.dns.type field has been removed since CoreDNS is the only supported DNS server type by kubeadm. -Include "datapolicy" tags on the fields that hold secrets. This would result in the field values to be omitted when API structures are printed with klog. -Add InitConfiguration.skipPhases, JoinConfiguration.skipPhases to allow skipping a list of phases during kubeadm init/join command execution. -Add InitConfiguration.nodeRegistration.imagePullPolicy" andJoinConfiguration.nodeRegistration.imagePullPolicy` to allow specifying the images pull policy during kubeadm "init" and "join". The value must be one of "Always", "Never" or "IfNotPresent". "IfNotPresent" is the default, which has been the existing behavior prior to this addition. -Add InitConfiguration.patches.directory, JoinConfiguration.patches.directory to allow the user to configure a directory from which to take patches for components deployed by kubeadm. -Move the BootstrapToken∗ API and related utilities out of the "kubeadm" API group to a new group "bootstraptoken". The kubeadm API version v1beta3 no longer contains the BootstrapToken∗ structures. + The deprecated ClusterConfiguration.useHyperKubeImage field has been removed. Kubeadm no longer supports the hyperkube image. + The ClusterConfiguration.dns.type field has been removed since CoreDNS is the only supported DNS server type by kubeadm. + Include "datapolicy" tags on the fields that hold secrets. This would result in the field values to be omitted when API structures are printed with klog. + Add InitConfiguration.skipPhases, JoinConfiguration.skipPhases to allow skipping a list of phases during kubeadm init/join command execution. + Add InitConfiguration.nodeRegistration.imagePullPolicy" andJoinConfiguration.nodeRegistration.imagePullPolicy` to allow specifying the images pull policy during kubeadm "init" and "join". The value must be one of "Always", "Never" or "IfNotPresent". "IfNotPresent" is the default, which has been the existing behavior prior to this addition. + Add InitConfiguration.patches.directory, JoinConfiguration.patches.directory to allow the user to configure a directory from which to take patches for components deployed by kubeadm. + Move the BootstrapToken∗ API and related utilities out of the "kubeadm" API group to a new group "bootstraptoken". The kubeadm API version v1beta3 no longer contains the BootstrapToken∗ structures. -*/ + */ ) const ( diff --git a/install/download.go b/install/download.go index e264f67a9dc..ce7d884aebb 100644 --- a/install/download.go +++ b/install/download.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -6,9 +20,9 @@ import ( "path" "sync" + "github.com/fanux/sealos/pkg/logger" "github.com/fanux/sealos/pkg/sshcmd/cmd" "github.com/fanux/sealos/pkg/sshcmd/md5sum" - "github.com/wonderivan/logger" ) //location : url @@ -68,7 +82,7 @@ func DownloadFile(location string) (filePATH, md5 string) { // func downloadFile(location string) (filePATH, md5 string) { - if _, isUrl := isUrl(location); isUrl { + if _, isURL := isURL(location); isURL { absPATH := "/tmp/sealos/" + path.Base(location) if !cmd.IsFileExist(absPATH) { //generator download cmd @@ -86,9 +100,9 @@ func downloadFile(location string) (filePATH, md5 string) { //根据url 获取command func downloadCmd(url string) string { //only http - u, isHttp := isUrl(url) + u, isHTTP := isURL(url) var c = "" - if isHttp { + if isHTTP { param := "" if u.Scheme == "https" { param = "--no-check-certificate" @@ -98,7 +112,7 @@ func downloadCmd(url string) string { return c } -func isUrl(u string) (url.URL, bool) { +func isURL(u string) (url.URL, bool) { if uu, err := url.Parse(u); err == nil && uu != nil && uu.Host != "" { return *uu, true } diff --git a/install/etcd_restore.go b/install/etcd_restore.go index 582cb66de48..a457e119986 100644 --- a/install/etcd_restore.go +++ b/install/etcd_restore.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -11,7 +25,7 @@ import ( "go.etcd.io/etcd/clientv3/snapshot" "go.uber.org/zap" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) func init() { @@ -34,8 +48,7 @@ func GetRestoreFlags(cfgFile string) *EtcdFlags { logger.Error("ETCD CaCert or key file is not exist.") os.Exit(1) } - err := e.Load(cfgFile) - if err != nil { + if err := e.Load(cfgFile); err != nil { logger.Error(err) e.ShowDefaultConfig() os.Exit(0) @@ -45,7 +58,7 @@ func GetRestoreFlags(cfgFile string) *EtcdFlags { e.RestoreDir = RestorePath e.LongName = fmt.Sprintf("%s/%s", e.BackDir, e.Name) for _, h := range e.Masters { - ip := reFormatHostToIp(h) + ip := reFormatHostToIP(h) enpoint := fmt.Sprintf("%s:2379", ip) e.EtcdHosts = append(e.EtcdHosts, ip) e.Endpoints = append(e.Endpoints, enpoint) @@ -62,7 +75,7 @@ func (e *EtcdFlags) StopPod() (string, error) { var wg sync.WaitGroup for _, host := range e.EtcdHosts { wg.Add(1) - host = reFormatHostToIp(host) + host = reFormatHostToIP(host) go func(host string) { defer wg.Done() // backup dir to random dir to avoid dir exist err @@ -89,7 +102,7 @@ func (e *EtcdFlags) RestoreAll() { hostname := SSHConfig.CmdToString(host, "hostname", "") // remove first cmd := fmt.Sprintf("rm -rf %s-%s", e.RestoreDir, hostname) - CmdWork(host, cmd, TMPDIR) + _ = CmdWork(host, cmd, TMPDIR) logger.Info("execute %s", cmd) e.restore(hostname, host) } @@ -156,7 +169,7 @@ func (e *EtcdFlags) StartPod(dir string) { var wg sync.WaitGroup for _, host := range e.EtcdHosts { wg.Add(1) - host = reFormatHostToIp(host) + host = reFormatHostToIP(host) go func(host string) { defer wg.Done() // start kube-apiserver @@ -174,25 +187,24 @@ func (e *EtcdFlags) StartPod(dir string) { func (e *EtcdFlags) RecoveryKuBeCluster(dir string) { // restore old file first for _, host := range e.EtcdHosts { - host = reFormatHostToIp(host) + host = reFormatHostToIP(host) // rm old file then start cp bak to etcd dir recoverEtcdCmd := fmt.Sprintf(`rm -rf %s && mv %s %s`, ETCDDATADIR, ETCDDATADIR+dir, ETCDDATADIR) - CmdWork(host, recoverEtcdCmd, TMPDIR) - + _ = CmdWork(host, recoverEtcdCmd, TMPDIR) } // start pod next for _, host := range e.EtcdHosts { - host = reFormatHostToIp(host) + host = reFormatHostToIP(host) // start kube-apiserver stopEtcdCmd := fmt.Sprintf(`mv /etc/kubernetes/manifests%s /etc/kubernetes/manifests`, dir) - CmdWork(host, stopEtcdCmd, TMPDIR) + _ = CmdWork(host, stopEtcdCmd, TMPDIR) } } func GetEtcdInitialCluster(hosts []string) string { initialCluster := "" for i, host := range hosts { hostname := SSHConfig.CmdToString(host, "hostname", "") - ip := reFormatHostToIp(host) + ip := reFormatHostToIP(host) initialCluster += fmt.Sprintf("etcd-%s=https://%s:2380", hostname, ip) if i < (len(hosts) - 1) { initialCluster += "," @@ -204,7 +216,7 @@ func GetEtcdInitialCluster(hosts []string) string { func GetEtcdPeerURLs(host string) []string { var peerUrls []string - ip := reFormatHostToIp(host) + ip := reFormatHostToIP(host) url := fmt.Sprintf("https://%s:2380", ip) peerUrls = append(peerUrls, url) return peerUrls diff --git a/install/etcd_save.go b/install/etcd_save.go index c69dddfd17c..64505700fa0 100644 --- a/install/etcd_save.go +++ b/install/etcd_save.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -16,7 +30,7 @@ import ( "go.etcd.io/etcd/pkg/transport" "go.uber.org/zap" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) type EtcdFlags struct { @@ -38,8 +52,7 @@ func GetEtcdBackFlags(cfgFile string) *EtcdFlags { logger.Error("ETCD CaCert or key file is not exist.") os.Exit(1) } - err := e.Load(cfgFile) - if err != nil { + if err := e.Load(cfgFile); err != nil { logger.Error(err) e.ShowDefaultConfig() os.Exit(0) @@ -50,10 +63,10 @@ func GetEtcdBackFlags(cfgFile string) *EtcdFlags { e.LongName = fmt.Sprintf("%s/%s", e.BackDir, e.Name) //get oss 。如果AccessKeyId不为空,则读取使命令行,如果为空,load的时候则读取配置文件 - if AccessKeyId != "" { + if AccessKeyID != "" { e.ObjectPath = ObjectPath e.OssEndpoint = OssEndpoint - e.AccessKeyId = AccessKeyId + e.AccessKeyID = AccessKeyID e.AccessKeySecrets = AccessKeySecrets e.BucketName = BucketName } @@ -67,11 +80,11 @@ func GetEtcdBackFlags(cfgFile string) *EtcdFlags { } for _, h := range e.Masters { - ip = reFormatHostToIp(h) + ip = reFormatHostToIP(h) e.EtcdHosts = append(e.EtcdHosts, ip) } // snapshot must be requested to one selected node, not multiple. - endpoint = fmt.Sprintf("%s:2379", reFormatHostToIp(e.Masters[0])) + endpoint = fmt.Sprintf("%s:2379", reFormatHostToIP(e.Masters[0])) e.Endpoints = append(e.Endpoints, endpoint) return e } @@ -111,15 +124,14 @@ func (e *EtcdFlags) Save(inDocker bool) error { } // trimPathForOss is trim this `/sealos//snapshot-1598146449` to `sealos/snapshot-1598146449` e.ObjectPath = trimPathForOss(e.ObjectPath + "/" + e.Name) - if e.AccessKeyId != "" { - err := saveToOss(e.OssEndpoint, e.AccessKeyId, e.AccessKeySecrets, e.BucketName, e.ObjectPath, e.LongName) + if e.AccessKeyID != "" { + err := saveToOss(e.OssEndpoint, e.AccessKeyID, e.AccessKeySecrets, e.BucketName, e.ObjectPath, e.LongName) if err != nil { logger.Error("save to oss err,", err) return fmt.Errorf("save to oss err: %q", err) } // 如果没有报错, 保存一下最新命令行配置。 logger.Info("Finished saving/uploading snapshot [%s] on aliyun oss [%s] bucket", e.Name, e.BucketName) - } return nil } @@ -129,7 +141,7 @@ func trimPathForOss(path string) string { return s[1:] } -func reFormatHostToIp(host string) string { +func reFormatHostToIP(host string) string { if strings.Contains(host, ":") { s := strings.Split(host, ":") return s[0] @@ -139,14 +151,14 @@ func reFormatHostToIp(host string) string { type AliOss struct { OssEndpoint string - AccessKeyId string + AccessKeyID string AccessKeySecrets string BucketName string ObjectPath string } -func saveToOss(aliEndpoint, accessKeyId, accessKeySecrets, bucketName, objectName, localFileName string) error { - ossClient, err := oss.New(aliEndpoint, accessKeyId, accessKeySecrets) +func saveToOss(aliEndpoint, accessKeyID, accessKeySecrets, bucketName, objectName, localFileName string) error { + ossClient, err := oss.New(aliEndpoint, accessKeyID, accessKeySecrets) if err != nil { return err } @@ -156,7 +168,6 @@ func saveToOss(aliEndpoint, accessKeyId, accessKeySecrets, bucketName, objectNam } // return bucket.PutObjectFromFile(objectName, localFileName) - } func GetCfg(ep []string) (*clientv3.Config, error) { @@ -213,14 +224,13 @@ func GetHealthFlag(cfgFile string) *EtcdFlags { logger.Error("ETCD CaCert or key file is not exist.") os.Exit(1) } - err := e.Load(cfgFile) - if err != nil { + if err := e.Load(cfgFile); err != nil { logger.Error(err) e.ShowDefaultConfig() os.Exit(0) } for _, h := range e.Masters { - ip := reFormatHostToIp(h) + ip := reFormatHostToIP(h) enpoint := fmt.Sprintf("%s:2379", ip) e.EtcdHosts = append(e.EtcdHosts, ip) e.Endpoints = append(e.Endpoints, enpoint) diff --git a/install/etcd_save_test.go b/install/etcd_save_test.go index e9bd2e87293..66ff39b73be 100644 --- a/install/etcd_save_test.go +++ b/install/etcd_save_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -20,7 +34,7 @@ func Test_reFormatHostToIp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := reFormatHostToIp(tt.args.host); got != tt.want { + if got := reFormatHostToIP(tt.args.host); got != tt.want { t.Errorf("reFormatHostToIp() = %v, want %v", got, tt.want) } }) diff --git a/install/exec.go b/install/exec.go index f5903af0949..971ebd54837 100644 --- a/install/exec.go +++ b/install/exec.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -6,7 +20,7 @@ import ( "sync" "github.com/fanux/sealos/k8s" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) type ExecFlag struct { @@ -98,16 +112,16 @@ func (e *ExecFlag) IsUseNode() bool { // Copy is cp src file to dst file func (e *ExecFlag) Copy() { - e.copyByNodeIp() + e.copyByNodeIP() } // Exec is cp src file to dst file func (e *ExecFlag) Exec() { - e.execByNodeIp() + e.execByNodeIP() } // copyByNodeIp is cp src file to dst file -func (e *ExecFlag) copyByNodeIp() { +func (e *ExecFlag) copyByNodeIP() { var wg sync.WaitGroup for _, n := range e.ExecNode { wg.Add(1) @@ -125,7 +139,7 @@ func (e *ExecFlag) copyByNodeIp() { } // execByNodeIp is exec cmd in Node -func (e *ExecFlag) execByNodeIp() { +func (e *ExecFlag) execByNodeIP() { var wg sync.WaitGroup for _, n := range e.ExecNode { wg.Add(1) diff --git a/install/generator.go b/install/generator.go index a5283503387..da1fa8c8ec0 100644 --- a/install/generator.go +++ b/install/generator.go @@ -1,34 +1,49 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( "bytes" "fmt" - "github.com/wonderivan/logger" - "sigs.k8s.io/yaml" "strings" "text/template" + + "github.com/fanux/sealos/pkg/logger" + "sigs.k8s.io/yaml" ) var ConfigType string -func setKubeadmApi(version string) { +func setKubeadmAPI(version string) { major, _ := GetMajorMinorInt(version) switch { // case major < 120: - KubeadmApi = KubeadmV1beta1 + KubeadmAPI = KubeadmV1beta1 CriSocket = DefaultDockerCRISocket case major < 123 && major >= 120: - KubeadmApi = KubeadmV1beta2 + KubeadmAPI = KubeadmV1beta2 CriSocket = DefaultContainerdCRISocket case major >= 123: - KubeadmApi = KubeadmV1beta3 + KubeadmAPI = KubeadmV1beta3 CriSocket = DefaultContainerdCRISocket default: - KubeadmApi = KubeadmV1beta3 + KubeadmAPI = KubeadmV1beta3 CriSocket = DefaultContainerdCRISocket } - logger.Debug("KubeadmApi: %s", KubeadmApi) + logger.Debug("KubeadmApi: %s", KubeadmAPI) logger.Debug("CriSocket: %s", CriSocket) } @@ -74,7 +89,7 @@ func JoinTemplate(ip string, cgroup string) []byte { } func JoinTemplateFromTemplateContent(templateContent, ip, cgroup string) []byte { - setKubeadmApi(Version) + setKubeadmAPI(Version) tmpl, err := template.New("text").Parse(templateContent) defer func() { if r := recover(); r != nil { @@ -85,12 +100,12 @@ func JoinTemplateFromTemplateContent(templateContent, ip, cgroup string) []byte panic(1) } var envMap = make(map[string]interface{}) - envMap["Master0"] = IpFormat(MasterIPs[0]) + envMap["Master0"] = IPFormat(MasterIPs[0]) envMap["Master"] = ip envMap["TokenDiscovery"] = JoinToken envMap["TokenDiscoveryCAHash"] = TokenCaCertHash envMap["VIP"] = VIP - envMap["KubeadmApi"] = KubeadmApi + envMap["KubeadmApi"] = KubeadmAPI envMap["CriSocket"] = CriSocket envMap["CgroupDriver"] = cgroup var buffer bytes.Buffer @@ -99,7 +114,7 @@ func JoinTemplateFromTemplateContent(templateContent, ip, cgroup string) []byte } func TemplateFromTemplateContent(templateContent string) []byte { - setKubeadmApi(Version) + setKubeadmAPI(Version) tmpl, err := template.New("text").Parse(templateContent) defer func() { if r := recover(); r != nil { @@ -112,21 +127,21 @@ func TemplateFromTemplateContent(templateContent string) []byte { var masters []string getmasters := MasterIPs for _, h := range getmasters { - masters = append(masters, IpFormat(h)) + masters = append(masters, IPFormat(h)) } var envMap = make(map[string]interface{}) envMap["CertSANS"] = CertSANS envMap["VIP"] = VIP envMap["Masters"] = masters envMap["Version"] = Version - envMap["ApiServer"] = ApiServer + envMap["ApiServer"] = APIServer envMap["PodCIDR"] = PodCIDR envMap["SvcCIDR"] = SvcCIDR envMap["Repo"] = Repo - envMap["Master0"] = IpFormat(MasterIPs[0]) + envMap["Master0"] = IPFormat(MasterIPs[0]) envMap["Network"] = Network envMap["CgroupDriver"] = CgroupDriver - envMap["KubeadmApi"] = KubeadmApi + envMap["KubeadmApi"] = KubeadmAPI envMap["CriSocket"] = CriSocket var buffer bytes.Buffer _ = tmpl.Execute(&buffer, envMap) @@ -146,8 +161,8 @@ func KubeadmDataFromYaml(context string) *KubeadmType { if err := yaml.Unmarshal([]byte(cfg), kubeadm); err == nil { // if kubeadm.Kind == "ClusterConfiguration" { - if kubeadm.Networking.DnsDomain == "" { - kubeadm.Networking.DnsDomain = "cluster.local" + if kubeadm.Networking.DNSDomain == "" { + kubeadm.Networking.DNSDomain = "cluster.local" } return kubeadm } @@ -160,10 +175,10 @@ func KubeadmDataFromYaml(context string) *KubeadmType { type KubeadmType struct { Kind string `yaml:"kind,omitempty"` - ApiServer struct { + APIServer struct { CertSANs []string `yaml:"certSANs,omitempty"` } `yaml:"apiServer"` Networking struct { - DnsDomain string `yaml:"dnsDomain,omitempty"` + DNSDomain string `yaml:"dnsDomain,omitempty"` } `yaml:"networking"` } diff --git a/install/generator_test.go b/install/generator_test.go index ce173eac02c..9d3068eef20 100644 --- a/install/generator_test.go +++ b/install/generator_test.go @@ -1,9 +1,24 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( - "github.com/fanux/sealos/pkg/sshcmd/sshutil" "reflect" "testing" + + "github.com/fanux/sealos/pkg/sshcmd/sshutil" ) func Test_generatorKubeadmConfig(t *testing.T) { @@ -19,7 +34,7 @@ func TestTemplate(t *testing.T) { } MasterIPs = masters VIP = vip - ApiServer = "apiserver.cluster.local" + APIServer = "apiserver.cluster.local" config.Cmd("127.0.0.1", "echo \""+string(Template())+"\" > ~/aa") t.Log(string(Template())) } @@ -29,7 +44,7 @@ func TestNetCiliumTemplate(t *testing.T) { var vip = "10.103.97.1" MasterIPs = masters VIP = vip - ApiServer = "apiserver.cluster.local" + APIServer = "apiserver.cluster.local" Version = "1.20.5" Network = "cilium" CgroupDriver = DefaultCgroupDriver @@ -110,11 +125,11 @@ func TestKubeadmDataFromYaml(t *testing.T) { args{testYaml}, &KubeadmType{ Kind: "ClusterConfiguration", - ApiServer: struct { + APIServer: struct { CertSANs []string `yaml:"certSANs,omitempty"` }{}, Networking: struct { - DnsDomain string `yaml:"dnsDomain,omitempty"` + DNSDomain string `yaml:"dnsDomain,omitempty"` }{}, }, }, @@ -122,7 +137,7 @@ func TestKubeadmDataFromYaml(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := KubeadmDataFromYaml(tt.args.context) - if !reflect.DeepEqual(got.ApiServer.CertSANs, []string{"127.0.0.1", "apiserver.cluster.local", "172.16.9.202", "172.16.9.200", "172.16.9.201", "10.103.97.2"}) { + if !reflect.DeepEqual(got.APIServer.CertSANs, []string{"127.0.0.1", "apiserver.cluster.local", "172.16.9.202", "172.16.9.200", "172.16.9.201", "10.103.97.2"}) { t.Errorf("%v", got) } }) @@ -143,8 +158,8 @@ func TestJoinTemplate(t *testing.T) { TokenCaCertHash = "sha256:a68c79c87368ff794ae50c5fd6a8ce13fdb2778764f1080614ddfeaa0e2b9d14" VIP = vip - config.Cmd("127.0.0.1", "echo \""+string(JoinTemplate(IpFormat(masters[0]), "systemd"))+"\" > ~/aa") - t.Log(string(JoinTemplate(IpFormat(masters[0]), "cgroupfs"))) + config.Cmd("127.0.0.1", "echo \""+string(JoinTemplate(IPFormat(masters[0]), "systemd"))+"\" > ~/aa") + t.Log(string(JoinTemplate(IPFormat(masters[0]), "cgroupfs"))) Version = "v1.19.0" config.Cmd("127.0.0.1", "echo \""+string(JoinTemplate("", "systemd"))+"\" > ~/aa") diff --git a/install/init.go b/install/init.go index de19e964d50..deadb504d4b 100644 --- a/install/init.go +++ b/install/init.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -14,7 +28,7 @@ import ( "github.com/fanux/sealos/cert" "github.com/fanux/sealos/net" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) //BuildInit is @@ -31,7 +45,7 @@ func BuildInit() { Masters: masters, Nodes: nodes, Network: Network, - ApiServer: ApiServer, + APIServer: APIServer, } i.CheckValid() i.Print() @@ -69,7 +83,7 @@ func (s *SealosInstaller) getCgroupDriverFromShell(h string) string { } output = strings.TrimSpace(output) logger.Info("cgroup driver is %s", output) - return output + return output } //KubeadmConfigInstall is @@ -96,11 +110,11 @@ func (s *SealosInstaller) KubeadmConfigInstall() { //读取模板数据 kubeadm := KubeadmDataFromYaml(templateData) if kubeadm != nil { - DnsDomain = kubeadm.Networking.DnsDomain - ApiServerCertSANs = kubeadm.ApiServer.CertSANs + DNSDomain = kubeadm.Networking.DNSDomain + APIServerCertSANs = kubeadm.APIServer.CertSANs } else { logger.Warn("decode certSANs from config failed, using default SANs") - ApiServerCertSANs = getDefaultSANs() + APIServerCertSANs = getDefaultSANs() } } @@ -111,14 +125,14 @@ func getDefaultSANs() []string { sans = append(sans, CertSANS...) } for _, master := range MasterIPs { - sans = append(sans, IpFormat(master)) + sans = append(sans, IPFormat(master)) } return sans } -func (s *SealosInstaller) appendApiServer() error { +func (s *SealosInstaller) appendAPIServer() error { etcHostPath := "/etc/hosts" - etcHostMap := fmt.Sprintf("%s %s", IpFormat(s.Masters[0]), ApiServer) + etcHostMap := fmt.Sprintf("%s %s", IPFormat(s.Masters[0]), APIServer) file, err := os.OpenFile(etcHostPath, os.O_RDWR|os.O_APPEND, 0666) if err != nil { os.Exit(1) @@ -127,8 +141,8 @@ func (s *SealosInstaller) appendApiServer() error { reader := bufio.NewReader(file) for { str, err := reader.ReadString('\n') - if strings.Contains(str, ApiServer) { - logger.Info("local %s is already exists %s", etcHostPath, ApiServer) + if strings.Contains(str, APIServer) { + logger.Info("local %s is already exists %s", etcHostPath, APIServer) return nil } if err == io.EOF { @@ -136,14 +150,14 @@ func (s *SealosInstaller) appendApiServer() error { } } write := bufio.NewWriter(file) - write.WriteString(etcHostMap) + _, _ = write.WriteString(etcHostMap) return write.Flush() } func (s *SealosInstaller) GenerateCert() { //cert generator in sealos hostname := GetRemoteHostName(s.Masters[0]) - cert.GenerateCert(CertPath, CertEtcdPath, ApiServerCertSANs, IpFormat(s.Masters[0]), hostname, SvcCIDR, DnsDomain) + cert.GenerateCert(CertPath, CertEtcdPath, APIServerCertSANs, IPFormat(s.Masters[0]), hostname, SvcCIDR, DNSDomain) //copy all cert to master0 //CertSA(kye,pub) + CertCA(key,crt) //s.sendNewCertAndKey(s.Masters) @@ -158,7 +172,7 @@ func (s *SealosInstaller) CreateKubeconfig() { BaseName: "ca", } - controlPlaneEndpoint := fmt.Sprintf("https://%s:6443", ApiServer) + controlPlaneEndpoint := fmt.Sprintf("https://%s:6443", APIServer) err := cert.CreateJoinControlPlaneKubeConfigFiles(cert.SealosConfigDir, certConfig, hostname, controlPlaneEndpoint, "kubernetes") @@ -166,7 +180,6 @@ func (s *SealosInstaller) CreateKubeconfig() { logger.Error("generator kubeconfig failed %s", err) os.Exit(-1) } - } //InstallMaster0 is @@ -175,12 +188,12 @@ func (s *SealosInstaller) InstallMaster0() { s.sendNewCertAndKey([]string{s.Masters[0]}) // remote server run sealos init . it can not reach apiserver.cluster.local , should add masterip apiserver.cluster.local to /etc/hosts - err := s.appendApiServer() + err := s.appendAPIServer() if err != nil { - logger.Warn("append %s %s to /etc/hosts err: %s", IpFormat(s.Masters[0]), ApiServer, err) + logger.Warn("append %s %s to /etc/hosts err: %s", IPFormat(s.Masters[0]), APIServer, err) } //master0 do sth - cmd := fmt.Sprintf("grep -qF '%s %s' /etc/hosts || echo %s %s >> /etc/hosts", IpFormat(s.Masters[0]), ApiServer, IpFormat(s.Masters[0]), ApiServer) + cmd := fmt.Sprintf("grep -qF '%s %s' /etc/hosts || echo %s %s >> /etc/hosts", IPFormat(s.Masters[0]), APIServer, IPFormat(s.Masters[0]), APIServer) _ = SSHConfig.CmdAsync(s.Masters[0], cmd) cmd = s.Command(Version, InitMaster) @@ -204,7 +217,7 @@ func (s *SealosInstaller) InstallMaster0() { // can-reach is used by calico multi network , flannel has nothing to add. just Use it. if k8s.IsIpv4(Interface) && Network == net.CALICO { Interface = "can-reach=" + Interface - } else if !k8s.IsIpv4(Interface) && Network == net.CALICO { + } else if !k8s.IsIpv4(Interface) && Network == net.CALICO { //nolint:gofmt Interface = "interface=" + Interface } @@ -228,13 +241,13 @@ func (s *SealosInstaller) InstallMaster0() { IPIP: !BGP, MTU: MTU, CniRepo: Repo, - K8sServiceHost: s.ApiServer, + K8sServiceHost: s.APIServer, Version: cniVersion, }).Manifests("") logger.Debug("cni yaml : \n", netyaml) home := cert.GetUserHomeDir() configYamlDir := filepath.Join(home, ".sealos", "cni.yaml") - ioutil.WriteFile(configYamlDir, []byte(netyaml), 0755) + _ = ioutil.WriteFile(configYamlDir, []byte(netyaml), 0755) SSHConfig.Copy(s.Masters[0], configYamlDir, "/tmp/cni.yaml") SSHConfig.Cmd(s.Masters[0], "kubectl apply -f /tmp/cni.yaml") } @@ -264,12 +277,12 @@ func (s *SealosInstaller) to11911192(masters []string) (to11911192 bool) { // fix > 1.19.1 kube-controller-manager and kube-scheduler use the LocalAPIEndpoint instead of the ControlPlaneEndpoint. if VersionToIntAll(Version) >= 1191 && VersionToIntAll(Version) <= 1192 { for _, v := range masters { - ip := IpFormat(v) + ip := IPFormat(v) // use grep -qF if already use sed then skip.... cmd := fmt.Sprintf(`grep -qF "apiserver.cluster.local" %s && \ sed -i 's/apiserver.cluster.local/%s/' %s && \ sed -i 's/apiserver.cluster.local/%s/' %s`, KUBESCHEDULERCONFIGFILE, ip, KUBECONTROLLERCONFIGFILE, ip, KUBESCHEDULERCONFIGFILE) - SSHConfig.CmdAsync(v, cmd) + _ = SSHConfig.CmdAsync(v, cmd) } to11911192 = true } else { diff --git a/install/interface.go b/install/interface.go index 061a30c2782..481995b0eeb 100644 --- a/install/interface.go +++ b/install/interface.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install type Check interface { diff --git a/install/join.go b/install/join.go index f08fb529f60..5ce0964a7d6 100644 --- a/install/join.go +++ b/install/join.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -7,7 +21,7 @@ import ( "github.com/fanux/sealos/cert" "github.com/fanux/sealos/ipvs" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) //BuildJoin is @@ -28,7 +42,7 @@ func joinMastersFunc(joinMasters []string) { Masters: masters, Nodes: nodes, Network: Network, - ApiServer: ApiServer, + APIServer: APIServer, } i.CheckValid() i.SendSealos() @@ -38,7 +52,6 @@ func joinMastersFunc(joinMasters []string) { //master join to MasterIPs MasterIPs = append(MasterIPs, joinMasters...) i.lvscare() - } //joinNodesFunc is join nodes func @@ -82,7 +95,7 @@ func (s *SealosInstaller) GeneratorToken() { // 返回/etc/hosts记录 func getApiserverHost(ipAddr string) (host string) { - return fmt.Sprintf("%s %s", ipAddr, ApiServer) + return fmt.Sprintf("%s %s", ipAddr, APIServer) } // sendJoinCPConfig send join CP nodes configuration @@ -93,7 +106,7 @@ func (s *SealosInstaller) sendJoinCPConfig(joinMaster []string) { go func(master string) { defer wg.Done() cgroup := s.getCgroupDriverFromShell(master) - templateData := string(JoinTemplate(IpFormat(master), cgroup)) + templateData := string(JoinTemplate(IPFormat(master), cgroup)) cmd := fmt.Sprintf(`echo "%s" > /root/kubeadm-join-config.yaml`, templateData) _ = SSHConfig.CmdAsync(master, cmd) }(master) @@ -117,14 +130,14 @@ func (s *SealosInstaller) JoinMasters(masters []string) { go func(master string) { defer wg.Done() hostname := GetRemoteHostName(master) - certCMD := cert.CMD(ApiServerCertSANs, IpFormat(master), hostname, SvcCIDR, DnsDomain) + certCMD := cert.CMD(APIServerCertSANs, IPFormat(master), hostname, SvcCIDR, DNSDomain) _ = SSHConfig.CmdAsync(master, certCMD) - cmdHosts := fmt.Sprintf("echo %s >> /etc/hosts", getApiserverHost(IpFormat(s.Masters[0]))) + cmdHosts := fmt.Sprintf("echo %s >> /etc/hosts", getApiserverHost(IPFormat(s.Masters[0]))) _ = SSHConfig.CmdAsync(master, cmdHosts) // cmdMult := fmt.Sprintf("%s --apiserver-advertise-address %s", cmd, IpFormat(master)) _ = SSHConfig.CmdAsync(master, cmd) - cmdHosts = fmt.Sprintf(`sed "s/%s/%s/g" -i /etc/hosts`, getApiserverHost(IpFormat(s.Masters[0])), getApiserverHost(IpFormat(master))) + cmdHosts = fmt.Sprintf(`sed "s/%s/%s/g" -i /etc/hosts`, getApiserverHost(IPFormat(s.Masters[0])), getApiserverHost(IPFormat(master))) _ = SSHConfig.CmdAsync(master, cmdHosts) copyk8sConf := `rm -rf .kube/config && mkdir -p /root/.kube && cp /etc/kubernetes/admin.conf /root/.kube/config && chmod 600 /root/.kube/config` _ = SSHConfig.CmdAsync(master, copyk8sConf) @@ -140,7 +153,7 @@ func (s *SealosInstaller) JoinNodes() { var masters string var wg sync.WaitGroup for _, master := range s.Masters { - masters += fmt.Sprintf(" --rs %s:6443", IpFormat(master)) + masters += fmt.Sprintf(" --rs %s:6443", IPFormat(master)) } ipvsCmd := fmt.Sprintf("sealos ipvs --vs %s:6443 %s --health-path /healthz --health-schem https --run-once", VIP, masters) for _, node := range s.Nodes { @@ -153,15 +166,15 @@ func (s *SealosInstaller) JoinNodes() { cmdJoinConfig := fmt.Sprintf(`echo "%s" > /root/kubeadm-join-config.yaml`, templateData) _ = SSHConfig.CmdAsync(node, cmdJoinConfig) - cmdHosts := fmt.Sprintf("echo %s %s >> /etc/hosts", VIP, ApiServer) + cmdHosts := fmt.Sprintf("echo %s %s >> /etc/hosts", VIP, APIServer) _ = SSHConfig.CmdAsync(node, cmdHosts) // 如果不是默认路由, 则添加 vip 到 master的路由。 - cmdRoute := fmt.Sprintf("sealos route --host %s", IpFormat(node)) + cmdRoute := fmt.Sprintf("sealos route --host %s", IPFormat(node)) status := SSHConfig.CmdToString(node, cmdRoute, "") if status != "ok" { // 以自己的ip作为路由网关 - addRouteCmd := fmt.Sprintf("sealos route add --host %s --gateway %s", VIP, IpFormat(node)) + addRouteCmd := fmt.Sprintf("sealos route add --host %s --gateway %s", VIP, IPFormat(node)) SSHConfig.CmdToString(node, addRouteCmd, "") } diff --git a/install/print.go b/install/print.go index a78ebd87ff2..d8e4cb7ab2f 100644 --- a/install/print.go +++ b/install/print.go @@ -1,17 +1,31 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( "encoding/json" "strings" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) //Print is func (s *SealosInstaller) Print(process ...string) { if len(process) == 0 { - configJson, _ := json.Marshal(s) - logger.Info("\n[globals]sealos config is: ", string(configJson)) + configJSON, _ := json.Marshal(s) + logger.Info("\n[globals]sealos config is: ", string(configJSON)) } else { var sb strings.Builder for _, v := range process { @@ -20,7 +34,6 @@ func (s *SealosInstaller) Print(process ...string) { } logger.Debug(sb.String()) } - } func (s *SealosInstaller) PrintFinish() { logger.Info("sealos install success.") diff --git a/install/route.go b/install/route.go index ff9c0483095..82b6b7ca89b 100644 --- a/install/route.go +++ b/install/route.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -20,7 +34,6 @@ func GetRouteFlag(host, gateway string) *RouteFlags { Host: host, Gateway: gateway, } - } func (r *RouteFlags) useHostCheckRoute() bool { @@ -33,12 +46,11 @@ func (r *RouteFlags) useGatewayManageRoute() bool { func (r *RouteFlags) CheckRoute() { if r.useHostCheckRoute() { - if isDefaultRouteIp(r.Host) { + if isDefaultRouteIP(r.Host) { fmt.Println("ok") return - } else { - fmt.Println("failed") } + fmt.Println("failed") } } @@ -61,17 +73,17 @@ func (r *RouteFlags) DelRoute() { } // getDefaultRouteIp is get host ip by ChooseHostInterface() . -func getDefaultRouteIp() (ip string, err error) { - netIp, err := k8snet.ChooseHostInterface() +func getDefaultRouteIP() (ip string, err error) { + netIP, err := k8snet.ChooseHostInterface() if err != nil { return "", err } - return netIp.String(), nil + return netIP.String(), nil } // isDefaultRouteIp return true if host equal default route ip host. -func isDefaultRouteIp(host string) bool { - ip, _ := getDefaultRouteIp() +func isDefaultRouteIP(host string) bool { + ip, _ := getDefaultRouteIP() return ip == host } diff --git a/install/sealos.go b/install/sealos.go index b9187f64099..c0a792d8430 100644 --- a/install/sealos.go +++ b/install/sealos.go @@ -1,10 +1,26 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( "fmt" "strings" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/net" + + "github.com/fanux/sealos/pkg/logger" ) type CleanCluster interface { @@ -44,7 +60,7 @@ type SealosInstaller struct { Masters []string Nodes []string Network string - ApiServer string + APIServer string } type CommandType string @@ -55,20 +71,19 @@ const JoinMaster CommandType = "joinMaster" const JoinNode CommandType = "joinNode" func (s *SealosInstaller) Command(version string, name CommandType) (cmd string) { - cmds := make(map[CommandType]string) // Please convert your v1beta1 configuration files to v1beta2 using the // "kubeadm config migrate" command of kubeadm v1.15.x, 因此1.14 版本不支持双网卡. - cmds = map[CommandType]string{ + commands := map[CommandType]string{ InitMaster: `kubeadm init --config=/root/kubeadm-config.yaml --experimental-upload-certs` + vlogToStr(), - JoinMaster: fmt.Sprintf("kubeadm join %s:6443 --token %s --discovery-token-ca-cert-hash %s --experimental-control-plane --certificate-key %s"+vlogToStr(), IpFormat(s.Masters[0]), JoinToken, TokenCaCertHash, CertificateKey), + JoinMaster: fmt.Sprintf("kubeadm join %s:6443 --token %s --discovery-token-ca-cert-hash %s --experimental-control-plane --certificate-key %s"+vlogToStr(), IPFormat(s.Masters[0]), JoinToken, TokenCaCertHash, CertificateKey), JoinNode: fmt.Sprintf("kubeadm join %s:6443 --token %s --discovery-token-ca-cert-hash %s"+vlogToStr(), VIP, JoinToken, TokenCaCertHash), } //other version >= 1.15.x //todo if VersionToInt(version) >= 115 { - cmds[InitMaster] = `kubeadm init --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr() - cmds[JoinMaster] = "kubeadm join --config=/root/kubeadm-join-config.yaml " + vlogToStr() - cmds[JoinNode] = "kubeadm join --config=/root/kubeadm-join-config.yaml " + vlogToStr() + commands[InitMaster] = `kubeadm init --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr() + commands[JoinMaster] = "kubeadm join --config=/root/kubeadm-join-config.yaml " + vlogToStr() + commands[JoinNode] = "kubeadm join --config=/root/kubeadm-join-config.yaml " + vlogToStr() } // version >= 1.16.x support kubeadm init --skip-phases=addon/kube-proxy @@ -76,15 +91,15 @@ func (s *SealosInstaller) Command(version string, name CommandType) (cmd string) // kubectl -n kube-system delete ds kube-proxy // # Run on each node: // iptables-restore <(iptables-save | grep -v KUBE) - if s.Network == "cilium" { + if s.Network == net.CILIUM { if VersionToInt(version) >= 116 { - cmds[InitMaster] = `kubeadm init --skip-phases=addon/kube-proxy --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr() + commands[InitMaster] = `kubeadm init --skip-phases=addon/kube-proxy --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr() } else { - cmds[InitMaster] = `kubeadm init --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr() + commands[InitMaster] = `kubeadm init --config=/root/kubeadm-config.yaml --upload-certs` + vlogToStr() } } - v, ok := cmds[name] + v, ok := commands[name] defer func() { if r := recover(); r != nil { logger.Error("[globals]fetch command error") @@ -132,7 +147,7 @@ func decodeJoinCmd(cmd string) { if strings.Contains(r, "--discovery-token-ca-cert-hash") { TokenCaCertHash = stringSlice[i+1] } - + if strings.Contains(r, "--certificate-key") { CertificateKey = stringSlice[i+1][:64] } @@ -140,5 +155,4 @@ func decodeJoinCmd(cmd string) { logger.Debug("[####]JoinToken :%s", JoinToken) logger.Debug("[####]TokenCaCertHash :%s", TokenCaCertHash) logger.Debug("[####]CertificateKey :%s", CertificateKey) - } diff --git a/install/send.go b/install/send.go index f0414bfb1dd..226f40851eb 100755 --- a/install/send.go +++ b/install/send.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -7,14 +21,13 @@ import ( //SendPackage is func (s *SealosInstaller) SendPackage() { - pkg := path.Base(PkgUrl) + pkg := path.Base(PkgURL) // rm old sealos in package avoid old version problem. if sealos not exist in package then skip rm kubeHook := fmt.Sprintf("cd /root && rm -rf kube && tar zxvf %s && cd /root/kube/shell && rm -f ../bin/sealos && bash init.sh", pkg) deletekubectl := `sed -i '/kubectl/d;/sealos/d' /root/.bashrc ` completion := "echo 'command -v kubectl &>/dev/null && source <(kubectl completion bash)' >> /root/.bashrc && echo '[ -x /usr/bin/sealos ] && source <(sealos completion bash)' >> /root/.bashrc && source /root/.bashrc" kubeHook = kubeHook + " && " + deletekubectl + " && " + completion - PkgUrl = SendPackage(PkgUrl, s.Hosts, "/root", nil, &kubeHook) - + PkgURL = SendPackage(PkgURL, s.Hosts, "/root", nil, &kubeHook) } // SendSealos is send the exec sealos to /usr/bin/sealos @@ -29,7 +42,7 @@ func (s *SealosInstaller) SendSealos() { // SendPackage is send new pkg to all nodes. func (u *SealosUpgrade) SendPackage() { all := append(u.Masters, u.Nodes...) - pkg := path.Base(u.NewPkgUrl) + pkg := path.Base(u.NewPkgURL) // rm old sealos in package avoid old version problem. if sealos not exist in package then skip rm var kubeHook string if For120(Version) { @@ -38,8 +51,6 @@ func (u *SealosUpgrade) SendPackage() { kubeHook = fmt.Sprintf("cd /root && rm -rf kube && tar zxvf %s && cd /root/kube/shell && rm -f ../bin/sealos && (ctr -n=k8s.io image import ../images/images.tar || true) && cp -f ../bin/* /usr/bin/ ", pkg) } else { kubeHook = fmt.Sprintf("cd /root && rm -rf kube && tar zxvf %s && cd /root/kube/shell && rm -f ../bin/sealos && (docker load -i ../images/images.tar || true) && cp -f ../bin/* /usr/bin/ ", pkg) - } - - PkgUrl = SendPackage(pkg, all, "/root", nil, &kubeHook) + PkgURL = SendPackage(pkg, all, "/root", nil, &kubeHook) } diff --git a/install/upgrade.go b/install/upgrade.go index 515ba4d705a..3e89eaef3ce 100644 --- a/install/upgrade.go +++ b/install/upgrade.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -8,13 +22,13 @@ import ( "k8s.io/client-go/kubernetes" "github.com/fanux/sealos/k8s" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) type SealosUpgrade struct { SealConfig NewVersion string - NewPkgUrl string + NewPkgURL string IPtoHostName map[string]string Client *kubernetes.Clientset } @@ -23,10 +37,10 @@ var ( upgradeSealos = &SealosUpgrade{} ) -func NewUpgrade(version, pkgUrl string) *SealosUpgrade { +func NewUpgrade(version, pkgURL string) *SealosUpgrade { u := upgradeSealos u.NewVersion = version - u.NewPkgUrl = pkgUrl + u.NewPkgURL = pkgURL // add ip -> hostname u.SetIPtoHostName() var err error @@ -38,13 +52,12 @@ func NewUpgrade(version, pkgUrl string) *SealosUpgrade { return u } -func ExitUpgradeCase(version, pkgUrl, cfgFile string) error { - if pkgUrl == "" || version == "" { +func ExitUpgradeCase(version, pkgURL, cfgFile string) error { + if pkgURL == "" || version == "" { return fmt.Errorf("version or pkg-url is required, Exit") - } - if pkgUrlCheck(pkgUrl) { - return fmt.Errorf("pkgurl %s check err, Exit", pkgUrl) + if pkgURLCheck(pkgURL) { + return fmt.Errorf("pkgurl %s check err, Exit", pkgURL) } if !FileExist(k8s.KubeDefaultConfigPath) { return fmt.Errorf("KubeDefaultConfigPath %s is not exist, Exit", k8s.KubeDefaultConfigPath) @@ -68,7 +81,7 @@ func (u *SealosUpgrade) SetUP() { } // store latest version and pkgUrl Version = u.NewVersion - PkgUrl = u.NewPkgUrl + PkgURL = u.NewPkgURL } // UpgradeMaster0 is upgrade master first. @@ -90,7 +103,6 @@ func (u *SealosUpgrade) UpgradeOtherMaster() { logger.Info("UpgradeOtherMasters") hostnames := u.GetHostNamesFromIps(u.Masters[1:]) u.upgradeNodes(hostnames, true) - } func (u *SealosUpgrade) upgradeNodes(hostnames []string, isMaster bool) { @@ -100,7 +112,7 @@ func (u *SealosUpgrade) upgradeNodes(hostnames []string, isMaster bool) { wg.Add(1) go func(node string) { defer wg.Done() - ip := u.GetIpByHostname(node) + ip := u.GetIPByHostname(node) // drain worker node is too danger for prod use; do not drain nodes if worker nodes~ if isMaster { logger.Info("[%s] first: to drain master node %s", ip, node) @@ -154,9 +166,7 @@ func (u *SealosUpgrade) upgradeNodes(hostnames []string, isMaster bool) { } else { logger.Error("fourth: %s nodes is not ready, please check the nodes logs to find out reason", node) } - }(hostname) - } wg.Wait() } @@ -183,7 +193,7 @@ func (u *SealosUpgrade) GetHostNamesFromIps(ips []string) []string { return hostnames } -func (u *SealosUpgrade) GetIpByHostname(host string) string { +func (u *SealosUpgrade) GetIPByHostname(host string) string { for nip, hostname := range u.IPtoHostName { if host == hostname { return nip diff --git a/install/upgrade_pool.go b/install/upgrade_pool.go index 02907d530b5..05b4d6389a5 100644 --- a/install/upgrade_pool.go +++ b/install/upgrade_pool.go @@ -1,23 +1,37 @@ +// Copyright © 2021 sealos. +// +// 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 install import "sync" -type uPool struct { +type Pool struct { queue chan int wg *sync.WaitGroup } -func NewPool(size int) *uPool { +func NewPool(size int) *Pool { if size <= 1 { size = 1 } - return &uPool{ + return &Pool{ queue: make(chan int, size), wg: &sync.WaitGroup{}, } } -func (p *uPool) Add(delta int) { +func (p *Pool) Add(delta int) { for i := 0; i < delta; i++ { p.queue <- 1 } @@ -27,11 +41,11 @@ func (p *uPool) Add(delta int) { p.wg.Add(delta) } -func (p *uPool) Done() { +func (p *Pool) Done() { <-p.queue p.wg.Done() } -func (p *uPool) Wait() { +func (p *Pool) Wait() { p.wg.Wait() } diff --git a/install/utils.go b/install/utils.go index 7dfc05347f8..576065d50d9 100644 --- a/install/utils.go +++ b/install/utils.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -19,7 +33,7 @@ import ( "strings" "time" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) var message string @@ -44,18 +58,18 @@ func ExitInitCase() bool { return true } - return pkgUrlCheck(PkgUrl) + return pkgURLCheck(PkgURL) } -func ExitDeleteCase(pkgUrl string) bool { +func ExitDeleteCase(pkgURL string) bool { if PackageConfig != "" && !FileExist(PackageConfig) { logger.Error("your APP pkg-config File is not exist, Please check your pkg-config is exist") return true } - return pkgUrlCheck(pkgUrl) + return pkgURLCheck(pkgURL) } -func ExitInstallCase(pkgUrl string) bool { +func ExitInstallCase(pkgURL string) bool { // values.yaml 使用了-f 但是文件不存在. 并且不使用 stdin if Values != "-" && !FileExist(Values) && Values != "" { logger.Error("your values File is not exist and you have no stdin input, Please check your Values.yaml is exist") @@ -66,28 +80,28 @@ func ExitInstallCase(pkgUrl string) bool { logger.Error("your install APP pkg-config File is not exist, Please check your pkg-config is exist") return true } - return pkgUrlCheck(pkgUrl) + return pkgURLCheck(pkgURL) } -func pkgUrlCheck(pkgUrl string) bool { - if !strings.HasPrefix(pkgUrl, "http") && !FileExist(pkgUrl) { +func pkgURLCheck(pkgURL string) bool { + if !strings.HasPrefix(pkgURL, "http") && !FileExist(pkgURL) { message = ErrorFileNotExist logger.Error(message + "please check where your PkgUrl is right?") return true } // 判断PkgUrl, 有http前缀时, 下载的文件如果小于400M ,则报错. - return strings.HasPrefix(pkgUrl, "http") && !downloadFileCheck(pkgUrl) + return strings.HasPrefix(pkgURL, "http") && !downloadFileCheck(pkgURL) } -func downloadFileCheck(pkgUrl string) bool { - u, err := url.Parse(pkgUrl) +func downloadFileCheck(pkgURL string) bool { + u, err := url.Parse(pkgURL) if err != nil { return false } if u != nil { req, err := http.NewRequest("GET", u.String(), nil) if err != nil { - logger.Error(ErrorPkgUrlNotExist, "please check where your PkgUrl is right?") + logger.Error(ErrorPkgURLNotExist, "please check where your PkgUrl is right?") return false } client := &http.Client{ @@ -147,8 +161,8 @@ func VersionToIntAll(version string) int { return 0 } -//IpFormat is -func IpFormat(host string) string { +//IPFormat is +func IPFormat(host string) string { ipAndPort := strings.Split(host, ":") if len(ipAndPort) != 2 { logger.Error("invalied host fomat [%s], must like 172.0.0.2:22", host) @@ -428,7 +442,6 @@ func CompressZip(fileDir string, outputPath string) error { compress(rel, path, w) return nil }) - } func compress(rel string, path string, zw *zip.Writer) { @@ -437,7 +450,7 @@ func compress(rel string, path string, zw *zip.Writer) { header, _ := zip.FileInfoHeader(info) header.Name = rel writer, _ := zw.CreateHeader(header) - io.Copy(writer, file) + _, _ = io.Copy(writer, file) defer file.Close() } @@ -495,9 +508,6 @@ func For120(version string) bool { if newMajor >= 120 { logger.Info("install version is: %s, Use kubeadm v1beta2 InitConfig,OCI use containerd instead", version) return true - } else { - //logger.Info("install version is: %s, Use kubeadm v1beta1 InitConfig, docker", version) - return false } - + return false } diff --git a/install/utils_test.go b/install/utils_test.go index a81a2677643..5ca2ace6669 100644 --- a/install/utils_test.go +++ b/install/utils_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( @@ -9,7 +23,7 @@ import ( "testing" "time" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) func TestPath(t *testing.T) { @@ -253,10 +267,10 @@ func Test_Example(t *testing.T) { func Test_Cmd(t *testing.T) { tmpcmd := "cat /tmp/tmp.json" host := "192.168.218.97" - var cniVersion string + var cniVersion string var metajson string var tmpdata metadata - + SSHConfig.User = "louis" SSHConfig.Password = "210010" @@ -269,4 +283,4 @@ func Test_Cmd(t *testing.T) { Network = tmpdata.CniName } fmt.Println(cniVersion, Network) -} \ No newline at end of file +} diff --git a/install/vars.go b/install/vars.go index 57ac8a05ba7..5908f370585 100644 --- a/install/vars.go +++ b/install/vars.go @@ -1,10 +1,24 @@ +// Copyright © 2021 sealos. +// +// 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 install import ( "regexp" "strconv" - "github.com/fanux/lvscare/care" + "github.com/sealyun/lvscare/care" "github.com/fanux/sealos/cert" "github.com/fanux/sealos/ipvs" @@ -12,29 +26,25 @@ import ( ) var ( - MasterIPs []string - NodeIPs []string - CertSANS []string - //config from kubeadm.cfg - DnsDomain string - ApiServerCertSANs []string - // - SSHConfig sshutil.SSH - ApiServer string - //cert abs path - CertPath = cert.SealosConfigDir + "/pki" - CertEtcdPath = cert.SealosConfigDir + "/pki/etcd" - EtcdCacart = cert.SealosConfigDir + "/pki/etcd/ca.crt" - EtcdCert = cert.SealosConfigDir + "/pki/etcd/healthcheck-client.crt" - EtcdKey = cert.SealosConfigDir + "/pki/etcd/healthcheck-client.key" - - //criSocket + MasterIPs []string + NodeIPs []string + CertSANS []string + DNSDomain string + APIServerCertSANs []string + SSHConfig sshutil.SSH + APIServer string + CertPath = cert.SealosConfigDir + "/pki" + CertEtcdPath = cert.SealosConfigDir + "/pki/etcd" + EtcdCacart = cert.SealosConfigDir + "/pki/etcd/ca.crt" + EtcdCert = cert.SealosConfigDir + "/pki/etcd/healthcheck-client.crt" + EtcdKey = cert.SealosConfigDir + "/pki/etcd/healthcheck-client.key" + CriSocket string CgroupDriver string - KubeadmApi string + KubeadmAPI string VIP string - PkgUrl string + PkgURL string Version string Repo string PodCIDR string @@ -45,20 +55,19 @@ var ( Values string // values for install package values.yaml WorkDir string // workdir for install/delete package home - // Ipvs care.LvsCare LvscareImage ipvs.LvscareImage KubeadmFile string - // network type, calico or flannel etc.. - Network string - // if true don't install cni plugin - WithoutCNI bool - //network interface name, like "eth.*|en.*" - Interface string - // the ipip mode of the calico - BGP bool - // mtu size - MTU string + + Network string // network type, calico or flannel etc.. + + WithoutCNI bool // if true don't install cni plugin + + Interface string //network interface name, like "eth.*|en.*" + + BGP bool // the ipip mode of the calico + + MTU string // mtu size YesRx = regexp.MustCompile("^(?i:y(?:es)?)$") @@ -67,15 +76,13 @@ var ( Vlog int - // etcd backup InDocker bool SnapshotName string EtcdBackDir string RestorePath string - // oss OssEndpoint string - AccessKeyId string + AccessKeyID string AccessKeySecrets string BucketName string ObjectPath string diff --git a/ipvs/lvscare.go b/ipvs/lvscare.go index aac91906db1..8c4d4104f62 100644 --- a/ipvs/lvscare.go +++ b/ipvs/lvscare.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 ipvs import ( @@ -9,7 +23,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/scheme" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) type LvscareImage struct { diff --git a/ipvs/lvscare_test.go b/ipvs/lvscare_test.go index b154f95ea81..f42e7439675 100644 --- a/ipvs/lvscare_test.go +++ b/ipvs/lvscare_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 ipvs import ( diff --git a/k8s/drain.go b/k8s/drain.go index 5b2ec77cfe7..f5449c4557e 100644 --- a/k8s/drain.go +++ b/k8s/drain.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 k8s import ( @@ -9,7 +23,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) const ( @@ -61,7 +75,7 @@ func CordonUnCordon(k8sClient *kubernetes.Clientset, nodeName string, cordoned b node.Spec.Unschedulable = cordoned _, err = k8sClient.CoreV1().Nodes().Update(context.TODO(), node, metav1.UpdateOptions{}) if err != nil { - return fmt.Errorf("Error setting cordoned state for %s node err: %v", nodeName, err) + return fmt.Errorf("error setting cordoned state for %s node err: %v", nodeName, err) } return nil } diff --git a/k8s/node.go b/k8s/node.go index 4d74f281b44..a4fd54c3ad3 100644 --- a/k8s/node.go +++ b/k8s/node.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 k8s import ( @@ -73,8 +87,8 @@ func GetNodeListByLabel(k8sClient *kubernetes.Clientset, label string) (*v1.Node return k8sClient.CoreV1().Nodes().List(context.TODO(), *listOption) } -// GetNodeIpByName is get node internalIp by nodeName -func GetNodeIpByName(k8sClient *kubernetes.Clientset, nodeName string) (ip string, err error) { +// GetNodeIPByName is get node internalIp by nodeName +func GetNodeIPByName(k8sClient *kubernetes.Clientset, nodeName string) (ip string, err error) { node, err := k8sClient.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{}) if err != nil { return "", err @@ -89,41 +103,8 @@ func GetNodeIpByName(k8sClient *kubernetes.Clientset, nodeName string) (ip strin return "", apierrors.NewNotFound(schema.GroupResource{}, nodeName) } -// GetNodeNameByIp is get node name by node ip -func GetNodeNameByIp(k8sClient *kubernetes.Clientset, ip string) (name string, err error) { - nodes, err := GetNodeList(k8sClient) - if err != nil { - return "", err - } - for _, node := range nodes.Items { - for _, v := range node.Status.Addresses { - if v.Type == v1.NodeInternalIP && ip == v.Address { - return node.Name, nil - } - } - } - return "", fmt.Errorf("ip [%s] is not fount in kubernetes nodes", ip) -} - -// GetNodeNameByLabel is get node name by label -func GetNodeNameByLabel(k8sClient *kubernetes.Clientset, label string) ([]string, error) { - var ns []string - nodes, err := GetNodeListByLabel(k8sClient, label) - if err != nil { - return nil, err - } - for _, node := range nodes.Items { - ns = append(ns, node.Name) - } - if len(ns) != 0 { - return ns, nil - } - - return nil, fmt.Errorf("label %s is not fount in kubernetes nodes", label) -} - -// GetNodeIpByLabel is is get node ip by label -func GetNodeIpByLabel(k8sClient *kubernetes.Clientset, label string) ([]string, error) { +// GetNodeIPByLabel is is get node ip by label +func GetNodeIPByLabel(k8sClient *kubernetes.Clientset, label string) ([]string, error) { var ips []string if label == "" { return ips, nil @@ -164,14 +145,14 @@ func IsNodeReady(node v1.Node) bool { // TransToIP is use kubernetes label or hostname/ip to get ip func TransToIP(k8sClient *kubernetes.Clientset, label string, hostname []string) ([]string, error) { var ips []string - ips, err := GetNodeIpByLabel(k8sClient, label) + ips, err := GetNodeIPByLabel(k8sClient, label) if err != nil { return nil, err } - resHost, resIp := getHostnameAndIp(hostname) - ips = append(ips, resIp...) + resHost, resIP := getHostnameAndIP(hostname) + ips = append(ips, resIP...) for _, node := range resHost { - ip, err := GetNodeIpByName(k8sClient, node) + ip, err := GetNodeIPByName(k8sClient, node) if err == nil { ips = append(ips, ip) } diff --git a/k8s/utlis.go b/k8s/utlis.go index 6412d4f951b..0d6de01c9f4 100644 --- a/k8s/utlis.go +++ b/k8s/utlis.go @@ -1,11 +1,25 @@ +// Copyright © 2021 sealos. +// +// 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 k8s import ( "strings" ) -func getHostnameAndIp(node []string) ([]string, []string) { - var resHost, resIp []string +func getHostnameAndIP(node []string) ([]string, []string) { + var resHost, resIP []string if len(node) == 0 { return node, node } @@ -13,10 +27,10 @@ func getHostnameAndIp(node []string) ([]string, []string) { if !IsIpv4(n) { resHost = append(resHost, n) } else { - resIp = append(resIp, n) + resIP = append(resIP, n) } } - return resHost, resIp + return resHost, resIP } func IsIpv4(ip string) bool { diff --git a/k8s/utlis_test.go b/k8s/utlis_test.go index bbb84600bcc..575a5ebce97 100644 --- a/k8s/utlis_test.go +++ b/k8s/utlis_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 k8s import ( @@ -165,7 +179,7 @@ func Benchmark_removeRep(b *testing.B) { func BenchmarkIsIpv4(b *testing.B) { b.ResetTimer() - origin := "192.168.00.1" + const origin = "192.168.00.1" for i := 0; i < b.N; i++ { IsIpv4(origin) } @@ -175,7 +189,7 @@ func BenchmarkIsIpv42(b *testing.B) { b.ResetTimer() origin := "192.168.00.1" for i := 0; i < b.N; i++ { - _, _ = regexp.MatchString("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}", origin) + _, _ = regexp.MatchString("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}", origin) //nolint:staticcheck } } diff --git a/main.go b/main.go index 284b9eab214..161f2c20ff5 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -// Copyright © 2019 NAME HERE +// Copyright © 2021 sealos. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/net/calico.go b/net/calico.go index 7cdf2a61b2f..a3a3ad541c8 100644 --- a/net/calico.go +++ b/net/calico.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 net type Calico struct { @@ -15,798 +29,24 @@ func (c Calico) Manifests(template string) string { c.metadata.CIDR = defaultCIDR } - if c.metadata.CniRepo == "" || c.metadata.CniRepo == "k8s.gcr.io" { + if c.metadata.CniRepo == "" || c.metadata.CniRepo == defaultCNIRepo { c.metadata.CniRepo = "calico" } - if c.metadata.Version == "" { - c.metadata.Version = "v3.8.2" - } + if c.metadata.Version == "" { + c.metadata.Version = "v3.8.2" + } return render(c.metadata, template) } func (c Calico) Template() string { - switch c.metadata.Version { - case "v3.19.1": - return CalicoV3191Manifests - case"v3.8.2": - return CalicoManifests - default: - return CalicoManifests - } + switch c.metadata.Version { + case "v3.19.1": + return CalicoV3191Manifests + case "v3.8.2": + return CalicoManifests + default: + return CalicoManifests + } } - -const CalicoManifests = ` ---- -# Source: calico/templates/calico-config.yaml -# This ConfigMap is used to configure a self-hosted Calico installation. -kind: ConfigMap -apiVersion: v1 -metadata: - name: calico-config - namespace: kube-system -data: - # Typha is disabled. - typha_service_name: "none" - # Configure the backend to use. - calico_backend: "bird" - - # Configure the MTU to use - veth_mtu: "{{ .MTU }}" - - # The CNI network configuration to install on each node. The special - # values in this config will be automatically populated. - cni_network_config: |- - { - "name": "k8s-pod-network", - "cniVersion": "0.3.1", - "plugins": [ - { - "type": "calico", - "log_level": "info", - "datastore_type": "kubernetes", - "nodename": "__KUBERNETES_NODE_NAME__", - "mtu": __CNI_MTU__, - "ipam": { - "type": "calico-ipam" - }, - "policy": { - "type": "k8s" - }, - "kubernetes": { - "kubeconfig": "__KUBECONFIG_FILEPATH__" - } - }, - { - "type": "portmap", - "snat": true, - "capabilities": {"portMappings": true} - } - ] - } ---- -# Source: calico/templates/kdd-crds.yaml -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: felixconfigurations.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: FelixConfiguration - plural: felixconfigurations - singular: felixconfiguration ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: ipamblocks.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: IPAMBlock - plural: ipamblocks - singular: ipamblock - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: blockaffinities.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: BlockAffinity - plural: blockaffinities - singular: blockaffinity - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: ipamhandles.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: IPAMHandle - plural: ipamhandles - singular: ipamhandle - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: ipamconfigs.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: IPAMConfig - plural: ipamconfigs - singular: ipamconfig - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: bgppeers.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: BGPPeer - plural: bgppeers - singular: bgppeer - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: bgpconfigurations.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: BGPConfiguration - plural: bgpconfigurations - singular: bgpconfiguration - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: ippools.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: IPPool - plural: ippools - singular: ippool - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: hostendpoints.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: HostEndpoint - plural: hostendpoints - singular: hostendpoint - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: clusterinformations.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: ClusterInformation - plural: clusterinformations - singular: clusterinformation - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: globalnetworkpolicies.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: GlobalNetworkPolicy - plural: globalnetworkpolicies - singular: globalnetworkpolicy - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: globalnetworksets.crd.projectcalico.org -spec: - scope: Cluster - group: crd.projectcalico.org - version: v1 - names: - kind: GlobalNetworkSet - plural: globalnetworksets - singular: globalnetworkset - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: networkpolicies.crd.projectcalico.org -spec: - scope: Namespaced - group: crd.projectcalico.org - version: v1 - names: - kind: NetworkPolicy - plural: networkpolicies - singular: networkpolicy - ---- - -apiVersion: apiextensions.k8s.io/v1beta1 -kind: CustomResourceDefinition -metadata: - name: networksets.crd.projectcalico.org -spec: - scope: Namespaced - group: crd.projectcalico.org - version: v1 - names: - kind: NetworkSet - plural: networksets - singular: networkset ---- -# Source: calico/templates/rbac.yaml - -# Include a clusterrole for the kube-controllers component, -# and bind it to the calico-kube-controllers serviceaccount. -kind: ClusterRole -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: calico-kube-controllers -rules: - # Nodes are watched to monitor for deletions. - - apiGroups: [""] - resources: - - nodes - verbs: - - watch - - list - - get - # Pods are queried to check for existence. - - apiGroups: [""] - resources: - - pods - verbs: - - get - # IPAM resources are manipulated when nodes are deleted. - - apiGroups: ["crd.projectcalico.org"] - resources: - - ippools - verbs: - - list - - apiGroups: ["crd.projectcalico.org"] - resources: - - blockaffinities - - ipamblocks - - ipamhandles - verbs: - - get - - list - - create - - update - - delete - # Needs access to update clusterinformations. - - apiGroups: ["crd.projectcalico.org"] - resources: - - clusterinformations - verbs: - - get - - create - - update ---- -kind: ClusterRoleBinding -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: calico-kube-controllers -roleRef: - apiGroup: rbac.authorization.k8s.io - kind: ClusterRole - name: calico-kube-controllers -subjects: -- kind: ServiceAccount - name: calico-kube-controllers - namespace: kube-system ---- -# Include a clusterrole for the calico-node DaemonSet, -# and bind it to the calico-node serviceaccount. -kind: ClusterRole -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: calico-node -rules: - # The CNI plugin needs to get pods, nodes, and namespaces. - - apiGroups: [""] - resources: - - pods - - nodes - - namespaces - verbs: - - get - - apiGroups: [""] - resources: - - endpoints - - services - verbs: - # Used to discover service IPs for advertisement. - - watch - - list - # Used to discover Typhas. - - get - - apiGroups: [""] - resources: - - nodes/status - verbs: - # Needed for clearing NodeNetworkUnavailable flag. - - patch - # Calico stores some configuration information in node annotations. - - update - # Watch for changes to Kubernetes NetworkPolicies. - - apiGroups: ["networking.k8s.io"] - resources: - - networkpolicies - verbs: - - watch - - list - # Used by Calico for policy information. - - apiGroups: [""] - resources: - - pods - - namespaces - - serviceaccounts - verbs: - - list - - watch - # The CNI plugin patches pods/status. - - apiGroups: [""] - resources: - - pods/status - verbs: - - patch - # Calico monitors various CRDs for config. - - apiGroups: ["crd.projectcalico.org"] - resources: - - globalfelixconfigs - - felixconfigurations - - bgppeers - - globalbgpconfigs - - bgpconfigurations - - ippools - - ipamblocks - - globalnetworkpolicies - - globalnetworksets - - networkpolicies - - networksets - - clusterinformations - - hostendpoints - verbs: - - get - - list - - watch - # Calico must create and update some CRDs on startup. - - apiGroups: ["crd.projectcalico.org"] - resources: - - ippools - - felixconfigurations - - clusterinformations - verbs: - - create - - update - # Calico stores some configuration information on the node. - - apiGroups: [""] - resources: - - nodes - verbs: - - get - - list - - watch - # These permissions are only requried for upgrade from v2.6, and can - # be removed after upgrade or on fresh installations. - - apiGroups: ["crd.projectcalico.org"] - resources: - - bgpconfigurations - - bgppeers - verbs: - - create - - update - # These permissions are required for Calico CNI to perform IPAM allocations. - - apiGroups: ["crd.projectcalico.org"] - resources: - - blockaffinities - - ipamblocks - - ipamhandles - verbs: - - get - - list - - create - - update - - delete - - apiGroups: ["crd.projectcalico.org"] - resources: - - ipamconfigs - verbs: - - get - # Block affinities must also be watchable by confd for route aggregation. - - apiGroups: ["crd.projectcalico.org"] - resources: - - blockaffinities - verbs: - - watch - # The Calico IPAM migration needs to get daemonsets. These permissions can be - # removed if not upgrading from an installation using host-local IPAM. - - apiGroups: ["apps"] - resources: - - daemonsets - verbs: - - get ---- -apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRoleBinding -metadata: - name: calico-node -roleRef: - apiGroup: rbac.authorization.k8s.io - kind: ClusterRole - name: calico-node -subjects: -- kind: ServiceAccount - name: calico-node - namespace: kube-system - ---- -# Source: calico/templates/calico-node.yaml -# This manifest installs the calico-node container, as well -# as the CNI plugins and network config on -# each master and worker node in a Kubernetes cluster. -kind: DaemonSet -apiVersion: apps/v1 -metadata: - name: calico-node - namespace: kube-system - labels: - k8s-app: calico-node -spec: - selector: - matchLabels: - k8s-app: calico-node - updateStrategy: - type: RollingUpdate - rollingUpdate: - maxUnavailable: 1 - template: - metadata: - labels: - k8s-app: calico-node - annotations: - # This, along with the CriticalAddonsOnly toleration below, - # marks the pod as a critical add-on, ensuring it gets - # priority scheduling and that its resources are reserved - # if it ever gets evicted. - spec: - nodeSelector: - beta.kubernetes.io/os: linux - hostNetwork: true - tolerations: - # Make sure calico-node gets scheduled on all nodes. - - effect: NoSchedule - operator: Exists - # Mark the pod as a critical add-on for rescheduling. - - key: CriticalAddonsOnly - operator: Exists - - effect: NoExecute - operator: Exists - serviceAccountName: calico-node - # Minimize downtime during a rolling upgrade or deletion; tell Kubernetes to do a "force - # deletion": https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods. - terminationGracePeriodSeconds: 0 - priorityClassName: system-node-critical - initContainers: - # This container performs upgrade from host-local IPAM to calico-ipam. - # It can be deleted if this is a fresh installation, or if you have already - # upgraded to use calico-ipam. - - name: upgrade-ipam - image: {{ .CniRepo }}/cni:v3.8.2 - command: ["/opt/cni/bin/calico-ipam", "-upgrade"] - env: - - name: KUBERNETES_NODE_NAME - valueFrom: - fieldRef: - fieldPath: spec.nodeName - - name: CALICO_NETWORKING_BACKEND - valueFrom: - configMapKeyRef: - name: calico-config - key: calico_backend - volumeMounts: - - mountPath: /var/lib/cni/networks - name: host-local-net-dir - - mountPath: /host/opt/cni/bin - name: cni-bin-dir - # This container installs the CNI binaries - # and CNI network config file on each node. - - name: install-cni - image: {{ .CniRepo }}/cni:v3.8.2 - command: ["/install-cni.sh"] - env: - # Name of the CNI config file to create. - - name: CNI_CONF_NAME - value: "10-calico.conflist" - # The CNI network config to install on each node. - - name: CNI_NETWORK_CONFIG - valueFrom: - configMapKeyRef: - name: calico-config - key: cni_network_config - # Set the hostname based on the k8s node name. - - name: KUBERNETES_NODE_NAME - valueFrom: - fieldRef: - fieldPath: spec.nodeName - # CNI MTU Config variable - - name: CNI_MTU - valueFrom: - configMapKeyRef: - name: calico-config - key: veth_mtu - # Prevents the container from sleeping forever. - - name: SLEEP - value: "false" - volumeMounts: - - mountPath: /host/opt/cni/bin - name: cni-bin-dir - - mountPath: /host/etc/cni/net.d - name: cni-net-dir - # Adds a Flex Volume Driver that creates a per-pod Unix Domain Socket to allow Dikastes - # to communicate with Felix over the Policy Sync API. - - name: flexvol-driver - image: {{ .CniRepo }}/pod2daemon-flexvol:v3.8.2 - volumeMounts: - - name: flexvol-driver-host - mountPath: /host/driver - containers: - # Runs calico-node container on each Kubernetes node. This - # container programs network policy and routes on each - # host. - - name: calico-node - image: {{ .CniRepo }}/node:v3.8.2 - env: - # Use Kubernetes API as the backing datastore. - - name: DATASTORE_TYPE - value: "kubernetes" - # Wait for the datastore. - - name: WAIT_FOR_DATASTORE - value: "true" - # Set based on the k8s node name. - - name: NODENAME - valueFrom: - fieldRef: - fieldPath: spec.nodeName - # Choose the backend to use. - - name: CALICO_NETWORKING_BACKEND - valueFrom: - configMapKeyRef: - name: calico-config - key: calico_backend - # Cluster type to identify the deployment type - - name: CLUSTER_TYPE - value: "k8s,bgp" - # Auto-detect the BGP IP address. - - name: IP - value: "autodetect" - - name: IP_AUTODETECTION_METHOD - value: "{{ .Interface }}" - # Enable IPIP - - name: CALICO_IPV4POOL_IPIP - value: "{{if not .IPIP }}Off{{else}}Always{{end}}" - # Set MTU for tunnel device used if ipip is enabled - - name: FELIX_IPINIPMTU - valueFrom: - configMapKeyRef: - name: calico-config - key: veth_mtu - # The default IPv4 pool to create on startup if none exists. Pod IPs will be - # chosen from this range. Changing this value after installation will have - - name: CALICO_IPV4POOL_CIDR - value: "{{ .CIDR }}" - - name: CALICO_DISABLE_FILE_LOGGING - value: "true" - # Set Felix endpoint to host default action to ACCEPT. - - name: FELIX_DEFAULTENDPOINTTOHOSTACTION - value: "ACCEPT" - # Disable IPv6 on Kubernetes. - - name: FELIX_IPV6SUPPORT - value: "false" - # Set Felix logging to "info" - - name: FELIX_LOGSEVERITYSCREEN - value: "info" - - name: FELIX_HEALTHENABLED - value: "true" - securityContext: - privileged: true - resources: - requests: - cpu: 250m - livenessProbe: - httpGet: - path: /liveness - port: 9099 - host: localhost - periodSeconds: 10 - initialDelaySeconds: 10 - failureThreshold: 6 - readinessProbe: - exec: - command: - - /bin/calico-node - - -bird-ready - - -felix-ready - periodSeconds: 10 - volumeMounts: - - mountPath: /lib/modules - name: lib-modules - readOnly: true - - mountPath: /run/xtables.lock - name: xtables-lock - readOnly: false - - mountPath: /var/run/calico - name: var-run-calico - readOnly: false - - mountPath: /var/lib/calico - name: var-lib-calico - readOnly: false - - name: policysync - mountPath: /var/run/nodeagent - volumes: - # Used by calico-node. - - name: lib-modules - hostPath: - path: /lib/modules - - name: var-run-calico - hostPath: - path: /var/run/calico - - name: var-lib-calico - hostPath: - path: /var/lib/calico - - name: xtables-lock - hostPath: - path: /run/xtables.lock - type: FileOrCreate - # Used to install CNI. - - name: cni-bin-dir - hostPath: - path: /opt/cni/bin - - name: cni-net-dir - hostPath: - path: /etc/cni/net.d - # Mount in the directory for host-local IPAM allocations. This is - # used when upgrading from host-local to calico-ipam, and can be removed - # if not using the upgrade-ipam init container. - - name: host-local-net-dir - hostPath: - path: /var/lib/cni/networks - # Used to create per-pod Unix Domain Sockets - - name: policysync - hostPath: - type: DirectoryOrCreate - path: /var/run/nodeagent - # Used to install Flex Volume Driver - - name: flexvol-driver-host - hostPath: - type: DirectoryOrCreate - path: /usr/libexec/kubernetes/kubelet-plugins/volume/exec/nodeagent~uds ---- - -apiVersion: v1 -kind: ServiceAccount -metadata: - name: calico-node - namespace: kube-system - ---- -# Source: calico/templates/calico-kube-controllers.yaml - -# See https://github.com/projectcalico/kube-controllers -apiVersion: apps/v1 -kind: Deployment -metadata: - name: calico-kube-controllers - namespace: kube-system - labels: - k8s-app: calico-kube-controllers -spec: - # The controllers can only have a single active instance. - replicas: 1 - selector: - matchLabels: - k8s-app: calico-kube-controllers - strategy: - type: Recreate - template: - metadata: - name: calico-kube-controllers - namespace: kube-system - labels: - k8s-app: calico-kube-controllers - annotations: - spec: - nodeSelector: - beta.kubernetes.io/os: linux - tolerations: - # Mark the pod as a critical add-on for rescheduling. - - key: CriticalAddonsOnly - operator: Exists - - key: node-role.kubernetes.io/master - effect: NoSchedule - serviceAccountName: calico-kube-controllers - priorityClassName: system-cluster-critical - containers: - - name: calico-kube-controllers - image: {{ .CniRepo }}/kube-controllers:v3.8.2 - env: - # Choose which controllers to run. - - name: ENABLED_CONTROLLERS - value: node - - name: DATASTORE_TYPE - value: kubernetes - readinessProbe: - exec: - command: - - /usr/bin/check-status - - -r - ---- - -apiVersion: v1 -kind: ServiceAccount -metadata: - name: calico-kube-controllers - namespace: kube-system -` diff --git a/net/calico_yaml.go b/net/calico_yaml.go index 5c4cebdebd7..0f250bdd3ec 100644 --- a/net/calico_yaml.go +++ b/net/calico_yaml.go @@ -1,6 +1,20 @@ +// Copyright © 2021 sealos. +// +// 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 net -const CalicoV3191Manifests= ` +const CalicoV3191Manifests = ` --- # Source: calico/templates/calico-config.yaml # This ConfigMap is used to configure a self-hosted Calico installation. @@ -3870,4 +3884,778 @@ spec: selector: matchLabels: k8s-app: calico-kube-controllers -` \ No newline at end of file +` + +const CalicoManifests = ` +--- +# Source: calico/templates/calico-config.yaml +# This ConfigMap is used to configure a self-hosted Calico installation. +kind: ConfigMap +apiVersion: v1 +metadata: + name: calico-config + namespace: kube-system +data: + # Typha is disabled. + typha_service_name: "none" + # Configure the backend to use. + calico_backend: "bird" + + # Configure the MTU to use + veth_mtu: "{{ .MTU }}" + + # The CNI network configuration to install on each node. The special + # values in this config will be automatically populated. + cni_network_config: |- + { + "name": "k8s-pod-network", + "cniVersion": "0.3.1", + "plugins": [ + { + "type": "calico", + "log_level": "info", + "datastore_type": "kubernetes", + "nodename": "__KUBERNETES_NODE_NAME__", + "mtu": __CNI_MTU__, + "ipam": { + "type": "calico-ipam" + }, + "policy": { + "type": "k8s" + }, + "kubernetes": { + "kubeconfig": "__KUBECONFIG_FILEPATH__" + } + }, + { + "type": "portmap", + "snat": true, + "capabilities": {"portMappings": true} + } + ] + } +--- +# Source: calico/templates/kdd-crds.yaml +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: felixconfigurations.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: FelixConfiguration + plural: felixconfigurations + singular: felixconfiguration +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: ipamblocks.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: IPAMBlock + plural: ipamblocks + singular: ipamblock + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: blockaffinities.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: BlockAffinity + plural: blockaffinities + singular: blockaffinity + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: ipamhandles.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: IPAMHandle + plural: ipamhandles + singular: ipamhandle + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: ipamconfigs.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: IPAMConfig + plural: ipamconfigs + singular: ipamconfig + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: bgppeers.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: BGPPeer + plural: bgppeers + singular: bgppeer + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: bgpconfigurations.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: BGPConfiguration + plural: bgpconfigurations + singular: bgpconfiguration + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: ippools.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: IPPool + plural: ippools + singular: ippool + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: hostendpoints.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: HostEndpoint + plural: hostendpoints + singular: hostendpoint + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: clusterinformations.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: ClusterInformation + plural: clusterinformations + singular: clusterinformation + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: globalnetworkpolicies.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: GlobalNetworkPolicy + plural: globalnetworkpolicies + singular: globalnetworkpolicy + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: globalnetworksets.crd.projectcalico.org +spec: + scope: Cluster + group: crd.projectcalico.org + version: v1 + names: + kind: GlobalNetworkSet + plural: globalnetworksets + singular: globalnetworkset + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: networkpolicies.crd.projectcalico.org +spec: + scope: Namespaced + group: crd.projectcalico.org + version: v1 + names: + kind: NetworkPolicy + plural: networkpolicies + singular: networkpolicy + +--- + +apiVersion: apiextensions.k8s.io/v1beta1 +kind: CustomResourceDefinition +metadata: + name: networksets.crd.projectcalico.org +spec: + scope: Namespaced + group: crd.projectcalico.org + version: v1 + names: + kind: NetworkSet + plural: networksets + singular: networkset +--- +# Source: calico/templates/rbac.yaml + +# Include a clusterrole for the kube-controllers component, +# and bind it to the calico-kube-controllers serviceaccount. +kind: ClusterRole +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: calico-kube-controllers +rules: + # Nodes are watched to monitor for deletions. + - apiGroups: [""] + resources: + - nodes + verbs: + - watch + - list + - get + # Pods are queried to check for existence. + - apiGroups: [""] + resources: + - pods + verbs: + - get + # IPAM resources are manipulated when nodes are deleted. + - apiGroups: ["crd.projectcalico.org"] + resources: + - ippools + verbs: + - list + - apiGroups: ["crd.projectcalico.org"] + resources: + - blockaffinities + - ipamblocks + - ipamhandles + verbs: + - get + - list + - create + - update + - delete + # Needs access to update clusterinformations. + - apiGroups: ["crd.projectcalico.org"] + resources: + - clusterinformations + verbs: + - get + - create + - update +--- +kind: ClusterRoleBinding +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: calico-kube-controllers +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: calico-kube-controllers +subjects: +- kind: ServiceAccount + name: calico-kube-controllers + namespace: kube-system +--- +# Include a clusterrole for the calico-node DaemonSet, +# and bind it to the calico-node serviceaccount. +kind: ClusterRole +apiVersion: rbac.authorization.k8s.io/v1 +metadata: + name: calico-node +rules: + # The CNI plugin needs to get pods, nodes, and namespaces. + - apiGroups: [""] + resources: + - pods + - nodes + - namespaces + verbs: + - get + - apiGroups: [""] + resources: + - endpoints + - services + verbs: + # Used to discover service IPs for advertisement. + - watch + - list + # Used to discover Typhas. + - get + - apiGroups: [""] + resources: + - nodes/status + verbs: + # Needed for clearing NodeNetworkUnavailable flag. + - patch + # Calico stores some configuration information in node annotations. + - update + # Watch for changes to Kubernetes NetworkPolicies. + - apiGroups: ["networking.k8s.io"] + resources: + - networkpolicies + verbs: + - watch + - list + # Used by Calico for policy information. + - apiGroups: [""] + resources: + - pods + - namespaces + - serviceaccounts + verbs: + - list + - watch + # The CNI plugin patches pods/status. + - apiGroups: [""] + resources: + - pods/status + verbs: + - patch + # Calico monitors various CRDs for config. + - apiGroups: ["crd.projectcalico.org"] + resources: + - globalfelixconfigs + - felixconfigurations + - bgppeers + - globalbgpconfigs + - bgpconfigurations + - ippools + - ipamblocks + - globalnetworkpolicies + - globalnetworksets + - networkpolicies + - networksets + - clusterinformations + - hostendpoints + verbs: + - get + - list + - watch + # Calico must create and update some CRDs on startup. + - apiGroups: ["crd.projectcalico.org"] + resources: + - ippools + - felixconfigurations + - clusterinformations + verbs: + - create + - update + # Calico stores some configuration information on the node. + - apiGroups: [""] + resources: + - nodes + verbs: + - get + - list + - watch + # These permissions are only required for upgrade from v2.6, and can + # be removed after upgrade or on fresh installations. + - apiGroups: ["crd.projectcalico.org"] + resources: + - bgpconfigurations + - bgppeers + verbs: + - create + - update + # These permissions are required for Calico CNI to perform IPAM allocations. + - apiGroups: ["crd.projectcalico.org"] + resources: + - blockaffinities + - ipamblocks + - ipamhandles + verbs: + - get + - list + - create + - update + - delete + - apiGroups: ["crd.projectcalico.org"] + resources: + - ipamconfigs + verbs: + - get + # Block affinities must also be watchable by confd for route aggregation. + - apiGroups: ["crd.projectcalico.org"] + resources: + - blockaffinities + verbs: + - watch + # The Calico IPAM migration needs to get daemonsets. These permissions can be + # removed if not upgrading from an installation using host-local IPAM. + - apiGroups: ["apps"] + resources: + - daemonsets + verbs: + - get +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: calico-node +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: calico-node +subjects: +- kind: ServiceAccount + name: calico-node + namespace: kube-system + +--- +# Source: calico/templates/calico-node.yaml +# This manifest installs the calico-node container, as well +# as the CNI plugins and network config on +# each master and worker node in a Kubernetes cluster. +kind: DaemonSet +apiVersion: apps/v1 +metadata: + name: calico-node + namespace: kube-system + labels: + k8s-app: calico-node +spec: + selector: + matchLabels: + k8s-app: calico-node + updateStrategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 1 + template: + metadata: + labels: + k8s-app: calico-node + annotations: + # This, along with the CriticalAddonsOnly toleration below, + # marks the pod as a critical add-on, ensuring it gets + # priority scheduling and that its resources are reserved + # if it ever gets evicted. + spec: + nodeSelector: + beta.kubernetes.io/os: linux + hostNetwork: true + tolerations: + # Make sure calico-node gets scheduled on all nodes. + - effect: NoSchedule + operator: Exists + # Mark the pod as a critical add-on for rescheduling. + - key: CriticalAddonsOnly + operator: Exists + - effect: NoExecute + operator: Exists + serviceAccountName: calico-node + # Minimize downtime during a rolling upgrade or deletion; tell Kubernetes to do a "force + # deletion": https://kubernetes.io/docs/concepts/workloads/pods/pod/#termination-of-pods. + terminationGracePeriodSeconds: 0 + priorityClassName: system-node-critical + initContainers: + # This container performs upgrade from host-local IPAM to calico-ipam. + # It can be deleted if this is a fresh installation, or if you have already + # upgraded to use calico-ipam. + - name: upgrade-ipam + image: {{ .CniRepo }}/cni:v3.8.2 + command: ["/opt/cni/bin/calico-ipam", "-upgrade"] + env: + - name: KUBERNETES_NODE_NAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName + - name: CALICO_NETWORKING_BACKEND + valueFrom: + configMapKeyRef: + name: calico-config + key: calico_backend + volumeMounts: + - mountPath: /var/lib/cni/networks + name: host-local-net-dir + - mountPath: /host/opt/cni/bin + name: cni-bin-dir + # This container installs the CNI binaries + # and CNI network config file on each node. + - name: install-cni + image: {{ .CniRepo }}/cni:v3.8.2 + command: ["/install-cni.sh"] + env: + # Name of the CNI config file to create. + - name: CNI_CONF_NAME + value: "10-calico.conflist" + # The CNI network config to install on each node. + - name: CNI_NETWORK_CONFIG + valueFrom: + configMapKeyRef: + name: calico-config + key: cni_network_config + # Set the hostname based on the k8s node name. + - name: KUBERNETES_NODE_NAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName + # CNI MTU Config variable + - name: CNI_MTU + valueFrom: + configMapKeyRef: + name: calico-config + key: veth_mtu + # Prevents the container from sleeping forever. + - name: SLEEP + value: "false" + volumeMounts: + - mountPath: /host/opt/cni/bin + name: cni-bin-dir + - mountPath: /host/etc/cni/net.d + name: cni-net-dir + # Adds a Flex Volume Driver that creates a per-pod Unix Domain Socket to allow Dikastes + # to communicate with Felix over the Policy Sync API. + - name: flexvol-driver + image: {{ .CniRepo }}/pod2daemon-flexvol:v3.8.2 + volumeMounts: + - name: flexvol-driver-host + mountPath: /host/driver + containers: + # Runs calico-node container on each Kubernetes node. This + # container programs network policy and routes on each + # host. + - name: calico-node + image: {{ .CniRepo }}/node:v3.8.2 + env: + # Use Kubernetes API as the backing datastore. + - name: DATASTORE_TYPE + value: "kubernetes" + # Wait for the datastore. + - name: WAIT_FOR_DATASTORE + value: "true" + # Set based on the k8s node name. + - name: NODENAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName + # Choose the backend to use. + - name: CALICO_NETWORKING_BACKEND + valueFrom: + configMapKeyRef: + name: calico-config + key: calico_backend + # Cluster type to identify the deployment type + - name: CLUSTER_TYPE + value: "k8s,bgp" + # Auto-detect the BGP IP address. + - name: IP + value: "autodetect" + - name: IP_AUTODETECTION_METHOD + value: "{{ .Interface }}" + # Enable IPIP + - name: CALICO_IPV4POOL_IPIP + value: "{{if not .IPIP }}Off{{else}}Always{{end}}" + # Set MTU for tunnel device used if ipip is enabled + - name: FELIX_IPINIPMTU + valueFrom: + configMapKeyRef: + name: calico-config + key: veth_mtu + # The default IPv4 pool to create on startup if none exists. Pod IPs will be + # chosen from this range. Changing this value after installation will have + - name: CALICO_IPV4POOL_CIDR + value: "{{ .CIDR }}" + - name: CALICO_DISABLE_FILE_LOGGING + value: "true" + # Set Felix endpoint to host default action to ACCEPT. + - name: FELIX_DEFAULTENDPOINTTOHOSTACTION + value: "ACCEPT" + # Disable IPv6 on Kubernetes. + - name: FELIX_IPV6SUPPORT + value: "false" + # Set Felix logging to "info" + - name: FELIX_LOGSEVERITYSCREEN + value: "info" + - name: FELIX_HEALTHENABLED + value: "true" + securityContext: + privileged: true + resources: + requests: + cpu: 250m + livenessProbe: + httpGet: + path: /liveness + port: 9099 + host: localhost + periodSeconds: 10 + initialDelaySeconds: 10 + failureThreshold: 6 + readinessProbe: + exec: + command: + - /bin/calico-node + - -bird-ready + - -felix-ready + periodSeconds: 10 + volumeMounts: + - mountPath: /lib/modules + name: lib-modules + readOnly: true + - mountPath: /run/xtables.lock + name: xtables-lock + readOnly: false + - mountPath: /var/run/calico + name: var-run-calico + readOnly: false + - mountPath: /var/lib/calico + name: var-lib-calico + readOnly: false + - name: policysync + mountPath: /var/run/nodeagent + volumes: + # Used by calico-node. + - name: lib-modules + hostPath: + path: /lib/modules + - name: var-run-calico + hostPath: + path: /var/run/calico + - name: var-lib-calico + hostPath: + path: /var/lib/calico + - name: xtables-lock + hostPath: + path: /run/xtables.lock + type: FileOrCreate + # Used to install CNI. + - name: cni-bin-dir + hostPath: + path: /opt/cni/bin + - name: cni-net-dir + hostPath: + path: /etc/cni/net.d + # Mount in the directory for host-local IPAM allocations. This is + # used when upgrading from host-local to calico-ipam, and can be removed + # if not using the upgrade-ipam init container. + - name: host-local-net-dir + hostPath: + path: /var/lib/cni/networks + # Used to create per-pod Unix Domain Sockets + - name: policysync + hostPath: + type: DirectoryOrCreate + path: /var/run/nodeagent + # Used to install Flex Volume Driver + - name: flexvol-driver-host + hostPath: + type: DirectoryOrCreate + path: /usr/libexec/kubernetes/kubelet-plugins/volume/exec/nodeagent~uds +--- + +apiVersion: v1 +kind: ServiceAccount +metadata: + name: calico-node + namespace: kube-system + +--- +# Source: calico/templates/calico-kube-controllers.yaml + +# See https://github.com/projectcalico/kube-controllers +apiVersion: apps/v1 +kind: Deployment +metadata: + name: calico-kube-controllers + namespace: kube-system + labels: + k8s-app: calico-kube-controllers +spec: + # The controllers can only have a single active instance. + replicas: 1 + selector: + matchLabels: + k8s-app: calico-kube-controllers + strategy: + type: Recreate + template: + metadata: + name: calico-kube-controllers + namespace: kube-system + labels: + k8s-app: calico-kube-controllers + annotations: + spec: + nodeSelector: + beta.kubernetes.io/os: linux + tolerations: + # Mark the pod as a critical add-on for rescheduling. + - key: CriticalAddonsOnly + operator: Exists + - key: node-role.kubernetes.io/master + effect: NoSchedule + serviceAccountName: calico-kube-controllers + priorityClassName: system-cluster-critical + containers: + - name: calico-kube-controllers + image: {{ .CniRepo }}/kube-controllers:v3.8.2 + env: + # Choose which controllers to run. + - name: ENABLED_CONTROLLERS + value: node + - name: DATASTORE_TYPE + value: kubernetes + readinessProbe: + exec: + command: + - /usr/bin/check-status + - -r + +--- + +apiVersion: v1 +kind: ServiceAccount +metadata: + name: calico-kube-controllers + namespace: kube-system +` diff --git a/net/cilium.go b/net/cilium.go index 183d0d7a02c..9a0a1323647 100644 --- a/net/cilium.go +++ b/net/cilium.go @@ -1,5 +1,16 @@ -// MIT License -// Copyright (c) 2020 ysicing +// Copyright © 2021 sealos. +// +// 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 net @@ -381,7 +392,7 @@ rules: # For cilium-operator running in HA mode. # # Cilium operator running in HA mode requires the use of ResourceLock for Leader Election -# between mulitple running instances. +# between multiple running instances. # The preferred way of doing this is to use LeasesResourceLock as edits to Leases are less # common and fewer objects in the cluster watch "all Leases". # The support for leases was introduced in coordination.k8s.io/v1 during Kubernetes 1.14 release. diff --git a/net/flannel.go b/net/flannel.go index 023c249a33c..20e77f30a81 100644 --- a/net/flannel.go +++ b/net/flannel.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 net type Flannel struct { @@ -639,231 +653,3 @@ spec: configMap: name: kube-flannel-cfg ` - -// lastet flannel k8s manifests. For Kubernetes v1.17+ -const newFlannelYaml = ` ---- -apiVersion: policy/v1beta1 -kind: PodSecurityPolicy -metadata: - name: psp.flannel.unprivileged - annotations: - seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default - seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default - apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default - apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default -spec: - privileged: false - volumes: - - configMap - - secret - - emptyDir - - hostPath - allowedHostPaths: - - pathPrefix: "/etc/cni/net.d" - - pathPrefix: "/etc/kube-flannel" - - pathPrefix: "/run/flannel" - readOnlyRootFilesystem: false - # Users and groups - runAsUser: - rule: RunAsAny - supplementalGroups: - rule: RunAsAny - fsGroup: - rule: RunAsAny - # Privilege Escalation - allowPrivilegeEscalation: false - defaultAllowPrivilegeEscalation: false - # Capabilities - allowedCapabilities: ['NET_ADMIN', 'NET_RAW'] - defaultAddCapabilities: [] - requiredDropCapabilities: [] - # Host namespaces - hostPID: false - hostIPC: false - hostNetwork: true - hostPorts: - - min: 0 - max: 65535 - # SELinux - seLinux: - # SELinux is unused in CaaSP - rule: 'RunAsAny' ---- -kind: ClusterRole -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: flannel -rules: -- apiGroups: ['extensions'] - resources: ['podsecuritypolicies'] - verbs: ['use'] - resourceNames: ['psp.flannel.unprivileged'] -- apiGroups: - - "" - resources: - - pods - verbs: - - get -- apiGroups: - - "" - resources: - - nodes - verbs: - - list - - watch -- apiGroups: - - "" - resources: - - nodes/status - verbs: - - patch ---- -kind: ClusterRoleBinding -apiVersion: rbac.authorization.k8s.io/v1 -metadata: - name: flannel -roleRef: - apiGroup: rbac.authorization.k8s.io - kind: ClusterRole - name: flannel -subjects: -- kind: ServiceAccount - name: flannel - namespace: kube-system ---- -apiVersion: v1 -kind: ServiceAccount -metadata: - name: flannel - namespace: kube-system ---- -kind: ConfigMap -apiVersion: v1 -metadata: - name: kube-flannel-cfg - namespace: kube-system - labels: - tier: node - app: flannel -data: - cni-conf.json: | - { - "name": "cbr0", - "cniVersion": "0.3.1", - "plugins": [ - { - "type": "flannel", - "delegate": { - "hairpinMode": true, - "isDefaultGateway": true - } - }, - { - "type": "portmap", - "capabilities": { - "portMappings": true - } - } - ] - } - net-conf.json: | - { - "Network": "{{ .CIDR }}", - "Backend": { - "Type": "vxlan" - } - } ---- -apiVersion: apps/v1 -kind: DaemonSet -metadata: - name: kube-flannel-ds - namespace: kube-system - labels: - tier: node - app: flannel -spec: - selector: - matchLabels: - app: flannel - template: - metadata: - labels: - tier: node - app: flannel - spec: - affinity: - nodeAffinity: - requiredDuringSchedulingIgnoredDuringExecution: - nodeSelectorTerms: - - matchExpressions: - - key: kubernetes.io/os - operator: In - values: - - linux - hostNetwork: true - priorityClassName: system-node-critical - tolerations: - - operator: Exists - effect: NoSchedule - serviceAccountName: flannel - initContainers: - - name: install-cni - image: {{ .CniRepo }}/flannel:v0.13.1-rc1 - command: - - cp - args: - - -f - - /etc/kube-flannel/cni-conf.json - - /etc/cni/net.d/10-flannel.conflist - volumeMounts: - - name: cni - mountPath: /etc/cni/net.d - - name: flannel-cfg - mountPath: /etc/kube-flannel/ - containers: - - name: kube-flannel - image: {{ .CniRepo }}/flannel:v0.13.1-rc1 - command: - - /opt/bin/flanneld - args: - - --ip-masq - - --kube-subnet-mgr - - --iface-regex={{ .Interface }} - resources: - requests: - cpu: "100m" - memory: "50Mi" - limits: - cpu: "100m" - memory: "50Mi" - securityContext: - privileged: false - capabilities: - add: ["NET_ADMIN", "NET_RAW"] - env: - - name: POD_NAME - valueFrom: - fieldRef: - fieldPath: metadata.name - - name: POD_NAMESPACE - valueFrom: - fieldRef: - fieldPath: metadata.namespace - volumeMounts: - - name: run - mountPath: /run/flannel - - name: flannel-cfg - mountPath: /etc/kube-flannel/ - volumes: - - name: run - hostPath: - path: /run/flannel - - name: cni - hostPath: - path: /etc/cni/net.d - - name: flannel-cfg - configMap: - name: kube-flannel-cfg -` diff --git a/net/net.go b/net/net.go index 56a11343341..b3570549304 100644 --- a/net/net.go +++ b/net/net.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 net import ( @@ -13,6 +27,7 @@ const ( defaultCIDR = "100.64.0.0/10" defaultK8sServiceHost = "127.0.0.1" defaultK8sServicePort = "6443" + defaultCNIRepo = "k8s.gcr.io" ) type MetaData struct { @@ -25,7 +40,7 @@ type MetaData struct { CniRepo string K8sServiceHost string K8sServicePort string - Version string + Version string } // Net is CNI interface @@ -52,7 +67,6 @@ func NewNetwork(t string, metadata MetaData) Net { func render(data MetaData, temp string) string { var b bytes.Buffer t := template.Must(template.New("net").Parse(temp)) - t.Execute(&b, &data) + _ = t.Execute(&b, &data) return b.String() } - diff --git a/net/net_test.go b/net/net_test.go index e231cd61f62..d0b66f6699f 100644 --- a/net/net_test.go +++ b/net/net_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 net import ( @@ -20,11 +34,9 @@ func TestNewNetwork(t *testing.T) { // K8sServicePort: "6443", }).Manifests("") fmt.Println(netyaml) - } func TestNewNetworkCalico(t *testing.T) { - netyaml := NewNetwork("calico", MetaData{ Interface: "interface=en.*|eth.*", CIDR: "10.1.1.1/24", @@ -47,4 +59,3 @@ func TestNewNetworkCalico(t *testing.T) { }).Manifests("") fmt.Println(netyaml) } - diff --git a/pkg/appmanager/appmanager.go b/pkg/appmanager/appmanager.go index 91406329044..fa5b3c25062 100644 --- a/pkg/appmanager/appmanager.go +++ b/pkg/appmanager/appmanager.go @@ -1,16 +1,31 @@ +// Copyright © 2021 sealos. +// +// 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 appmanager import ( "archive/tar" "bufio" "fmt" - "github.com/fanux/sealos/install" - "github.com/wonderivan/logger" "io" "os" "path" "strings" "sync" + + "github.com/fanux/sealos/install" + "github.com/fanux/sealos/pkg/logger" ) //Command is @@ -29,7 +44,7 @@ type PkgConfig struct { Workspace string // fmt.Sprintf("%s/%s", p.Workdir, p.Name) } -func nameFromUrl(url string) string { +func nameFromURL(url string) string { tmp := path.Base(url) name := strings.Split(tmp, ".tar") if len(name) < 1 { @@ -55,7 +70,7 @@ func LoadAppConfig(url string, flagConfig string) (*PkgConfig, error) { logger.Error("load config failed: %s", err) os.Exit(0) } - pkgConfig, err = configFromReader(f) + pkgConfig, _ = configFromReader(f) } return pkgConfig, nil } @@ -206,7 +221,6 @@ func send(host string, p *PkgConfig) { tarCmd := fmt.Sprintf("tar xvf %s.tar", p.Name) fmt.Println(tarCmd) CmdWorkSpace(host, tarCmd, p.Workspace) - } // send package to master @@ -220,9 +234,7 @@ func (r *RunOnMaster) Send(config install.SealConfig, p *PkgConfig) { } } -func (r *RunOnMaster) Run(config install.SealConfig, p *PkgConfig) { - - // kubectl apply -f +func (r *RunOnMaster) Run(config install.SealConfig, p *PkgConfig) { // kubectl apply -f for _, cmd := range r.Cmd { CmdWorkSpace(config.Masters[0], cmd.Cmd, p.Workspace) } diff --git a/pkg/appmanager/deleteapp.go b/pkg/appmanager/deleteapp.go index c8ccb8332fc..b1133754209 100644 --- a/pkg/appmanager/deleteapp.go +++ b/pkg/appmanager/deleteapp.go @@ -1,10 +1,25 @@ +// Copyright © 2021 sealos. +// +// 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 appmanager import ( "fmt" - "github.com/fanux/sealos/install" - "github.com/wonderivan/logger" "os" + + "github.com/fanux/sealos/install" + "github.com/fanux/sealos/pkg/logger" ) type DeleteFlags struct { @@ -14,11 +29,11 @@ type DeleteFlags struct { CleanForce bool } -func GetDeleteFlags(appUrl string) *DeleteFlags { +func GetDeleteFlags(appURL string) *DeleteFlags { return &DeleteFlags{ Config: install.PackageConfig, WorkDir: install.WorkDir, - PkgURL: appUrl, + PkgURL: appURL, CleanForce: install.CleanForce, } } @@ -26,15 +41,14 @@ func GetDeleteFlags(appUrl string) *DeleteFlags { func DeleteApp(flag *DeleteFlags, cfgFile string) error { //TODO c := &install.SealConfig{} - err := c.Load(cfgFile) - if err != nil { + if err := c.Load(cfgFile); err != nil { logger.Error(err) c.ShowDefaultConfig() os.Exit(0) } pkgConfig, _ := LoadAppConfig(flag.PkgURL, flag.Config) pkgConfig.URL = flag.PkgURL - pkgConfig.Name = nameFromUrl(flag.PkgURL) + pkgConfig.Name = nameFromURL(flag.PkgURL) pkgConfig.Workdir = flag.WorkDir pkgConfig.Workspace = fmt.Sprintf("%s/%s", flag.WorkDir, pkgConfig.Name) diff --git a/pkg/appmanager/installapp.go b/pkg/appmanager/installapp.go index 9e67e12d111..c34b6747f23 100644 --- a/pkg/appmanager/installapp.go +++ b/pkg/appmanager/installapp.go @@ -1,12 +1,27 @@ +// Copyright © 2021 sealos. +// +// 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 appmanager import ( "bytes" "fmt" - "github.com/fanux/sealos/install" - "github.com/wonderivan/logger" "io/ioutil" + "github.com/fanux/sealos/install" + "github.com/fanux/sealos/pkg/logger" + "os" ) @@ -18,10 +33,10 @@ type InstallFlags struct { WorkDir string } -func GetInstallFlags(appUrl string) *InstallFlags { +func GetInstallFlags(appURL string) *InstallFlags { return &InstallFlags{ Config: install.PackageConfig, - PkgURL: appUrl, + PkgURL: appURL, WorkDir: install.WorkDir, Values: install.Values, } @@ -29,8 +44,7 @@ func GetInstallFlags(appUrl string) *InstallFlags { func InstallApp(flag *InstallFlags, cfgFile string) error { c := &install.SealConfig{} - err := c.Load(cfgFile) - if err != nil { + if err := c.Load(cfgFile); err != nil { logger.Error("%s", err) c.ShowDefaultConfig() os.Exit(0) @@ -42,7 +56,7 @@ func InstallApp(flag *InstallFlags, cfgFile string) error { os.Exit(0) } pkgConfig.URL = flag.PkgURL - pkgConfig.Name = nameFromUrl(flag.PkgURL) + pkgConfig.Name = nameFromURL(flag.PkgURL) pkgConfig.Workdir = flag.WorkDir pkgConfig.Workspace = fmt.Sprintf("%s/%s", flag.WorkDir, pkgConfig.Name) s, err := getValuesContent(flag.Values) diff --git a/pkg/logger/README.md b/pkg/logger/README.md index 43d62bd5904..c90df564d99 100644 --- a/pkg/logger/README.md +++ b/pkg/logger/README.md @@ -3,7 +3,7 @@ convenient log package # 1. 使用说明 ```go - import "github.com/wonderivan/logger" + import "github.com/fanux/sealos/pkg/logger" // 配置logger,如果不配置时默认为控制台输出,等级为DEBG logger.SetLogger(`{"Console": {"level": "DEBG"}`) @@ -103,35 +103,35 @@ logger当前支持控制台、文件、网络3种方式适配器输出,可以 - 时间格式打印: ``` ========RFC1123Z time format======== -Thu, 02 Aug 2018 18:48:04 +0800 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC1123Z +Thu, 02 Aug 2018 18:48:04 +0800 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RFC1123Z ========Stamp time format======== -Aug 2 18:48:04 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug Stamp +Aug 2 18:48:04 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug Stamp ========StampMilli time format======== -Aug 2 18:48:04.489 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug StampMilli +Aug 2 18:48:04.489 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug StampMilli ========StampNano time format======== -Aug 2 18:48:04.490002155 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug StampNano +Aug 2 18:48:04.490002155 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug StampNano ========RubyDate time format======== -Thu Aug 02 18:48:04 +0800 2018 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RubyDate +Thu Aug 02 18:48:04 +0800 2018 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RubyDate ========RFC822 time format======== -02 Aug 18 18:48 CST [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC822 +02 Aug 18 18:48 CST [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RFC822 ========RFC822Z time format======== -02 Aug 18 18:48 +0800 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC822Z +02 Aug 18 18:48 +0800 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RFC822Z ========RFC1123 time format======== -Thu, 02 Aug 2018 18:48:04 CST [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC1123 +Thu, 02 Aug 2018 18:48:04 CST [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RFC1123 ========RFC3339 time format======== -2018-08-02T18:48:04+08:00 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC3339 +2018-08-02T18:48:04+08:00 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RFC3339 ========RFC3339Nano time format======== -2018-08-02T18:48:04.490377325+08:00 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC3339Nano +2018-08-02T18:48:04.490377325+08:00 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RFC3339Nano ========ANSIC time format======== -Thu Aug 2 18:48:04 2018 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug ANSIC +Thu Aug 2 18:48:04 2018 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug ANSIC ========UnixDate time format======== -Thu Aug 2 18:48:04 CST 2018 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug UnixDate +Thu Aug 2 18:48:04 CST 2018 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug UnixDate ========RFC850 time format======== -Thursday, 02-Aug-18 18:48:04 CST [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug RFC850 +Thursday, 02-Aug-18 18:48:04 CST [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug RFC850 ========Kitchen time format======== -6:48PM [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug Kitchen +6:48PM [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug Kitchen ========StampMicro time format======== -Aug 2 18:48:04.490662 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Debug StampMicro +Aug 2 18:48:04.490662 [DEBG] [github.com/fanux/sealos/pkg/logger/log_test.go:115] Debug StampMicro ``` # 4. 其他 @@ -140,4 +140,4 @@ Aug 2 18:48:04.490662 [DEBG] [github.com/wonderivan/logger/log_test.go:115] Deb 2. 日志文件append为true时,当写入的日志文件发生跨天(daily为true)或超过最大限制时,会创建一个新文件,原有文件格式被重命名为: ****.xxxx-xx-xx.xxx.xxx 格式,例如:当向app.log写入日志时,触发了创建新文件操作,则将app.log重命名为 app.2018-01-01.001.log, 如果此时app.2018-01-01.001.log已经存在,则将刚才的app.log重命名为 app.2018-01-01.002.log,以此类推。 3. logger package默认初始化了全局的defaultLogger,直接调用logger包的Debug方法时,会默认调用defaultLogger.Debug,所以普通调用时,仅需要import logger即可使用。 4. 网络配置中的reconnectOnMsg为每条消息都重连一次网络日志中心,适用于写日志频率极低的情况下的服务调用,避免长时间连接,占用资源。但强烈不建议普通使用时设置为true,这将会导致调用方反复的网络重连,极大增加资源消耗和延迟。 -5. conn网络输出适配器经过ELK集成环境的测试验证,通过该方式发送的日志,能够正常通过Elecsearch和Kibana检索和分析 \ No newline at end of file +5. conn网络输出适配器经过ELK集成环境的测试验证,通过该方式发送的日志,能够正常通过Elecsearch和Kibana检索和分析 diff --git a/pkg/logger/config.go b/pkg/logger/config.go index a36a252034b..3e19c3564c4 100644 --- a/pkg/logger/config.go +++ b/pkg/logger/config.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -5,27 +19,29 @@ import ( "runtime" ) +const WINDOWS = "windows" + //二次开发logger func Cfg(level int, logFIle string) { config := logConfig{ TimeFormat: "15:04:05", Console: &consoleLogger{ LogLevel: level, - Colorful: runtime.GOOS != "windows", + Colorful: runtime.GOOS != WINDOWS, }, - File: &fileLogger{ - Filename: logFIle, - Level: "TRAC", - Daily: false, - MaxLines: 1000000, - MaxSize: 1, - MaxDays: -1, - Append: true, - PermitMask: "0660", + File: &fileLogger{ + Filename: logFIle, + Level: "TRAC", + Daily: false, + MaxLines: 1000000, + MaxSize: 1, + MaxDays: -1, + Append: true, + PermitMask: "0660", }, } cfg, _ := json.Marshal(config) - SetLogger(string(cfg)) + _ = SetLogger(string(cfg)) SetLogPath(true) } @@ -38,4 +54,4 @@ func Cfg(level int, logFIle string) { // "maxdays": -1, // 日志文件有效期 // "append": true, // 是否支持日志追加 // "permit": "0660" // 新创建的日志文件权限属性 -// }, \ No newline at end of file +// }, diff --git a/pkg/logger/conn.go b/pkg/logger/conn.go index 25982610663..dc2e790ff5e 100755 --- a/pkg/logger/conn.go +++ b/pkg/logger/conn.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -68,7 +82,7 @@ func (c *connLogger) LogWrite(when time.Time, msgText interface{}, level int) (e //网络异常时,消息发出 if !c.illNetFlag { - err = c.println(when, msg) + err = c.println(msg) //网络异常,通知处理网络的go程自动重连 if err != nil { c.illNetFlag = true @@ -99,7 +113,7 @@ func (c *connLogger) connect() error { } if tcpConn, ok := conn.(*net.TCPConn); ok { - tcpConn.SetKeepAlive(true) + _ = tcpConn.SetKeepAlive(true) } c.innerWriter = conn return nil @@ -123,7 +137,7 @@ func (c *connLogger) needToConnectOnMsg() bool { return c.ReconnectOnMsg } -func (c *connLogger) println(when time.Time, msg *loginfo) error { +func (c *connLogger) println(msg *loginfo) error { c.Lock() defer c.Unlock() ss, err := json.Marshal(msg) diff --git a/pkg/logger/conn_test.go b/pkg/logger/conn_test.go index 370b31ea9e3..e28b7273768 100755 --- a/pkg/logger/conn_test.go +++ b/pkg/logger/conn_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -6,6 +20,6 @@ import ( func TestConn(t *testing.T) { log := NewLogger() - log.SetLogger("conn", `{"net":"tcp","addr":"10.1.55.10:1024"}`) + _ = log.SetLogger("conn", `{"net":"tcp","addr":"10.1.55.10:1024"}`) log.Info("this is informational to net") } diff --git a/pkg/logger/console.go b/pkg/logger/console.go index 1cdee4e6bb5..f8db0c6474c 100755 --- a/pkg/logger/console.go +++ b/pkg/logger/console.go @@ -1,7 +1,22 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( "encoding/json" + "fmt" "os" "runtime" "sync" @@ -41,10 +56,10 @@ func (c *consoleLogger) Init(jsonConfig string) error { if len(jsonConfig) == 0 { return nil } - if jsonConfig != "{}" { - //fmt.Fprintf(os.Stdout, "consoleLogger Init:%s\n", jsonConfig) + if len(jsonConfig) == 0 { + return nil } - + fmt.Printf("consoleWriter Init:%s\n", jsonConfig) err := json.Unmarshal([]byte(jsonConfig), c) if runtime.GOOS == "windows" { c.Colorful = false @@ -77,6 +92,7 @@ func (c *consoleLogger) Destroy() { } +//nolint:unparam func (c *consoleLogger) printlnConsole(when time.Time, msg string) { c.Lock() defer c.Unlock() diff --git a/pkg/logger/console_test.go b/pkg/logger/console_test.go index 9e870ff21a7..8dd43db9bdd 100755 --- a/pkg/logger/console_test.go +++ b/pkg/logger/console_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -18,17 +32,17 @@ func testConsoleCalls(bl *LocalLogger) { func TestConsole(t *testing.T) { log1 := NewLogger() - log1.SetLogger("console", "") + _ = log1.SetLogger("console", "") testConsoleCalls(log1) log2 := NewLogger() - log2.SetLogger("console", `{"level":"EROR"}`) + _ = log2.SetLogger("console", `{"level":"EROR"}`) testConsoleCalls(log2) } // Test console without color func TestNoColorConsole(t *testing.T) { log := NewLogger() - log.SetLogger("console", `{"color":false}`) + _ = log.SetLogger("console", `{"color":false}`) testConsoleCalls(log) } diff --git a/pkg/logger/file.go b/pkg/logger/file.go index ccc53952268..3f173a10ed6 100755 --- a/pkg/logger/file.go +++ b/pkg/logger/file.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -75,7 +89,6 @@ func (f *fileLogger) needCreateFresh(size int, day int) bool { return (f.MaxLines > 0 && f.maxLinesCurLines >= f.MaxLines) || (f.MaxSize > 0 && f.maxSizeCurSize+size >= f.MaxSize) || (f.Daily && day != f.dailyOpenDate) - } // WriteMsg write logger message into file. @@ -125,7 +138,7 @@ func (f *fileLogger) createLogFile() (*os.File, error) { fd, err := os.OpenFile(f.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.FileMode(perm)) if err == nil { // Make sure file perm is user set perm cause of `os.OpenFile` will obey umask - os.Chmod(f.Filename, os.FileMode(perm)) + _ = os.Chmod(f.Filename, os.FileMode(perm)) } return fd, err } @@ -215,7 +228,7 @@ func (f *fileLogger) createFreshFile(logTime time.Time) error { } if err == nil { - return fmt.Errorf("Cannot find free log number to rename %s", f.Filename) + return fmt.Errorf("cannot find free log number to rename %s", f.Filename) } f.fileWriter.Close() @@ -238,17 +251,17 @@ RESTART_LOGGER: go f.deleteOldLog() if startLoggerErr != nil { - return fmt.Errorf("Rotate StartLogger: %s", startLoggerErr) + return fmt.Errorf("rotate StartLogger: %s", startLoggerErr) } if err != nil { - return fmt.Errorf("Rotate: %s", err) + return fmt.Errorf("rotate: %s", err) } return nil } func (f *fileLogger) deleteOldLog() { dir := filepath.Dir(f.Filename) - filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) { + _ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) { defer func() { if r := recover(); r != nil { fmt.Fprintf(os.Stderr, "Unable to delete old log '%s', error: %v\n", path, r) diff --git a/pkg/logger/file_test.go b/pkg/logger/file_test.go index 0cd139d3460..fe699540569 100755 --- a/pkg/logger/file_test.go +++ b/pkg/logger/file_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -11,7 +25,7 @@ import ( func TestFilePermit(t *testing.T) { log := NewLogger() - log.SetLogger(AdapterFile, `{"filename":"test.log", + _ = log.SetLogger(AdapterFile, `{"filename":"test.log", "rotateperm": "0666", "maxlines":100000, "maxsize":1, @@ -39,7 +53,7 @@ func TestFilePermit(t *testing.T) { func TestFileLine(t *testing.T) { log := NewLogger() - log.SetLogger("file", `{"filename":"test2.log"}`) + _ = log.SetLogger("file", `{"filename":"test2.log"}`) log.Debug("debug") log.Info("info") log.Debug("debug") @@ -72,7 +86,7 @@ func TestFileLine(t *testing.T) { func TestFileSize(t *testing.T) { log := NewLogger() - log.SetLogger(AdapterFile, `{"filename":"test.log", + _ = log.SetLogger(AdapterFile, `{"filename":"test.log", "rotateperm": "0666", "maxlines":100000, "maxsize":1, @@ -94,7 +108,7 @@ func TestFileSize(t *testing.T) { func TestFileByMaxLine(t *testing.T) { log := NewLogger() - log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) + _ = log.SetLogger("file", `{"filename":"test3.log","maxlines":4}`) log.Debug("debug") log.Info("info") log.Warn("warning") @@ -122,10 +136,10 @@ func TestFileByTime(t *testing.T) { LogLevel: LevelTrace, PermitMask: "0660", } - fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) + _ = fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) fw.dailyOpenDate = fw.dailyOpenTime.Day() - fw.LogWrite(time.Now(), "this is a msg for test", LevelTrace) + _ = fw.LogWrite(time.Now(), "this is a msg for test", LevelTrace) for _, file := range []string{fn1, fn2} { _, err := os.Stat(file) @@ -150,7 +164,7 @@ func exists(path string) (bool, error) { func BenchmarkFile(b *testing.B) { log := NewLogger() - log.SetLogger("file", `{"filename":"test4.log"}`) + _ = log.SetLogger("file", `{"filename":"test4.log"}`) for i := 0; i < b.N; i++ { log.Debug("debug") } @@ -159,7 +173,7 @@ func BenchmarkFile(b *testing.B) { func BenchmarkFileCallDepth(b *testing.B) { log := NewLogger() - log.SetLogger("file", `{"filename":"test4.log"}`) + _ = log.SetLogger("file", `{"filename":"test4.log"}`) for i := 0; i < b.N; i++ { log.Debug("debug") } @@ -168,7 +182,7 @@ func BenchmarkFileCallDepth(b *testing.B) { func BenchmarkFileOnGoroutine(b *testing.B) { log := NewLogger() - log.SetLogger("file", `{"filename":"test4.log"}`) + _ = log.SetLogger("file", `{"filename":"test4.log"}`) for i := 0; i < b.N; i++ { go log.Debug("debug") } diff --git a/pkg/logger/go.mod b/pkg/logger/go.mod deleted file mode 100644 index 3d8761e9ddc..00000000000 --- a/pkg/logger/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/fanux/sealos/pkg/logger - -go 1.13 diff --git a/pkg/logger/log.go b/pkg/logger/log.go index 261bfd62bbe..a64d1e3c4a5 100755 --- a/pkg/logger/log.go +++ b/pkg/logger/log.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -26,7 +40,6 @@ const ( LevelTrace // 用户级基本输出 ) -// 日志等级和描述映射关系 var LevelMap = map[string]int{ "EMER": LevelEmergency, "ALRT": LevelAlert, @@ -36,7 +49,7 @@ var LevelMap = map[string]int{ "INFO": LevelInformational, "DEBG": LevelDebug, "TRAC": LevelTrace, -} +} // 日志等级和描述映射关系 // 注册实现的适配器, 当前支持控制台,文件和网络输出 var adapters = make(map[string]Logger) @@ -113,7 +126,7 @@ func NewLogger(depth ...int) *LocalLogger { } l.appName = "[" + appSn + "]" l.callDepth = dep - l.SetLogger(AdapterConsole) + _ = l.SetLogger(AdapterConsole) l.timeFormat = logTimeDefaultFormat return l } @@ -130,24 +143,24 @@ func init() { defaultLogger = NewLogger(3) } -func (this *LocalLogger) SetLogger(adapterName string, configs ...string) error { - this.lock.Lock() - defer this.lock.Unlock() +func (r *LocalLogger) SetLogger(adapterName string, configs ...string) error { + r.lock.Lock() + defer r.lock.Unlock() - if !this.init { - this.outputs = []*nameLogger{} - this.init = true + if !r.init { + r.outputs = []*nameLogger{} + r.init = true } config := append(configs, "{}")[0] var num int = -1 var i int var l *nameLogger - for i, l = range this.outputs { + for i, l = range r.outputs { if l.name == adapterName { if l.config == config { //配置没有变动,不重新设置 - return fmt.Errorf("you have set same config for this adaptername %s", adapterName) + return fmt.Errorf("you have set same config for r adaptername %s", adapterName) } l.Logger.Destroy() num = i @@ -159,44 +172,43 @@ func (this *LocalLogger) SetLogger(adapterName string, configs ...string) error return fmt.Errorf("unknown adaptername %s (forgotten Register?)", adapterName) } - err := logger.Init(config) - if err != nil { - fmt.Fprintf(os.Stderr, "logger Init <%s> err:%v, %s output ignore!\n", + if err := logger.Init(config); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "logger Init <%s> err:%v, %s output ignore!\n", adapterName, err, adapterName) return err } if num >= 0 { - this.outputs[i] = &nameLogger{name: adapterName, Logger: logger, config: config} + r.outputs[i] = &nameLogger{name: adapterName, Logger: logger, config: config} return nil } - this.outputs = append(this.outputs, &nameLogger{name: adapterName, Logger: logger, config: config}) + r.outputs = append(r.outputs, &nameLogger{name: adapterName, Logger: logger, config: config}) return nil } -func (this *LocalLogger) DelLogger(adapterName string) error { - this.lock.Lock() - defer this.lock.Unlock() +func (r *LocalLogger) DelLogger(adapterName string) error { + r.lock.Lock() + defer r.lock.Unlock() outputs := []*nameLogger{} - for _, lg := range this.outputs { + for _, lg := range r.outputs { if lg.name == adapterName { lg.Destroy() } else { outputs = append(outputs, lg) } } - if len(outputs) == len(this.outputs) { + if len(outputs) == len(r.outputs) { return fmt.Errorf("logs: unknown adaptername %s (forgotten Register?)", adapterName) } - this.outputs = outputs + r.outputs = outputs return nil } // 设置日志起始路径 -func (this *LocalLogger) SetLogPath(bPath bool) { - this.usePath = bPath +func (r *LocalLogger) SetLogPath(bPath bool) { + r.usePath = bPath } -func (this *LocalLogger) writeToLoggers(when time.Time, msg *loginfo, level int) { - for _, l := range this.outputs { +func (r *LocalLogger) writeToLoggers(when time.Time, msg *loginfo, level int) { + for _, l := range r.outputs { if l.name == AdapterConn { //网络日志,使用json格式发送,此处使用结构体,用于类似ElasticSearch功能检索 err := l.LogWrite(when, msg, level) @@ -208,11 +220,11 @@ func (this *LocalLogger) writeToLoggers(when time.Time, msg *loginfo, level int) strLevel := " [" + msg.Level + "] " strPath := "[" + msg.Path + "] " - if !this.usePath { + if !r.usePath { strPath = "" } - msgStr := when.Format(this.timeFormat) + strLevel + strPath + msg.Content + msgStr := when.Format(r.timeFormat) + strLevel + strPath + msg.Content err := l.LogWrite(when, msgStr, level) if err != nil { fmt.Fprintf(os.Stderr, "unable to WriteMsg to adapter:%v,error:%v\n", l.name, err) @@ -220,9 +232,9 @@ func (this *LocalLogger) writeToLoggers(when time.Time, msg *loginfo, level int) } } -func (this *LocalLogger) writeMsg(logLevel int, msg string, v ...interface{}) error { - if !this.init { - this.SetLogger(AdapterConsole) +func (r *LocalLogger) writeMsg(logLevel int, msg string, v ...interface{}) { + if !r.init { + _ = r.SetLogger(AdapterConsole) } msgSt := new(loginfo) src := "" @@ -231,8 +243,8 @@ func (this *LocalLogger) writeMsg(logLevel int, msg string, v ...interface{}) er } when := time.Now() // - if this.usePath { - _, file, lineno, ok := runtime.Caller(this.callDepth) + if r.usePath { + _, file, lineno, ok := runtime.Caller(r.callDepth) var strim = "/" if ok { codeArr := strings.Split(file, strim) @@ -245,81 +257,77 @@ func (this *LocalLogger) writeMsg(logLevel int, msg string, v ...interface{}) er msgSt.Level = levelPrefix[logLevel] msgSt.Path = src msgSt.Content = msg - msgSt.Name = this.appName - msgSt.Time = when.Format(this.timeFormat) - this.writeToLoggers(when, msgSt, logLevel) - - return nil + msgSt.Name = r.appName + msgSt.Time = when.Format(r.timeFormat) + r.writeToLoggers(when, msgSt, logLevel) } -func (this *LocalLogger) Fatal(format string, args ...interface{}) { - this.Emer("###Exec Panic:"+format, args...) +func (r *LocalLogger) Fatal(format string, args ...interface{}) { + r.Emer("###Exec Panic:"+format, args...) os.Exit(1) } -func (this *LocalLogger) Panic(format string, args ...interface{}) { - this.Emer("###Exec Panic:"+format, args...) +func (r *LocalLogger) Panic(format string, args ...interface{}) { + r.Emer("###Exec Panic:"+format, args...) panic(fmt.Sprintf(format, args...)) } // Emer Log EMERGENCY level message. -func (this *LocalLogger) Emer(format string, v ...interface{}) { - this.writeMsg(LevelEmergency, format, v...) +func (r *LocalLogger) Emer(format string, v ...interface{}) { + r.writeMsg(LevelEmergency, format, v...) } // Alert Log ALERT level message. -func (this *LocalLogger) Alert(format string, v ...interface{}) { - this.writeMsg(LevelAlert, format, v...) +func (r *LocalLogger) Alert(format string, v ...interface{}) { + r.writeMsg(LevelAlert, format, v...) } // Crit Log CRITICAL level message. -func (this *LocalLogger) Crit(format string, v ...interface{}) { - this.writeMsg(LevelCritical, format, v...) +func (r *LocalLogger) Crit(format string, v ...interface{}) { + r.writeMsg(LevelCritical, format, v...) } // Error Log ERROR level message. -func (this *LocalLogger) Error(format string, v ...interface{}) { - this.writeMsg(LevelError, format, v...) +func (r *LocalLogger) Error(format string, v ...interface{}) { + r.writeMsg(LevelError, format, v...) } // Warn Log WARNING level message. -func (this *LocalLogger) Warn(format string, v ...interface{}) { - this.writeMsg(LevelWarning, format, v...) +func (r *LocalLogger) Warn(format string, v ...interface{}) { + r.writeMsg(LevelWarning, format, v...) } // Info Log INFO level message. -func (this *LocalLogger) Info(format string, v ...interface{}) { - this.writeMsg(LevelInformational, format, v...) +func (r *LocalLogger) Info(format string, v ...interface{}) { + r.writeMsg(LevelInformational, format, v...) } // Debug Log DEBUG level message. -func (this *LocalLogger) Debug(format string, v ...interface{}) { - this.writeMsg(LevelDebug, format, v...) +func (r *LocalLogger) Debug(format string, v ...interface{}) { + r.writeMsg(LevelDebug, format, v...) } // Trace Log TRAC level message. -func (this *LocalLogger) Trace(format string, v ...interface{}) { - this.writeMsg(LevelTrace, format, v...) +func (r *LocalLogger) Trace(format string, v ...interface{}) { + r.writeMsg(LevelTrace, format, v...) } -func (this *LocalLogger) Close() { - - for _, l := range this.outputs { +func (r *LocalLogger) Close() { + for _, l := range r.outputs { l.Destroy() } - this.outputs = nil - + r.outputs = nil } -func (this *LocalLogger) Reset() { - for _, l := range this.outputs { +func (r *LocalLogger) Reset() { + for _, l := range r.outputs { l.Destroy() } - this.outputs = nil + r.outputs = nil } -func (this *LocalLogger) SetCallDepth(depth int) { - this.callDepth = depth +func (r *LocalLogger) SetCallDepth(depth int) { + r.callDepth = depth } // GetlocalLogger returns the defaultLogger @@ -338,9 +346,9 @@ func SetLogPath(show bool) { // param 可以是log配置文件名,也可以是log配置内容,默认DEBUG输出到控制台 func SetLogger(param ...string) error { - if 0 == len(param) { + if len(param) == 0 { //默认只输出到控制台 - defaultLogger.SetLogger(AdapterConsole) + _ = defaultLogger.SetLogger(AdapterConsole) return nil } @@ -374,15 +382,15 @@ func SetLogger(param ...string) error { } if conf.Console != nil { console, _ := json.Marshal(conf.Console) - defaultLogger.SetLogger(AdapterConsole, string(console)) + _ = defaultLogger.SetLogger(AdapterConsole, string(console)) } if conf.File != nil { file, _ := json.Marshal(conf.File) - defaultLogger.SetLogger(AdapterFile, string(file)) + _ = defaultLogger.SetLogger(AdapterFile, string(file)) } if conf.Conn != nil { conn, _ := json.Marshal(conf.Conn) - defaultLogger.SetLogger(AdapterConn, string(conn)) + _ = defaultLogger.SetLogger(AdapterConn, string(conn)) } return nil } @@ -438,10 +446,9 @@ func Trace(f interface{}, v ...interface{}) { } func formatLog(f interface{}, v ...interface{}) string { - var msg string - switch f.(type) { + var out string + switch msg := f.(type) { case string: - msg = f.(string) if len(v) == 0 { return msg } @@ -451,20 +458,13 @@ func formatLog(f interface{}, v ...interface{}) string { //do not contain format char msg += strings.Repeat(" %v", len(v)) } + out = msg default: - msg = fmt.Sprint(f) + out = fmt.Sprint(f) if len(v) == 0 { - return msg + return out } - msg += strings.Repeat(" %v", len(v)) - } - return fmt.Sprintf(msg, v...) -} - -func stringTrim(s string, cut string) string { - ss := strings.SplitN(s, cut, 2) - if 1 == len(ss) { - return ss[0] + out += strings.Repeat(" %v", len(v)) } - return ss[1] + return fmt.Sprintf(out, v...) } diff --git a/pkg/logger/log_test.go b/pkg/logger/log_test.go index a9e073abc0f..2fd9537cf6c 100755 --- a/pkg/logger/log_test.go +++ b/pkg/logger/log_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 logger import ( @@ -24,7 +38,7 @@ var p = `{ }` func TestLogOut(t *testing.T) { - SetLogger(p) + _ = SetLogger(p) Trace("this is Trace") Debug("this is Debug") Info("this is Info") @@ -38,8 +52,8 @@ func TestLogOut(t *testing.T) { func TestLogConfigReload(t *testing.T) { go func() { for { - for level, _ := range LevelMap { - SetLogger(fmt.Sprintf(`{ + for level := range LevelMap { + _ = SetLogger(fmt.Sprintf(`{ "Console": { "level": "%s", "color": true @@ -72,11 +86,9 @@ func TestLogConfigReload(t *testing.T) { time.Sleep(time.Millisecond) } - } func TestLogTimeFormat(t *testing.T) { - var formats = map[string]string{"ANSIC": "Mon Jan _2 15:04:05 2006", "UnixDate": "Mon Jan _2 15:04:05 MST 2006", "RubyDate": "Mon Jan 02 15:04:05 -0700 2006", @@ -94,7 +106,7 @@ func TestLogTimeFormat(t *testing.T) { "StampNano": "Jan _2 15:04:05.000000000", } for timeType, format := range formats { - SetLogger(fmt.Sprintf(`{ + _ = SetLogger(fmt.Sprintf(`{ "TimeFormat":"%s", "Console": { "level": "TRAC", @@ -120,5 +132,4 @@ func TestLogTimeFormat(t *testing.T) { Alert("Alert", timeType) Emer("Emergency", timeType) } - } diff --git a/pkg/sshcmd/cmd/cmd.go b/pkg/sshcmd/cmd/cmd.go index e2a9f1a15f4..45785725f74 100644 --- a/pkg/sshcmd/cmd/cmd.go +++ b/pkg/sshcmd/cmd/cmd.go @@ -1,10 +1,25 @@ +// Copyright © 2021 sealos. +// +// 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 cmd import ( "bytes" - "github.com/wonderivan/logger" "os" "os/exec" + + "github.com/fanux/sealos/pkg/logger" ) //Cmd is exec on os ,no return @@ -14,22 +29,20 @@ func Cmd(name string, arg ...string) { cmd.Stdin = os.Stdin cmd.Stderr = os.Stderr cmd.Stdout = os.Stdout - err := cmd.Run() - if err != nil { + if err := cmd.Run(); err != nil { logger.Error("[os]os call error.", err) } } -//CmdToString is exec on os , return result -func CmdToString(name string, arg ...string) string { +//String is exec on os , return result +func String(name string, arg ...string) string { logger.Info("[os]exec cmd is : ", name, arg) cmd := exec.Command(name, arg[:]...) cmd.Stdin = os.Stdin var b bytes.Buffer cmd.Stdout = &b cmd.Stderr = &b - err := cmd.Run() - if err != nil { + if err := cmd.Run(); err != nil { logger.Error("[os]os call error.", err) return "" } diff --git a/pkg/sshcmd/cmd/watch.go b/pkg/sshcmd/cmd/watch.go index ca2e92dc8b0..734b4199592 100644 --- a/pkg/sshcmd/cmd/watch.go +++ b/pkg/sshcmd/cmd/watch.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 cmd import ( @@ -7,7 +21,7 @@ import ( "strings" "time" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) const oneMBByte = 1024 * 1024 @@ -17,9 +31,8 @@ func LoggerFileSize(filename string, size int) { t := time.NewTicker(3 * time.Second) //every 3s check file defer t.Stop() for { - select { - case <-t.C: - length := CmdToString("/bin/sh", "-c", "ls -l "+filename+" | awk '{print $5}'", "") + if <-t.C; true { + length := String("/bin/sh", "-c", "ls -l "+filename+" | awk '{print $5}'", "") length = strings.Replace(length, "\n", "", -1) length = strings.Replace(length, "\r", "", -1) lengthByte, _ := strconv.Atoi(length) @@ -40,7 +53,7 @@ func IsFileExist(filepath string) bool { fileName := path.Base(filepath) // aa fileDirName := path.Dir(filepath) fileCommand := fmt.Sprintf("ls -l %s | grep %s | wc -l", fileDirName, fileName) - data := strings.Replace(CmdToString("/bin/sh", "-c", fileCommand), "\r", "", -1) + data := strings.Replace(String("/bin/sh", "-c", fileCommand), "\r", "", -1) data = strings.Replace(data, "\n", "", -1) count, err := strconv.Atoi(strings.TrimSpace(data)) defer func() { diff --git a/pkg/sshcmd/filesize/filesize.go b/pkg/sshcmd/filesize/filesize.go index c437be363d7..56befd546fd 100644 --- a/pkg/sshcmd/filesize/filesize.go +++ b/pkg/sshcmd/filesize/filesize.go @@ -1,9 +1,24 @@ +// Copyright © 2021 sealos. +// +// 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 filesize import ( "crypto/tls" - "github.com/wonderivan/logger" "net/http" + + "github.com/fanux/sealos/pkg/logger" ) //Do is fetch file size diff --git a/pkg/sshcmd/md5sum/md5sum.go b/pkg/sshcmd/md5sum/md5sum.go index 541cafe776d..593f8232167 100644 --- a/pkg/sshcmd/md5sum/md5sum.go +++ b/pkg/sshcmd/md5sum/md5sum.go @@ -1,10 +1,25 @@ +// Copyright © 2021 sealos. +// +// 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 md5sum import ( "fmt" - "github.com/wonderivan/logger" "os/exec" "strings" + + "github.com/fanux/sealos/pkg/logger" ) func FromLocal(localPath string) string { diff --git a/pkg/sshcmd/md5sum/md5sum_test.go b/pkg/sshcmd/md5sum/md5sum_test.go index 09006c1ae60..a5d24e8fc1a 100644 --- a/pkg/sshcmd/md5sum/md5sum_test.go +++ b/pkg/sshcmd/md5sum/md5sum_test.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 md5sum import "testing" diff --git a/pkg/sshcmd/sshutil/connect.go b/pkg/sshcmd/sshutil/connect.go index 8aca7f92e0d..7111724aac8 100644 --- a/pkg/sshcmd/sshutil/connect.go +++ b/pkg/sshcmd/sshutil/connect.go @@ -1,13 +1,28 @@ +// Copyright © 2021 sealos. +// +// 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 sshutil import ( "fmt" - "github.com/wonderivan/logger" - "golang.org/x/crypto/ssh" "io/ioutil" "os" "strings" "time" + + "github.com/fanux/sealos/pkg/logger" + "golang.org/x/crypto/ssh" ) /** @@ -23,10 +38,10 @@ func (ss *SSH) connect(host string) (*ssh.Client, error) { ss.Timeout = &DefaultTimeout } clientConfig := &ssh.ClientConfig{ - User: ss.User, - Auth: auth, - Timeout: *ss.Timeout, - Config: config, + User: ss.User, + Auth: auth, + Timeout: *ss.Timeout, + Config: config, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } @@ -115,7 +130,7 @@ func (ss *SSH) readFile(name string) []byte { } func (ss *SSH) addrReformat(host string) string { - if strings.Index(host, ":") == -1 { + if !strings.Contains(host, ":") { host = fmt.Sprintf("%s:22", host) } return host diff --git a/pkg/sshcmd/sshutil/scp.go b/pkg/sshcmd/sshutil/scp.go index 05f943f3323..57277a6061c 100644 --- a/pkg/sshcmd/sshutil/scp.go +++ b/pkg/sshcmd/sshutil/scp.go @@ -1,12 +1,22 @@ +// Copyright © 2021 sealos. +// +// 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 sshutil import ( "bytes" "fmt" - "github.com/fanux/sealos/pkg/sshcmd/md5sum" - "github.com/pkg/sftp" - "github.com/wonderivan/logger" - "golang.org/x/crypto/ssh" "io" "io/ioutil" "net" @@ -15,6 +25,11 @@ import ( "path/filepath" "strings" "time" + + "github.com/fanux/sealos/pkg/logger" + "github.com/fanux/sealos/pkg/sshcmd/md5sum" + "github.com/pkg/sftp" + "golang.org/x/crypto/ssh" ) //Copy is @@ -236,7 +251,7 @@ func (ss *SSH) CopyRemoteFileToLocal(host, localFilePath, remoteFilePath string) } defer dstFile.Close() // copy to local file - srcFile.WriteTo(dstFile) + _, _ = srcFile.WriteTo(dstFile) } // CopyLocalToRemote is copy file or dir to remotePath, add md5 validate @@ -267,7 +282,7 @@ func (ss *SSH) CopyLocalToRemote(host, localPath, remotePath string) { } else { baseRemoteFilePath := filepath.Dir(remotePath) mkDstDir := fmt.Sprintf("mkdir -p %s || true", baseRemoteFilePath) - ss.CmdAsync(host, mkDstDir) + _ = ss.CmdAsync(host, mkDstDir) ss.copyLocalFileToRemote(host, sshClient, sftpClient, localPath, remotePath) } } @@ -283,12 +298,12 @@ func (ss *SSH) copyLocalDirToRemote(host string, sshClient *ssh.Client, sftpClie if err != nil { panic(1) } - sftpClient.Mkdir(remotePath) + _ = sftpClient.Mkdir(remotePath) for _, file := range localFiles { lfp := path.Join(localPath, file.Name()) rfp := path.Join(remotePath, file.Name()) if file.IsDir() { - sftpClient.Mkdir(rfp) + _ = sftpClient.Mkdir(rfp) ss.copyLocalDirToRemote(host, sshClient, sftpClient, lfp, rfp) } else { ss.copyLocalFileToRemote(host, sshClient, sftpClient, lfp, rfp) diff --git a/pkg/sshcmd/sshutil/scp_test.go b/pkg/sshcmd/sshutil/scp_test.go index 4c7c2aad1ae..986f6f166af 100644 --- a/pkg/sshcmd/sshutil/scp_test.go +++ b/pkg/sshcmd/sshutil/scp_test.go @@ -1,8 +1,23 @@ +// Copyright © 2021 sealos. +// +// 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 sshutil import ( - "github.com/wonderivan/logger" "testing" + + "github.com/fanux/sealos/pkg/logger" ) func TestSSHCopyLocalToRemote(t *testing.T) { diff --git a/pkg/sshcmd/sshutil/ssh.go b/pkg/sshcmd/sshutil/ssh.go index 48eddc86bfc..dfac19cc5bc 100644 --- a/pkg/sshcmd/sshutil/ssh.go +++ b/pkg/sshcmd/sshutil/ssh.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 sshutil import ( @@ -5,7 +19,7 @@ import ( "io" "strings" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) //Cmd is in host exec cmd @@ -93,8 +107,7 @@ func (ss *SSH) CmdAsync(host string, cmd string) error { //CmdToString is in host exec cmd and replace to spilt str func (ss *SSH) CmdToString(host, cmd, spilt string) string { - data := ss.Cmd(host, cmd) - if data != nil { + if data := ss.Cmd(host, cmd); data != nil { str := string(data) str = strings.ReplaceAll(str, "\r\n", spilt) return str diff --git a/pkg/sshcmd/sshutil/types.go b/pkg/sshcmd/sshutil/types.go index 08806307790..976179202f6 100644 --- a/pkg/sshcmd/sshutil/types.go +++ b/pkg/sshcmd/sshutil/types.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 sshutil import "time" diff --git a/pkg/sshcmd/sshutil/watch.go b/pkg/sshcmd/sshutil/watch.go index 80bbf804301..07a56b53c2d 100644 --- a/pkg/sshcmd/sshutil/watch.go +++ b/pkg/sshcmd/sshutil/watch.go @@ -1,3 +1,17 @@ +// Copyright © 2021 sealos. +// +// 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 sshutil import ( @@ -7,7 +21,7 @@ import ( "strings" "time" - "github.com/wonderivan/logger" + "github.com/fanux/sealos/pkg/logger" ) const oneKBByte = 1024 @@ -18,8 +32,7 @@ func (ss *SSH) LoggerFileSize(host, filename string, size int) { t := time.NewTicker(3 * time.Second) //every 3s check file defer t.Stop() for { - select { - case <-t.C: + if <-t.C; true { length := ss.CmdToString(host, "ls -l "+filename+" | awk '{print $5}'", "") length = strings.Replace(length, "\n", "", -1) length = strings.Replace(length, "\r", "", -1) @@ -58,12 +71,10 @@ func (ss *SSH) IsFileExist(host, remoteFilePath string) bool { } func toSizeFromInt(length int) (float64, string) { - isMb := length/oneMBByte > 1 value, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", float64(length)/oneMBByte), 64) - if isMb { + if isMb := length/oneMBByte > 1; isMb { return value, "MB" - } else { - value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", float64(length)/oneKBByte), 64) - return value, "KB" } + value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", float64(length)/oneKBByte), 64) + return value, "KB" } diff --git a/test/join-node-and-nodes.sh b/test/join-node-and-nodes.sh index ff1e0cfa732..e50d8ba663b 100644 --- a/test/join-node-and-nodes.sh +++ b/test/join-node-and-nodes.sh @@ -1,4 +1,18 @@ #!/bin/bash +# Copyright © 2021 sealos. +# +# 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. + # sh test.sh 1.15.4 pkgurl v3.2.0-alpha.0 # test sealos join command, sealos join --node 192.168.0.2 --nodes 192.168.0.3-192.168.0.5 diff --git a/test/network-cird-test.sh b/test/network-cird-test.sh index 667e61c5b52..a6568c4f69b 100644 --- a/test/network-cird-test.sh +++ b/test/network-cird-test.sh @@ -1,4 +1,18 @@ #!/bin/bash +# Copyright © 2021 sealos. +# +# 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. + # sh test.sh 1.15.4 pkgurl v3.1.0-alpha.3 # test network, podcidr network interface --podcidr 10.63.0.0/10 --svccidr 11.96.0.0/12 --network calico diff --git a/test/note.sh b/test/note.sh index 2e7192a750b..50a776ab177 100644 --- a/test/note.sh +++ b/test/note.sh @@ -1,4 +1,18 @@ #!/bin/bash +# Copyright © 2021 sealos. +# +# 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. + # Author:louisehong4168 # Blog:https://fenghong.tech # Time:2020-12-06 11:17:39 diff --git a/test/test-sealos-beijing.sh b/test/test-sealos-beijing.sh index c45444f8e9c..136828bbb26 100644 --- a/test/test-sealos-beijing.sh +++ b/test/test-sealos-beijing.sh @@ -1,4 +1,18 @@ #!/bin/bash +# Copyright © 2021 sealos. +# +# 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. + # sh test.sh [k8s version] [package url] [sealos version] # sh test.sh 1.15.4 pkgurl 3.1.0 # 如测试3.2.0-beta.2 版本 1.18.0版本离线包: diff --git a/test/test.sh b/test/test.sh index 33e4d36f6a7..ecc19dc3690 100644 --- a/test/test.sh +++ b/test/test.sh @@ -1,4 +1,18 @@ #!/bin/bash +# Copyright © 2021 sealos. +# +# 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. + # sh test.sh 1.15.4 pkgurl echo "create 4 vms" diff --git a/test/testforlocal.sh b/test/testforlocal.sh index 4d7bc188ce5..1d0b9cacc1a 100644 --- a/test/testforlocal.sh +++ b/test/testforlocal.sh @@ -1,4 +1,18 @@ #!/bin/bash +# Copyright © 2021 sealos. +# +# 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. + SEALOSCMD="./sealos" KUBETARBALL="/root/kube1.18.0.tar.gz"