Skip to content

Commit

Permalink
karmadactl logs uses factory to access member cluster
Browse files Browse the repository at this point in the history
Signed-off-by: carlory <baofa.fan@daocloud.io>
  • Loading branch information
carlory committed Aug 9, 2022
1 parent d6dc32b commit 2c3c7ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
4 changes: 3 additions & 1 deletion pkg/karmadactl/karmadactl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
var (
rootCmdShort = "%s controls a Kubernetes Cluster Federation."
rootCmdLong = "%s controls a Kubernetes Cluster Federation."

defaultConfigFlags = genericclioptions.NewConfigFlags(true).WithDeprecatedPasswordFlag().WithDiscoveryBurst(300).WithDiscoveryQPS(50.0)
)

// NewKarmadaCtlCommand creates the `karmadactl` command.
Expand Down Expand Up @@ -80,7 +82,7 @@ func NewKarmadaCtlCommand(cmdUse, parentCommand string) *cobra.Command {
{
Message: "Troubleshooting and Debugging Commands:",
Commands: []*cobra.Command{
NewCmdLogs(karmadaConfig, parentCommand, ioStreams),
NewCmdLogs(parentCommand, ioStreams),
NewCmdExec(karmadaConfig, parentCommand, ioStreams),
NewCmdDescribe(karmadaConfig, parentCommand, ioStreams),
},
Expand Down
33 changes: 15 additions & 18 deletions pkg/karmadactl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"k8s.io/kubectl/pkg/util/templates"

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"
)

const (
Expand Down Expand Up @@ -50,18 +50,20 @@ var (
)

// NewCmdLogs new logs command.
func NewCmdLogs(karmadaConfig KarmadaConfig, parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
// TODO(@carlory): take a factory as an argument when we resolve conflicts on flags.
func NewCmdLogs(parentCommand string, streams genericclioptions.IOStreams) *cobra.Command {
o := &LogsOptions{
KubectlLogsOptions: kubectllogs.NewLogsOptions(streams, false),
}

f := util.NewFactory(defaultConfigFlags)
cmd := &cobra.Command{
Use: logsUsageStr,
Short: "Print the logs for a container in a pod in a cluster",
SilenceUsage: true,
Example: fmt.Sprintf(logsExample, parentCommand),
RunE: func(cmd *cobra.Command, args []string) error {
if err := o.Complete(karmadaConfig, cmd, args); err != nil {
if err := o.Complete(cmd, args, f); err != nil {
return err
}
if err := o.Validate(); err != nil {
Expand All @@ -74,25 +76,25 @@ func NewCmdLogs(karmadaConfig KarmadaConfig, parentCommand string, streams gener
},
}

o.GlobalCommandOptions.AddFlags(cmd.Flags())
flags := cmd.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")
flags.StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster")
o.KubectlLogsOptions.AddFlags(cmd)
cmd.Flags().StringVarP(&o.Namespace, "namespace", "n", o.Namespace, "If present, the namespace scope for this CLI request")
cmd.Flags().StringVarP(&o.Cluster, "cluster", "C", "", "Specify a member cluster")

return cmd
}

// LogsOptions contains the input to the logs command.
type LogsOptions struct {
// global flags
options.GlobalCommandOptions
// flags specific to logs
KubectlLogsOptions *kubectllogs.LogsOptions
Namespace string
Cluster string
}

// Complete ensures that options are valid and marshals them if necessary
func (o *LogsOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command, args []string) error {
func (o *LogsOptions) Complete(cmd *cobra.Command, args []string, f util.Factory) error {
if o.Cluster == "" {
return fmt.Errorf("must specify a cluster")
}
Expand All @@ -112,17 +114,11 @@ func (o *LogsOptions) Complete(karmadaConfig KarmadaConfig, cmd *cobra.Command,
return cmdutil.UsageErrorf(cmd, "%s", logsUsageErrStr)
}

karmadaRestConfig, err := karmadaConfig.GetRestConfig(o.KarmadaContext, o.KubeConfig)
if err != nil {
return fmt.Errorf("failed to get control plane rest config. context: %s, kube-config: %s, error: %v",
o.KarmadaContext, o.KubeConfig, err)
}
clusterInfo, err := getClusterInfo(karmadaRestConfig, o.Cluster, o.KubeConfig, o.KarmadaContext)
memberFactory, err := f.FactoryForMemberCluster(o.Cluster)
if err != nil {
return err
}
f := getFactory(o.Cluster, clusterInfo, o.Namespace)
return o.KubectlLogsOptions.Complete(f, cmd, args)
return o.KubectlLogsOptions.Complete(memberFactory, cmd, args)
}

// Validate checks to the LogsOptions to see if there is sufficient information run the command
Expand All @@ -136,6 +132,7 @@ func (o *LogsOptions) Run() error {
}

// getClusterInfo get information of cluster
// TODO(@carlory): remove it when all sub command accepts factory as input parameter.
func getClusterInfo(karmadaRestConfig *rest.Config, clusterName, kubeConfig, karmadaContext string) (map[string]*ClusterInfo, error) {
clusterClient := karmadaclientset.NewForConfigOrDie(karmadaRestConfig).ClusterV1alpha1().Clusters()

Expand Down

0 comments on commit 2c3c7ea

Please sign in to comment.