flannel源码分析--newSubnetManager

main函数中调用newSubnetManager创建子网管理,具体流程如下;

  • 如果命令行opts中指定了子网管理,则通过命令行中的参数调用NewSubnetManager

  • BuildConfigFromFlags尝试从一个master url或者kubeconfig配置文件中构建配置,它们作为集群组件的命令行标志传。如果没有配置master url和kubeconfig文件路径,会回调InClusterConfig,若失败,则会使用默认etcd配置

  • NewNonInteractiveDeferredLoadingClientConfig调用创建一个DeferredLoadingClientConfig结构体变量,结构体变量如下:

    type DeferredLoadingClientConfig struct {
    	loader         ClientConfigLoader
    	overrides      *ConfigOverrides
    	fallbackReader io.Reader
    
    	clientConfig ClientConfig
    	loadingLock  sync.Mutex
    
    	// provided for testing
    	icc InClusterConfig
    }
    
  • NewForConfig根据配置创建一个新的Clientset,结构如下:

    type Clientset struct {
    	*discovery.DiscoveryClient
    	*corev1.CoreV1Client
    	*appsv1beta1.AppsV1beta1Client
    	*authenticationv1.AuthenticationV1Client
    	*authenticationv1beta1.AuthenticationV1beta1Client
    	*authorizationv1.AuthorizationV1Client
    	*authorizationv1beta1.AuthorizationV1beta1Client
    	*autoscalingv1.AutoscalingV1Client
    	*autoscalingv2alpha1.AutoscalingV2alpha1Client
    	*batchv1.BatchV1Client
    	*batchv2alpha1.BatchV2alpha1Client
    	*certificatesv1beta1.CertificatesV1beta1Client
    	*extensionsv1beta1.ExtensionsV1beta1Client
    	*policyv1beta1.PolicyV1beta1Client
    	*rbacv1beta1.RbacV1beta1Client
    	*rbacv1alpha1.RbacV1alpha1Client
    	*settingsv1alpha1.SettingsV1alpha1Client
    	*storagev1beta1.StorageV1beta1Client
    	*storagev1.StorageV1Client
    }
    
  • kube 子网管理需要知道运行它的k8s节点名,通过环境变量NODE_NAME获取,若获取不到则如果我们作为一个pod运行,那么将填充POD_NAME和POD_NAMESPACE,并可以使用它们来查找节点名

  • 得到节点名后,通过newKubeSubnetManager创建子网管理器并初始化,调用NewIndexerInformer

    type kubeSubnetManager struct {
    	annotations    annotations
    	client         clientset.Interface
    	nodeName       string
    	nodeStore      listers.NodeLister
    	nodeController cache.Controller
    	subnetConf     *subnet.Config
    	events         chan subnet.Event
    }
    
  • NewIndexerInformer返回一个索引器Indexer和一个控制器,用于填充索引,同时还提供事件通知。通过NewIndexer返回一个clientState,clientState实际上是实现了Indexer接口的cache对象,和通过Config生成的控制器,Config结构如下:

    // Config contains all the settings for a Controller.
    type Config struct {
    	// The queue for your objects; either a FIFO or
    	// a DeltaFIFO. Your Process() function should accept
    	// the output of this Queue's Pop() method.
    	Queue
    
    	// Something that can list and watch your objects.
    	ListerWatcher
    
    	// Something that can process your objects.
    	Process ProcessFunc
    
    	// The type of your objects.
    	ObjectType runtime.Object
    
    	// Reprocess everything at least this often.
    	// Note that if it takes longer for you to clear the queue than this
    	// period, you will end up processing items in the order determined
    	// by FIFO.Replace(). Currently, this is random. If this is a
    	// problem, we can change that replacement policy to append new
    	// things to the end of the queue instead of replacing the entire
    	// queue.
    	FullResyncPeriod time.Duration
    
    	// ShouldResync, if specified, is invoked when the controller's reflector determines the next
    	// periodic sync should occur. If this returns true, it means the reflector should proceed with
    	// the resync.
    	ShouldResync ShouldResyncFunc
    
    	// If true, when Process() returns an error, re-enqueue the object.
    	// TODO: add interface to let you inject a delay/backoff or drop
    	//       the object completely if desired. Pass the object in
    	//       question to this interface as a parameter.
    	RetryOnError bool
    }
    
  • 调用NewIndexerInformer传入ListWatch和ResourceEventHandlerFuncs,ResourceEventHandlerFuncs中添加handleAddLeaseEvent,handleUpdateLeaseEvent等函数

  • cache是一个结构体,内容如下:

    type cache struct {
    	// cacheStorage bears the burden of thread safety for the cache
    	cacheStorage ThreadSafeStore
    	// keyFunc is used to make the key for objects stored in and retrieved from items, and
    	// should be deterministic.
    	keyFunc KeyFunc
    }
    
  • 返回了索引器和控制器后,可以初始化kubeSubnetManager中的controller和nodeStore变量,nodeStore变量通过NewNodeLister新建,传入indexer作为参数,至此子网管理器创建完成。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值