自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

一篇帶你kubebuilder 實戰(zhàn): status & event

開發(fā) 項目管理
想知道一個節(jié)點池有多少的節(jié)點,現(xiàn)在的資源占比是多少,這樣可以清晰的知道我們現(xiàn)在的水位線是多少,除此之外也想知道節(jié)點池數(shù)量發(fā)生變化的相關(guān)事件信息,什么時候節(jié)點池增加或者是減少了一個節(jié)點等。

[[399605]]

在上篇文章當(dāng)中我們實現(xiàn)了 NodePool Operator 基本的 CURD 功能,跑了一小段時間之后除了 CURD 之外我們有了更高的需求,想知道一個節(jié)點池有多少的節(jié)點,現(xiàn)在的資源占比是多少,這樣可以清晰的知道我們現(xiàn)在的水位線是多少,除此之外也想知道節(jié)點池數(shù)量發(fā)生變化的相關(guān)事件信息,什么時候節(jié)點池增加或者是減少了一個節(jié)點等。

需求

我們先整理一下需求

能夠通過 kubectl get Nodepool了解當(dāng)前的節(jié)點池的以下信息

  • 節(jié)點池的狀態(tài),是否異常
  • 節(jié)點池現(xiàn)在包含多少個節(jié)點
  • 節(jié)點池的資源情況現(xiàn)在有多少 CPU、Memory

能夠通過事件信息得知 controller 的錯誤情況以及節(jié)點池內(nèi)節(jié)點的變化情況

實現(xiàn)

Status

先修改一下 status 對象,注意要確保下面的 //+kubebuilder:subresource:status注釋存在,這個表示開啟 status 子資源,status 對象修改好之后需要重新執(zhí)行一遍 make install

  1. // NodePoolStatus defines the observed state of NodePool 
  2. type NodePoolStatus struct { 
  3.  // status=200 說明正常,其他情況為異常情況 
  4.  Status int `json:"status"
  5.  
  6.  // 節(jié)點的數(shù)量 
  7.  NodeCount int `json:"nodeCount"
  8.  
  9.  // 允許被調(diào)度的容量 
  10.  Allocatable corev1.ResourceList `json:"allocatable,omitempty" protobuf:"bytes,2,rep,name=allocatable,casttype=ResourceList,castkey=ResourceName"
  11.  
  12. //+kubebuilder:object:root=true 
  13. //+kubebuilder:resource:scope=Cluster 
  14. //+kubebuilder:subresource:status 
  15.  
  16. // NodePool is the Schema for the nodepools API 
  17. type NodePool struct { 

然后修改 Reconcile 中的邏輯

  1. func (r *NodePoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 
  2.  // ...... 
  3.  
  4.  if len(nodes.Items) > 0 { 
  5.   r.Log.Info("find nodes, will merge data""nodes", len(nodes.Items)) 
  6. +  pool.Status.Allocatable = corev1.ResourceList{} 
  7. +  pool.Status.NodeCount = len(nodes.Items) 
  8.   for _, n := range nodes.Items { 
  9.    n := n 
  10.  
  11.    // 更新節(jié)點的標(biāo)簽和污點信息 
  12.    err := r.Update(ctx, pool.Spec.ApplyNode(n)) 
  13.    if err != nil { 
  14.     return ctrl.Result{}, err 
  15.    } 
  16.  
  17. +   for name, quantity := range n.Status.Allocatable { 
  18. +    q, ok := pool.Status.Allocatable[name
  19. +    if ok { 
  20. +     q.Add(quantity) 
  21. +     pool.Status.Allocatable[name] = q 
  22. +     continue 
  23. +    } 
  24. +    pool.Status.Allocatable[name] = quantity 
  25. +   } 
  26.   } 
  27.  } 
  28.  
  29.   // ...... 
  30.    
  31. + pool.Status.Status = 200 
  32. + err = r.Status().Update(ctx, pool) 
  33.  return ctrl.Result{}, err 

修改好了之后我們提交一個 NodePool 測試一下

  1. apiVersion: nodes.lailin.xyz/v1 
  2. kind: NodePool 
  3. metadata: 
  4.   name: worker 
  5. spec: 
  6.   taints: 
  7.     - key: node-pool.lailin.xyz 
  8.       value: worker 
  9.       effect: NoSchedule 
  10.   labels: 
  11.     "node-pool.lailin.xyz/worker""10" 
  12.   handler: runc 

可以看到我們現(xiàn)在是有兩個 worker 節(jié)點

  1. ▶ kubectl get no  
  2. NAME                 STATUS   ROLES                  AGE   VERSION 
  3. kind-control-plane   Ready    control-plane,master   29m   v1.20.2 
  4. kind-worker          Ready    worker                 28m   v1.20.2 
  5. kind-worker2         Ready    worker                 28m   v1.20.2 

 然后我們看看 NodePool,可以發(fā)現(xiàn)已經(jīng)存在了預(yù)期的 status

  1. status: 
  2.   allocatable: 
  3.     cpu: "8" 
  4.     ephemeral-storage: 184026512Ki 
  5.     hugepages-1Gi: "0" 
  6.     hugepages-2Mi: "0" 
  7.     memory: 6129040Ki 
  8.     pods: "220" 
  9.   nodeCount: 2 
  10.   status: 200 

現(xiàn)在這樣只能通過查看 yaml 詳情才能看到,當(dāng) NodePool 稍微多一些的時候就不太方便,我們現(xiàn)在給NodePool 增加一些 kubectl 展示的列

  1. +//+kubebuilder:printcolumn:JSONPath=".status.status",name=Status,type=integer 
  2. +//+kubebuilder:printcolumn:JSONPath=".status.nodeCount",name=NodeCount,type=integer 
  3. //+kubebuilder:object:root=true 
  4. //+kubebuilder:resource:scope=Cluster 
  5. //+kubebuilder:subresource:status 

如上所示只需要添加好對應(yīng)的注釋,然后執(zhí)行 make install即可

然后再執(zhí)行 kubectl get NodePool 就可以看到對應(yīng)的列了

  1. ▶ kubectl get NodePool  
  2. NAME     STATUS   NODECOUNT 
  3. worker   200      2 

Event

我們在 controller 當(dāng)中添加 Recorder 用來記錄事件,K8s 中事件有 Normal 和 Warning 兩種類型

  1. // NodePoolReconciler reconciles a NodePool object 
  2. type NodePoolReconciler struct { 
  3.  client.Client 
  4.  Log      logr.Logger 
  5.  Scheme   *runtime.Scheme 
  6. + Recorder record.EventRecorder 
  7.  
  8. func (r *NodePoolReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { 
  9.   
  10. + // 添加測試事件 
  11. + r.Recorder.Event(pool, corev1.EventTypeNormal, "test""test"
  12.  
  13.  pool.Status.Status = 200 
  14.  err = r.Status().Update(ctx, pool) 
  15.  return ctrl.Result{}, err 

添加好之后還需要在 main.go 中加上 Recorder的初始化邏輯

  1. if err = (&controllers.NodePoolReconciler{ 
  2.   Client:   mgr.GetClient(), 
  3.   Log:      ctrl.Log.WithName("controllers").WithName("NodePool"), 
  4.   Scheme:   mgr.GetScheme(), 
  5. +  Recorder: mgr.GetEventRecorderFor("NodePool"), 
  6.  }).SetupWithManager(mgr); err != nil { 
  7.   setupLog.Error(err, "unable to create controller""controller""NodePool"
  8.   os.Exit(1) 
  9.  } 

加好之后我們運行一下,然后在 describe Nodepool 對象就能看到事件信息了

  1. Events: 
  2.   Type    Reason  Age   From      Message 
  3.   ----    ------  ----  ----      ------- 
  4.   Normal  test    4s    NodePool  test 

監(jiān)聽更多資源

之前我們所有的代碼都是圍繞著 NodePool 的變化來展開的,但是我們?nèi)绻薷牧?Node 的相關(guān)標(biāo)簽,將 Node 添加到一個 NodePool,Node 上對應(yīng)的屬性和 NodePool 的 status 信息也不會改變。如果我們想要實現(xiàn)上面的效果就需要監(jiān)聽更多的資源變化。

在 controller 當(dāng)中我們可以看到一個 SetupWithManager方法,這個方法說明了我們需要監(jiān)聽哪些資源的變化

  1. // SetupWithManager sets up the controller with the Manager. 
  2. func (r *NodePoolReconciler) SetupWithManager(mgr ctrl.Manager) error { 
  3.  return ctrl.NewControllerManagedBy(mgr). 
  4.   For(&nodesv1.NodePool{}). 
  5.   Complete(r) 

其中 NewControllerManagedBy是一個建造者模式,返回的是一個 builder 對象,其包含了用于構(gòu)建的 For、Owns、Watches、WithEventFilter等方法

這里我們就可以利用 ``Watches方法來監(jiān)聽 Node 的變化,我們這里使用handler.Funcs`自定義了一個入隊器

監(jiān)聽 Node 對象的更新事件,如果存在和 NodePool 關(guān)聯(lián)的 node 對象更新就把對應(yīng)的 NodePool 入隊

  1. // SetupWithManager sets up the controller with the Manager. 
  2. func (r *NodePoolReconciler) SetupWithManager(mgr ctrl.Manager) error { 
  3.  return ctrl.NewControllerManagedBy(mgr). 
  4.   For(&nodesv1.NodePool{}). 
  5.   Watches(&source.Kind{Type: &corev1.Node{}}, handler.Funcs{UpdateFunc: r.nodeUpdateHandler}). 
  6.   Complete(r) 
  7.  
  8. func (r *NodePoolReconciler) nodeUpdateHandler(e event.UpdateEvent, q workqueue.RateLimitingInterface) { 
  9.  ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second
  10.  defer cancel() 
  11.  
  12.  oldPool, err := r.getNodePoolByLabels(ctx, e.ObjectOld.GetLabels()) 
  13.  if err != nil { 
  14.   r.Log.Error(err, "get node pool err"
  15.  } 
  16.  if oldPool != nil { 
  17.   q.Add(reconcile.Request{ 
  18.    NamespacedName: types.NamespacedName{Name: oldPool.Name}, 
  19.   }) 
  20.  } 
  21.  
  22.  newPool, err := r.getNodePoolByLabels(ctx, e.ObjectOld.GetLabels()) 
  23.  if err != nil { 
  24.   r.Log.Error(err, "get node pool err"
  25.  } 
  26.  if newPool != nil { 
  27.   q.Add(reconcile.Request{ 
  28.    NamespacedName: types.NamespacedName{Name: newPool.Name}, 
  29.   }) 
  30.  } 
  31.  
  32. func (r *NodePoolReconciler) getNodePoolByLabels(ctx context.Context, labels map[string]string) (*nodesv1.NodePool, error) { 
  33.  pool := &nodesv1.NodePool{} 
  34.  for k := range labels { 
  35.   ss := strings.Split(k, "node-role.kubernetes.io/"
  36.   if len(ss) != 2 { 
  37.    continue 
  38.   } 
  39.   err := r.Client.Get(ctx, types.NamespacedName{Name: ss[1]}, pool) 
  40.   if err == nil { 
  41.    return pool, nil 
  42.   } 
  43.  
  44.   if client.IgnoreNotFound(err) != nil { 
  45.    return nil, err 
  46.   } 
  47.  } 
  48.  return nil, nil 

總結(jié)

今天我們完善了 status & event 和自定義對象 watch 下一篇我們看一下如何對我們的 Operator 進行測試

 

責(zé)任編輯:姜華 來源: mohuishou
相關(guān)推薦

2021-05-12 06:18:19

KubeBuilderOperatork8s

2021-05-18 05:40:27

kubebuilderwebhook進階

2021-05-17 05:51:31

KubeBuilderOperator測試

2021-05-08 09:02:48

KubeBuilderOperatork8s

2021-05-20 06:57:16

RabbitMQ開源消息

2023-04-20 08:00:00

ES搜索引擎MySQL

2021-06-16 08:28:25

unary 方法函數(shù)技術(shù)

2022-03-10 08:31:51

REST接口規(guī)范設(shè)計Restful架構(gòu)

2025-01-17 07:00:00

2022-02-24 07:56:42

開發(fā)Viteesbuild

2022-02-21 09:44:45

Git開源分布式

2023-05-12 08:19:12

Netty程序框架

2021-06-30 00:20:12

Hangfire.NET平臺

2021-07-28 10:02:54

建造者模式代碼

2021-07-14 08:24:23

TCPIP 通信協(xié)議

2022-04-08 08:32:40

mobx狀態(tài)管理庫redux

2021-08-11 07:02:21

npm包管理器工具

2020-11-27 08:02:41

Promise

2021-08-02 06:34:55

Redis刪除策略開源

2021-11-08 08:42:44

CentOS Supervisor運維
點贊
收藏

51CTO技術(shù)棧公眾號