【Spark Operator】webhook的分析

时间:2022-07-22
本文章向大家介绍【Spark Operator】webhook的分析,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spark Operator 支持 Webhook,因为 Spark on Kubernetes 模块对 Pod 的特性支持得太有限了,这个问题主要就是如果通过 Spark Conf 传特性,那么 Spark Conf 就得膨胀,通过 Pod Template 去,又因为 Spark 本身没有对 Template 文件去校验,这样会导致调试很麻烦,而 Webhook 相对比较轻一点,但是也是需要 1.8 以上的 Kubernetes 版本才能用。Webhook 本身的代码量并不多,可以仔细看看,核心代码都在 webhook.go 文件里了。

另外就是 webhook 在 v1.14 里只支持 namespaceselector,也就是这个 namespace 下的 pod 都会过一遍这个 webhook,通过 patch 去调整 Pod,但如果业务使用的时候,一个 namespace 下有很多其他类型的计算任务,那么其他类型的计算任务也需要过一次 webhoo,那就是完全没必要的了。 幸好的是 v1.16 之后,支持类似于 podlabelselector 之类的功能,也就是 Pod 需要满足包含约定的 Label 的,才会过一遍 webhook。

	// NamespaceSelector decides whether to run the webhook on an object based
	// on whether the namespace for that object matches the selector. If the
	// object itself is a namespace, the matching is performed on
	// object.metadata.labels. If the object is another cluster scoped resource,
	// it never skips the webhook.
	//
	// For example, to run the webhook on any objects whose namespace is not
	// associated with "runlevel" of "0" or "1";  you will set the selector as
	// follows:
	// "namespaceSelector": {
	//   "matchExpressions": [
	//     {
	//       "key": "runlevel",
	//       "operator": "NotIn",
	//       "values": [
	//         "0",
	//         "1"
	//       ]
	//     }
	//   ]
	// }
	//
	// If instead you want to only run the webhook on any objects whose
	// namespace is associated with the "environment" of "prod" or "staging";
	// you will set the selector as follows:
	// "namespaceSelector": {
	//   "matchExpressions": [
	//     {
	//       "key": "environment",
	//       "operator": "In",
	//       "values": [
	//         "prod",
	//         "staging"
	//       ]
	//     }
	//   ]
	// }
	//
	// See
	// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
	// for more examples of label selectors.
	//
	// Default to the empty LabelSelector, which matches everything.
	// +optional
	NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty" protobuf:"bytes,5,opt,name=namespaceSelector"`

跟一般的 webhook 有点不一样的地方是,一般常见的 webhook 都是作为独立的服务部署在 k8s 集群里的,而 Spark Operator 的 webhook 相当于是 embeded 在 Spark Operator 里,跟 Spark Operator 在同一个 Pod 里。

Spark Operator 的 webhook 的第一个版本。 如果给 Spark Operator 传了 enableWebhook 的参数,那么 webhook 就会在下图的流程中启动服务。个人认为,webhook 单独摘出来会比较好。

Reference

  1. https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
  2. https://kubernetes.io/zh/docs/reference/access-authn-authz/extensible-admission-controllers/