跳到内容

使用 Knative 示例存储库

Knative 项目提供了一个 示例存储库,其中包含一个基本事件源控制器和接收器适配器的模板。

先决条件

  • 您熟悉 Kubernetes 和 Go 开发。
  • 您已安装 Git。
  • 您已安装 Go。
  • 您已克隆 sample-source 存储库

可选

示例文件概述

接收器适配器文件

  • cmd/receive_adapter/main.go - 将资源变量转换为底层适配器结构,以便可以将其传递到 Knative 系统中。

  • pkg/adapter/adapter.go - 支持将事件转换为 CloudEvents 的接收器适配器转换的函数。

控制器文件

  • cmd/controller/main.go - 将事件源的 NewController 实现传递给共享的 main 方法。

  • pkg/reconciler/sample/controller.go - 传递给共享的 main 方法的 NewController 实现。

CRD 文件

  • pkg/apis/samples/VERSION/samplesource_types.go - 底层 API 类型的架构,它提供在资源 YAML 文件中定义以供使用的变量。

协调器文件

  • pkg/reconciler/sample/samplesource.go - 接收器适配器的协调函数。

  • pkg/apis/samples/VERSION/samplesource_lifecycle.go - 包含事件源协调详细信息的*状态*信息

    • 源就绪
    • 提供接收器地址
    • 已部署
    • 提供事件类型
    • Kubernetes 资源正确

步骤

  1. pkg/apis/samples/v1alpha1/samplesource_types.go 中定义资源架构所需的类型。

    这包括资源 YAML 中需要的字段,以及控制器使用源的 clientset 和 API 引用到的字段

    // +genclient
    // +genreconciler
    // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    // +k8s:openapi-gen=true
    type SampleSource struct {
        metav1.TypeMeta `json:",inline"`
        // +optional
        metav1.ObjectMeta `json:"metadata,omitempty"`
    
        // Spec holds the desired state of the SampleSource (from the client).
        Spec SampleSourceSpec `json:"spec"`
    
        // Status communicates the observed state of the SampleSource (from the controller).
        // +optional
        Status SampleSourceStatus `json:"status,omitempty"`
    }
    
    // SampleSourceSpec holds the desired state of the SampleSource (from the client).
    type SampleSourceSpec struct {
        // inherits duck/v1 SourceSpec, which currently provides:
        // * Sink - a reference to an object that will resolve to a domain name or
        //   a URI directly to use as the sink.
        // * CloudEventOverrides - defines overrides to control the output format
        //   and modifications of the event sent to the sink.
        duckv1.SourceSpec `json:",inline"`
    
        // ServiceAccountName holds the name of the Kubernetes service account
        // as which the underlying K8s resources should be run. If unspecified
        // this will default to the "default" service account for the namespace
        // in which the SampleSource exists.
        // +optional
        ServiceAccountName string `json:"serviceAccountName,omitempty"`
    
        // Interval is the time interval between events.
        //
        // The string format is a sequence of decimal numbers, each with optional
        // fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time
        // units are "ns", "us" (or "µs"), "ms", "s", "m", "h". If unspecified
        // this will default to "10s".
        Interval string `json:"interval"`
    }
    
    // SampleSourceStatus communicates the observed state of the SampleSource (from the controller).
    type SampleSourceStatus struct {
        duckv1.Status `json:",inline"`
    
        // SinkURI is the current active sink URI that has been configured
        // for the SampleSource.
        // +optional
        SinkURI *apis.URL `json:"sinkUri,omitempty"`
    }
    
  2. 定义要反映在 statusSinkURI 字段中的生命周期

    const (
        // SampleConditionReady has status True when the SampleSource is ready to send events.
        SampleConditionReady = apis.ConditionReady
        // ...
    )
    
  3. 通过定义要从协调函数调用的函数来设置生命周期条件。这通常在 pkg/apis/samples/VERSION/samplesource_lifecycle.go 中完成

    // InitializeConditions sets relevant unset conditions to Unknown state.
    func (s *SampleSourceStatus) InitializeConditions() {
        SampleCondSet.Manage(s).InitializeConditions()
    }
    
    ...
    
    // MarkSink sets the condition that the source has a sink configured.
    func (s *SampleSourceStatus) MarkSink(uri *apis.URL) {
        s.SinkURI = uri
        if len(uri.String()) > 0 {
            SampleCondSet.Manage(s).MarkTrue(SampleConditionSinkProvided)
        } else {
            SampleCondSet.Manage(s).MarkUnknown(SampleConditionSinkProvided, "SinkEmpty", "Sink has resolved to empty.%s", "")
        }
    }
    
    // MarkNoSink sets the condition that the source does not have a sink configured.
    func (s *SampleSourceStatus) MarkNoSink(reason, messageFormat string, messageA ...interface{}) {
        SampleCondSet.Manage(s).MarkFalse(SampleConditionSinkProvided, reason, messageFormat, messageA...)
    }
    

我们使用分析和 cookie 来了解网站流量。有关您使用我们网站的信息会与 Google 共享以达到此目的。了解更多。