knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/configmap/static_watcher.go (about)

     1  /*
     2  Copyright 2018 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  package configmap
    18  
    19  import (
    20  	"fmt"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  )
    24  
    25  // NewStaticWatcher returns an StaticWatcher that exposes a collection of ConfigMaps.
    26  func NewStaticWatcher(cms ...*corev1.ConfigMap) *StaticWatcher {
    27  	cmm := make(map[string]*corev1.ConfigMap)
    28  	for _, cm := range cms {
    29  		cmm[cm.Name] = cm
    30  	}
    31  	return &StaticWatcher{cfgs: cmm}
    32  }
    33  
    34  // StaticWatcher is a Watcher with static ConfigMaps. Callbacks will
    35  // occur when Watch is invoked for a specific Observer
    36  type StaticWatcher struct {
    37  	cfgs map[string]*corev1.ConfigMap
    38  }
    39  
    40  // Asserts that fixedImpl implements Watcher.
    41  var _ Watcher = (*StaticWatcher)(nil)
    42  
    43  // Watch implements Watcher
    44  func (di *StaticWatcher) Watch(name string, o ...Observer) {
    45  	cm, ok := di.cfgs[name]
    46  	if ok {
    47  		for _, observer := range o {
    48  			observer(cm)
    49  		}
    50  	} else {
    51  		panic(fmt.Sprintf("Tried to watch unknown config with name %q", name))
    52  	}
    53  }
    54  
    55  // Start implements Watcher
    56  func (di *StaticWatcher) Start(<-chan struct{}) error {
    57  	return nil
    58  }