github.com/cilium/cilium@v1.16.2/clustermesh-apiserver/syncstate/syncstate.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright Authors of Cilium 3 4 package syncstate 5 6 import ( 7 "context" 8 9 "github.com/cilium/hive/cell" 10 11 "github.com/cilium/cilium/pkg/clustermesh/types" 12 "github.com/cilium/cilium/pkg/lock" 13 "github.com/cilium/cilium/pkg/metrics" 14 "github.com/cilium/cilium/pkg/metrics/metric" 15 "github.com/cilium/cilium/pkg/spanstat" 16 ) 17 18 var Cell = cell.Module( 19 "sync", 20 "ClusterMesh Sync", 21 22 metrics.Metric(MetricsProvider), 23 cell.Provide(new), 24 ) 25 26 func new(metrics Metrics, clusterInfo types.ClusterInfo) SyncState { 27 ss := SyncState{StoppableWaitGroup: lock.NewStoppableWaitGroup()} 28 29 go func() { 30 syncTime := spanstat.Start() 31 <-ss.WaitChannel() 32 metrics.BootstrapDuration.WithLabelValues(clusterInfo.Name).Set(syncTime.Seconds()) 33 }() 34 return ss 35 } 36 37 // SyncState is a wrapper around lock.StoppableWaitGroup used to keep track of the synchronization 38 // of various resources to the kvstore. 39 type SyncState struct { 40 *lock.StoppableWaitGroup 41 } 42 43 // Complete returns true if all resources have been synchronized to the kvstore. 44 func (ss SyncState) Complete() bool { 45 select { 46 case <-ss.WaitChannel(): 47 return true 48 default: 49 return false 50 } 51 } 52 53 // WaitForResource adds a resource to the SyncState and returns a callback function that should be 54 // called when the resource has been synchronized. 55 func (ss SyncState) WaitForResource() func(context.Context) { 56 ss.Add() 57 return func(_ context.Context) { 58 ss.Done() 59 } 60 } 61 62 // Metrics contains metrics that should only be exported by the 63 // clustermesh-apiserver or kvstoremesh. 64 type Metrics struct { 65 // BootstrapDuration tracks the duration in seconds until ready to serve requests. 66 BootstrapDuration metric.Vec[metric.Gauge] 67 } 68 69 func MetricsProvider() Metrics { 70 return Metrics{ 71 BootstrapDuration: metric.NewGaugeVec(metric.GaugeOpts{ 72 Namespace: metrics.Namespace, 73 Name: "bootstrap_seconds", 74 Help: "Duration in seconds to complete bootstrap", 75 }, []string{metrics.LabelSourceCluster}), 76 } 77 }