8000 [WIP]feat: add pre check for cluster install by kakaZhou719 · Pull Request #2262 · sealerio/sealer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[WIP]feat: add pre check for cluster install #2262

New issue

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

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

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/sealer/cmd/cluster/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/sealerio/sealer/pkg/imagedistributor"
"github.com/sealerio/sealer/pkg/imageengine"
"github.com/sealerio/sealer/pkg/infradriver"
"github.com/sealerio/sealer/pkg/preflight"
"github.com/sealerio/sealer/pkg/registry"
8000 v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
Expand Down Expand Up @@ -230,6 +231,15 @@ func (k KubeInstaller) Install(kubeImageName string, options KubeInstallOptions)
appNames = k.infraDriver.GetClusterLaunchApps()
)

runners := preflight.Runner{
Checkers: []preflight.Checker{preflight.ClusterIsExistsCheck{}},
}

err := runners.Execute()
if err != nil {
return err
}

logrus.Infof("start to create new cluster with image: %s", kubeImageName)
logrus.Debugf("will create a new cluster using: %+v\n", cluster)

Expand Down
46 changes: 46 additions & 0 deletions pkg/preflight/checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright © 2023 Alibaba Group Holding Ltd.
//
// 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 preflight

import (
"github.com/pkg/errors"
"github.com/sealerio/sealer/pkg/client/k8s"
)

// Checker validates the desired value
type Checker interface {
Check() (warnings, errorList []error)
Name() string
}

// ClusterIsExistsCheck verifies the given cluster is existed.
type ClusterIsExistsCheck struct {
}

// Name returns label for checker.
func (ClusterIsExistsCheck) Name() string {
return "ClusterExist"
}

// Check :if the given cluster is existed, return error.
func (c ClusterIsExistsCheck) Check() (warnings, errorList []error) {
// check K8s client
client, _ := k8s.NewK8sClient()
if client != nil {
return nil, []error{errors.Errorf("cluster is exist: could new k8s client via default kubeconfig")}
}

return nil, nil
}
91 changes: 91 additions & 0 deletions pkg/preflight/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright © 2023 Alibaba Group Holding Ltd.
//
// 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 preflight

import (
"bytes"
"fmt"

"github.com/sealerio/sealer/utils/strings"
"github.com/sirupsen/logrus"
)

// OptionFunc configures a runner list
type OptionFunc func(*RunOptions)

type RunOptions struct {
// SkipList: if checker name in this list, will skip to check. default is lowercase.
SkipList []string

// IgnoreList: if checker name in this list, will ignore its error after run check. default is lowercase.
IgnoreList []string
}

func WithSkips(skips []string) OptionFunc {
return func(o *RunOptions) {
o.SkipList = skips
}
}

func WithIgnores(ignores []string) OptionFunc {
return func(o *RunOptions) {
o.IgnoreList = ignores
}
}

type Runner struct {
Checkers []Checker
}

// Execute checker validate and dispatch to different results
func (r *Runner) Execute(optFunc ...OptionFunc) error {
var options RunOptions
for _, opt := range optFunc {
opt(&options)
}

var errsBuffer bytes.Buffer
for _, checker := range r.Checkers {
// run the validation
name := checker.Name()

// skip specified checker
if len(options.SkipList) > 0 && strings.IsInSlice(name, options.SkipList) {
continue
}

warnings, errs := checker.Check()

// ignore check error and append it to warnings
if strings.IsInSlice(name, options.IgnoreList) {
warnings = append(warnings, errs...)
errs = []error{}
}

for _, w := range warnings {
logrus.Warnf(fmt.Sprintf("\t[WARNING %s]: %v\n", name, w))
}

for _, i := range errs {
errsBuffer.WriteString(fmt.Sprintf("\t[ERROR %s]: %v\n", name, i.Error()))
}
}

if errsBuffer.Len() > 0 {
return fmt.Errorf("prechecked some error:%s", errsBuffer.String())
}

return nil
}
0