knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/client/injection/kube/reconciler/core/v1/configmap/state.go (about)

     1  /*
     2  Copyright 2022 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  // Code generated by injection-gen. DO NOT EDIT.
    18  
    19  package configmap
    20  
    21  import (
    22  	fmt "fmt"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	types "k8s.io/apimachinery/pkg/types"
    26  	cache "k8s.io/client-go/tools/cache"
    27  	reconciler "knative.dev/pkg/reconciler"
    28  )
    29  
    30  // state is used to track the state of a reconciler in a single run.
    31  type state struct {
    32  	// key is the original reconciliation key from the queue.
    33  	key string
    34  	// namespace is the namespace split from the reconciliation key.
    35  	namespace string
    36  	// name is the name split from the reconciliation key.
    37  	name string
    38  	// reconciler is the reconciler.
    39  	reconciler Interface
    40  	// roi is the read only interface cast of the reconciler.
    41  	roi ReadOnlyInterface
    42  	// isROI (Read Only Interface) the reconciler only observes reconciliation.
    43  	isROI bool
    44  	// isLeader the instance of the reconciler is the elected leader.
    45  	isLeader bool
    46  }
    47  
    48  func newState(key string, r *reconcilerImpl) (*state, error) {
    49  	// Convert the namespace/name string into a distinct namespace and name.
    50  	namespace, name, err := cache.SplitMetaNamespaceKey(key)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("invalid resource key: %s", key)
    53  	}
    54  
    55  	roi, isROI := r.reconciler.(ReadOnlyInterface)
    56  
    57  	isLeader := r.IsLeaderFor(types.NamespacedName{
    58  		Namespace: namespace,
    59  		Name:      name,
    60  	})
    61  
    62  	return &state{
    63  		key:        key,
    64  		namespace:  namespace,
    65  		name:       name,
    66  		reconciler: r.reconciler,
    67  		roi:        roi,
    68  		isROI:      isROI,
    69  		isLeader:   isLeader,
    70  	}, nil
    71  }
    72  
    73  // isNotLeaderNorObserver checks to see if this reconciler with the current
    74  // state is enabled to do any work or not.
    75  // isNotLeaderNorObserver returns true when there is no work possible for the
    76  // reconciler.
    77  func (s *state) isNotLeaderNorObserver() bool {
    78  	if !s.isLeader && !s.isROI {
    79  		// If we are not the leader, and we don't implement the ReadOnly
    80  		// interface, then take a fast-path out.
    81  		return true
    82  	}
    83  	return false
    84  }
    85  
    86  func (s *state) reconcileMethodFor(o *v1.ConfigMap) (string, doReconcile) {
    87  	if o.GetDeletionTimestamp().IsZero() {
    88  		if s.isLeader {
    89  			return reconciler.DoReconcileKind, s.reconciler.ReconcileKind
    90  		} else if s.isROI {
    91  			return reconciler.DoObserveKind, s.roi.ObserveKind
    92  		}
    93  	} else if fin, ok := s.reconciler.(Finalizer); s.isLeader && ok {
    94  		return reconciler.DoFinalizeKind, fin.FinalizeKind
    95  	}
    96  	return "unknown", nil
    97  }