Introduce --karmada-context flag to karmada-agent to select context in

kubeconfig file

Signed-off-by: lihanbo <lihanbo2@huawei.com>
This commit is contained in:
lihanbo 2021-06-03 20:10:35 +08:00
parent bd0d3b8a0c
commit 280abfe468
5 changed files with 14 additions and 8 deletions

View File

@ -26,6 +26,7 @@ spec:
command: command:
- /bin/karmada-agent - /bin/karmada-agent
- --karmada-kubeconfig=/etc/kubeconfig/karmada-kubeconfig - --karmada-kubeconfig=/etc/kubeconfig/karmada-kubeconfig
- --karmada-context=karmada-apiserver
- --cluster-name={{member_cluster_name}} - --cluster-name={{member_cluster_name}}
- --cluster-status-update-frequency=10s - --cluster-status-update-frequency=10s
- --v=4 - --v=4

View File

@ -30,12 +30,13 @@ import (
// NewAgentCommand creates a *cobra.Command object with default parameters // NewAgentCommand creates a *cobra.Command object with default parameters
func NewAgentCommand(ctx context.Context) *cobra.Command { func NewAgentCommand(ctx context.Context) *cobra.Command {
opts := options.NewOptions() opts := options.NewOptions()
karmadaConfig := karmadactl.NewKarmadaConfig(clientcmd.NewDefaultPathOptions())
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "karmada-agent", Use: "karmada-agent",
Long: `The karmada agent runs the cluster registration agent`, Long: `The karmada agent runs the cluster registration agent`,
Run: func(cmd *cobra.Command, args []string) { 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) fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1) os.Exit(1)
} }
@ -47,8 +48,8 @@ func NewAgentCommand(ctx context.Context) *cobra.Command {
return cmd return cmd
} }
func run(ctx context.Context, opts *options.Options) error { func run(ctx context.Context, karmadaConfig karmadactl.KarmadaConfig, opts *options.Options) error {
controlPlaneRestConfig, err := clientcmd.BuildConfigFromFlags("", opts.KarmadaKubeConfig) controlPlaneRestConfig, err := karmadaConfig.GetRestConfig(opts.KarmadaContext, opts.KarmadaKubeConfig)
if err != nil { if err != nil {
return fmt.Errorf("error building kubeconfig of karmada control plane: %s", err.Error()) return fmt.Errorf("error building kubeconfig of karmada control plane: %s", err.Error())
} }

View File

@ -10,7 +10,10 @@ import (
// Options contains everything necessary to create and run controller-manager. // Options contains everything necessary to create and run controller-manager.
type Options struct { type Options struct {
KarmadaKubeConfig string 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. // 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 // 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 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.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.") 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.")
} }

View File

@ -14,7 +14,7 @@ source ${REPO_ROOT}/hack/util.sh
function usage() { function usage() {
echo "This script will deploy karmada agent to a cluster." echo "This script will deploy karmada agent to a cluster."
echo "Usage: hack/deploy-karmada-agent.sh" 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" export REGISTRY="swr.ap-southeast-1.myhuaweicloud.com/karmada"

View File

@ -24,8 +24,8 @@ type GlobalCommandOptions struct {
// AddFlags adds flags to the specified FlagSet. // AddFlags adds flags to the specified FlagSet.
func (o *GlobalCommandOptions) AddFlags(flags *pflag.FlagSet) { func (o *GlobalCommandOptions) AddFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.KubeConfig, "kubeconfig", "", "Path to the 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.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.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.") flags.BoolVar(&o.DryRun, "dry-run", false, "Run the command in dry-run mode, without making any server requests.")
} }