Go之Operator(三)

学习链接operator实战

关于k8s常用lib库项目介绍

  • apimachinery

apimachinery项目概览

apimachinery子项目主要是k8s服务端和客户段项目都共同依赖的一些公共方法,struct,工具的定义. 主要是服务于kubernetes,client-go,apiserver这三个项目.

api目录

在这里插入图片描述

(1)apitesting

这里主要是帮我们做了一些默认字段的填充,就比如你创建的时候有些字段没设置,这里就会设置默认的

(2)equality

用来做转换的一些东西

(3)errors

k8s中的错误信息,以及判断错误类型

(4)meta

api版本转换以及api注册的时候会用到这里面的代码

(5)resource

类型的转换以及单位的转换

(6)validation

主要用来做校验

在这里插入图片描述

其他目录

apis跟api类似会一些label的判断之类的
conversion主要是做了一些api转换用的
fileds主要是做了一些字段判断

type Fields interface {
	// Has returns whether the provided field exists.
	Has(field string) (exists bool)

	// Get returns the value for the provided field.
	Get(field string) (value string)
}

// Set is a map of field:value. It implements Fields.
type Set map[string]string

label就是label的转换 以及label的操作
selector 就是判断标签是否存在当中或者是否匹配一个正则表达式
runtime 定义了k8s最核心的东西schema,所有的api注册都需要注册到schema中

// staging/src/k8s.io/apimachinery/pkg/runtime/interfaces.go 
//这块定义了转换
type Serializer interface {
	Encoder
	Decoder
}
//staging/src/k8s.io/apimachinery/pkg/runtime/schema.go
type Scheme struct {
	// versionMap allows one to figure out the go type of an object with
	// the given version and name.
	gvkToType map[schema.GroupVersionKind]reflect.Type

	// typeToGroupVersion allows one to find metadata for a given go object.
	// The reflect.Type we index by should *not* be a pointer.
	typeToGVK map[reflect.Type][]schema.GroupVersionKind

	// unversionedTypes are transformed without conversion in ConvertToVersion.
	unversionedTypes map[reflect.Type]schema.GroupVersionKind

	// unversionedKinds are the names of kinds that can be created in the context of any group
	// or version
	// TODO: resolve the status of unversioned types.
	unversionedKinds map[string]reflect.Type

	// Map from version and resource to the corresponding func to convert
	// resource field labels in that version to internal version.
	fieldLabelConversionFuncs map[schema.GroupVersionKind]FieldLabelConversionFunc

	// defaulterFuncs is an array of interfaces to be called with an object to provide defaulting
	// the provided object must be a pointer.
	defaulterFuncs map[reflect.Type]func(interface{})

	// converter stores all registered conversion functions. It also has
	// default converting behavior.
	converter *conversion.Converter

	// versionPriority is a map of groups to ordered lists of versions for those groups indicating the
	// default priorities of these versions as registered in the scheme
	versionPriority map[string][]string

	// observedVersions keeps track of the order we've seen versions during type registration
	observedVersions []schema.GroupVersion

	// schemeName is the name of this scheme.  If you don't specify a name, the stack of the NewScheme caller will be used.
	// This is useful for error reporting to indicate the origin of the scheme.
	schemeName string
}

在这里插入图片描述
k8s本身调用apiserver 然后调用runtime的schema的代码吧api注册(这里会用api的install)在schema中会起一个对应的healder来对外服务,下图可以看到每个api都会有install

//pkg/apis/apps/install/install.go
func Install(scheme *runtime.Scheme) {
	utilruntime.Must(apps.AddToScheme(scheme))
	utilruntime.Must(v1beta1.AddToScheme(scheme))
	utilruntime.Must(v1beta2.AddToScheme(scheme))
	utilruntime.Must(v1.AddToScheme(scheme))
	utilruntime.Must(scheme.SetVersionPriority(v1.SchemeGroupVersion, v1beta2.SchemeGroupVersion, v1beta1.SchemeGroupVersion))
}

在这里插入图片描述

test就是用来做测试的
type定义了一些类型
util定义了一些资源转换
在这里插入图片描述
version定义了版本
在这里插入图片描述
wathc的核心是event

//staging/src/k8s.io/apimachinery/pkg/watch/watch.go
type Interface interface {
	// Stops watching. Will close the channel returned by ResultChan(). Releases
	// any resources used by the watch.
	Stop()

	// Returns a chan which will receive all the events. If an error occurs
	// or Stop() is called, this channel will be closed, in which case the
	// watch should be completely cleaned up.
	//我们可以从这个管道获取一个事件
	ResultChan() <-chan Event
}

// EventType defines the possible types of events.
type EventType string
//定义了k8s event的类型
const (
	Added    EventType = "ADDED"
	Modified EventType = "MODIFIED"
	Deleted  EventType = "DELETED"
	Bookmark EventType = "BOOKMARK"
	Error    EventType = "ERROR"

	DefaultChanSize int32 = 100
)

// Event represents a single event to a watched resource.
// +k8s:deepcopy-gen=true
type Event struct {
	Type EventType

	// Object is:
	//  * If Type is Added or Modified: the new state of the object.
	//  * If Type is Deleted: the state of the object immediately before deletion.
	//  * If Type is Bookmark: the object (instance of a type being watched) where
	//    only ResourceVersion field is set. On successful restart of watch from a
	//    bookmark resourceVersion, client is guaranteed to not get repeat event
	//    nor miss any events.
	//  * If Type is Error: *api.Status is recommended; other types may make sense
	//    depending on context.
	Object runtime.Object
}
//type可以理解为什么样的事件
//object可以理解为对象
//那么结合起来就是一个什么样的事件发生在谁的身上
// staging/src/k8s.io/apimachinery/pkg/watch/mux.go
//事件广播器
//那么肯定有事件监听器 写入管道,广播器从管道拿出来
type Broadcaster struct {
	// TODO: see if this lock is needed now that new watchers go through
	// the incoming channel.
	lock sync.Mutex

	watchers     map[int64]*broadcasterWatcher
	nextWatcher  int64
	distributing sync.WaitGroup
	//从incoming中拿到事件 然后循环watchers 把事件发送给所有的watcher
	incoming chan Event

	// How large to make watcher's channel.
	watchQueueLength int
	// If one of the watch channels is full, don't wait for it to become empty.
	// Instead just deliver it to the watchers that do have space in their
	// channels and move on to the next event.
	// It's more fair to do this on a per-watcher basis than to do it on the
	// "incoming" channel, which would allow one slow watcher to prevent all
	// other watchers from getting new events.
	//这里定义了一个行为,等incoming满了之后是等待还是删除
	fullChannelBehavior FullChannelBehavior
}



type FullChannelBehavior int

const (
	WaitIfChannelFull FullChannelBehavior = iota
	DropIfChannelFull
)
//staging/src/k8s.io/apimachinery/pkg/watch/filter.go
//事件进来做一些填充在输出
func Filter(w Interface, f FilterFunc) Interface {
	fw := &filteredWatch{
		incoming: w,
		result:   make(chan Event),
		f:        f,
	}
	go fw.loop()
	return fw
}

猜你喜欢

转载自blog.csdn.net/weixin_45413603/article/details/107582415