zoukankan      html  css  js  c++  java
  • kubernetes api-server 源码阅读(二) 函数介绍

    导航:

      1. CreateServerChain

        1.1 CreateNodeDialer

        1.2  CreateKubeAPIServerConfig 

        1.3  createAPIExtensionsConfig

        1.4  createAPIExtensionsServer

        1.5  CreateKubeAPIServer

        1.6   PrepareRun()

        1.7   createAggregatorConfig

        1.8   createAggregatorServer

      2. 重要的结构体

    1.1 CreateNodeDialer: 创建到节点拨号连接。设置网络隧道,如果在云平台中,则需要安装本机的SSH Key到Kubernetes集群中所有节点上,可通过用户名和私钥,SSH到node节点

    1.2  CreateKubeAPIServerConfig 函数

    // CreateKubeAPIServerConfig creates all the resources for running the API server, but runs none of them

          CreateKubeAPIServerConfig函数创建运行API Server的配置,设置默认的advertise address,service Ip range,storage,etcd等

    // CreateKubeAPIServerConfig creates all the resources for running the API server, but runs none of them
    func CreateKubeAPIServerConfig(
    	s completedServerRunOptions,
    	nodeTunneler tunneler.Tunneler,
    	proxyTransport *http.Transport,
    ) (
    	config *master.Config,
    	insecureServingInfo *genericapiserver.DeprecatedInsecureServingInfo,
    	serviceResolver aggregatorapiserver.ServiceResolver,
    	pluginInitializers []admission.PluginInitializer,
    	admissionPostStartHook genericapiserver.PostStartHookFunc,
    	lastErr error,
    ) 
    

      

    	config = &master.Config{
    		GenericConfig: genericConfig,
    		ExtraConfig: master.ExtraConfig{
    			ClientCARegistrationHook: master.ClientCARegistrationHook{
    				ClientCA:                         clientCA,
    				RequestHeaderUsernameHeaders:     s.Authentication.RequestHeader.UsernameHeaders,
    				RequestHeaderGroupHeaders:        s.Authentication.RequestHeader.GroupHeaders,
    				RequestHeaderExtraHeaderPrefixes: s.Authentication.RequestHeader.ExtraHeaderPrefixes,
    				RequestHeaderCA:                  requestHeaderProxyCA,
    				RequestHeaderAllowedNames:        s.Authentication.RequestHeader.AllowedNames,
    			},
    
    			APIResourceConfigSource: storageFactory.APIResourceConfigSource,
    			StorageFactory:          storageFactory,
                  。。。。。
    
    			VersionedInformers: versionedInformers,
    		},
    	}
    

      

    1.3  CreateKubeAPIServer

    kubeAPIServerConfig, insecureServingInfo, serviceResolver, pluginInitializer, admissionPostStartHook, err := CreateKubeAPIServerConfig(completedOptions, nodeTunneler, proxyTransport)
      apiextensionsConfig.Complete().New(delegateAPIServer)
      kubeAPIServer.GenericAPIServer.AddPostStartHookOrDie("start-kube-apiserver-admission-initializer", admissionPostStartHook)
    
    
    type completedConfig struct {
    	*Config
    
    	//===========================================================================
    	// values below here are filled in during completion
    	//===========================================================================
    
    	// SharedInformerFactory provides shared informers for resources
    	SharedInformerFactory informers.SharedInformerFactory
    }
    

      

    // New returns a new instance of Master from the given config.
    // Certain config fields will be set to a default value if unset.
    // Certain config fields must be specified, including:
    //   KubeletClientConfig
    func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*Master, error) {
    	if reflect.DeepEqual(c.ExtraConfig.KubeletClientConfig, kubeletclient.KubeletClientConfig{}) {
    		return nil, fmt.Errorf("Master.New() called with empty config.KubeletClientConfig")
    	}
    
    	s, err := c.GenericConfig.New("kube-apiserver", delegationTarget)
    	if err != nil {
    		return nil, err
    	}
    
    	if c.ExtraConfig.EnableLogsSupport {
    		routes.Logs{}.Install(s.Handler.GoRestfulContainer)
    	}
    
    	m := &Master{
    		GenericAPIServer: s,
    	}
    
    	// install legacy rest storage
    	if c.ExtraConfig.APIResourceConfigSource.VersionEnabled(apiv1.SchemeGroupVersion) {
    		legacyRESTStorageProvider := corerest.LegacyRESTStorageProvider{
    			StorageFactory:              c.ExtraConfig.StorageFactory,
    			ProxyTransport:              c.ExtraConfig.ProxyTransport,
    			KubeletClientConfig:         c.ExtraConfig.KubeletClientConfig,
    			EventTTL:                    c.ExtraConfig.EventTTL,
    			ServiceIPRange:              c.ExtraConfig.ServiceIPRange,
    			ServiceNodePortRange:        c.ExtraConfig.ServiceNodePortRange,
    			LoopbackClientConfig:        c.GenericConfig.LoopbackClientConfig,
    			ServiceAccountIssuer:        c.ExtraConfig.ServiceAccountIssuer,
    			ServiceAccountMaxExpiration: c.ExtraConfig.ServiceAccountMaxExpiration,
    			APIAudiences:                c.GenericConfig.Authentication.APIAudiences,
    		}
    		m.InstallLegacyAPI(&c, c.GenericConfig.RESTOptionsGetter, legacyRESTStorageProvider)
    	}
    
    	// The order here is preserved in discovery.
    	// If resources with identical names exist in more than one of these groups (e.g. "deployments.apps"" and "deployments.extensions"),
    	// the order of this list determines which group an unqualified resource name (e.g. "deployments") should prefer.
    	// This priority order is used for local discovery, but it ends up aggregated in `k8s.io/kubernetes/cmd/kube-apiserver/app/aggregator.go
    	// with specific priorities.
    	// TODO: describe the priority all the way down in the RESTStorageProviders and plumb it back through the various discovery
    	// handlers that we have.
    	restStorageProviders := []RESTStorageProvider{
    		auditregistrationrest.RESTStorageProvider{},
    		authenticationrest.RESTStorageProvider{Authenticator: c.GenericConfig.Authentication.Authenticator, APIAudiences: c.GenericConfig.Authentication.APIAudiences},
    		authorizationrest.RESTStorageProvider{Authorizer: c.GenericConfig.Authorization.Authorizer, RuleResolver: c.GenericConfig.RuleResolver},
    		autoscalingrest.RESTStorageProvider{},
    		batchrest.RESTStorageProvider{},
    		certificatesrest.RESTStorageProvider{},
    		coordinationrest.RESTStorageProvider{},
    		extensionsrest.RESTStorageProvider{},
    		networkingrest.RESTStorageProvider{},
    		policyrest.RESTStorageProvider{},
    		rbacrest.RESTStorageProvider{Authorizer: c.GenericConfig.Authorization.Authorizer},
    		schedulingrest.RESTStorageProvider{},
    		settingsrest.RESTStorageProvider{},
    		storagerest.RESTStorageProvider{},
    		// keep apps after extensions so legacy clients resolve the extensions versions of shared resource names.
    		// See https://github.com/kubernetes/kubernetes/issues/42392
    		appsrest.RESTStorageProvider{},
    		admissionregistrationrest.RESTStorageProvider{},
    		eventsrest.RESTStorageProvider{TTL: c.ExtraConfig.EventTTL},
    	}
    	m.InstallAPIs(c.ExtraConfig.APIResourceConfigSource, c.GenericConfig.RESTOptionsGetter, restStorageProviders...)
    
    	if c.ExtraConfig.Tunneler != nil {
    		m.installTunneler(c.ExtraConfig.Tunneler, corev1client.NewForConfigOrDie(c.GenericConfig.LoopbackClientConfig).Nodes())
    	}
    
    	m.GenericAPIServer.AddPostStartHookOrDie("ca-registration", c.ExtraConfig.ClientCARegistrationHook.PostStartHook)
    
    	return m, nil
    }
    

      

    2. 重要的结构体

      2.1 DefaultStorageFactory

    // DefaultStorageFactory takes a GroupResource and returns back its storage interface.  This result includes:
    // 1. Merged etcd config, including: auth, server locations, prefixes
    // 2. Resource encodings for storage: group,version,kind to store as
    // 3. Cohabitating default: some resources like hpa are exposed through multiple APIs.  They must agree on 1 and 2
    type DefaultStorageFactory struct {
    	// StorageConfig describes how to create a storage backend in general.
    	// Its authentication information will be used for every storage.Interface returned.
    	StorageConfig storagebackend.Config
    
    	Overrides map[schema.GroupResource]groupResourceOverrides
    
    	DefaultResourcePrefixes map[schema.GroupResource]string
    
    	// DefaultMediaType is the media type used to store resources. If it is not set, "application/json" is used.
    	DefaultMediaType string
    
    	// DefaultSerializer is used to create encoders and decoders for the storage.Interface.
    	DefaultSerializer runtime.StorageSerializer
    
    	// ResourceEncodingConfig describes how to encode a particular GroupVersionResource
    	ResourceEncodingConfig ResourceEncodingConfig
    
    	// APIResourceConfigSource indicates whether the *storage* is enabled, NOT the API
    	// This is discrete from resource enablement because those are separate concerns.  How this source is configured
    	// is left to the caller.
    	APIResourceConfigSource APIResourceConfigSource
    
    	// newStorageCodecFn exists to be overwritten for unit testing.
    	newStorageCodecFn func(opts StorageCodecConfig) (codec runtime.Codec, err error)
    }
    

      

        2.2 config

     apiserver 配置的结构体

    // Config is a structure used to configure a GenericAPIServer.
    // Its members are sorted roughly in order of importance for composers.
    type Config struct {
    	// SecureServing is required to serve https
    	SecureServing *SecureServingInfo
    
    	// Authentication is the configuration for authentication
    	Authentication AuthenticationInfo
    
    	// Authorization is the configuration for authorization
    	Authorization AuthorizationInfo
    
    	// LoopbackClientConfig is a config for a privileged loopback connection to the API server
    	// This is required for proper functioning of the PostStartHooks on a GenericAPIServer
    	// TODO: move into SecureServing(WithLoopback) as soon as insecure serving is gone
    	LoopbackClientConfig *restclient.Config
    	// RuleResolver is required to get the list of rules that apply to a given user
    	// in a given namespace
    	RuleResolver authorizer.RuleResolver
    	// AdmissionControl performs deep inspection of a given request (including content)
    	// to set values and determine whether its allowed
    	AdmissionControl      admission.Interface
    	CorsAllowedOriginList []string
    
    	EnableIndex     bool
    	EnableProfiling bool
    	EnableDiscovery bool
    	// Requires generic profiling enabled
    	EnableContentionProfiling bool
    	EnableMetrics             bool
    
    	DisabledPostStartHooks sets.String
    
    	// Version will enable the /version endpoint if non-nil
    	Version *version.Info
    	// AuditBackend is where audit events are sent to.
    	AuditBackend audit.Backend
    	// AuditPolicyChecker makes the decision of whether and how to audit log a request.
    	AuditPolicyChecker auditpolicy.Checker
    	// ExternalAddress is the host name to use for external (public internet) facing URLs (e.g. Swagger)
    	// Will default to a value based on secure serving info and available ipv4 IPs.
    	ExternalAddress string
    
    	//===========================================================================
    	// Fields you probably don't care about changing
    	//===========================================================================
    
    	// BuildHandlerChainFunc allows you to build custom handler chains by decorating the apiHandler.
    	BuildHandlerChainFunc func(apiHandler http.Handler, c *Config) (secure http.Handler)
    	// HandlerChainWaitGroup allows you to wait for all chain handlers exit after the server shutdown.
    	HandlerChainWaitGroup *utilwaitgroup.SafeWaitGroup
    	// DiscoveryAddresses is used to build the IPs pass to discovery. If nil, the ExternalAddress is
    	// always reported
    	DiscoveryAddresses discovery.Addresses
    	// The default set of healthz checks. There might be more added via AddHealthzChecks dynamically.
    	HealthzChecks []healthz.HealthzChecker
    	// LegacyAPIGroupPrefixes is used to set up URL parsing for authorization and for validating requests
    	// to InstallLegacyAPIGroup. New API servers don't generally have legacy groups at all.
    	LegacyAPIGroupPrefixes sets.String
    	// RequestInfoResolver is used to assign attributes (used by admission and authorization) based on a request URL.
    	// Use-cases that are like kubelets may need to customize this.
    	RequestInfoResolver apirequest.RequestInfoResolver
    	// Serializer is required and provides the interface for serializing and converting objects to and from the wire
    	// The default (api.Codecs) usually works fine.
    	Serializer runtime.NegotiatedSerializer
    	// OpenAPIConfig will be used in generating OpenAPI spec. This is nil by default. Use DefaultOpenAPIConfig for "working" defaults.
    	OpenAPIConfig *openapicommon.Config
    
    	// RESTOptionsGetter is used to construct RESTStorage types via the generic registry.
    	RESTOptionsGetter genericregistry.RESTOptionsGetter
    
    	// If specified, all requests except those which match the LongRunningFunc predicate will timeout
    	// after this duration.
    	RequestTimeout time.Duration
    	// If specified, long running requests such as watch will be allocated a random timeout between this value, and
    	// twice this value.  Note that it is up to the request handlers to ignore or honor this timeout. In seconds.
    	MinRequestTimeout int
    	// The limit on the total size increase all "copy" operations in a json
    	// patch may cause.
    	// This affects all places that applies json patch in the binary.
    	JSONPatchMaxCopyBytes int64
    	// MaxRequestsInFlight is the maximum number of parallel non-long-running requests. Every further
    	// request has to wait. Applies only to non-mutating requests.
    	MaxRequestsInFlight int
    	// MaxMutatingRequestsInFlight is the maximum number of parallel mutating requests. Every further
    	// request has to wait.
    	MaxMutatingRequestsInFlight int
    	// Predicate which is true for paths of long-running http requests
    	LongRunningFunc apirequest.LongRunningRequestCheck
    
    	// EnableAPIResponseCompression indicates whether API Responses should support compression
    	// if the client requests it via Accept-Encoding
    	EnableAPIResponseCompression bool
    
    	// MergedResourceConfig indicates which groupVersion enabled and its resources enabled/disabled.
    	// This is composed of genericapiserver defaultAPIResourceConfig and those parsed from flags.
    	// If not specify any in flags, then genericapiserver will only enable defaultAPIResourceConfig.
    	MergedResourceConfig *serverstore.ResourceConfig
    
    	//===========================================================================
    	// values below here are targets for removal
    	//===========================================================================
    
    	// PublicAddress is the IP address where members of the cluster (kubelet,
    	// kube-proxy, services, etc.) can reach the GenericAPIServer.
    	// If nil or 0.0.0.0, the host's default interface will be used.
    	PublicAddress net.IP
    }
    
    2.3 SharedInformerFactory
    所有已知的group version 资源对象共有的方法
    Informer(就是SharedInformer)是client-go的重要组成部分,在了解client-go之前,了解一下Informer的实现是很有必要的,下面引用了官方的图,可以看到Informer在client-go中的位置。
    https://blog.csdn.net/weixin_42663840/article/details/81699303

    // NewSharedInformerFactoryWithOptions constructs a new instance of a SharedInformerFactory with additional options.
    func NewSharedInformerFactoryWithOptions(client kubernetes.Interface, defaultResync time.Duration, options ...SharedInformerOption) SharedInformerFactory {
    	factory := &sharedInformerFactory{
    		client:           client,
    		namespace:        v1.NamespaceAll,
    		defaultResync:    defaultResync,
    		informers:        make(map[reflect.Type]cache.SharedIndexInformer),
    		startedInformers: make(map[reflect.Type]bool),
    		customResync:     make(map[reflect.Type]time.Duration),
    	}
    
    	// Apply all options
    	for _, opt := range options {
    		factory = opt(factory)
    	}
    
    	return factory
    }
    

      

    
    
    // SharedInformerFactory provides shared informers for resources in all known
    // API group versions.
    type SharedInformerFactory interface {
    	internalinterfaces.SharedInformerFactory
    	ForResource(resource schema.GroupVersionResource) (GenericInformer, error)
    	WaitForCacheSync(stopCh <-chan struct{}) map[reflect.Type]bool
    
    	Admissionregistration() admissionregistration.Interface
    	Apps() apps.Interface
    	Auditregistration() auditregistration.Interface
    	Autoscaling() autoscaling.Interface
    	Batch() batch.Interface
    	Certificates() certificates.Interface
    	Coordination() coordination.Interface
    	Core() core.Interface
    	Events() events.Interface
    	Extensions() extensions.Interface
    	Networking() networking.Interface
    	Policy() policy.Interface
    	Rbac() rbac.Interface
    	Scheduling() scheduling.Interface
    	Settings() settings.Interface
    	Storage() storage.Interface
    }
    

      

    没有什么是写一万遍还不会的,如果有那就再写一万遍。
  • 相关阅读:
    金庸的武侠世界和SAP的江湖
    如何处理用代码创建SD Sales order时遇到的错误消息KI 180
    如何利用BAPI SD_SALESDOCUMENT_CHANGE修改Sales Order的字段
    如何查找BAPI SD_SALESDOCUMENT_CHANGE里的字段对应的数据库存储表
    SAP标准培训课程C4C10学习笔记(四)第四单元
    C4C Product Price List的模型中和有效期相关的两个字段
    SAP成都研究院Sunshine: 我的C4C实习感受和保研之路
    SAP CRM和C4C的产品主数据price维护
    运行npm update等命令出错后如何分析问题根源
    ERP和C4C中的function location
  • 原文地址:https://www.cnblogs.com/waken-captain/p/10518887.html
Copyright © 2011-2022 走看看