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

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