Knative Eventing 中 Channel 如何注入默認 Provisioner

摘要: 在 Knative Eventing 中建立 Broker 時,若是不指定 provisioner, 系統會自動建立默認的 provisioner, 那麼這個機制是如何實現的呢? 本文基於 Knative Eventing 0.5 版本,介紹了這個實現機制。

場景

一般的在建立Broker時,咱們須要經過 spec.ChannelTemplate 指定使用某個具體的 Channel Provisioner。例如這樣的Broker:web

apiVersion: eventing.knative.dev/v1alpha1
kind: Broker
metadata:
  name: pubsub-channel
spec:
  channelTemplate:
    provisioner:
      apiVersion: eventing.knative.dev/v1alpha1
      kind: ClusterChannelProvisioner
      name: gcp-pubsub

這裏經過spec.ChannelTemplate 指定了名稱爲gcp-pubsub的provisioner。那麼咱們也遇到過這樣的Broker:api

apiVersion: eventing.knative.dev/v1alpha1
kind: Broker
metadata:
  name: default

並無指定使用某個具體的 channel, 但建立完Broker以後會發現已經建立出來了Channel:ide

apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
metadata:
  ...
  name: default-broker-8ml79
  namespace: default
  ownerReferences:
  - apiVersion: eventing.knative.dev/v1alpha1
    blockOwnerDeletion: true
    controller: true
    kind: Broker
    name: default
    uid: 2e4c3332-6755-11e9-a81f-00163f005e02
spec:
  provisioner:
    apiVersion: eventing.knative.dev/v1alpha1
    kind: ClusterChannelProvisioner
    name: in-memory
...

分析

咱們知道 Broker建立以後,會經過 reconcile controller 會建立相應的Channel, 也就是下面這段代碼:ui

// newChannel creates a new Channel for Broker 'b'.
func newChannel(b *v1alpha1.Broker, l map[string]string) *v1alpha1.Channel {
    var spec v1alpha1.ChannelSpec
    if b.Spec.ChannelTemplate != nil {
        spec = *b.Spec.ChannelTemplate
    }

    return &v1alpha1.Channel{
        ObjectMeta: metav1.ObjectMeta{
            Namespace:    b.Namespace,
            GenerateName: fmt.Sprintf("%s-broker-", b.Name),
            Labels:       l,
            OwnerReferences: []metav1.OwnerReference{
                *metav1.NewControllerRef(b, schema.GroupVersionKind{
                    Group:   v1alpha1.SchemeGroupVersion.Group,
                    Version: v1alpha1.SchemeGroupVersion.Version,
                    Kind:    "Broker",
                }),
            },
        },
        Spec: spec,
    }
}

分析上面這段代碼,咱們能夠很清楚得出這樣的結論:若是Broker中設置了Spec.ChannelTemplate, 那麼Channel中會直接使用ChannelTemplate所對應的provisioner。
但若是沒有設置的話, 那麼Channel中的spec應該設置爲nil。但事實上設置了in-memory provisioner, 那麼這個是在哪裏注入的呢?this

注入機制

通過定位源代碼,咱們發如今channel_defaults.go中,發現下面這段代碼:spa

func (c *Channel) SetDefaults(ctx context.Context) {
    if c != nil && c.Spec.Provisioner == nil {
        // The singleton may not have been set, if so ignore it and validation will reject the
        // Channel.
        if cd := ChannelDefaulterSingleton; cd != nil {
            prov, args := cd.GetDefault(c.DeepCopy())
            c.Spec.Provisioner = prov
            c.Spec.Arguments = args
        }
    }
    c.Spec.SetDefaults(ctx)
}

分析一下,咱們能夠看到當c.Spec.Provisioner==nil時, 會設置默認的Provisioner。
進一步分析ChannelDefaulterSingleton, 咱們能夠在webhook中賦予了實現設置:3d

...
// Watch the default-channel-webhook ConfigMap and dynamically update the default
// ClusterChannelProvisioner.
channelDefaulter := channeldefaulter.New(logger.Desugar())
eventingv1alpha1.ChannelDefaulterSingleton = channelDefaulter
configMapWatcher.Watch(channeldefaulter.ConfigMapName, channelDefaulter.UpdateConfigMap)
...

接着分析發現 ChannelDefaulter 實現了 GetDefault 方法:code

// GetDefault determines the default provisioner and arguments for the provided channel.
func (cd *ChannelDefaulter) GetDefault(c *eventingv1alpha1.Channel) (*corev1.ObjectReference, *runtime.RawExtension) {
    // Because we are treating this as a singleton, be tolerant to it having not been setup at all.
    if cd == nil {
        return nil, nil
    }
    if c == nil {
        return nil, nil
    }
    config := cd.getConfig()
    if config == nil {
        return nil, nil
    }

    // TODO Don't use a single default, instead use the Channel's arguments to determine the type of
    // Channel to use (e.g. it can say whether it needs to be persistent, strictly ordered, etc.).
    dp := getDefaultProvisioner(config, c.Namespace)
    cd.logger.Info("Defaulting the ClusterChannelProvisioner", zap.Any("defaultClusterChannelProvisioner", dp))
    return dp, nil
}

而且這裏是經過一個ConfigMap設置使用的默認provisioner, 這個ConfigMap名稱爲default-channel-webhook, 沒錯能夠在 Knative Eventing 安裝文件中發現這個資源:blog

apiVersion: v1
data:
  default-channel-config: |
    clusterdefault:
      apiversion: eventing.knative.dev/v1alpha1
      kind: ClusterChannelProvisioner
      name: in-memory
    namespacedefaults:
      some-namespace:
        apiversion: eventing.knative.dev/v1alpha1
        kind: ClusterChannelProvisioner
        name: some-other-provisioner
kind: ConfigMap
metadata:
  name: default-channel-webhook
  namespace: knative-eventing

那麼分析到此,咱們梳理一下整個注入的流程:ci

結論

經過上面的分析, 咱們如今瞭解了默認provisioner的注入機制, 同時咱們也能夠經過 webhook 修改默認的provisioner。



本文做者:元毅

閱讀原文

本文爲雲棲社區原創內容,未經容許不得轉載。

相關文章
相關標籤/搜索