diff --git a/artifacts/agent/karmada-agent.yaml b/artifacts/agent/karmada-agent.yaml index b1b4719b2..2e5d2c39f 100644 --- a/artifacts/agent/karmada-agent.yaml +++ b/artifacts/agent/karmada-agent.yaml @@ -26,6 +26,7 @@ spec: command: - /bin/karmada-agent - --karmada-kubeconfig=/etc/kubeconfig/karmada-kubeconfig + - --karmada-context=karmada-apiserver - --cluster-name={{member_cluster_name}} - --cluster-status-update-frequency=10s - --v=4 diff --git a/cmd/agent/app/agent.go b/cmd/agent/app/agent.go index cd9043818..4f4067962 100644 --- a/cmd/agent/app/agent.go +++ b/cmd/agent/app/agent.go @@ -30,12 +30,13 @@ import ( // NewAgentCommand creates a *cobra.Command object with default parameters func NewAgentCommand(ctx context.Context) *cobra.Command { opts := options.NewOptions() + karmadaConfig := karmadactl.NewKarmadaConfig(clientcmd.NewDefaultPathOptions()) cmd := &cobra.Command{ Use: "karmada-agent", Long: `The karmada agent runs the cluster registration agent`, Run: func(cmd *cobra.Command, args []string) { - if err := run(ctx, opts); err != nil { + if err := run(ctx, karmadaConfig, opts); err != nil { fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } @@ -47,8 +48,8 @@ func NewAgentCommand(ctx context.Context) *cobra.Command { return cmd } -func run(ctx context.Context, opts *options.Options) error { - controlPlaneRestConfig, err := clientcmd.BuildConfigFromFlags("", opts.KarmadaKubeConfig) +func run(ctx context.Context, karmadaConfig karmadactl.KarmadaConfig, opts *options.Options) error { + controlPlaneRestConfig, err := karmadaConfig.GetRestConfig(opts.KarmadaContext, opts.KarmadaKubeConfig) if err != nil { return fmt.Errorf("error building kubeconfig of karmada control plane: %s", err.Error()) } diff --git a/cmd/agent/app/options/options.go b/cmd/agent/app/options/options.go index 01f0b6b54..f5e5bc547 100644 --- a/cmd/agent/app/options/options.go +++ b/cmd/agent/app/options/options.go @@ -10,7 +10,10 @@ import ( // Options contains everything necessary to create and run controller-manager. type Options struct { KarmadaKubeConfig string - ClusterName string + // ClusterContext is the name of the cluster context in control plane KUBECONFIG file. + // Default value is the current-context. + KarmadaContext string + ClusterName string // ClusterStatusUpdateFrequency is the frequency that karmada-agent computes cluster status. // If cluster lease feature is not enabled, it is also the frequency that karmada-agent posts cluster status @@ -30,7 +33,8 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) { return } - fs.StringVar(&o.KarmadaKubeConfig, "karmada-kubeconfig", o.KarmadaKubeConfig, "Path to karmada kubeconfig.") + fs.StringVar(&o.KarmadaKubeConfig, "karmada-kubeconfig", o.KarmadaKubeConfig, "Path to karmada control plane kubeconfig file.") + fs.StringVar(&o.KarmadaContext, "karmada-context", "", "Name of the cluster context in karmada control plane kubeconfig file.") fs.StringVar(&o.ClusterName, "cluster-name", o.ClusterName, "Name of member cluster that the agent serves for.") fs.DurationVar(&o.ClusterStatusUpdateFrequency.Duration, "cluster-status-update-frequency", 10*time.Second, "Specifies how often karmada-agent posts cluster status to karmada-apiserver. Note: be cautious when changing the constant, it must work with ClusterMonitorGracePeriod in karmada-controller-manager.") } diff --git a/hack/deploy-karmada-agent.sh b/hack/deploy-karmada-agent.sh index c94604cf6..b2552b4a8 100755 --- a/hack/deploy-karmada-agent.sh +++ b/hack/deploy-karmada-agent.sh @@ -14,7 +14,7 @@ source ${REPO_ROOT}/hack/util.sh function usage() { echo "This script will deploy karmada agent to a cluster." echo "Usage: hack/deploy-karmada-agent.sh" - echo "Example: hack/deploy-karmada.sh" + echo "Example: hack/deploy-karmada-agent.sh" } export REGISTRY="swr.ap-southeast-1.myhuaweicloud.com/karmada" diff --git a/pkg/karmadactl/options/global.go b/pkg/karmadactl/options/global.go index a1866bfac..324fce42c 100644 --- a/pkg/karmadactl/options/global.go +++ b/pkg/karmadactl/options/global.go @@ -24,8 +24,8 @@ type GlobalCommandOptions struct { // AddFlags adds flags to the specified FlagSet. func (o *GlobalCommandOptions) AddFlags(flags *pflag.FlagSet) { - flags.StringVar(&o.KubeConfig, "kubeconfig", "", "Path to the control plane KUBECONFIG file.") - flags.StringVar(&o.KarmadaContext, "karmada-context", "", "Name of the cluster context in control plane KUBECONFIG file.") + flags.StringVar(&o.KubeConfig, "kubeconfig", "", "Path to the control plane kubeconfig file.") + flags.StringVar(&o.KarmadaContext, "karmada-context", "", "Name of the cluster context in control plane kubeconfig file.") flags.StringVar(&o.ClusterNamespace, "cluster-namespace", DefaultKarmadaClusterNamespace, "Namespace in the control plane where member cluster are stored.") flags.BoolVar(&o.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.") }