mirror of https://github.com/knative/docs.git
557 lines
115 KiB
XML
557 lines
115 KiB
XML
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||
<channel>
|
||
<title>Knative – Creating an event source by using the sample event source</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/</link>
|
||
<description>Recent content in Creating an event source by using the sample event source on Knative</description>
|
||
<generator>Hugo -- gohugo.io</generator>
|
||
|
||
<atom:link href="https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/index.xml" rel="self" type="application/rss+xml" />
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
<item>
|
||
<title>V0.23-Docs: Design and Theory Behind an Event Source</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/01-theory/</link>
|
||
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
||
|
||
<guid>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/01-theory/</guid>
|
||
<description>
|
||
|
||
|
||
<h1 id="topics">Topics</h1>
|
||
<p>What are the personas and critical paths?</p>
|
||
<ul>
|
||
<li>Contributor: implement a new source with minimal k8s overhead (don&rsquo;t have to learn controller/k8s internals)</li>
|
||
<li>Operator: easily install Sources and verify that they are &ldquo;safe&rdquo;</li>
|
||
<li>Developer: easily discover what Sources they can pull from on this cluster</li>
|
||
<li>Developer: easily configure a Source based on existing knowledge of other Sources.</li>
|
||
</ul>
|
||
<h2 id="separation-of-concerns">Separation of concerns</h2>
|
||
<h3 id="contributor">Contributor:</h3>
|
||
<ul>
|
||
<li>Receive Adapter (RA) - process that receives incoming events.</li>
|
||
<li>Implement CloudEvent binding interfaces, <a href="https://github.com/cloudevents/sdk-go">cloudevent&rsquo;s go sdk</a> provides libraries for standard access to configure interfaces as needed.</li>
|
||
<li>Passing configuration from the Source <code>CRD</code> YAML, that the controller needs to configure the <code>Receive Adapter</code></li>
|
||
</ul>
|
||
<h3 id="source-library-provided-by-knative">Source library (provided by Knative):</h3>
|
||
<ul>
|
||
<li>Controller runtime (this is what we share via injection) incorporates protocol specific config into &ldquo;generic controller&rdquo; CRD.</li>
|
||
<li>Identifying specific event characteristics (i.e. value of interest, relevant metadata, etc) to pass along to the serverless system</li>
|
||
<li>Propagating events internally to the system (i.e. cloudevents)</li>
|
||
</ul>
|
||
<h1 id="theory">Theory</h1>
|
||
<p>Quick Introduction to Knative Eventing Sources
|
||
A Knative Source is Kubernetes Custom Resource that generates or imports an event and pushes that event to another endpoint on the cluster via a <a href="https://github.com/cloudevents/spec/blob/v1.0/primer.md">CloudEvents</a>.</p>
|
||
<p><a href="https://github.com/knative/eventing/blob/main/docs/spec/sources.md">The specification</a>
|
||
for Knative Eventing Sources contains a number of requirements that
|
||
together define a well-behaved Knative Source.</p>
|
||
<p>To achieve this, there are several separations of concerns that we have to keep in mind:</p>
|
||
<ol>
|
||
<li>A controller to run our Event Source and reconcile the underlying <code>Receive Adapter</code> deployments</li>
|
||
<li>A &ldquo;receive adapter&rdquo; which generates or imports the actual events</li>
|
||
<li>A series of identifying characteristics for our event</li>
|
||
<li>Transporting a valid event to the serverless system for further processing</li>
|
||
</ol>
|
||
<p>There are also two different classes of developer to consider:</p>
|
||
<ol>
|
||
<li>A &ldquo;contributor&rdquo; knows about the foreign protocol but is not a Knative expert.</li>
|
||
<li>Knative Eventing expert knows how Knative Eventing components are implemented, configured and deployed, but is not an expert in all the foreign protocols that sources may implement.</li>
|
||
</ol>
|
||
<p>These two roles will often not be the same person. We want to confine the job of the &ldquo;contributor&rdquo; to implementing the <code>Receive Adapter</code>, and specifying what configuration their adapter needs to connect, subscribe, or do whatever it does.</p>
|
||
<p>The Knative Eventing developer exposes the protocol configuration as part of the Source <code>CRD</code>, and the controller passes any required configuration (which may include resolved data like URLs) to the <code>Receive Adapter</code>.</p>
|
||
<p>API Resources required:</p>
|
||
<ul>
|
||
<li><code>KubeClientSet.Appsv1.Deployment</code> (Inherited via the Eventing base reconciler)
|
||
Used to deploy the <code>Receive Adapter</code> for &ldquo;importing&rdquo; events</li>
|
||
<li><code>EventingClientSet.EventingV1Alpha1</code> (Inherited via the Eventing base reconciler)
|
||
Used to interact with Events within the Knative system</li>
|
||
<li><code>SourceClientSet.SourcesV1Alpha1</code>
|
||
Used for source — in this case, <code>samplesource</code> — specific config and translated to the underlying deployment (via the inherited KubeClientSet)</li>
|
||
</ul>
|
||
<p>To ease writing a new event source, the eventing subsystem has offloaded several core functionalities (via injection) to the <code>eventing-sources-controller</code>.</p>
|
||
<p><img src="https://raw.githubusercontent.com/knative/docs/main/docs/eventing/samples/writing-event-source/simplified-controller.png" alt="Simplified Controller"></p>
|
||
<p>Fig 1. - Via shared <a href="https://docs.google.com/presentation/d/e/2PACX-1vQbpISBvY7jqzu2wy2t1_0R4LSBEBS0JrUS7M7V3BMVqy2K1Zk_0Xhy7WPPaeANLHE0yqtz1DuWlSAl/pub?resourcekey=0-mf6dN2vu9SS3bo2TUeCk9Q&amp;slide=id.g596dcbbefb_0_40">Knative Dependency Injection</a></p>
|
||
<p>Specifically, the <code>clientset</code>, <code>cache</code>, <code>informers</code>, and <code>listers</code> can all be generated and shared. Thus, they can be generated, imported, and assigned to the underlying reconciler when creating a new controller source implementation:</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-golang" data-lang="golang"><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000;font-weight:bold">(</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">sampleSourceClient</span> <span style="color:#4e9a06">&#34;knative.dev/sample-source/pkg/client/injection/client&#34;</span>
|
||
<span style="color:#000">samplesourceinformer</span> <span style="color:#4e9a06">&#34;knative.dev/sample-source/pkg/client/injection/informers/samples/v1alpha1/samplesource&#34;</span>
|
||
<span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">NewController</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span> <span style="color:#000">context</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Context</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cmw</span> <span style="color:#000">configmap</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Watcher</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">controller</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Impl</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">sampleSourceInformer</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">samplesourceinformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">)</span>
|
||
|
||
<span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">Reconciler</span><span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">samplesourceClientSet</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">sampleSourceClient</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">),</span>
|
||
<span style="color:#000">samplesourceLister</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">sampleSourceInformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Lister</span><span style="color:#000;font-weight:bold">(),</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><p>Sample source&rsquo;s <a href="https://github.com/knative-sandbox/sample-source/blob/main/hack/update-codegen.sh"><code>update-codegen.sh</code></a> have the configuration
|
||
to have the required things above generated and injected:</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-bash" data-lang="bash"><span style="color:#8f5902;font-style:italic"># Generation</span>
|
||
<span style="color:#4e9a06">${</span><span style="color:#000">CODEGEN_PKG</span><span style="color:#4e9a06">}</span>/generate-groups.sh <span style="color:#4e9a06">&#34;deepcopy,client,informer,lister&#34;</span> <span style="color:#4e9a06">\
|
||
</span><span style="color:#4e9a06"></span> knative.dev/sample-source/pkg/client knative.dev/sample-source/pkg/apis <span style="color:#4e9a06">\
|
||
</span><span style="color:#4e9a06"></span> <span style="color:#4e9a06">&#34;samples:v1alpha1&#34;</span> <span style="color:#4e9a06">\
|
||
</span><span style="color:#4e9a06"></span> --go-header-file <span style="color:#4e9a06">${</span><span style="color:#000">REPO_ROOT</span><span style="color:#4e9a06">}</span>/hack/boilerplate/boilerplate.go.txt
|
||
|
||
<span style="color:#8f5902;font-style:italic"># Injection</span>
|
||
<span style="color:#4e9a06">${</span><span style="color:#000">KNATIVE_CODEGEN_PKG</span><span style="color:#4e9a06">}</span>/hack/generate-knative.sh <span style="color:#4e9a06">&#34;injection&#34;</span> <span style="color:#4e9a06">\
|
||
</span><span style="color:#4e9a06"></span> knative.dev/sample-source/pkg/client knative.dev/sample-source/pkg/apis <span style="color:#4e9a06">\
|
||
</span><span style="color:#4e9a06"></span> <span style="color:#4e9a06">&#34;samples:v1alpha1&#34;</span> <span style="color:#4e9a06">\
|
||
</span><span style="color:#4e9a06"></span> --go-header-file <span style="color:#4e9a06">${</span><span style="color:#000">REPO_ROOT</span><span style="color:#4e9a06">}</span>/hack/boilerplate/boilerplate.go.txt
|
||
</code></pre></div><p>File Layout &amp; Hierarchy:</p>
|
||
<ul>
|
||
<li><code>cmd/controller/main.go</code> - Pass source’s NewController implementation to the shared main</li>
|
||
<li><code>cmd/receive_adapter/main.go</code> - Translate resource variables to underlying adapter struct (to eventually be passed into the serverless system)</li>
|
||
<li><code>pkg/reconciler/sample/controller.go</code> - NewController implementation to pass to sharedmain</li>
|
||
<li><code>pkg/reconciler/sample/samplesource.go</code> - reconciliation functions for the receive adapter</li>
|
||
<li><code>pkg/apis/samples/VERSION/samplesource_types.go</code> - schema for the underlying api types (variables to be defined in the resource yaml)</li>
|
||
<li><code>pkg/apis/samples/VERSION/samplesource_lifecycle.go</code> - status updates for the source’s reconciliation details
|
||
<ul>
|
||
<li>Source ready</li>
|
||
<li>Sink provided</li>
|
||
<li>Deployed</li>
|
||
<li>Eventtype Provided</li>
|
||
<li>K8s Resources Correct</li>
|
||
</ul>
|
||
</li>
|
||
<li><code>pkg/adapter/adapter.go</code> - receive_adapter functions supporting translation of events to CloudEvents</li>
|
||
</ul>
|
||
|
||
</description>
|
||
</item>
|
||
|
||
<item>
|
||
<title>V0.23-Docs: Sample Source Lifecycle and Types</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/02-lifecycle-and-types/</link>
|
||
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
||
|
||
<guid>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/02-lifecycle-and-types/</guid>
|
||
<description>
|
||
|
||
|
||
<h2 id="api-definition">API Definition</h2>
|
||
<ol>
|
||
<li>Define the types required in the resource’s schema in
|
||
<code>pkg/apis/samples/v1alpha1/samplesource_types.go</code>
|
||
This includes the fields that will be required in the resource yaml as
|
||
well as what will be referenced in the controller using the source’s
|
||
clientset and API</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#8f5902;font-style:italic">// +genclient
|
||
</span><span style="color:#8f5902;font-style:italic">// +genreconciler
|
||
</span><span style="color:#8f5902;font-style:italic">// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
|
||
</span><span style="color:#8f5902;font-style:italic">// +k8s:openapi-gen=true
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">type</span> <span style="color:#000">SampleSource</span> <span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">metav1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">TypeMeta</span> <span style="color:#4e9a06">`json:&#34;,inline&#34;`</span>
|
||
<span style="color:#8f5902;font-style:italic">// +optional
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">metav1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ObjectMeta</span> <span style="color:#4e9a06">`json:&#34;metadata,omitempty&#34;`</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// Spec holds the desired state of the SampleSource (from the client).
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">Spec</span> <span style="color:#000">SampleSourceSpec</span> <span style="color:#4e9a06">`json:&#34;spec&#34;`</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// Status communicates the observed state of the SampleSource (from the controller).
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// +optional
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">Status</span> <span style="color:#000">SampleSourceStatus</span> <span style="color:#4e9a06">`json:&#34;status,omitempty&#34;`</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// SampleSourceSpec holds the desired state of the SampleSource (from the client).
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">type</span> <span style="color:#000">SampleSourceSpec</span> <span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#8f5902;font-style:italic">// inherits duck/v1 SourceSpec, which currently provides:
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// * Sink - a reference to an object that will resolve to a domain name or
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// a URI directly to use as the sink.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// * CloudEventOverrides - defines overrides to control the output format
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// and modifications of the event sent to the sink.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">duckv1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SourceSpec</span> <span style="color:#4e9a06">`json:&#34;,inline&#34;`</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// ServiceAccountName holds the name of the Kubernetes service account
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// as which the underlying K8s resources should be run. If unspecified
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// this will default to the &#34;default&#34; service account for the namespace
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// in which the SampleSource exists.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// +optional
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">ServiceAccountName</span> <span style="color:#204a87;font-weight:bold">string</span> <span style="color:#4e9a06">`json:&#34;serviceAccountName,omitempty&#34;`</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// Interval is the time interval between events.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">//
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// The string format is a sequence of decimal numbers, each with optional
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// fraction and a unit suffix, such as &#34;300ms&#34;, &#34;-1.5h&#34; or &#34;2h45m&#34;. Valid time
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// units are &#34;ns&#34;, &#34;us&#34; (or &#34;µs&#34;), &#34;ms&#34;, &#34;s&#34;, &#34;m&#34;, &#34;h&#34;. If unspecified
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// this will default to &#34;10s&#34;.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">Interval</span> <span style="color:#204a87;font-weight:bold">string</span> <span style="color:#4e9a06">`json:&#34;interval&#34;`</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// SampleSourceStatus communicates the observed state of the SampleSource (from the controller).
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">type</span> <span style="color:#000">SampleSourceStatus</span> <span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">duckv1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Status</span> <span style="color:#4e9a06">`json:&#34;,inline&#34;`</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// SinkURI is the current active sink URI that has been configured
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// for the SampleSource.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#8f5902;font-style:italic">// +optional
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">SinkURI</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">apis</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">URL</span> <span style="color:#4e9a06">`json:&#34;sinkUri,omitempty&#34;`</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><p>Define the lifecycle that will be reflected in the status and SinkURI fields</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">const</span> <span style="color:#000;font-weight:bold">(</span>
|
||
<span style="color:#8f5902;font-style:italic">// SampleConditionReady has status True when the SampleSource is ready to send events.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">SampleConditionReady</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">apis</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ConditionReady</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">)</span>
|
||
|
||
</code></pre></div><p>Define the functions that will be called from the Reconciler functions to set the lifecycle conditions. This is typically done in
|
||
<code>pkg/apis/samples/VERSION/samplesource_lifecycle.go</code></p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#8f5902;font-style:italic">// InitializeConditions sets relevant unset conditions to Unknown state.
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">SampleSourceStatus</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">InitializeConditions</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">SampleCondSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Manage</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">InitializeConditions</span><span style="color:#000;font-weight:bold">()</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
|
||
<span style="color:#ce5c00;font-weight:bold">...</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// MarkSink sets the condition that the source has a sink configured.
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">SampleSourceStatus</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">MarkSink</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">uri</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">apis</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">URL</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">s</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SinkURI</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">uri</span>
|
||
<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#204a87">len</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">uri</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">())</span> <span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">SampleCondSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Manage</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">MarkTrue</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">SampleConditionSinkProvided</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">SampleCondSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Manage</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">MarkUnknown</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">SampleConditionSinkProvided</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;SinkEmpty&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;Sink has resolved to empty.%s&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;&#34;</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// MarkNoSink sets the condition that the source does not have a sink configured.
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">SampleSourceStatus</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">MarkNoSink</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">reason</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">messageFormat</span> <span style="color:#204a87;font-weight:bold">string</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">messageA</span> <span style="color:#ce5c00;font-weight:bold">...</span><span style="color:#204a87;font-weight:bold">interface</span><span style="color:#000;font-weight:bold">{})</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">SampleCondSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Manage</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">s</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">MarkFalse</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">SampleConditionSinkProvided</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">reason</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">messageFormat</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">messageA</span><span style="color:#ce5c00;font-weight:bold">...</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div>
|
||
</description>
|
||
</item>
|
||
|
||
<item>
|
||
<title>V0.23-Docs: Controller Implementation and Design</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/03-controller/</link>
|
||
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
||
|
||
<guid>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/03-controller/</guid>
|
||
<description>
|
||
|
||
|
||
<h1 id="controller-implementation">Controller Implementation</h1>
|
||
<h2 id="cmdcontroller">cmd/controller</h2>
|
||
<p>Pass the new controller implementation to the shared main</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000;font-weight:bold">(</span>
|
||
<span style="color:#8f5902;font-style:italic">// The set of controllers this controller process runs.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#4e9a06">&#34;knative.dev/sample-source/pkg/reconciler/sample&#34;</span>
|
||
|
||
<span style="color:#8f5902;font-style:italic">// This defines the shared main for injected controllers.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#4e9a06">&#34;knative.dev/pkg/injection/sharedmain&#34;</span>
|
||
<span style="color:#000;font-weight:bold">)</span>
|
||
|
||
<span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">main</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">sharedmain</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Main</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;sample-source-controller&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">sample</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewController</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><p>Define the NewController implementation, it will be passed a <code>configmap.Watcher</code>, as well as a context which the injected listers will use for the reconciler struct arguments</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">NewController</span><span style="color:#000;font-weight:bold">(</span>
|
||
<span style="color:#000">ctx</span> <span style="color:#000">context</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Context</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000">cmw</span> <span style="color:#000">configmap</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Watcher</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">controller</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Impl</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">deploymentInformer</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">deploymentinformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#000">sinkBindingInformer</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">sinkbindinginformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#000">sampleSourceInformer</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">samplesourceinformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">)</span>
|
||
|
||
<span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">Reconciler</span><span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">dr</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">reconciler</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">DeploymentReconciler</span><span style="color:#000;font-weight:bold">{</span><span style="color:#000">KubeClientSet</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">kubeclient</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">)},</span>
|
||
<span style="color:#000">sbr</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">reconciler</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SinkBindingReconciler</span><span style="color:#000;font-weight:bold">{</span><span style="color:#000">EventingClientSet</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">eventingclient</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">)},</span>
|
||
<span style="color:#8f5902;font-style:italic">// Config accessor takes care of tracing/config/logging config propagation to the receive adapter
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">configAccessor</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">reconcilersource</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">WatchConfigurations</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ctx</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;sample-source&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">cmw</span><span style="color:#000;font-weight:bold">),</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><p>The base reconciler is imported from the knative.dev/pkg dependency:</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000;font-weight:bold">(</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">reconcilersource</span> <span style="color:#4e9a06">&#34;knative.dev/eventing/pkg/reconciler/source&#34;</span>
|
||
<span style="color:#8f5902;font-style:italic">// ...
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div><p>Ensure the correct informers have EventHandlers filtered to them</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"> <span style="color:#000">sampleSourceInformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Informer</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">AddEventHandler</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">controller</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">HandleAll</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">impl</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Enqueue</span><span style="color:#000;font-weight:bold">))</span>
|
||
</code></pre></div><p>Controller for the <code>SampleSource</code> uses <code>Deployment</code> and <code>SinkBinding</code> resources to deploy and also bind the event source and the receive adapter. Also ensure the informers are set up correctly for these secondary resources</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"> <span style="color:#000">deploymentInformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Informer</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">AddEventHandler</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">cache</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">FilteringResourceEventHandler</span><span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">FilterFunc</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">controller</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">FilterGroupKind</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">v1alpha1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Kind</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;SampleSource&#34;</span><span style="color:#000;font-weight:bold">)),</span>
|
||
<span style="color:#000">Handler</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">controller</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">HandleAll</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">impl</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EnqueueControllerOf</span><span style="color:#000;font-weight:bold">),</span>
|
||
<span style="color:#000;font-weight:bold">})</span>
|
||
|
||
<span style="color:#000">sinkBindingInformer</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Informer</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">AddEventHandler</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">cache</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">FilteringResourceEventHandler</span><span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">FilterFunc</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">controller</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">FilterGroupKind</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">v1alpha1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Kind</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;SampleSource&#34;</span><span style="color:#000;font-weight:bold">)),</span>
|
||
<span style="color:#000">Handler</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">controller</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">HandleAll</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">impl</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EnqueueControllerOf</span><span style="color:#000;font-weight:bold">),</span>
|
||
<span style="color:#000;font-weight:bold">})</span>
|
||
</code></pre></div>
|
||
</description>
|
||
</item>
|
||
|
||
<item>
|
||
<title>V0.23-Docs: Reconciler Implementation and Design</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/04-reconciler/</link>
|
||
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
||
|
||
<guid>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/04-reconciler/</guid>
|
||
<description>
|
||
|
||
|
||
<h2 id="reconciler-functionality">Reconciler Functionality</h2>
|
||
<p>General steps the reconciliation process needs to cover:</p>
|
||
<ol>
|
||
<li>Update the <code>ObservedGeneration</code> and initialize the <code>Status</code> conditions (as defined in <code>samplesource_lifecycle.go</code> and <code>samplesource_types.go</code>)</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Status</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">InitializeConditions</span><span style="color:#000;font-weight:bold">()</span>
|
||
<span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Status</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ObservedGeneration</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Generation</span>
|
||
</code></pre></div><ol start="2">
|
||
<li>Create/reconcile the Receive Adapter (detailed below)</li>
|
||
<li>If successful, update the <code>Status</code> and <code>MarkDeployed</code></li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Status</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">PropagateDeploymentAvailability</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ra</span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div><ol start="4">
|
||
<li>Create/reconcile the <code>SinkBinding</code> for the Receive Adapter targeting the <code>Sink</code> (detailed below)</li>
|
||
<li>MarkSink with the result</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Status</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">MarkSink</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sb</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Status</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SinkURI</span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div><ol start="6">
|
||
<li>Return a new reconciler event stating that the process is done</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">pkgreconciler</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewEvent</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">corev1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EventTypeNormal</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;SampleSourceReconciled&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;SampleSource reconciled: \&#34;%s/%s\&#34;&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">name</span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div><h2 id="reconcilecreate-the-receive-adapter">Reconcile/Create The Receive Adapter</h2>
|
||
<p>As part of the source reconciliation, we have to create and deploy
|
||
(and update if necessary) the underlying receive adapter.</p>
|
||
<p>Verify the specified kubernetes resources are valid, and update the <code>Status</code> accordingly</p>
|
||
<p>Assemble the ReceiveAdapterArgs</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">raArgs</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">resources</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ReceiveAdapterArgs</span><span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">EventSource</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Namespace</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34;/&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Name</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000">Image</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ReceiveAdapterImage</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000">Source</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000">Labels</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">resources</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Labels</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Name</span><span style="color:#000;font-weight:bold">),</span>
|
||
<span style="color:#000">AdditionalEnvs</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">configAccessor</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ToEnvVars</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#8f5902;font-style:italic">// Grab config envs for tracing/logging/metrics
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><p>NB The exact arguments may change based on functional requirements
|
||
Create the underlying deployment from the arguments provided, matching pod templates, labels, owner references, etc as needed to fill out the deployment
|
||
Example: <a href="https://github.com/knative-sandbox/sample-source/blob/main/pkg/reconciler/sample/resources/receive_adapter.go">pkg/reconciler/sample/resources/receive_adapter.go</a></p>
|
||
<ol>
|
||
<li>Fetch the existing receive adapter deployment</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">namespace</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">owner</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">GetObjectMeta</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">GetNamespace</span><span style="color:#000;font-weight:bold">()</span>
|
||
<span style="color:#000">ra</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">KubeClientSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">AppsV1</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">Deployments</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">expected</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Name</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">metav1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">GetOptions</span><span style="color:#000;font-weight:bold">{})</span>
|
||
</code></pre></div><ol start="2">
|
||
<li>Otherwise, create the deployment</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">ra</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">KubeClientSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">AppsV1</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">Deployments</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">expected</span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div><ol start="3">
|
||
<li>Check if the expected vs existing spec is different, and update the deployment if required</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">podSpecImageSync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">expected</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Template</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">ra</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Template</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">ra</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Template</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">expected</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Template</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span>
|
||
<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">ra</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">KubeClientSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">AppsV1</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">Deployments</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Update</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">ra</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ra</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><ol start="4">
|
||
<li>If updated, record the event</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">pkgreconciler</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewEvent</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">corev1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EventTypeNormal</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;DeploymentUpdated&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;updated deployment: \&#34;%s/%s\&#34;&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">name</span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div><h2 id="reconcilecreate-the-sinkbinding">Reconcile/Create The SinkBinding</h2>
|
||
<p>Instead of directly giving the details of the sink to the receive adapter, use a <code>SinkBinding</code> to bind the receive adapter with the sink.</p>
|
||
<p>Steps here are almost the same with the <code>Deployment</code> reconciliation above, but it is for another resource, <code>SinkBinding</code>.</p>
|
||
<ol>
|
||
<li>Create a <code>Reference</code> for the receive adapter deployment. This deployment will be <code>SinkBinding</code>&rsquo;s source:</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">tracker</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Reference</span><span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">APIVersion</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">appsv1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SchemeGroupVersion</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">(),</span>
|
||
<span style="color:#000">Kind</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Deployment&#34;</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000">Namespace</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">ra</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Namespace</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000">Name</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000">ra</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Name</span><span style="color:#000;font-weight:bold">,</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><ol start="2">
|
||
<li>Fetch the existing <code>SinkBinding</code></li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">namespace</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">owner</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">GetObjectMeta</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">GetNamespace</span><span style="color:#000;font-weight:bold">()</span>
|
||
<span style="color:#000">sb</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EventingClientSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SourcesV1alpha2</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">SinkBindings</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">expected</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Name</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">metav1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">GetOptions</span><span style="color:#000;font-weight:bold">{})</span>
|
||
</code></pre></div><ol start="2">
|
||
<li>If it doesn&rsquo;t exist, create it</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#000">sb</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EventingClientSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SourcesV1alpha2</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">SinkBindings</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">expected</span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div><ol start="3">
|
||
<li>Check if the expected vs existing spec is different, and update the <code>SinkBinding</code> if required</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">else</span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">specChanged</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sb</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">expected</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">sb</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">expected</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Spec</span>
|
||
<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">sb</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EventingClientSet</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SourcesV1alpha2</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">SinkBindings</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Update</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">sb</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">sb</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><ol start="4">
|
||
<li>If updated, record the event</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">pkgreconciler</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewEvent</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">corev1</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">EventTypeNormal</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;SinkBindingUpdated&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;updated SinkBinding: \&#34;%s/%s\&#34;&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">namespace</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">name</span><span style="color:#000;font-weight:bold">)</span>
|
||
</code></pre></div>
|
||
</description>
|
||
</item>
|
||
|
||
<item>
|
||
<title>V0.23-Docs: Receive Adapter Implementation and Design</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/05-receive-adapter/</link>
|
||
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
||
|
||
<guid>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/05-receive-adapter/</guid>
|
||
<description>
|
||
|
||
|
||
<h2 id="receive-adapter-cmd">Receive Adapter cmd</h2>
|
||
<p>Similar to the controller, we&rsquo;ll need an injection based <code>main.go</code> similar to the controller under <code>cmd/receiver_adapter/main.go</code></p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#8f5902;font-style:italic">// This Adapter generates events at a regular interval.
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">main</span>
|
||
|
||
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000;font-weight:bold">(</span>
|
||
<span style="color:#4e9a06">&#34;knative.dev/eventing/pkg/adapter&#34;</span>
|
||
<span style="color:#000">myadapter</span> <span style="color:#4e9a06">&#34;knative.dev/sample-source/pkg/adapter&#34;</span>
|
||
<span style="color:#000;font-weight:bold">)</span>
|
||
|
||
<span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">main</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">adapter</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Main</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;sample-source&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">myadapter</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewEnv</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">myadapter</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewAdapter</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
|
||
</code></pre></div><h2 id="defining-newadapter-implementation-and-start-function">Defining NewAdapter implementation and Start function</h2>
|
||
<p>The adapter&rsquo;s <code>pkg</code> implementation consists of two main functions;</p>
|
||
<ol>
|
||
<li>A <code>NewAdapter(ctx context.Context, aEnv adapter.EnvConfigAccessor, ceClient cloudevents.Client) adapter.Adapter {}</code> call, which creates the
|
||
new adapter with passed variables via the <code>EnvConfigAccessor</code>. The created adapter will be passed the cloudevents client (which is where the events are forwarded to). This is sometimes referred
|
||
to as a sink, or <code>ceClient</code> in the Knative ecosystem. The return value is a reference to the adapter as defined by the adapter&rsquo;s local struct.</li>
|
||
</ol>
|
||
<p>In our <code>sample-source</code>&rsquo;s case;</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#8f5902;font-style:italic">// Adapter generates events at a regular interval.
|
||
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">type</span> <span style="color:#000">Adapter</span> <span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">logger</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">zap</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Logger</span>
|
||
<span style="color:#000">interval</span> <span style="color:#000">time</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Duration</span>
|
||
<span style="color:#000">nextID</span> <span style="color:#204a87;font-weight:bold">int</span>
|
||
<span style="color:#000">client</span> <span style="color:#000">cloudevents</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Client</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div><ol start="2">
|
||
<li><code>Start</code> function implemented as an interface to the adapter struct.</li>
|
||
</ol>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">Adapter</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">Start</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">stopCh</span> <span style="color:#ce5c00;font-weight:bold">&lt;-</span><span style="color:#204a87;font-weight:bold">chan</span> <span style="color:#204a87;font-weight:bold">struct</span><span style="color:#000;font-weight:bold">{})</span> <span style="color:#204a87;font-weight:bold">error</span> <span style="color:#000;font-weight:bold">{</span>
|
||
</code></pre></div><p><code>stopCh</code> is the signal to stop the Adapter. Otherwise the role of the function is to process the next
|
||
event. In the case of the <code>sample-source</code>, it creates an event to forward to the specified cloudevent sink/client
|
||
every X interval, as specified by the loaded <code>EnvConfigAccessor</code> (loaded via the resource yaml).</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">Adapter</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">Start</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">stopCh</span> <span style="color:#ce5c00;font-weight:bold">&lt;-</span><span style="color:#204a87;font-weight:bold">chan</span> <span style="color:#204a87;font-weight:bold">struct</span><span style="color:#000;font-weight:bold">{})</span> <span style="color:#204a87;font-weight:bold">error</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">logger</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Infow</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Starting heartbeat&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">zap</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;interval&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">interval</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">()))</span>
|
||
<span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#204a87;font-weight:bold">select</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#204a87;font-weight:bold">case</span> <span style="color:#ce5c00;font-weight:bold">&lt;-</span><span style="color:#000">time</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">After</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">interval</span><span style="color:#000;font-weight:bold">):</span>
|
||
<span style="color:#000">event</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">newEvent</span><span style="color:#000;font-weight:bold">()</span>
|
||
<span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">logger</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Infow</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Sending new event&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">zap</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;event&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">event</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">()))</span>
|
||
<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">result</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">client</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Send</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">context</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Background</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">event</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#000;font-weight:bold">!</span><span style="color:#000">cloudevents</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">IsACK</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">result</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
|
||
<span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">logger</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Infow</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;failed to send event&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">zap</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;event&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">event</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">String</span><span style="color:#000;font-weight:bold">()),</span> <span style="color:#000">zap</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Error</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">result</span><span style="color:#000;font-weight:bold">))</span>
|
||
<span style="color:#8f5902;font-style:italic">// We got an error but it could be transient, try again next interval.
|
||
</span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#204a87;font-weight:bold">continue</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
<span style="color:#204a87;font-weight:bold">case</span> <span style="color:#ce5c00;font-weight:bold">&lt;-</span><span style="color:#000">stopCh</span><span style="color:#000;font-weight:bold">:</span>
|
||
<span style="color:#000">a</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">logger</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Shutting down...&#34;</span><span style="color:#000;font-weight:bold">)</span>
|
||
<span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">nil</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
<span style="color:#000;font-weight:bold">}</span>
|
||
</code></pre></div>
|
||
</description>
|
||
</item>
|
||
|
||
<item>
|
||
<title>V0.23-Docs: Publishing to your Kubernetes cluster</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/06-yaml/</link>
|
||
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
||
|
||
<guid>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/06-yaml/</guid>
|
||
<description>
|
||
|
||
|
||
<h2 id="run-the-sample-source-locally">Run the sample source locally</h2>
|
||
<p>Start a minikube cluster.</p>
|
||
<p><em>If you already have a Kubernetes cluster running, you can skip this step. The
|
||
cluster must be 1.15+</em></p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">minikube start
|
||
</code></pre></div><p>Setup <code>ko</code> to use the minikube docker instance and local registry</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh"><span style="color:#204a87">eval</span> <span style="color:#204a87;font-weight:bold">$(</span>minikube docker-env<span style="color:#204a87;font-weight:bold">)</span>
|
||
<span style="color:#204a87">export</span> <span style="color:#000">KO_DOCKER_REPO</span><span style="color:#ce5c00;font-weight:bold">=</span>ko.local
|
||
</code></pre></div><p>Apply the CRD and configuration yaml</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">ko apply -f config
|
||
</code></pre></div><p>Once the <code>sample-source-controller-manager</code> is running in the <code>knative-samples</code> namespace, you can
|
||
apply the <code>example.yaml</code> to connect our <code>sample-source</code> every <code>10s</code> directly to a <code>ksvc</code>.</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-yaml" data-lang="yaml"><span style="color:#204a87;font-weight:bold">apiVersion</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">serving.knative.dev/v1</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">kind</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">Service</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">metadata</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">name</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">event-display</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">namespace</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">knative-samples</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">spec</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">template</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">spec</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">containers</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span>- <span style="color:#204a87;font-weight:bold">image</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">gcr.io/knative-releases/knative.dev/eventing/cmd/event_display</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#000">---</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">apiVersion</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">samples.knative.dev/v1alpha1</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">kind</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">SampleSource</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">metadata</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">name</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">sample-source</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">namespace</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">knative-samples</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">spec</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">interval</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#4e9a06">&#34;10s&#34;</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">sink</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">ref</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">apiVersion</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">serving.knative.dev/v1</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">kind</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">Service</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">name</span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000">event-display</span><span style="color:#f8f8f8;text-decoration:underline">
|
||
</span></code></pre></div><div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">ko apply -f example.yaml
|
||
</code></pre></div><p>Once reconciled, you can confirm the <code>ksvc</code> is outputting valid cloudevents every <code>10s</code> to align with our specified interval.</p>
|
||
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-sh" data-lang="sh">% kubectl -n knative-samples logs -l serving.knative.dev/service<span style="color:#ce5c00;font-weight:bold">=</span>event-display -c user-container -f
|
||
</code></pre></div><pre><code>☁️ cloudevents.Event
|
||
Validation: valid
|
||
Context Attributes,
|
||
specversion: 1.0
|
||
type: dev.knative.sample
|
||
source: http://sample.knative.dev/heartbeat-source
|
||
id: d4619592-363e-4a41-82d1-b1586c390e24
|
||
time: 2019-12-17T01:31:10.795588888Z
|
||
datacontenttype: application/json
|
||
Data,
|
||
{
|
||
&quot;Sequence&quot;: 0,
|
||
&quot;Heartbeat&quot;: &quot;10s&quot;
|
||
}
|
||
☁️ cloudevents.Event
|
||
Validation: valid
|
||
Context Attributes,
|
||
specversion: 1.0
|
||
type: dev.knative.sample
|
||
source: http://sample.knative.dev/heartbeat-source
|
||
id: db2edad0-06bc-4234-b9e1-7ea3955841d6
|
||
time: 2019-12-17T01:31:20.825969504Z
|
||
datacontenttype: application/json
|
||
Data,
|
||
{
|
||
&quot;Sequence&quot;: 1,
|
||
&quot;Heartbeat&quot;: &quot;10s&quot;
|
||
}
|
||
</code></pre>
|
||
</description>
|
||
</item>
|
||
|
||
<item>
|
||
<title>V0.23-Docs: Moving the event source to knative-sandbox</title>
|
||
<link>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/07-knative-sandbox/</link>
|
||
<pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
|
||
|
||
<guid>https://knative.dev/v0.23-docs/eventing/sources/creating-event-sources/writing-event-source/07-knative-sandbox/</guid>
|
||
<description>
|
||
|
||
|
||
<p>If you would like to move your source over to the <a href="https://github.com/knative-sandbox"><code>knative-sandbox</code></a> organization follow the instructions to <a href="https://knative.dev/community/contributing/mechanics/creating-a-sandbox-repo/">create a sandbox repository</a>.</p>
|
||
|
||
</description>
|
||
</item>
|
||
|
||
</channel>
|
||
</rss>
|