Skip to content

Kubernetes-Learning-Playground/imitate-k8s-kubectl-clientgo-apiserver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

模拟k8s中kubectl命令模式与client-go调用sdk与api-server开发设计。

目前还在一边开发一边设计,主要为了学习k8s的设计理念而创建的。

api server资源

模仿k8s api server 的资源对象分类,分为apps、core等。

  1. 目前支持两种资源大类,并可以在api server中不断扩展 a. core:可以看成水果类资源 b. apps:可以看成汽车类资源
  2. 提供每种资源的create update delete get list 方法,目前支持list create apply delete watch等命令行操作
  3. 资源对象改成声明式api的形式,每次更新底层都是使用createOrUpdate方法
  4. 提供scheme注册表与GVK功能。

RoadMap

TODO 提供scheme注册表。(已支持简单功能)

TODO 新增aggregator apiserver功能。(已支持)

TODO 结合store-scheduler调度器,模拟创建资源后,调度到特定map中

TODO 实现informer机制。(时间周期长,预计缓更)

clientSet 风格客户端封装

如下图所示:基于net/http基础库的封装,并依据k8s风格封装http CRUD接口。

功能:目前实现apple car资源对象(ex: pod),并实现GET LIST DELETE CREATE UPDATE WATCH 方法

范例文件

func main() {
	// 配置文件
	config := &rest.Config{
		Host:    fmt.Sprintf("http://localhost:8888"),
		Timeout: time.Second,
	}
	clientSet := stores.NewForConfig(config)


	// 创建操作
	a := &v1.Apple{
		TypeMeta: metav1.TypeMeta{
			ApiVersion: "core/v1",
			Kind: "Apple",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name: "apple-test11",
		},
		Spec: v1.AppleSpec{
			Size: "apple1",
			Color: "apple1",
			Place: "apple1",
			Price: "apple1",
		},
		Status: v1.AppleStatus{
			Status: "created",
		},

	}
	c, err := clientSet.CoreV1().Apple().Create(a)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("name:", c.Name,  "size:", c.Spec.Size, "color:", c.Spec.Color, "place:", c.Spec.Place, "price:", c.Spec.Price)

	apple1, err := clientSet.CoreV1().Apple().Get(c.Name)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("name: ", apple1.Name)

	aaa := &v1.Apple{
		TypeMeta: metav1.TypeMeta{
			ApiVersion: "core/v1",
			Kind: "Apple",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name: "apple-test11",
		},
		Spec: v1.AppleSpec{
			Size: "apple1dddd",
			Color: "apple1ccc",
			Place: "apple1ccc",
			Price: "apple1ccc",
		},
	}

	appleUpdate, err := clientSet.CoreV1().Apple().Update(aaa)
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println("name: ", appleUpdate.Name,  "size: ", appleUpdate.Spec.Size, "color: ", appleUpdate.Spec.Color, "place: ", appleUpdate.Spec.Place, "price: ", appleUpdate.Spec.Price)

	appleList, err := clientSet.CoreV1().Apple().List()
	if err != nil {
		log.Fatalln(err)
	}
	for _, apple := range appleList.Item {
		fmt.Println(apple.Name)
	}

	err = clientSet.CoreV1().Apple().Delete("applexxxxxxx")
	if err != nil {
		log.Fatalln(err)
	}

}

watch 操作

func main() {
	//// 配置文件
	config := &rest.Config{
		Host:    fmt.Sprintf("http://localhost:8080"),
		Timeout: time.Second,
	}
	clientSet := stores.NewForConfig(config)

	// watch apple对象
	res := clientSet.CoreV1().Apple().Watch()
	for i := range res.WChan {
		r := i.([]byte)
		var resApple v1.Apple
		err := json.Unmarshal(r, &resApple)
		if err != nil {
			klog.Error(err)
			return
		}
		klog.Info(resApple)
	}

	// watch car对象
	res1 := clientSet.AppsV1().Car().Watch()
	for i := range res1.WChan {
		r := i.([]byte)
		var resCar appsv1.Car
		err := json.Unmarshal(r, &resCar)
		if err != nil {
			klog.Error(err)
			return
		}
		klog.Info(resCar)
	}
}

kubectl 风格命令行封装

底层使用client-go对资源进行CRUD操作。

使用方法

  1. 还没编译,所以执行使用 go run storectl.go xxxx 测试,编译后放入执行的/bin目录中,可以使用storectl list xxxx
  2. 目前仅支持list create apply delete describe等操作。
  3. 同时支持json yaml两种方式创建与修改。
# json yaml 范例文件 json目录 yaml目录
# 进入:cmd/storectl目录中
# 获取命令: storectl list apples 
➜  storectl git:(main) ✗ go run storectl.go apply cars ../../json/car.json
+-----------+-----------+-----------+-----------+----------+
| APPLE名称 |   PRICE   |   PLACE   |   COLOR   |   SIZE   |
+-----------+-----------+-----------+-----------+----------+
| aaaa      | ccc       | ccc       | aaa       | aaa      |
| initApple | initPrice | initPlace | initColor | initSize |
+-----------+-----------+-----------+-----------+----------+
# 创建命令 : 类似 storectl create apples aaa.json
➜  storectl git:(main) ✗ go run storectl.go create apples aaa.json
name:dddafjjhadsklfhaaaa is created

Releases

No releases published

Packages

No packages published