istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/kube/krt/recomputetrigger.go (about)

     1  // Copyright Istio Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package krt
    16  
    17  import (
    18  	"go.uber.org/atomic"
    19  
    20  	"istio.io/istio/pkg/ptr"
    21  )
    22  
    23  // RecomputeTrigger trigger provides an escape hatch to allow krt transformations to depend on external state and recompute
    24  // correctly when those change.
    25  // Typically, all state is registered and fetched through krt.Fetch. Through this mechanism, any changes are automatically
    26  // propagated through the system to dependencies.
    27  // In some cases, it may not be feasible to get all state into krt; hopefully, this is a temporary state.
    28  // RecomputeTrigger works around this by allowing an explicit call to recompute a collection; the caller must be sure to call Trigger()
    29  // any time the state changes.
    30  type RecomputeTrigger struct {
    31  	inner StaticSingleton[int32]
    32  	// krt will suppress events for unchanged resources. To workaround this, we constantly change and int each time TriggerRecomputation
    33  	// is called to ensure our event is not suppressed.
    34  	i *atomic.Int32
    35  }
    36  
    37  func NewRecomputeTrigger() *RecomputeTrigger {
    38  	inner := NewStatic[int32](ptr.Of(int32(0)))
    39  	return &RecomputeTrigger{inner: inner, i: atomic.NewInt32(0)}
    40  }
    41  
    42  // TriggerRecomputation tells all dependants to recompute
    43  func (r *RecomputeTrigger) TriggerRecomputation() {
    44  	v := r.i.Inc()
    45  	r.inner.Set(ptr.Of(v))
    46  }
    47  
    48  // MarkDependant marks the given context as depending on this trigger. This registers it to be recomputed when TriggerRecomputation
    49  // is called.
    50  func (r *RecomputeTrigger) MarkDependant(ctx HandlerContext) {
    51  	_ = Fetch(ctx, r.inner.AsCollection())
    52  }