Skip to content

Commit

Permalink
feat(application): set syncPeriod and concurrentSyncs by config file (#…
Browse files Browse the repository at this point in the history
…1762)

Co-authored-by: xdonggao <[email protected]>
  • Loading branch information
GaoXiaodong and xdonggao committed Feb 8, 2022
1 parent 61fef6a commit be8d413
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
10 changes: 2 additions & 8 deletions cmd/tke-application-controller/app/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ package app

import (
"net/http"
"time"

"k8s.io/apimachinery/pkg/runtime/schema"
applicationv1 "tkestack.io/tke/api/application/v1"
"tkestack.io/tke/pkg/application/controller/app"
)

const (
applicationSyncPeriod = 30 * time.Second
concurrentApplicationSyncs = 10
)

func startAppController(ctx ControllerContext) (http.Handler, bool, error) {
if !ctx.AvailableResources[schema.GroupVersionResource{Group: applicationv1.GroupName, Version: "v1", Resource: "apps"}] {
return nil, false, nil
Expand All @@ -42,11 +36,11 @@ func startAppController(ctx ControllerContext) (http.Handler, bool, error) {
ctx.PlatformClient,
ctx.Repo,
ctx.InformerFactory.Application().V1().Apps(),
applicationSyncPeriod,
ctx.Config.AppControllerConfiguration.SyncPeriod,
applicationv1.AppFinalize,
)

go ctrl.Run(concurrentApplicationSyncs, ctx.Stop)
go ctrl.Run(ctx.Config.AppControllerConfiguration.ConcurrentSyncs, ctx.Stop)

return nil, true, nil
}
4 changes: 4 additions & 0 deletions cmd/tke-application-controller/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Config struct {
// the rest config for the platform apiserver
PlatformAPIServerClientConfig *restclient.Config
RepoConfiguration appconfig.RepoConfiguration
AppControllerConfiguration appconfig.AppControllerConfiguration
}

// CreateConfigFromOptions creates a running configuration instance based
Expand Down Expand Up @@ -93,6 +94,9 @@ func CreateConfigFromOptions(serverName string, opts *options.Options) (*Config,
PlatformAPIServerClientConfig: platformAPIServerClientConfig,
}

if err := (&opts.FeatureOptions.AppController).ApplyTo(&controllerManagerConfig.AppControllerConfiguration); err != nil {
return nil, err
}
if err := (&opts.FeatureOptions.Repo).ApplyTo(&controllerManagerConfig.RepoConfiguration); err != nil {
return nil, err
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/tke-application-controller/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type ControllerContext struct {
// InformerFactory gives access to informers for the controller.
InformerFactory versionedinformers.SharedInformerFactory

// Config provides access to init options for a given controller
Config config.Config

// DeferredDiscoveryRESTMapper is a RESTMapper that will defer
// initialization of the RESTMapper until the first mapping is
// requested.
Expand Down Expand Up @@ -114,6 +117,7 @@ func CreateControllerContext(cfg *config.Config, rootClientBuilder controller.Cl
ctx := ControllerContext{
ClientBuilder: rootClientBuilder,
InformerFactory: sharedInformers,
Config: *cfg,
RESTMapper: restMapper,
AvailableResources: availableResources,
Stop: stop,
Expand Down
51 changes: 44 additions & 7 deletions cmd/tke-application-controller/app/options/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ package options

import (
"fmt"
"time"

"github.com/spf13/pflag"
"github.com/spf13/viper"
appconfig "tkestack.io/tke/pkg/application/config"
)

const (
flagConcurrentSyncs = "concurrent-app-syncs"
flagSyncAppPeriod = "sync-app-period"
flagRepoScheme = "features-repo-scheme"
flagRepoDomainSuffix = "features-repo-domain-suffix"
flagRepoCaFile = "features-repo-cafile"
Expand All @@ -35,11 +38,13 @@ const (
)

const (
configRepoScheme = "features.repo.scheme"
configRepoDomainSuffix = "features.repo.domain_suffix"
configRepoCaFile = "features.repo.cafile"
configRepoAdmin = "features.repo.admin"
configRepoAdminPassword = "features.repo.admin_password"
configSyncAppPeriod = "controller.sync_app_period"
configConcurrentAppSyncs = "controller.concurrent_app_syncs"
configRepoScheme = "features.repo.scheme"
configRepoDomainSuffix = "features.repo.domain_suffix"
configRepoCaFile = "features.repo.cafile"
configRepoAdmin = "features.repo.admin"
configRepoAdminPassword = "features.repo.admin_password"
)

// RepoOptions contains configuration items related to application attributes.
Expand All @@ -51,14 +56,26 @@ type RepoOptions struct {
AdminPassword string
}

// ControllerOptions contains configuration items related to application attributes.
type AppControllerOptions struct {
SyncAppPeriod time.Duration
ConcurrentAppSyncs int
}

// FeatureOptions contains configuration items related to application attributes.
type FeatureOptions struct {
Repo RepoOptions
Repo RepoOptions
AppController AppControllerOptions
}

// NewFeatureOptions creates a FeatureOptions object with default parameters.
func NewFeatureOptions() *FeatureOptions {
return &FeatureOptions{}
return &FeatureOptions{
AppController: AppControllerOptions{
SyncAppPeriod: defaultSyncPeriod,
ConcurrentAppSyncs: defaultconcurrentSyncs,
},
}
}

// AddFlags adds flags for console to the specified FlagSet object.
Expand All @@ -82,6 +99,12 @@ func (o *FeatureOptions) AddFlags(fs *pflag.FlagSet) {
fs.String(flagRepoAdminPassword, o.Repo.AdminPassword,
"Repo admin user password.")
_ = viper.BindPFlag(configRepoAdminPassword, fs.Lookup(flagRepoAdminPassword))

fs.DurationVar(&o.AppController.SyncAppPeriod, flagSyncAppPeriod, o.AppController.SyncAppPeriod, "The period for app health checks")
_ = viper.BindPFlag(configSyncAppPeriod, fs.Lookup(flagSyncAppPeriod))

fs.IntVar(&o.AppController.ConcurrentAppSyncs, flagConcurrentSyncs, o.AppController.ConcurrentAppSyncs, "The number of app objects that are allowed to sync concurrently. Larger number = more responsive app termination, but more CPU (and network) load")
_ = viper.BindPFlag(configConcurrentAppSyncs, fs.Lookup(flagConcurrentSyncs))
}

// ApplyFlags parsing parameters from the command line or configuration file
Expand All @@ -103,6 +126,8 @@ func (o *FeatureOptions) ApplyFlags() []error {
o.Repo.Admin = viper.GetString(configRepoAdmin)
o.Repo.AdminPassword = viper.GetString(configRepoAdminPassword)

o.AppController.SyncAppPeriod = viper.GetDuration(configSyncAppPeriod)
o.AppController.ConcurrentAppSyncs = viper.GetInt(configConcurrentAppSyncs)
return errs
}

Expand All @@ -120,3 +145,15 @@ func (o *RepoOptions) ApplyTo(cfg *appconfig.RepoConfiguration) error {

return nil
}

// ApplyTo fills up Debugging config with options.
func (o *AppControllerOptions) ApplyTo(cfg *appconfig.AppControllerConfiguration) error {
if o == nil {
return nil
}

cfg.ConcurrentSyncs = o.ConcurrentAppSyncs
cfg.SyncPeriod = o.SyncAppPeriod

return nil
}
7 changes: 7 additions & 0 deletions cmd/tke-application-controller/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@
package options

import (
"time"

"github.com/spf13/pflag"
apiserveroptions "tkestack.io/tke/pkg/apiserver/options"
controlleroptions "tkestack.io/tke/pkg/controller/options"
"tkestack.io/tke/pkg/util/log"
)

const (
defaultSyncPeriod = 30 * time.Second
defaultconcurrentSyncs = 10
)

// Options is the main context object for the TKE controller manager.
type Options struct {
Log *log.Options
Expand Down
7 changes: 7 additions & 0 deletions pkg/application/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package config

import "time"

// RepoConfiguration contains options to connect to a chart repo.
type RepoConfiguration struct {
Scheme string
Expand All @@ -26,3 +28,8 @@ type RepoConfiguration struct {
Admin string
AdminPassword string
}

type AppControllerConfiguration struct {
SyncPeriod time.Duration
ConcurrentSyncs int
}

0 comments on commit be8d413

Please sign in to comment.