diff --git a/pkg/karmadactl/exec/exec.go b/pkg/karmadactl/exec/exec.go index 663478d5d..0df1be5d6 100644 --- a/pkg/karmadactl/exec/exec.go +++ b/pkg/karmadactl/exec/exec.go @@ -36,27 +36,30 @@ const ( var ( execLong = templates.LongDesc(` - Execute a command in a container in a cluster.`) + Execute a command in a container.`) execExample = templates.Examples(` + # Get output from running the 'date' command from pod mypod, using the first container by default + %[1]s exec mypod -- date + # Get output from running the 'date' command from pod mypod, using the first container by default in cluster(member1) - %[1]s exec mypod -C=member1 -- date + %[1]s exec mypod --operation-scope=members --cluster=member1 -- date # Get output from running the 'date' command in ruby-container from pod mypod in cluster(member1) - %[1]s exec mypod -c ruby-container -C=member1 -- date + %[1]s exec mypod -c ruby-container --operation-scope=members --cluster=member1 -- date # Get output from running the 'date' command in ruby-container from pod mypod in cluster(member1) - %[1]s exec mypod -c ruby-container -C=member1 -- date + %[1]s exec mypod -c ruby-container --operation-scope=members --cluster=member1 -- date - # Switch to raw terminal mode; sends stdin to 'bash' in ruby-container from pod mypod in cluster(member1) + # Switch to raw terminal mode; sends stdin to 'bash' in ruby-container from pod mypod # and sends stdout/stderr from 'bash' back to the client - %[1]s exec mypod -c ruby-container -C=member1 -i -t -- bash -il + %[1]s exec mypod -c ruby-container -i -t -- bash -il # Get output from running 'date' command from the first pod of the deployment mydeployment, using the first container by default in cluster(member1) - %[1]s exec deploy/mydeployment -C=member1 -- date + %[1]s exec deploy/mydeployment --operation-scope=members --cluster=member1 -- date # Get output from running 'date' command from the first pod of the service myservice, using the first container by default in cluster(member1) - %[1]s exec svc/myservice -C=member1 -- date`) + %[1]s exec svc/myservice --operation-scope=members --cluster=member1 -- date`) ) // NewCmdExec new exec command. @@ -95,6 +98,7 @@ func NewCmdExec(f util.Factory, parentCommand string, streams genericiooptions.I }, } + o.OperationScope = options.KarmadaControlPlane flags := cmd.Flags() options.AddKubeConfigFlags(flags) flags.StringVarP(options.DefaultConfigFlags.Namespace, "namespace", "n", *options.DefaultConfigFlags.Namespace, "If present, the namespace scope for this CLI request") @@ -105,7 +109,8 @@ func NewCmdExec(f util.Factory, parentCommand string, streams genericiooptions.I flags.BoolVarP(&o.KubectlExecOptions.Stdin, "stdin", "i", o.KubectlExecOptions.Stdin, "Pass stdin to the container") flags.BoolVarP(&o.KubectlExecOptions.TTY, "tty", "t", o.KubectlExecOptions.TTY, "Stdin is a TTY") flags.BoolVarP(&o.KubectlExecOptions.Quiet, "quiet", "q", o.KubectlExecOptions.Quiet, "Only print output from the remote session") - flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster") + flags.Var(&o.OperationScope, "operation-scope", "Used to control the operation scope of the command. The optional values are karmada and members. Defaults to karmada.") + flags.StringVar(&o.Cluster, "cluster", "", "Used to specify a target member cluster and only takes effect when the command's operation scope is members, for example: --operation-scope=members --cluster=member1") return cmd } @@ -114,22 +119,31 @@ type CommandExecOptions struct { // flags specific to exec KubectlExecOptions *kubectlexec.ExecOptions Cluster string + OperationScope options.OperationScope } // Complete verifies command line arguments and loads data from the command environment func (o *CommandExecOptions) Complete(f util.Factory, cmd *cobra.Command, argsIn []string, argsLenAtDash int) error { - if len(o.Cluster) == 0 { - return fmt.Errorf("must specify a cluster") + var execFactory cmdutil.Factory = f + if o.OperationScope == options.Members && len(o.Cluster) != 0 { + memberFactory, err := f.FactoryForMemberCluster(o.Cluster) + if err != nil { + return err + } + execFactory = memberFactory } - memberFactory, err := f.FactoryForMemberCluster(o.Cluster) - if err != nil { - return err - } - return o.KubectlExecOptions.Complete(memberFactory, cmd, argsIn, argsLenAtDash) + return o.KubectlExecOptions.Complete(execFactory, cmd, argsIn, argsLenAtDash) } // Validate checks that the provided exec options are specified. func (o *CommandExecOptions) Validate() error { + err := options.VerifyOperationScopeFlags(o.OperationScope, options.KarmadaControlPlane, options.Members) + if err != nil { + return err + } + if o.OperationScope == options.Members && len(o.Cluster) == 0 { + return fmt.Errorf("must specify a member cluster") + } return o.KubectlExecOptions.Validate() } diff --git a/test/e2e/karmadactl_test.go b/test/e2e/karmadactl_test.go index ad2752fda..1ecf71347 100644 --- a/test/e2e/karmadactl_test.go +++ b/test/e2e/karmadactl_test.go @@ -529,7 +529,7 @@ var _ = ginkgo.Describe("Karmadactl exec testing", func() { }) for _, clusterName := range framework.ClusterNames() { - cmd := framework.NewKarmadactlCommand(kubeconfig, karmadaContext, karmadactlPath, pod.Namespace, karmadactlTimeout, "exec", pod.Name, "-C", clusterName, "--", "echo", "hello") + cmd := framework.NewKarmadactlCommand(kubeconfig, karmadaContext, karmadactlPath, pod.Namespace, karmadactlTimeout, "exec", pod.Name, "--operation-scope", "members", "--cluster", clusterName, "--", "echo", "hello") _, err := cmd.ExecOrDie() gomega.Expect(err).ShouldNot(gomega.HaveOccurred()) }