istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/kube/krt/static.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 "istio.io/istio/pkg/kube/controllers" 19 "istio.io/istio/pkg/maps" 20 "istio.io/istio/pkg/slices" 21 ) 22 23 type staticList[T any] struct { 24 vals map[Key[T]]T 25 id collectionUID 26 } 27 28 func NewStaticCollection[T any](vals []T) Collection[T] { 29 res := map[Key[T]]T{} 30 for _, v := range vals { 31 res[GetKey(v)] = v 32 } 33 return &staticList[T]{ 34 vals: res, 35 id: nextUID(), 36 } 37 } 38 39 func (s *staticList[T]) GetKey(k Key[T]) *T { 40 if o, f := s.vals[k]; f { 41 return &o 42 } 43 return nil 44 } 45 46 // nolint: unused // (not true, its to implement an interface) 47 func (s *staticList[T]) name() string { 48 return "staticList" 49 } 50 51 // nolint: unused // (not true, its to implement an interface) 52 func (s *staticList[T]) uid() collectionUID { 53 return s.id 54 } 55 56 // nolint: unused // (not true, its to implement an interface) 57 func (s *staticList[T]) dump() { 58 } 59 60 // nolint: unused // (not true, its to implement an interface) 61 func (s *staticList[T]) augment(a any) any { 62 return a 63 } 64 65 func (s *staticList[T]) List() []T { 66 return maps.Values(s.vals) 67 } 68 69 func (s *staticList[T]) Register(f func(o Event[T])) Syncer { 70 return registerHandlerAsBatched(s, f) 71 } 72 73 func (s *staticList[T]) Synced() Syncer { 74 return alwaysSynced{} 75 } 76 77 func (s *staticList[T]) RegisterBatch(f func(o []Event[T], initialSync bool), runExistingState bool) Syncer { 78 if runExistingState { 79 f(slices.Map(s.List(), func(e T) Event[T] { 80 return Event[T]{ 81 New: &e, 82 Event: controllers.EventAdd, 83 } 84 }), true) 85 } 86 return alwaysSynced{} 87 } 88 89 var _ internalCollection[any] = &staticList[any]{}