Skip to content

Commit

Permalink
Adding Source ducktype. (knative#566)
Browse files Browse the repository at this point in the history
* Adding Source ducktype.

* removing output format for now, revisit if we find more sources and consumers asking for it.
  • Loading branch information
n3wscott authored and knative-prow-robot committed Aug 13, 2019
1 parent 1b8b127 commit 11fec63
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 0 deletions.
157 changes: 157 additions & 0 deletions apis/duck/v1beta1/source_types.go
@@ -0,0 +1,157 @@
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
apisv1alpha1 "knative.dev/pkg/apis/v1alpha1"
)

// Source is an Implementable "duck type".
var _ duck.Implementable = (*Source)(nil)

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// Source is the minimum resource shape to adhere to the Source Specification.
// This duck type is intended to allow implementors of Sources and
// Importers to verify their own resources meet the expectations.
// This is not a real resource.
// NOTE: The Source Specification is in progress and the shape and names could
// be modified until it has been accepted.
type Source struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec SourceSpec `json:"spec"`
Status SourceStatus `json:"status"`
}

type SourceSpec struct {
// Sink is a reference to an object that will resolve to a domain name or a
// URI directly to use as the sink.
Sink apisv1alpha1.Destination `json:"sink,omitempty"`

// CloudEventOverrides defines overrides to control the output format and
// modifications of the event sent to the sink.
// +optional
CloudEventOverrides *CloudEventOverrides `json:"ceOverrides,omitempty"`
}

// CloudEventOverrides defines arguments for a Source that control the output
// format of the CloudEvents produced by the Source.
type CloudEventOverrides struct {
// Extensions specify what attribute are added or overridden on the
// outbound event. Each `Extensions` key-value pair are set on the event as
// an attribute extension independently.
// +optional
Extensions map[string]string `json:"extensions,omitempty"`
}

// SourceStatus shows how we expect folks to embed Addressable in
// their Status field.
type SourceStatus struct {
// inherits duck/v1beta1 Status, which currently provides:
// * ObservedGeneration - the 'Generation' of the Service that was last
// processed by the controller.
// * Conditions - the latest available observations of a resource's current
// state.
Status `json:",inline"`

// SinkURI is the current active sink URI that has been configured for the
// Source.
// +optional
SinkURI *apis.URL `json:"sinkUri,omitempty"`
}

// IsReady returns true if the resource is ready overall.
func (ss *SourceStatus) IsReady() bool {
for _, c := range ss.Conditions {
switch c.Type {
// Look for the "happy" condition, which is the only condition that
// we can reliably understand to be the overall state of the resource.
case apis.ConditionReady, apis.ConditionSucceeded:
return c.IsTrue()
}
}
return false
}

var (
// Verify Source resources meet duck contracts.
_ duck.Populatable = (*Source)(nil)
_ apis.Listable = (*Source)(nil)
)

const (
// SourceConditionSinkProvided has status True when the Source
// has been configured with a sink target that is resolvable.
SourceConditionSinkProvided apis.ConditionType = "SinkProvided"
)

// GetFullType implements duck.Implementable
func (*Source) GetFullType() duck.Populatable {
return &Source{}
}

// Populate implements duck.Populatable
func (s *Source) Populate() {
s.Spec.Sink = apisv1alpha1.Destination{
URI: &apis.URL{
Scheme: "https",
Host: "tableflip.dev",
RawQuery: "flip=mattmoor",
},
}
s.Spec.CloudEventOverrides = &CloudEventOverrides{
Extensions: map[string]string{"boosh": "kakow"},
}
s.Status.ObservedGeneration = 42
s.Status.Conditions = Conditions{{
// Populate ALL fields
Type: SourceConditionSinkProvided,
Status: corev1.ConditionTrue,
LastTransitionTime: apis.VolatileTime{Inner: metav1.NewTime(time.Date(1984, 02, 28, 18, 52, 00, 00, time.UTC))},
}}
s.Status.SinkURI = &apis.URL{
Scheme: "https",
Host: "tableflip.dev",
RawQuery: "flip=mattmoor",
}
}

// GetListType implements apis.Listable
func (*Source) GetListType() runtime.Object {
return &SourceList{}
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// SourceList is a list of Source resources
type SourceList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []Source `json:"items"`
}
128 changes: 128 additions & 0 deletions apis/duck/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 11fec63

Please sign in to comment.