Duck typing
Duck typing means that the compatibility of a resource for use in a Knative system is determined by certain properties that are used to identify the resource control plane shape and behaviors. These properties are based on a set of common definitions for different types of resources, called duck types.
Knative can use a resource as if it is the generic duck type, without specific knowledge about the resource type, if:
- The resource has the same fields in the same schema locations as the common definition specifies
- The same control or data plane behaviors as the common definition specifies
Some resources can opt in to multiple duck types.
A fundamental use of duck typing in Knative is using object references in resource specs to point to other resources. The definition of the object containing the reference prescribes the expected duck type of the resource being referenced.
If the expected shape of a Sizable duck type is that, in the status
, the schema shape is the following:
status:
height: <in centimetres>
weight: <in kilograms>
Now the instance of pointee
could look like this:
apiVersion: extension.example.com/v1
kind: Dog
metadata:
name: pointee
spec:
owner: Smith Family
etc: more here
status:
lastFeeding: 2 hours ago
hungry: true
age: 2
height: 60
weight: 20
When the Example
resource functions, it only acts on the information in the Sizable duck type shape, and the Dog
implementation is free to have the information that makes the most sense for that resource. The power of duck typing is apparent when we extend the system with a new type, for example, Human
, if the new resource adheres to the contract set by Sizable.
The Example
resource is able to apply the logic configured for it, without explicit knowledge of or Dog
.
Knative Duck Types
- Binding
- Channelable
- Podspecable
Addressable is expected to be the following shape:
apiVersion: group/version
kind: Kind
status:
address:
url: http://host/path?query
With a direct subject
, Binding is expected to be in the following shape:
apiVersion: group/version
kind: Kind
spec:
subject:
apiVersion: group/version
kind: SomeKind
namespace: the-namespace
name: a-name
With an indirect subject
, Binding is expected to be in the following shape:
With a ref
Sink, Source is expected to be in the following shape:
apiVersion: group/version
kind: Kind
spec:
sink:
ref:
apiVersion: group/version
name: a-name
extensions:
key: value
status:
observedGeneration: 1
conditions:
- type: Ready
status: "True"
sinkUri: http://host
apiVersion: group/version
kind: Kind
spec:
sink:
uri: http://host/path?query
ceOverrides:
extensions:
key: value
status:
observedGeneration: 1
conditions:
- type: Ready
status: "True"
With ref
and uri
Sinks, Source is expected to be in the following shape: