istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/kube/krt/sync.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 "istio.io/istio/pkg/kube"
    18  
    19  type Syncer interface {
    20  	WaitUntilSynced(stop <-chan struct{}) bool
    21  	HasSynced() bool
    22  }
    23  
    24  var (
    25  	_ Syncer = channelSyncer{}
    26  	_ Syncer = pollSyncer{}
    27  )
    28  
    29  type channelSyncer struct {
    30  	name   string
    31  	synced <-chan struct{}
    32  }
    33  
    34  func (c channelSyncer) WaitUntilSynced(stop <-chan struct{}) bool {
    35  	return waitForCacheSync(c.name, stop, c.synced)
    36  }
    37  
    38  func (c channelSyncer) HasSynced() bool {
    39  	select {
    40  	case <-c.synced:
    41  		return true
    42  	default:
    43  		return false
    44  	}
    45  }
    46  
    47  type pollSyncer struct {
    48  	name string
    49  	f    func() bool
    50  }
    51  
    52  func (c pollSyncer) WaitUntilSynced(stop <-chan struct{}) bool {
    53  	return kube.WaitForCacheSync(c.name, stop, c.f)
    54  }
    55  
    56  func (c pollSyncer) HasSynced() bool {
    57  	return c.f()
    58  }
    59  
    60  type alwaysSynced struct{}
    61  
    62  func (c alwaysSynced) WaitUntilSynced(stop <-chan struct{}) bool {
    63  	return true
    64  }
    65  
    66  func (c alwaysSynced) HasSynced() bool {
    67  	return true
    68  }
    69  
    70  type multiSyncer struct {
    71  	syncers []Syncer
    72  }
    73  
    74  func (c multiSyncer) WaitUntilSynced(stop <-chan struct{}) bool {
    75  	for _, s := range c.syncers {
    76  		if !s.WaitUntilSynced(stop) {
    77  			return false
    78  		}
    79  	}
    80  	return true
    81  }
    82  
    83  func (c multiSyncer) HasSynced() bool {
    84  	for _, s := range c.syncers {
    85  		if !s.HasSynced() {
    86  			return false
    87  		}
    88  	}
    89  	return true
    90  }