sigs.k8s.io controller-runtime系列之九 config分析
简介之前介绍过sigs.k8s.io controller-runtime系列之八cluster分析sigs.k8s.io controller-runtime-cluster 。本文主要介绍pkg/config的源码分析。主要为controllerManager中的属性提供默认配置目录结构config.goControllerManagerConfiguration结构体// 定义了解析conf
·
简介
之前介绍过sigs.k8s.io controller-runtime系列之八 cluster分析sigs.k8s.io controller-runtime-cluster 。
本文主要介绍pkg/config的源码分析。主要为controllerManager中的属性提供默认配置
目录结构
- config.go
- ControllerManagerConfiguration结构体
// 定义了解析config文件必须的方法并且为ctrl.Manager配置Options type ControllerManagerConfiguration interface { runtime.Object // 返回版本化的配置 Complete() (v1alpha1.ControllerManagerConfigurationSpec, error) }
- DeferredFileLoader结构体 用于配置加载控制器的解码器运行时组件的配置类型
type DeferredFileLoader struct { ControllerManagerConfiguration // config文件的路径 path string scheme *runtime.Scheme once sync.Once err error }
- File函数 构建一个DeferredFileLoader指针对象
func File() *DeferredFileLoader { scheme := runtime.NewScheme() utilruntime.Must(v1alpha1.AddToScheme(scheme)) return &DeferredFileLoader{ path: "./config.yaml", ControllerManagerConfiguration: &v1alpha1.ControllerManagerConfiguration{}, scheme: scheme, } }
- Complete函数 获取一个版本化的configuration
// 使用sync.Once来执行该方法 func (d *DeferredFileLoader) Complete() (v1alpha1.ControllerManagerConfigurationSpec, error) { d.once.Do(d.loadFile) if d.err != nil { return v1alpha1.ControllerManagerConfigurationSpec{}, d.err } return d.ControllerManagerConfiguration.Complete() }
- DeferredFileLoader指针修改其属性的一些函数
// 修改path func (d *DeferredFileLoader) AtPath(path string) *DeferredFileLoader { d.path = path return d } // 修改ControllerManagerConfiguration func (d *DeferredFileLoader) OfKind(obj ControllerManagerConfiguration) *DeferredFileLoader { d.ControllerManagerConfiguration = obj return d } // 修改scheme func (d *DeferredFileLoader) InjectScheme(scheme *runtime.Scheme) error { d.scheme = scheme return nil } // 加载config文件 func (d *DeferredFileLoader) loadFile() { if d.scheme == nil { d.err = fmt.Errorf("scheme not supplied to controller configuration loader") return } content, err := ioutil.ReadFile(d.path) if err != nil { d.err = fmt.Errorf("could not read file at %s", d.path) return } codecs := serializer.NewCodecFactory(d.scheme) // 通过decoder把content转化为制定的结构体d.ControllerManagerConfiguration if err = runtime.DecodeInto(codecs.UniversalDecoder(), content, d.ControllerManagerConfiguration); err != nil { d.err = fmt.Errorf("could not decode file into runtime.Object") } return }
- v1alpha1/types.go
- ControllerManagerConfigurationSpec结构体 定义了ControllerManagerConfiguration中除了meta信息以外的规格信息
// ControllerManagerConfigurationSpec defines the desired state of GenericControllerManagerConfiguration type ControllerManagerConfigurationSpec struct { // reconciled的同步频率 SyncPeriod *metav1.Duration `json:"syncPeriod,omitempty"` // 为manager.Manager提供配置信息 LeaderElection *configv1alpha1.LeaderElectionConfiguration `json:"leaderElection,omitempty"` // 限定了manager的cache监视的对象,默认是全部命名空间 // // Note: 有的指定了命名空间,但是controllers还是可以监视全部命名空间 (例如 Node). // 对于制定命名空间的,cache仅仅可以监视对应命名空间的对象 CacheNamespace string `json:"cacheNamespace,omitempty"` // 是在管理器实际返回stop之前为runnable to stop指定的持续时间。 // 要禁用正常关机,请设置为time.Duration(0) // 若要使用无超时的正常关机,请将持续时间设置为负,例如time.duration(-1) // 出于安全原因,为防止leader选举租约丢失,将跳过正常关机 GracefulShutdownTimeout *metav1.Duration `json:"gracefulShutDown,omitempty"` // 为注册到manager的controller提供一个全局的配置项 Controller *ControllerConfigurationSpec `json:"controller,omitempty"` // 为controller metrics提供一个配置 Metrics ControllerMetrics `json:"metrics,omitempty"` // 为controller health 提供一个配置 Health ControllerHealth `json:"health,omitempty"` // 为controllers webhook提供一个配置 Webhook ControllerWebhook `json:"webhook,omitempty"` } // 为注册到manager的controller提供一个全局的配置项 type ControllerConfigurationSpec struct { // map key: kind value:controller并发reconcile的数量 // 使用生成器实用程序在此管理器中注册控制器时, // 用户必须指定控制器在For(…)调用中协调的类型。 // 如果传递的对象的种类与此映射中的一个键匹配,则并发性 // 为该控制器设置为指定的数字。 GroupKindConcurrency map[string]int `json:"groupKindConcurrency,omitempty"` // 等待同步cache的过期时长 // 默认是2分钟 CacheSyncTimeout *time.Duration `json:"cacheSyncTimeout,omitempty"` } // 定义metrics的配置 type ControllerMetrics struct { // controller绑定到prometheus服务器的地址,用来 // 0表示禁用metrics serving BindAddress string `json:"bindAddress,omitempty"` } // 定义health的配置 type ControllerHealth struct { // 控制器应绑定到的TCP地址来提供健康探头 HealthProbeBindAddress string `json:"healthProbeBindAddress,omitempty"` // 默认是 "readyz" ReadinessEndpointName string `json:"readinessEndpointName,omitempty"` // 默认是 "healthz" LivenessEndpointName string `json:"livenessEndpointName,omitempty"` } // 定义了controller的webhook server配置 type ControllerWebhook struct { // webhook server 的端口 Port *int `json:"port,omitempty"` // webhook server的hostname Host string `json:"host,omitempty"` // 证书的path // 默认是{TempDir}/k8s-webhook-server/serving-certs. 并且文件名称必须是tls.key 和 tls.crt. CertDir string `json:"certDir,omitempty"` }
- ControllerManagerConfiguration结构体
type ControllerManagerConfiguration struct { metav1.TypeMeta `json:",inline"` // controllers的配置文件 ControllerManagerConfigurationSpec `json:",inline"` }
更多推荐
已为社区贡献15条内容
所有评论(0)