学习链接operator实战

tools

path :kubernetes\staging\src\k8s.io\client-go\tools

在这里插入图片描述

- auth

auth这里封装了一些认证的信息

type Info struct {
	User        string
	Password    string
	CAFile      string
	CertFile    string
	KeyFile     string
	BearerToken string
	Insecure    *bool
}
//获取一个info
func LoadFromFile(path string) (*Info, error) {
	var info Info
	if _, err := os.Stat(path); os.IsNotExist(err) {
		return nil, err
	}
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal(data, &info)
	if err != nil {
		return nil, err
	}
	return &info, err
}

// MergeWithConfig returns a copy of a client.Config with values from the Info.
// The fields of client.Config with a corresponding field in the Info are set
// with the value from the Info.
//然后通过一个用户,我们获取一个config 那么就可以创建一个restclient
func (info Info) MergeWithConfig(c restclient.Config) (restclient.Config, error) {
	var config = c
	config.Username = info.User
	config.Password = info.Password
	config.CAFile = info.CAFile
	config.CertFile = info.CertFile
	config.KeyFile = info.KeyFile
	config.BearerToken = info.BearerToken
	if info.Insecure != nil {
		config.Insecure = *info.Insecure
	}
	return config, nil
}

- clientcmd

这个目录主要是做了一些我们通过命令行或者config 或者其他方式创建一个config 那么通过这个config我们就可以创建一个restclient
在这里插入图片描述
validation用来参数验证 验证config是否合法 或者其他是否合法
overrides就是获取到以后可以修改值
flag loader merged等用来从命令行或者env 或者默认.kube/config 或者其他方式获取 如下

 type DeferredLoadingClientConfig struct {
	loader         ClientConfigLoader
	overrides      *ConfigOverrides
	fallbackReader io.Reader

	clientConfig ClientConfig
	loadingLock  sync.Mutex

	// provided for testing
	icc InClusterConfig
}

- record

我们可以获取一些事件更新事件之类的

 type EventSink interface {
	Create(event *v1.Event) (*v1.Event, error)
	Update(event *v1.Event) (*v1.Event, error)
	Patch(oldEvent *v1.Event, data []byte) (*v1.Event, error)
}
const (
	maxLruCacheEntries = 4096
	// if we see the same event that varies only by message
	// more than 10 times in a 10 minute period, aggregate the event
	//defaultAggregateMaxEvents         这里定义了事件信息的数量 以及事件聚合 就比如镜像无线重启或者等等故障的时候 会有event 然后后面有个括号多少次 那么表示同种类型的事件 我们做聚合 然后显示多少次而不是刷屏的感觉 
	//10分钟内超过10次就聚合
	//间隔时间600s
	defaultAggregateMaxEvents         = 10
	defaultAggregateIntervalInSeconds = 600
	// by default, allow a source to send 25 events about an object
	// but control the refill rate to 1 new event every 5 minutes
	// this helps control the long-tail of events for things that are always
	// unhealthy
	defaultSpamBurst = 25
	defaultSpamQPS   = 1. / 300.
)

- reference

在这里插入图片描述
这里reference 来吧一些资源做关联,作级联删除 就比如我删除这里的daemonset 那么他的pod也要被k8s的垃圾回收机制回收掉
在这里插入图片描述

func GetReference(scheme *runtime.Scheme, obj runtime.Object) (*v1.ObjectReference, error) {
	//这里可以理解为scheme就是kind apiversion 然后比如传入一个pod pod实现了object的接口,然后我们就能拿到这个pod的references
	if obj == nil {
		return nil, ErrNilObject
	}
	if ref, ok := obj.(*v1.ObjectReference); ok {
		// Don't make a reference to a reference.
		return ref, nil
	}

	// An object that implements only List has enough metadata to build a reference
	var listMeta metav1.Common
	objectMeta, err := meta.Accessor(obj)
	if err != nil {
		listMeta, err = meta.CommonAccessor(obj)
		if err != nil {
			return nil, err
		}
	} else {
		listMeta = objectMeta
	}

	gvk := obj.GetObjectKind().GroupVersionKind()

	// If object meta doesn't contain data about kind and/or version,
	// we are falling back to scheme.
	//
	// TODO: This doesn't work for CRDs, which are not registered in scheme.
	if gvk.Empty() {
		gvks, _, err := scheme.ObjectKinds(obj)
		if err != nil {
			return nil, err
		}
		if len(gvks) == 0 || gvks[0].Empty() {
			return nil, fmt.Errorf("unexpected gvks registered for object %T: %v", obj, gvks)
		}
		// TODO: The same object can be registered for multiple group versions
		// (although in practise this doesn't seem to be used).
		// In such case, the version set may not be correct.
		gvk = gvks[0]
	}

	kind := gvk.Kind
	version := gvk.GroupVersion().String()

	// only has list metadata
	if objectMeta == nil {
		return &v1.ObjectReference{
			Kind:            kind,
			APIVersion:      version,
			ResourceVersion: listMeta.GetResourceVersion(),
		}, nil
	}

	return &v1.ObjectReference{
		Kind:            kind,
		APIVersion:      version,
		Name:            objectMeta.GetName(),
		Namespace:       objectMeta.GetNamespace(),
		UID:             objectMeta.GetUID(),
		ResourceVersion: objectMeta.GetResourceVersion(),
	}, nil
}

- cache

cache

- 其他

  • wathc
    提供了一个便捷的函数让我们获取一个indexer controller以及interface
func NewIndexerInformerWatcher(lw cache.ListerWatcher, objType runtime.Object) (cache.Indexer, cache.Controller, watch.Interface, <-chan struct{}) {---}

- remotecommand

这里主要是kubectl exec xxx的时候会用到 我们在使用的时候就比如我们要做一个web 然后我们要做一个web连入容器的操作的时候会用到这个
- leaderelection

选举相关的东西

- metrics

监控指标的一些封装

- pager

可以通过传一些参数类似于做分页处理

- portforward

做svc相关操作会用到
k8s地址/proxy/ns/资源

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐