knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1beta1/source_types.go (about)

     1  /*
     2  Copyright 2019 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1beta1
    18  
    19  import (
    20  	"time"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/apimachinery/pkg/runtime"
    25  
    26  	"knative.dev/pkg/apis"
    27  	"knative.dev/pkg/apis/duck/ducktypes"
    28  )
    29  
    30  // +genduck
    31  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    32  
    33  // Source is the minimum resource shape to adhere to the Source Specification.
    34  // This duck type is intended to allow implementors of Sources and
    35  // Importers to verify their own resources meet the expectations.
    36  // This is not a real resource.
    37  // NOTE: The Source Specification is in progress and the shape and names could
    38  // be modified until it has been accepted.
    39  type Source struct {
    40  	metav1.TypeMeta   `json:",inline"`
    41  	metav1.ObjectMeta `json:"metadata,omitempty"`
    42  
    43  	Spec   SourceSpec   `json:"spec"`
    44  	Status SourceStatus `json:"status"`
    45  }
    46  
    47  type SourceSpec struct {
    48  	// Sink is a reference to an object that will resolve to a domain name or a
    49  	// URI directly to use as the sink.
    50  	Sink Destination `json:"sink,omitempty"`
    51  
    52  	// CloudEventOverrides defines overrides to control the output format and
    53  	// modifications of the event sent to the sink.
    54  	// +optional
    55  	CloudEventOverrides *CloudEventOverrides `json:"ceOverrides,omitempty"`
    56  }
    57  
    58  // CloudEventOverrides defines arguments for a Source that control the output
    59  // format of the CloudEvents produced by the Source.
    60  type CloudEventOverrides struct {
    61  	// Extensions specify what attribute are added or overridden on the
    62  	// outbound event. Each `Extensions` key-value pair are set on the event as
    63  	// an attribute extension independently.
    64  	// +optional
    65  	Extensions map[string]string `json:"extensions,omitempty"`
    66  }
    67  
    68  // SourceStatus shows how we expect folks to embed Addressable in
    69  // their Status field.
    70  type SourceStatus struct {
    71  	// inherits duck/v1beta1 Status, which currently provides:
    72  	// * ObservedGeneration - the 'Generation' of the Service that was last
    73  	//   processed by the controller.
    74  	// * Conditions - the latest available observations of a resource's current
    75  	//   state.
    76  	Status `json:",inline"`
    77  
    78  	// SinkURI is the current active sink URI that has been configured for the
    79  	// Source.
    80  	// +optional
    81  	SinkURI *apis.URL `json:"sinkUri,omitempty"`
    82  }
    83  
    84  // IsReady returns true if the resource is ready overall.
    85  func (ss *SourceStatus) IsReady() bool {
    86  	for _, c := range ss.Conditions {
    87  		switch c.Type {
    88  		// Look for the "happy" condition, which is the only condition that
    89  		// we can reliably understand to be the overall state of the resource.
    90  		case apis.ConditionReady, apis.ConditionSucceeded:
    91  			return c.IsTrue()
    92  		}
    93  	}
    94  	return false
    95  }
    96  
    97  // Verify Source resources meet duck contracts.
    98  var (
    99  	_ apis.Listable           = (*Source)(nil)
   100  	_ ducktypes.Implementable = (*Source)(nil)
   101  	_ ducktypes.Populatable   = (*Source)(nil)
   102  )
   103  
   104  const (
   105  	// SourceConditionSinkProvided has status True when the Source
   106  	// has been configured with a sink target that is resolvable.
   107  	SourceConditionSinkProvided apis.ConditionType = "SinkProvided"
   108  )
   109  
   110  // GetFullType implements duck.Implementable
   111  func (*Source) GetFullType() ducktypes.Populatable {
   112  	return &Source{}
   113  }
   114  
   115  // Populate implements duck.Populatable
   116  func (s *Source) Populate() {
   117  	s.Spec.Sink = Destination{
   118  		URI: &apis.URL{
   119  			Scheme:   "https",
   120  			Host:     "tableflip.dev",
   121  			RawQuery: "flip=mattmoor",
   122  		},
   123  	}
   124  	s.Spec.CloudEventOverrides = &CloudEventOverrides{
   125  		Extensions: map[string]string{"boosh": "kakow"},
   126  	}
   127  	s.Status.ObservedGeneration = 42
   128  	s.Status.Conditions = Conditions{{
   129  		// Populate ALL fields
   130  		Type:               SourceConditionSinkProvided,
   131  		Status:             corev1.ConditionTrue,
   132  		LastTransitionTime: apis.VolatileTime{Inner: metav1.NewTime(time.Date(1984, 2, 28, 18, 52, 0, 0, time.UTC))},
   133  	}}
   134  	s.Status.SinkURI = &apis.URL{
   135  		Scheme:   "https",
   136  		Host:     "tableflip.dev",
   137  		RawQuery: "flip=mattmoor",
   138  	}
   139  }
   140  
   141  // GetListType implements apis.Listable
   142  func (*Source) GetListType() runtime.Object {
   143  	return &SourceList{}
   144  }
   145  
   146  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   147  
   148  // SourceList is a list of Source resources
   149  type SourceList struct {
   150  	metav1.TypeMeta `json:",inline"`
   151  	metav1.ListMeta `json:"metadata"`
   152  
   153  	Items []Source `json:"items"`
   154  }