diff --git a/cmd/agent/app/agent.go b/cmd/agent/app/agent.go index 87a4f6236544..ee89085e8fd8 100644 --- a/cmd/agent/app/agent.go +++ b/cmd/agent/app/agent.go @@ -20,8 +20,6 @@ import ( "context" "flag" "fmt" - "net" - "strconv" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -80,6 +78,10 @@ func NewAgentCommand(ctx context.Context) *cobra.Command { plane and sync manifests from the Karmada control plane to the member cluster. In addition, it also syncs the status of member cluster and manifests to the Karmada control plane.`, RunE: func(_ *cobra.Command, _ []string) error { + // complete options + if err := opts.Complete(); err != nil { + return err + } // validate options if errs := opts.Validate(); len(errs) != 0 { return errs.ToAggregate() @@ -205,7 +207,7 @@ func run(ctx context.Context, opts *options.Options) error { LeaseDuration: &opts.LeaderElection.LeaseDuration.Duration, RenewDeadline: &opts.LeaderElection.RenewDeadline.Duration, RetryPeriod: &opts.LeaderElection.RetryPeriod.Duration, - HealthProbeBindAddress: net.JoinHostPort(opts.BindAddress, strconv.Itoa(opts.SecurePort)), + HealthProbeBindAddress: opts.HealthProbeBindAddress, LivenessEndpointName: "/healthz", Metrics: metricsserver.Options{BindAddress: opts.MetricsBindAddress}, MapperProvider: restmapper.MapperProvider, diff --git a/cmd/agent/app/options/options.go b/cmd/agent/app/options/options.go index 783900db58ab..5c29e4348a7f 100644 --- a/cmd/agent/app/options/options.go +++ b/cmd/agent/app/options/options.go @@ -17,7 +17,10 @@ limitations under the License. package options import ( + "errors" "fmt" + "net" + "strconv" "strings" "time" @@ -51,9 +54,11 @@ type Options struct { Controllers []string LeaderElection componentbaseconfig.LeaderElectionConfiguration // BindAddress is the IP address on which to listen for the --secure-port port. + // Deprecated BindAddress string // SecurePort is the port that the the server serves at. // Note: We hope support https in the future once controller-runtime provides the functionality. + // Deprecated SecurePort int KarmadaKubeConfig string // ClusterContext is the name of the cluster context in control plane KUBECONFIG file. @@ -104,6 +109,10 @@ type Options struct { // It can be set to "0" to disable the metrics serving. // Defaults to ":8080". MetricsBindAddress string + // HealthProbeBindAddress is the TCP address that the controller should bind to + // for serving health probes + // It can be set to "0" or "" to disable serving the health probe. + HealthProbeBindAddress string RateLimiterOpts ratelimiterflag.Options @@ -164,10 +173,14 @@ func (o *Options) AddFlags(fs *pflag.FlagSet, allControllers []string) { "A list of controllers to enable. '*' enables all on-by-default controllers, 'foo' enables the controller named 'foo', '-foo' disables the controller named 'foo'. All controllers: %s.", strings.Join(allControllers, ", "), )) - fs.StringVar(&o.BindAddress, "bind-address", defaultBindAddress, + fs.StringVar(&o.BindAddress, "bind-address", o.BindAddress, "The IP address on which to listen for the --secure-port port.") - fs.IntVar(&o.SecurePort, "secure-port", defaultPort, + fs.IntVar(&o.SecurePort, "secure-port", o.SecurePort, "The secure port on which to serve HTTPS.") + // nolint: errcheck + fs.MarkDeprecated("bind-address", "This flag is deprecated and will be removed in future releases. Use --health-probe-bind-address instead.") + // nolint: errcheck + fs.MarkDeprecated("secure-port", "This flag is deprecated and will be removed in future releases. Use --health-probe-bind-address instead.") fs.BoolVar(&o.LeaderElection.LeaderElect, "leader-elect", true, "Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.") fs.StringVar(&o.LeaderElection.ResourceNamespace, "leader-elect-resource-namespace", util.NamespaceKarmadaSystem, "The namespace of resource object that is used for locking during leader election.") fs.DurationVar(&o.LeaderElection.LeaseDuration.Duration, "leader-elect-lease-duration", defaultElectionLeaseDuration.Duration, ""+ @@ -206,6 +219,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet, allControllers []string) { fs.IntVar(&o.ConcurrentWorkSyncs, "concurrent-work-syncs", 5, "The number of Works that are allowed to sync concurrently.") fs.StringSliceVar(&o.ReportSecrets, "report-secrets", []string{"KubeCredentials", "KubeImpersonator"}, "The secrets that are allowed to be reported to the Karmada control plane during registering. Valid values are 'KubeCredentials', 'KubeImpersonator' and 'None'. e.g 'KubeCredentials,KubeImpersonator' or 'None'.") fs.StringVar(&o.MetricsBindAddress, "metrics-bind-address", ":8080", "The TCP address that the controller should bind to for serving prometheus metrics(e.g. 127.0.0.1:8080, :8080). It can be set to \"0\" to disable the metrics serving.") + fs.StringVar(&o.HealthProbeBindAddress, "health-probe-bind-address", ":0", "The TCP address that the controller should bind to for serving health probes(e.g. 127.0.0.1:10357, :10357). It can be set to \"0\" or \"\" to disable serving the health probe.") fs.StringVar(&o.ClusterProvider, "cluster-provider", "", "Provider of the joining cluster. The Karmada scheduler can use this information to spread workloads across providers for higher availability.") fs.StringVar(&o.ClusterRegion, "cluster-region", "", "The region of the joining cluster. The Karmada scheduler can use this information to spread workloads across regions for higher availability.") fs.StringSliceVar(&o.ClusterZones, "cluster-zones", []string{}, "The zones of the joining cluster. The Karmada scheduler can use this information to spread workloads across zones for higher availability.") @@ -219,3 +233,23 @@ func (o *Options) AddFlags(fs *pflag.FlagSet, allControllers []string) { features.FeatureGate.AddFlag(fs) o.ProfileOpts.AddFlags(fs) } + +// Complete ensures that options are valid and marshals them if necessary. +func (o *Options) Complete() error { + if o.BindAddress == "" && o.SecurePort == 0 { + return nil + } + if o.HealthProbeBindAddress != ":0" { + return errors.New("--bind-address and --secure-port can not co-exist with --health-probe-bind-address") + } + + if o.BindAddress == "" { + o.BindAddress = defaultBindAddress + } + if o.SecurePort == 0 { + o.SecurePort = defaultPort + } + // Prefer deprecated parameters. + o.HealthProbeBindAddress = net.JoinHostPort(o.BindAddress, strconv.Itoa(o.SecurePort)) + return nil +} diff --git a/cmd/agent/app/options/validation.go b/cmd/agent/app/options/validation.go index 789fd55afede..f561274cbb19 100644 --- a/cmd/agent/app/options/validation.go +++ b/cmd/agent/app/options/validation.go @@ -33,10 +33,6 @@ func (o *Options) Validate() field.ErrorList { errs = append(errs, field.Invalid(newPath.Child("ClusterName"), o.ClusterName, strings.Join(errMsgs, ","))) } - if o.SecurePort < 0 || o.SecurePort > 65535 { - errs = append(errs, field.Invalid(newPath.Child("SecurePort"), o.SecurePort, "must be between 0 and 65535 inclusive")) - } - if o.ClusterStatusUpdateFrequency.Duration < 0 { errs = append(errs, field.Invalid(newPath.Child("ClusterStatusUpdateFrequency"), o.ClusterStatusUpdateFrequency, "must be greater than or equal to 0")) } diff --git a/cmd/agent/app/options/validation_test.go b/cmd/agent/app/options/validation_test.go index 7398713a3cf5..29dfc2be1099 100644 --- a/cmd/agent/app/options/validation_test.go +++ b/cmd/agent/app/options/validation_test.go @@ -35,7 +35,6 @@ func New(modifyOptions ModifyOptions) Options { LeaderElect: false, }, ClusterName: "demo", - SecurePort: 8090, ClusterStatusUpdateFrequency: metav1.Duration{Duration: 10 * time.Second}, ClusterLeaseDuration: metav1.Duration{Duration: 40 * time.Second}, ClusterLeaseRenewIntervalFraction: 0.25, @@ -72,12 +71,6 @@ func TestValidateKarmadaAgentConfiguration(t *testing.T) { }), expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ClusterName"), "", "must be not empty")}, }, - "invalid SecurePort": { - opt: New(func(options *Options) { - options.SecurePort = -10 - }), - expectedErrs: field.ErrorList{field.Invalid(newPath.Child("SecurePort"), -10, "must be between 0 and 65535 inclusive")}, - }, "invalid ClusterStatusUpdateFrequency": { opt: New(func(options *Options) { options.ClusterStatusUpdateFrequency = metav1.Duration{Duration: -10 * time.Second}