karmadactl get: add validate cluster exist
Signed-off-by: helen <haitao.zhang@daocloud.io>
This commit is contained in:
parent
3cbae40434
commit
39baa63c7c
|
@ -166,6 +166,8 @@ type CommandGetOptions struct {
|
||||||
Export bool
|
Export bool
|
||||||
|
|
||||||
genericclioptions.IOStreams
|
genericclioptions.IOStreams
|
||||||
|
|
||||||
|
karmadaClient karmadaclientset.Interface
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCommandGetOptions returns a CommandGetOptions with default chunk size 500.
|
// NewCommandGetOptions returns a CommandGetOptions with default chunk size 500.
|
||||||
|
@ -230,6 +232,11 @@ func (g *CommandGetOptions) Complete(f util.Factory) error {
|
||||||
|
|
||||||
return printer.PrintObj, nil
|
return printer.PrintObj, nil
|
||||||
}
|
}
|
||||||
|
karmadaClient, err := f.KarmadaClientSet()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
g.karmadaClient = karmadaClient
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,6 +251,27 @@ func (g *CommandGetOptions) Validate(cmd *cobra.Command) error {
|
||||||
if g.OutputWatchEvents && !(g.Watch || g.WatchOnly) {
|
if g.OutputWatchEvents && !(g.Watch || g.WatchOnly) {
|
||||||
return fmt.Errorf("--output-watch-events option can only be used with --watch or --watch-only")
|
return fmt.Errorf("--output-watch-events option can only be used with --watch or --watch-only")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(g.Clusters) > 0 {
|
||||||
|
clusters, err := g.karmadaClient.ClusterV1alpha1().Clusters().List(context.TODO(), metav1.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
clusterSet := sets.NewString()
|
||||||
|
for _, cluster := range clusters.Items {
|
||||||
|
clusterSet.Insert(cluster.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
noneExistClusters := []string{}
|
||||||
|
for _, cluster := range g.Clusters {
|
||||||
|
if !clusterSet.Has(cluster) {
|
||||||
|
noneExistClusters = append(noneExistClusters, cluster)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(noneExistClusters) != 0 {
|
||||||
|
return fmt.Errorf("clusters don't exist: " + strings.Join(noneExistClusters, ","))
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,17 +315,12 @@ func (g *CommandGetOptions) Run(f util.Factory, cmd *cobra.Command, args []strin
|
||||||
|
|
||||||
RBInfo = make(map[string]*OtherPrint)
|
RBInfo = make(map[string]*OtherPrint)
|
||||||
|
|
||||||
gclient, err := f.KarmadaClientSet()
|
if err := g.getRBInKarmada(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := g.getRBInKarmada(gclient); err != nil {
|
if len(g.Clusters) == 0 {
|
||||||
return err
|
clusterList, err := g.karmadaClient.ClusterV1alpha1().Clusters().List(context.TODO(), metav1.ListOptions{})
|
||||||
}
|
|
||||||
|
|
||||||
if len(g.Clusters) <= 0 {
|
|
||||||
clusterList, err := gclient.ClusterV1alpha1().Clusters().List(context.TODO(), metav1.ListOptions{})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to list all member clusters in control plane, err: %w", err)
|
return fmt.Errorf("failed to list all member clusters in control plane, err: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -886,21 +909,21 @@ func (g *CommandGetOptions) transformRequests(req *rest.Request) {
|
||||||
}, ","))
|
}, ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *CommandGetOptions) getRBInKarmada(gclient karmadaclientset.Interface) error {
|
func (g *CommandGetOptions) getRBInKarmada() error {
|
||||||
var rbList *workv1alpha2.ResourceBindingList
|
var rbList *workv1alpha2.ResourceBindingList
|
||||||
var crbList *workv1alpha2.ClusterResourceBindingList
|
var crbList *workv1alpha2.ClusterResourceBindingList
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if !g.AllNamespaces {
|
if !g.AllNamespaces {
|
||||||
rbList, err = gclient.WorkV1alpha2().ResourceBindings(*options.DefaultConfigFlags.Namespace).List(context.TODO(), metav1.ListOptions{})
|
rbList, err = g.karmadaClient.WorkV1alpha2().ResourceBindings(*options.DefaultConfigFlags.Namespace).List(context.TODO(), metav1.ListOptions{})
|
||||||
} else {
|
} else {
|
||||||
rbList, err = gclient.WorkV1alpha2().ResourceBindings("").List(context.TODO(), metav1.ListOptions{})
|
rbList, err = g.karmadaClient.WorkV1alpha2().ResourceBindings("").List(context.TODO(), metav1.ListOptions{})
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if crbList, err = gclient.WorkV1alpha2().ClusterResourceBindings().List(context.TODO(), metav1.ListOptions{}); err != nil {
|
if crbList, err = g.karmadaClient.WorkV1alpha2().ClusterResourceBindings().List(context.TODO(), metav1.ListOptions{}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue