Merge pull request #2611 from helen-frank/feature/cordonFactory
karmadactl cordon&uncordon uses factory to access cluster
This commit is contained in:
commit
3e8ed1db96
|
@ -15,7 +15,7 @@ import (
|
||||||
|
|
||||||
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
|
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
|
||||||
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
|
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -42,7 +42,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewCmdCordon defines the `cordon` command that mark cluster as unschedulable.
|
// NewCmdCordon defines the `cordon` command that mark cluster as unschedulable.
|
||||||
func NewCmdCordon(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Command {
|
func NewCmdCordon(f util.Factory, parentCommand string) *cobra.Command {
|
||||||
opts := CommandCordonOption{}
|
opts := CommandCordonOption{}
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "cordon CLUSTER",
|
Use: "cordon CLUSTER",
|
||||||
|
@ -55,7 +55,7 @@ func NewCmdCordon(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Comm
|
||||||
if err := opts.Complete(args); err != nil {
|
if err := opts.Complete(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := RunCordonOrUncordon(DesiredCordon, karmadaConfig, opts); err != nil {
|
if err := RunCordonOrUncordon(DesiredCordon, f, opts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -63,15 +63,16 @@ func NewCmdCordon(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Comm
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
opts.GlobalCommandOptions.AddFlags(flags)
|
|
||||||
|
|
||||||
flags.BoolVar(&opts.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.")
|
flags.BoolVar(&opts.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.")
|
||||||
|
flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
|
||||||
|
flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use")
|
||||||
|
flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCmdUncordon defines the `uncordon` command that mark cluster as schedulable.
|
// NewCmdUncordon defines the `uncordon` command that mark cluster as schedulable.
|
||||||
func NewCmdUncordon(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Command {
|
func NewCmdUncordon(f util.Factory, parentCommand string) *cobra.Command {
|
||||||
opts := CommandCordonOption{}
|
opts := CommandCordonOption{}
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "uncordon CLUSTER",
|
Use: "uncordon CLUSTER",
|
||||||
|
@ -84,7 +85,7 @@ func NewCmdUncordon(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Co
|
||||||
if err := opts.Complete(args); err != nil {
|
if err := opts.Complete(args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := RunCordonOrUncordon(DesiredUnCordon, karmadaConfig, opts); err != nil {
|
if err := RunCordonOrUncordon(DesiredUnCordon, f, opts); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -92,16 +93,15 @@ func NewCmdUncordon(karmadaConfig KarmadaConfig, parentCommand string) *cobra.Co
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
opts.AddFlags(flags)
|
flags.StringVar(defaultConfigFlags.KubeConfig, "kubeconfig", *defaultConfigFlags.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
|
||||||
|
flags.StringVar(defaultConfigFlags.Context, "karmada-context", *defaultConfigFlags.Context, "The name of the kubeconfig context to use")
|
||||||
|
flags.StringVarP(defaultConfigFlags.Namespace, "namespace", "n", *defaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandCordonOption holds all command options for cordon and uncordon
|
// CommandCordonOption holds all command options for cordon and uncordon
|
||||||
type CommandCordonOption struct {
|
type CommandCordonOption struct {
|
||||||
// global flags
|
|
||||||
options.GlobalCommandOptions
|
|
||||||
|
|
||||||
// ClusterName is the cluster's name that we are going to join with.
|
// ClusterName is the cluster's name that we are going to join with.
|
||||||
ClusterName string
|
ClusterName string
|
||||||
|
|
||||||
|
@ -170,8 +170,8 @@ func (c *CordonHelper) hasUnschedulerTaint() bool {
|
||||||
// PatchOrReplace uses given karmada clientset to update the cluster unschedulable scheduler, either by patching or
|
// PatchOrReplace uses given karmada clientset to update the cluster unschedulable scheduler, either by patching or
|
||||||
// updating the given cluster object; it may return error if the object cannot be encoded as
|
// updating the given cluster object; it may return error if the object cannot be encoded as
|
||||||
// JSON, or if either patch or update calls fail; it will also return error whenever creating a patch has failed
|
// JSON, or if either patch or update calls fail; it will also return error whenever creating a patch has failed
|
||||||
func (c *CordonHelper) PatchOrReplace(controlPlaneClient *karmadaclientset.Clientset) error {
|
func (c *CordonHelper) PatchOrReplace(karmadaClient karmadaclientset.Interface) error {
|
||||||
client := controlPlaneClient.ClusterV1alpha1().Clusters()
|
client := karmadaClient.ClusterV1alpha1().Clusters()
|
||||||
oldData, err := json.Marshal(c.cluster)
|
oldData, err := json.Marshal(c.cluster)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -212,22 +212,18 @@ func (c *CordonHelper) PatchOrReplace(controlPlaneClient *karmadaclientset.Clien
|
||||||
|
|
||||||
// RunCordonOrUncordon exec marks the cluster unschedulable or schedulable according to desired.
|
// RunCordonOrUncordon exec marks the cluster unschedulable or schedulable according to desired.
|
||||||
// if true cordon cluster otherwise uncordon cluster.
|
// if true cordon cluster otherwise uncordon cluster.
|
||||||
func RunCordonOrUncordon(desired int, karmadaConfig KarmadaConfig, opts CommandCordonOption) error {
|
func RunCordonOrUncordon(desired int, f util.Factory, opts CommandCordonOption) error {
|
||||||
cordonOrUncordon := "cordon"
|
cordonOrUncordon := "cordon"
|
||||||
if desired == DesiredUnCordon {
|
if desired == DesiredUnCordon {
|
||||||
cordonOrUncordon = "un" + cordonOrUncordon
|
cordonOrUncordon = "un" + cordonOrUncordon
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get control plane kube-apiserver client
|
karmadaClient, err := f.KarmadaClientSet()
|
||||||
controlPlaneRestConfig, err := karmadaConfig.GetRestConfig(opts.KarmadaContext, opts.KubeConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get control plane rest config. context: %s, kube-config: %s, error: %v",
|
return err
|
||||||
opts.KarmadaContext, opts.KubeConfig, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
controlPlaneKarmadaClient := karmadaclientset.NewForConfigOrDie(controlPlaneRestConfig)
|
cluster, err := karmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), opts.ClusterName, metav1.GetOptions{})
|
||||||
|
|
||||||
cluster, err := controlPlaneKarmadaClient.ClusterV1alpha1().Clusters().Get(context.TODO(), opts.ClusterName, metav1.GetOptions{})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -239,7 +235,7 @@ func RunCordonOrUncordon(desired int, karmadaConfig KarmadaConfig, opts CommandC
|
||||||
}
|
}
|
||||||
|
|
||||||
if !opts.DryRun {
|
if !opts.DryRun {
|
||||||
err := cordonHelper.PatchOrReplace(controlPlaneKarmadaClient)
|
err := cordonHelper.PatchOrReplace(karmadaClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,8 +79,8 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
|
||||||
{
|
{
|
||||||
Message: "Cluster Management Commands:",
|
Message: "Cluster Management Commands:",
|
||||||
Commands: []*cobra.Command{
|
Commands: []*cobra.Command{
|
||||||
NewCmdCordon(karmadaConfig, parentCommand),
|
NewCmdCordon(f, parentCommand),
|
||||||
NewCmdUncordon(karmadaConfig, parentCommand),
|
NewCmdUncordon(f, parentCommand),
|
||||||
NewCmdTaint(karmadaConfig, parentCommand),
|
NewCmdTaint(karmadaConfig, parentCommand),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -14,6 +14,7 @@ import (
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/util/rand"
|
"k8s.io/apimachinery/pkg/util/rand"
|
||||||
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
@ -23,6 +24,7 @@ import (
|
||||||
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl"
|
"github.com/karmada-io/karmada/pkg/karmadactl"
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
||||||
|
cmdutil "github.com/karmada-io/karmada/pkg/karmadactl/util"
|
||||||
"github.com/karmada-io/karmada/pkg/util"
|
"github.com/karmada-io/karmada/pkg/util"
|
||||||
khelper "github.com/karmada-io/karmada/pkg/util/helper"
|
khelper "github.com/karmada-io/karmada/pkg/util/helper"
|
||||||
"github.com/karmada-io/karmada/pkg/util/names"
|
"github.com/karmada-io/karmada/pkg/util/names"
|
||||||
|
@ -463,6 +465,7 @@ var _ = framework.SerialDescribe("Karmadactl cordon/uncordon testing", ginkgo.La
|
||||||
var kubeConfigPath string
|
var kubeConfigPath string
|
||||||
var clusterContext string
|
var clusterContext string
|
||||||
var karmadaConfig karmadactl.KarmadaConfig
|
var karmadaConfig karmadactl.KarmadaConfig
|
||||||
|
var f cmdutil.Factory
|
||||||
|
|
||||||
ginkgo.BeforeEach(func() {
|
ginkgo.BeforeEach(func() {
|
||||||
clusterName = "member-e2e-" + rand.String(3)
|
clusterName = "member-e2e-" + rand.String(3)
|
||||||
|
@ -472,6 +475,9 @@ var _ = framework.SerialDescribe("Karmadactl cordon/uncordon testing", ginkgo.La
|
||||||
clusterContext = fmt.Sprintf("kind-%s", clusterName)
|
clusterContext = fmt.Sprintf("kind-%s", clusterName)
|
||||||
|
|
||||||
karmadaConfig = karmadactl.NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
|
karmadaConfig = karmadactl.NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
|
||||||
|
defaultConfigFlags := genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag().WithDiscoveryBurst(300).WithDiscoveryQPS(50.0)
|
||||||
|
defaultConfigFlags.Context = &karmadaContext
|
||||||
|
f = cmdutil.NewFactory(defaultConfigFlags)
|
||||||
})
|
})
|
||||||
|
|
||||||
ginkgo.BeforeEach(func() {
|
ginkgo.BeforeEach(func() {
|
||||||
|
@ -527,13 +533,10 @@ var _ = framework.SerialDescribe("Karmadactl cordon/uncordon testing", ginkgo.La
|
||||||
ginkgo.Context("cordon/uncordon cluster taint check", func() {
|
ginkgo.Context("cordon/uncordon cluster taint check", func() {
|
||||||
ginkgo.BeforeEach(func() {
|
ginkgo.BeforeEach(func() {
|
||||||
opts := karmadactl.CommandCordonOption{
|
opts := karmadactl.CommandCordonOption{
|
||||||
GlobalCommandOptions: options.GlobalCommandOptions{
|
|
||||||
KarmadaContext: karmadaContext,
|
|
||||||
},
|
|
||||||
DryRun: false,
|
DryRun: false,
|
||||||
ClusterName: clusterName,
|
ClusterName: clusterName,
|
||||||
}
|
}
|
||||||
err := karmadactl.RunCordonOrUncordon(karmadactl.DesiredCordon, karmadaConfig, opts)
|
err := karmadactl.RunCordonOrUncordon(karmadactl.DesiredCordon, f, opts)
|
||||||
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
|
||||||
|
|
||||||
})
|
})
|
||||||
|
@ -554,13 +557,10 @@ var _ = framework.SerialDescribe("Karmadactl cordon/uncordon testing", ginkgo.La
|
||||||
|
|
||||||
ginkgo.It(fmt.Sprintf("cluster %s should not have unschedulable:NoSchedule taint", clusterName), func() {
|
ginkgo.It(fmt.Sprintf("cluster %s should not have unschedulable:NoSchedule taint", clusterName), func() {
|
||||||
opts := karmadactl.CommandCordonOption{
|
opts := karmadactl.CommandCordonOption{
|
||||||
GlobalCommandOptions: options.GlobalCommandOptions{
|
|
||||||
KarmadaContext: karmadaContext,
|
|
||||||
},
|
|
||||||
DryRun: false,
|
DryRun: false,
|
||||||
ClusterName: clusterName,
|
ClusterName: clusterName,
|
||||||
}
|
}
|
||||||
err := karmadactl.RunCordonOrUncordon(karmadactl.DesiredUnCordon, karmadaConfig, opts)
|
err := karmadactl.RunCordonOrUncordon(karmadactl.DesiredUnCordon, f, opts)
|
||||||
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
|
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
|
||||||
|
|
||||||
ginkgo.By(fmt.Sprintf("cluster %s taint(unschedulable:NoSchedule) will be removed", clusterName), func() {
|
ginkgo.By(fmt.Sprintf("cluster %s taint(unschedulable:NoSchedule) will be removed", clusterName), func() {
|
||||||
|
|
Loading…
Reference in New Issue