operator最佳实践

https://www.openshift.com/blog/kubernetes-operators-best-practices

代码看起来都是似懂非懂的,有个印象和概念吧,里面说到了谓词【funny】

introduction

  • actions
    • connect the master api and watch for events
    • event occurs, operator reacts and performs a specific action[master api or other system]
  • implemented as a collection of controllers
    • each controller watches a specific resource type
    • event occurs on a watched resource
    • a reconcile cycle is started
  • level-based triggering
    • because signals can be lost or retransmitted multiple times

the lifecycle of an api request

  • authorization webhook
  • mutating webhook
  • valicating webhook

create watches

  • api server(Events), Predicates(Filtered Events), Handle, reconcile instance

Resource Reconciliation Cycle

  • Retrieve the interested CR instance
  • Manage the instance validity
  • Manage instance initialization
  • Manage instance deletion
  • Manage controller business logic

Resource Validation

  • 两种验证类型:Syntactic validation and semantic validation.

  • SYNTACTIC VALIDATION

    • OpenAPI validation rules can be added as described here.

    • Recommendation: model as much of the custom resource as possible of your validation as syntactic validation. Syntactic validation is relatively straightforward and prevents badly formed CRs from being stored in etcd, so it should be used as much as possible.

  • SEMANTIC VALIDATION

    • Semantic validation is about making sure that fields have sensible values and that the entire resource record is meaningful. Semantic validation business logic depends on the concept that the CR represents and must be coded by the operator developer.
    • If semantic validation is required by the given CR, then the operator should expose a webhook and ValidatingAdmissionConfiguration should be created as part of the operator deployment.
  • compare

    • It is better to reject an invalid CR rather than to accept it in etcd and then manage the error condition. That said, there could be situations in which the ValidatingAdmissionConfiguration is not deployed or not available at all.

Resource Initialization

  • content
    • user: only the needed fields of a resource are to be initialized
    • coder or debugger: it is actually better to have all the fields initialized
  • Define an initialization method
    • MutatingAdmissionConfiguration

Resource Finalization

  • Finalizers
    • inform the Kubernetes control plane that an action needs to take place before the standard Kubernetes garbage collection logic can be performe
  • pseudo code algorithm to manage finalizers
    • If needed, add finalizers during the initialization method.
    • If the resource is being deleted, check if the finalizer owned by this controller is present.
      • If not, return
      • If yes, execute the cleanup logic
        • If successful, update the CR by removing the finalizer.
        • If failure decide whether to retry or give up and likely leave garbage (in some situations this can be acceptable).
  • notice
    • If your clean-up logic requires creating additional resources, do keep in mind that additional resources cannot be created in a namespace that is being deleted. A To-be-deleted namespace will trigger a delete of all in the included resources including your CR with the finalizer.

Resource Ownership 资源所有权

  • delete resource,guarantee correct garbage collection
    • When a resource is deleted and it owns other resources the children resources will be, by default, also deleted (you can disable this behavior, by setting cascade=false).
  • rules
    • The owner object must be in the same namespace as the owned object.
    • A namespaced resource can own a cluster level resource. We have to be careful here. An object can have a list of owners. If multiple namespaced objects own the same cluster-level object then each should claim ownership without overwriting the others’ ownership (the above API takes care of that).
    • A cluster level resource cannot own a namespaced resource.
    • A cluster level object can own another cluster level object.

managing status

  • the status of the resource
  • update our resource, execute the reconcile cycle

managing error

  • If a controller enters an error condition and returns an error in the reconcile method, the error will be logged by the operator to standard output and a reconciliation event will be immediately rescheduled.
  • notify the user of an error:
    • Return the error in the status of the object
    • Generate an event describing the error
  • time
    • a calculation is made as to when to reschedule the next attempt. The algorithm tries to double the time every loop, up to a maximum of six hours.

conclusion

  • see before chapter
  • refer to the operator-utils repository.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值