knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1beta1/addressable_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  	"context"
    21  	"fmt"
    22  
    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  	v1 "knative.dev/pkg/apis/duck/v1"
    29  )
    30  
    31  // +genduck
    32  
    33  // Addressable provides a generic mechanism for a custom resource
    34  // definition to indicate a destination for message delivery.
    35  //
    36  // Addressable is the schema for the destination information. This is
    37  // typically stored in the object's `status`, as this information may
    38  // be generated by the controller.
    39  type Addressable struct {
    40  	// Name is the name of the address.
    41  	// +optional
    42  	Name *string `json:"name,omitempty"`
    43  
    44  	URL *apis.URL `json:"url,omitempty"`
    45  
    46  	// CACerts is the Certification Authority (CA) certificates in PEM format
    47  	// according to https://www.rfc-editor.org/rfc/rfc7468.
    48  	// +optional
    49  	CACerts *string `json:"CACerts,omitempty"`
    50  }
    51  
    52  var (
    53  	// Addressable is a Convertible type.
    54  	_ apis.Convertible = (*Addressable)(nil)
    55  
    56  	// Addressable is an Implementable "duck type".
    57  	_ ducktypes.Implementable = (*Addressable)(nil)
    58  )
    59  
    60  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    61  
    62  // AddressableType is a skeleton type wrapping Addressable in the manner we expect
    63  // resource writers defining compatible resources to embed it.  We will
    64  // typically use this type to deserialize Addressable ObjectReferences and
    65  // access the Addressable data.  This is not a real resource.
    66  type AddressableType struct {
    67  	metav1.TypeMeta   `json:",inline"`
    68  	metav1.ObjectMeta `json:"metadata,omitempty"`
    69  
    70  	Status AddressStatus `json:"status"`
    71  }
    72  
    73  // AddressStatus shows how we expect folks to embed Addressable in
    74  // their Status field.
    75  type AddressStatus struct {
    76  	// Address is a single Addressable address.
    77  	// If Addresses is present, Address will be ignored by clients.
    78  	// +optional
    79  	Address *Addressable `json:"address,omitempty"`
    80  
    81  	// Addresses is a list of addresses for different protocols (HTTP and HTTPS)
    82  	// If Addresses is present, Address must be ignored by clients.
    83  	// +optional
    84  	Addresses []Addressable `json:"addresses,omitempty"`
    85  }
    86  
    87  // Verify AddressableType resources meet duck contracts.
    88  var (
    89  	_ apis.Listable         = (*AddressableType)(nil)
    90  	_ ducktypes.Populatable = (*AddressableType)(nil)
    91  )
    92  
    93  // GetFullType implements duck.Implementable
    94  func (*Addressable) GetFullType() ducktypes.Populatable {
    95  	return &AddressableType{}
    96  }
    97  
    98  // ConvertTo implements apis.Convertible
    99  func (a *Addressable) ConvertTo(ctx context.Context, to apis.Convertible) error {
   100  	switch sink := to.(type) {
   101  	case *v1.Addressable:
   102  		sink.URL = a.URL.DeepCopy()
   103  		return nil
   104  	default:
   105  		return fmt.Errorf("unknown version, got: %T", to)
   106  	}
   107  }
   108  
   109  // ConvertFrom implements apis.Convertible
   110  func (a *Addressable) ConvertFrom(ctx context.Context, from apis.Convertible) error {
   111  	switch source := from.(type) {
   112  	case *v1.Addressable:
   113  		a.URL = source.URL.DeepCopy()
   114  		return nil
   115  	default:
   116  		return fmt.Errorf("unknown version, got: %T", from)
   117  	}
   118  }
   119  
   120  // Populate implements duck.Populatable
   121  func (t *AddressableType) Populate() {
   122  	name := "http"
   123  	t.Status = AddressStatus{
   124  		Address: &Addressable{
   125  			// Populate ALL fields
   126  			Name: &name,
   127  			URL: &apis.URL{
   128  				Scheme: "http",
   129  				Host:   "foo.com",
   130  			},
   131  		},
   132  	}
   133  }
   134  
   135  // GetListType implements apis.Listable
   136  func (*AddressableType) GetListType() runtime.Object {
   137  	return &AddressableTypeList{}
   138  }
   139  
   140  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   141  
   142  // AddressableTypeList is a list of AddressableType resources
   143  type AddressableTypeList struct {
   144  	metav1.TypeMeta `json:",inline"`
   145  	metav1.ListMeta `json:"metadata"`
   146  
   147  	Items []AddressableType `json:"items"`
   148  }