knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1beta1/status_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 "time" 22 23 corev1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 27 "knative.dev/pkg/apis" 28 "knative.dev/pkg/apis/duck/ducktypes" 29 "knative.dev/pkg/kmap" 30 ) 31 32 // +genduck 33 34 // Conditions is a simple wrapper around apis.Conditions to implement duck.Implementable. 35 type Conditions apis.Conditions 36 37 // Conditions is an Implementable duck type. 38 var _ ducktypes.Implementable = (*Conditions)(nil) 39 40 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 41 42 // KResource is a skeleton type wrapping Conditions in the manner we expect 43 // resource writers defining compatible resources to embed it. We will 44 // typically use this type to deserialize Conditions ObjectReferences and 45 // access the Conditions data. This is not a real resource. 46 type KResource struct { 47 metav1.TypeMeta `json:",inline"` 48 metav1.ObjectMeta `json:"metadata,omitempty"` 49 50 Status Status `json:"status"` 51 } 52 53 // Status shows how we expect folks to embed Conditions in 54 // their Status field. 55 // WARNING: Adding fields to this struct will add them to all Knative resources. 56 type Status struct { 57 // ObservedGeneration is the 'Generation' of the Service that 58 // was last processed by the controller. 59 // +optional 60 ObservedGeneration int64 `json:"observedGeneration,omitempty"` 61 62 // Conditions the latest available observations of a resource's current state. 63 // +optional 64 // +patchMergeKey=type 65 // +patchStrategy=merge 66 Conditions Conditions `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"` 67 68 // Annotations is additional Status fields for the Resource to save some 69 // additional State as well as convey more information to the user. This is 70 // roughly akin to Annotations on any k8s resource, just the reconciler conveying 71 // richer information outwards. 72 Annotations map[string]string `json:"annotations,omitempty"` 73 } 74 75 var _ apis.ConditionsAccessor = (*Status)(nil) 76 77 // GetConditions implements apis.ConditionsAccessor 78 func (s *Status) GetConditions() apis.Conditions { 79 return apis.Conditions(s.Conditions) 80 } 81 82 // SetConditions implements apis.ConditionsAccessor 83 func (s *Status) SetConditions(c apis.Conditions) { 84 s.Conditions = Conditions(c) 85 } 86 87 // Verify KResource resources meet duck contracts. 88 var ( 89 _ apis.Listable = (*KResource)(nil) 90 _ ducktypes.Populatable = (*KResource)(nil) 91 ) 92 93 // GetFullType implements duck.Implementable 94 func (*Conditions) GetFullType() ducktypes.Populatable { 95 return &KResource{} 96 } 97 98 // GetCondition fetches the condition of the specified type. 99 func (s *Status) GetCondition(t apis.ConditionType) *apis.Condition { 100 for _, cond := range s.Conditions { 101 if cond.Type == t { 102 return &cond 103 } 104 } 105 return nil 106 } 107 108 // ConvertTo helps implement apis.Convertible for types embedding this Status. 109 func (s *Status) ConvertTo(ctx context.Context, sink *Status) { 110 sink.ObservedGeneration = s.ObservedGeneration 111 if s.Annotations != nil { 112 sink.Annotations = kmap.Copy(s.Annotations) 113 } 114 for _, c := range s.Conditions { 115 switch c.Type { 116 // Copy over the "happy" condition, which is the only condition that 117 // we can reliably transfer. 118 case apis.ConditionReady, apis.ConditionSucceeded: 119 sink.SetConditions(apis.Conditions{c}) 120 return 121 } 122 } 123 } 124 125 // Populate implements duck.Populatable 126 func (t *KResource) Populate() { 127 t.Status.ObservedGeneration = 42 128 t.Status.Conditions = Conditions{{ 129 // Populate ALL fields 130 Type: "Birthday", 131 Status: corev1.ConditionTrue, 132 LastTransitionTime: apis.VolatileTime{Inner: metav1.NewTime(time.Date(1984, 2, 28, 18, 52, 0, 0, time.UTC))}, 133 Reason: "Celebrate", 134 Message: "n3wScott, find your party hat :tada:", 135 }} 136 } 137 138 // GetListType implements apis.Listable 139 func (*KResource) GetListType() runtime.Object { 140 return &KResourceList{} 141 } 142 143 // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object 144 145 // KResourceList is a list of KResource resources 146 type KResourceList struct { 147 metav1.TypeMeta `json:",inline"` 148 metav1.ListMeta `json:"metadata"` 149 150 Items []KResource `json:"items"` 151 }