Skip to content
Merged
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
5 changes: 3 additions & 2 deletions pkg/cmd/init/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"open-cluster-management.io/clusteradm/pkg/cmd/init/scenario"
"open-cluster-management.io/clusteradm/pkg/helpers"
clusteradmjson "open-cluster-management.io/clusteradm/pkg/helpers/json"
preflightinterface "open-cluster-management.io/clusteradm/pkg/helpers/preflight"
version "open-cluster-management.io/clusteradm/pkg/helpers/version"
helperwait "open-cluster-management.io/clusteradm/pkg/helpers/wait"
)
Expand Down Expand Up @@ -55,8 +56,8 @@ func (o *Options) validate() error {
if err != nil {
return err
}
if err := preflight.RunChecks(
[]preflight.Checker{
if err := preflightinterface.RunChecks(
[]preflightinterface.Checker{
preflight.HubApiServerCheck{
ClusterCtx: o.ClusteradmFlags.Context,
ConfigPath: "", // TODO(@Promacanthus): user custom kubeconfig path from command line arguments.
Expand Down
54 changes: 6 additions & 48 deletions pkg/cmd/init/preflight/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
package preflight

import (
"bytes"
"context"
"fmt"
"io"
"net"
"net/url"

Expand All @@ -21,31 +18,12 @@ import (

var BootstrapConfigMap = "cluster-info"

type Error struct {
Msg string
}

func (e Error) Error() string {
return fmt.Sprintf("[preflight] Some fatal errors occurred:\n%s", e.Msg)
}

func (e *Error) Preflight() bool {
return true
}

// Checker validates the state of the cluster to ensure
// clusteradm will be successfully as often as possible.
type Checker interface {
Check() (warnings, errorList []error)
Name() string
}

type HubApiServerCheck struct {
ClusterCtx string // current-context in kubeconfig
ConfigPath string // kubeconfig file path
}

func (c HubApiServerCheck) Check() (warnings []error, errorList []error) {
func (c HubApiServerCheck) Check() (warnings []string, errorList []error) {
cluster, err := loadCurrentCluster(c.ClusterCtx, c.ConfigPath)
if err != nil {
return nil, []error{err}
Expand All @@ -59,7 +37,7 @@ func (c HubApiServerCheck) Check() (warnings []error, errorList []error) {
return nil, []error{err}
}
if net.ParseIP(host) == nil {
return []error{errors.New("Hub Api Server is a domain name, maybe you should set HostAlias in klusterlet")}, nil
return []string{"Hub Api Server is a domain name, maybe you should set HostAlias in klusterlet"}, nil
}
return nil, nil
}
Expand All @@ -77,19 +55,19 @@ type ClusterInfoCheck struct {
Client kubernetes.Interface
}

func (c ClusterInfoCheck) Check() (warnings []error, errorList []error) {
func (c ClusterInfoCheck) Check() (warnings []string, errorList []error) {
cm, err := c.Client.CoreV1().ConfigMaps(c.Namespace).Get(context.Background(), c.ResourceName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
resourceNotFound := errors.New("no ConfigMap named cluster-info in the kube-public namespace, clusteradm will creates it")
cluster, err := loadCurrentCluster(c.ClusterCtx, c.ConfigPath)
if err != nil {
return []error{resourceNotFound}, []error{err}
return []string{resourceNotFound.Error()}, []error{err}
}
if err := createClusterInfo(c.Client, cluster); err != nil {
return []error{resourceNotFound}, []error{err}
return []string{resourceNotFound.Error()}, []error{err}
}
return []error{resourceNotFound}, nil
return []string{resourceNotFound.Error()}, nil
}
return nil, []error{err}
}
Expand Down Expand Up @@ -161,23 +139,3 @@ func createClusterInfo(client kubernetes.Interface, cluster *clientcmdapi.Cluste
}
return CreateOrUpdateConfigMap(client, clusterInfo)
}

// RunChecks runs each check, display it's check/errors,
// and once all are processed will exist if any errors occured.
func RunChecks(checks []Checker, ww io.Writer) error {
var errsBuffer bytes.Buffer
for _, check := range checks {
name := check.Name()
warnings, errs := check.Check()
for _, warning := range warnings {
_, _ = io.WriteString(ww, fmt.Sprintf("\t[WARNING %s]: %v\n", name, warning))
}
for _, err := range errs {
_, _ = errsBuffer.WriteString(fmt.Sprintf("\t[ERROR %s]: %v\n", name, err.Error()))
}
}
if errsBuffer.Len() > 0 {
return &Error{Msg: errsBuffer.String()}
}
return nil
}
14 changes: 7 additions & 7 deletions pkg/cmd/init/preflight/checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestHubApiServerCheck_Check(t *testing.T) {
tests := []struct {
name string
fields fields
wantWarnings []error
wantWarnings []string
wantErrorList []error
}{
{
Expand All @@ -124,7 +124,7 @@ func TestHubApiServerCheck_Check(t *testing.T) {
ClusterCtx: "",
ConfigPath: kubeconfigFilePath,
},
wantWarnings: []error{errors.New("Hub Api Server is a domain name, maybe you should set HostAlias in klusterlet")},
wantWarnings: []string{"Hub Api Server is a domain name, maybe you should set HostAlias in klusterlet"},
wantErrorList: nil,
},
{
Expand All @@ -144,7 +144,7 @@ func TestHubApiServerCheck_Check(t *testing.T) {
ClusterCtx: currentContext,
ConfigPath: kubeconfigFilePath,
},
wantWarnings: []error{errors.New("Hub Api Server is a domain name, maybe you should set HostAlias in klusterlet")},
wantWarnings: []string{"Hub Api Server is a domain name, maybe you should set HostAlias in klusterlet"},
wantErrorList: nil,
},
{
Expand All @@ -164,7 +164,7 @@ func TestHubApiServerCheck_Check(t *testing.T) {
ConfigPath: tt.fields.ConfigPath,
}
gotWarnings, gotErrorList := c.Check()
testinghelper.AssertErrors(t, gotWarnings, tt.wantWarnings)
testinghelper.AssertWarnings(t, gotWarnings, tt.wantWarnings)
testinghelper.AssertErrors(t, gotErrorList, tt.wantErrorList)
})
}
Expand All @@ -183,7 +183,7 @@ func TestClusterInfoCheck_Check(t *testing.T) {
fields fields
actionIndex int
action string
wantWarnings []error
wantWarnings []string
wantErrorList []error
}{
{
Expand Down Expand Up @@ -225,7 +225,7 @@ func TestClusterInfoCheck_Check(t *testing.T) {
},
actionIndex: 1,
action: "create",
wantWarnings: []error{errors.New("no ConfigMap named cluster-info in the kube-public namespace, clusteradm will creates it")},
wantWarnings: []string{"no ConfigMap named cluster-info in the kube-public namespace, clusteradm will creates it"},
wantErrorList: nil,
},
}
Expand All @@ -242,7 +242,7 @@ func TestClusterInfoCheck_Check(t *testing.T) {
}
gotWarnings, gotErrorList := c.Check()
testinghelper.AssertAction(t, client.Actions()[tt.actionIndex], tt.action)
testinghelper.AssertErrors(t, gotWarnings, tt.wantWarnings)
testinghelper.AssertWarnings(t, gotWarnings, tt.wantWarnings)
testinghelper.AssertErrors(t, gotErrorList, tt.wantErrorList)
})
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/join/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ package join
import (
"fmt"

genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
"open-cluster-management.io/clusteradm/pkg/helpers"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
"open-cluster-management.io/clusteradm/pkg/helpers"
)

var example = `
Expand Down Expand Up @@ -46,6 +45,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream

cmd.Flags().StringVar(&o.token, "hub-token", "", "The token to access the hub")
cmd.Flags().StringVar(&o.hubAPIServer, "hub-apiserver", "", "The api server url to the hub")
cmd.Flags().StringVar(&o.caFile, "ca-file", "", "the file path to hub ca, optional")
cmd.Flags().StringVar(&o.clusterName, "cluster-name", "", "The name of the joining cluster")
cmd.Flags().StringVar(&o.outputFile, "output-file", "", "The generated resources will be copied in the specified file")
cmd.Flags().StringVar(&o.registry, "image-registry", "quay.io/open-cluster-management", "The name of the image registry serving OCM images.")
Expand Down
Loading