knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1/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 v1
    18  
    19  import (
    20  	"context"
    21  
    22  	"knative.dev/pkg/apis"
    23  	"knative.dev/pkg/apis/duck/ducktypes"
    24  	"knative.dev/pkg/kmap"
    25  )
    26  
    27  // +genduck
    28  
    29  // Conditions is a simple wrapper around apis.Conditions to implement duck.Implementable.
    30  type Conditions apis.Conditions
    31  
    32  // Conditions is an Implementable duck type.
    33  var _ ducktypes.Implementable = (*Conditions)(nil)
    34  
    35  // Status shows how we expect folks to embed Conditions in
    36  // their Status field.
    37  // WARNING: Adding fields to this struct will add them to all Knative resources.
    38  type Status struct {
    39  	// ObservedGeneration is the 'Generation' of the Service that
    40  	// was last processed by the controller.
    41  	// +optional
    42  	ObservedGeneration int64 `json:"observedGeneration,omitempty"`
    43  
    44  	// Conditions the latest available observations of a resource's current state.
    45  	// +optional
    46  	// +patchMergeKey=type
    47  	// +patchStrategy=merge
    48  	Conditions Conditions `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
    49  
    50  	// Annotations is additional Status fields for the Resource to save some
    51  	// additional State as well as convey more information to the user. This is
    52  	// roughly akin to Annotations on any k8s resource, just the reconciler conveying
    53  	// richer information outwards.
    54  	Annotations map[string]string `json:"annotations,omitempty"`
    55  }
    56  
    57  var _ apis.ConditionsAccessor = (*Status)(nil)
    58  
    59  // GetConditions implements apis.ConditionsAccessor
    60  func (s *Status) GetConditions() apis.Conditions {
    61  	return apis.Conditions(s.Conditions)
    62  }
    63  
    64  // SetConditions implements apis.ConditionsAccessor
    65  func (s *Status) SetConditions(c apis.Conditions) {
    66  	s.Conditions = Conditions(c)
    67  }
    68  
    69  // Ensure KResource satisfies apis.Listable
    70  var _ apis.Listable = (*KResource)(nil)
    71  
    72  // GetFullType implements duck.Implementable
    73  func (*Conditions) GetFullType() ducktypes.Populatable {
    74  	return &KResource{}
    75  }
    76  
    77  // GetCondition fetches a copy of the condition of the specified type.
    78  func (s *Status) GetCondition(t apis.ConditionType) *apis.Condition {
    79  	for _, cond := range s.Conditions {
    80  		if cond.Type == t {
    81  			return &cond
    82  		}
    83  	}
    84  	return nil
    85  }
    86  
    87  // ConvertTo helps implement apis.Convertible for types embedding this Status.
    88  //
    89  // By default apis.ConditionReady and apis.ConditionSucceeded will be copied over to the
    90  // sink. Other conditions types are tested against a list of predicates. If any of the predicates
    91  // return true the condition type will be copied to the sink
    92  func (s *Status) ConvertTo(ctx context.Context, sink *Status, predicates ...func(apis.ConditionType) bool) {
    93  	sink.ObservedGeneration = s.ObservedGeneration
    94  	if s.Annotations != nil {
    95  		sink.Annotations = kmap.Union(s.Annotations)
    96  	}
    97  
    98  	conditions := make(apis.Conditions, 0, len(s.Conditions))
    99  	for _, c := range s.Conditions {
   100  		// Copy over the "happy" condition, which is the only condition that
   101  		// we can reliably transfer.
   102  		if c.Type == apis.ConditionReady || c.Type == apis.ConditionSucceeded {
   103  			conditions = append(conditions, c)
   104  			continue
   105  		}
   106  
   107  		for _, predicate := range predicates {
   108  			if predicate(c.Type) {
   109  				conditions = append(conditions, c)
   110  				break
   111  			}
   112  		}
   113  	}
   114  
   115  	sink.SetConditions(conditions)
   116  }