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

     1  /*
     2  Copyright 2020 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 v1
    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  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  
    27  	"knative.dev/pkg/apis/duck/ducktypes"
    28  
    29  	"knative.dev/pkg/apis"
    30  )
    31  
    32  // KRShaped is an interface for retrieving the duck elements of an arbitrary resource.
    33  type KRShaped interface {
    34  	metav1.Object
    35  	schema.ObjectKind
    36  
    37  	GetStatus() *Status
    38  
    39  	GetConditionSet() apis.ConditionSet
    40  }
    41  
    42  // Asserts KResource conformance with KRShaped
    43  var _ KRShaped = (*KResource)(nil)
    44  
    45  // +genduck
    46  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    47  
    48  // KResource is a skeleton type wrapping Conditions in the manner we expect
    49  // resource writers defining compatible resources to embed it.  We will
    50  // typically use this type to deserialize Conditions ObjectReferences and
    51  // access the Conditions data.  This is not a real resource.
    52  type KResource struct {
    53  	metav1.TypeMeta   `json:",inline"`
    54  	metav1.ObjectMeta `json:"metadata,omitempty"`
    55  
    56  	Status Status `json:"status"`
    57  }
    58  
    59  // GetFullType implements duck.Implementable
    60  func (*KResource) GetFullType() ducktypes.Populatable {
    61  	return &KResource{}
    62  }
    63  
    64  // Populate implements duck.Populatable
    65  func (t *KResource) Populate() {
    66  	t.Status.ObservedGeneration = 42
    67  	t.Status.Conditions = Conditions{{
    68  		// Populate ALL fields
    69  		Type:               "Birthday",
    70  		Status:             corev1.ConditionTrue,
    71  		LastTransitionTime: apis.VolatileTime{Inner: metav1.NewTime(time.Date(1984, 2, 28, 18, 52, 0, 0, time.UTC))},
    72  		Reason:             "Celebrate",
    73  		Message:            "n3wScott, find your party hat :tada:",
    74  	}}
    75  }
    76  
    77  // Verify KResource resources meet duck contracts.
    78  var (
    79  	_ apis.Listable         = (*KResource)(nil)
    80  	_ ducktypes.Populatable = (*KResource)(nil)
    81  )
    82  
    83  // GetListType implements apis.Listable
    84  func (*KResource) GetListType() runtime.Object {
    85  	return &KResourceList{}
    86  }
    87  
    88  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    89  
    90  // KResourceList is a list of KResource resources
    91  type KResourceList struct {
    92  	metav1.TypeMeta `json:",inline"`
    93  	metav1.ListMeta `json:"metadata"`
    94  
    95  	Items []KResource `json:"items"`
    96  }
    97  
    98  // GetStatus retrieves the status of the KResource. Implements the KRShaped interface.
    99  func (t *KResource) GetStatus() *Status {
   100  	return &t.Status
   101  }
   102  
   103  // GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface.
   104  func (t *KResource) GetConditionSet() apis.ConditionSet {
   105  	// Note: KResources are unmarshalled from existing resources. This will only work properly for resources that
   106  	// have already been initialized to their type.
   107  	if cond := t.Status.GetCondition(apis.ConditionSucceeded); cond != nil {
   108  		return apis.NewBatchConditionSet()
   109  	}
   110  	return apis.NewLivingConditionSet()
   111  }