...

Source file src/k8s.io/kubernetes/pkg/kubelet/config/mux.go

Documentation: k8s.io/kubernetes/pkg/kubelet/config

     1  /*
     2  Copyright 2014 The Kubernetes 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 config
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  
    23  	"k8s.io/apimachinery/pkg/util/wait"
    24  )
    25  
    26  type merger interface {
    27  	// Invoked when a change from a source is received.  May also function as an incremental
    28  	// merger if you wish to consume changes incrementally.  Must be reentrant when more than
    29  	// one source is defined.
    30  	Merge(source string, update interface{}) error
    31  }
    32  
    33  // mux is a class for merging configuration from multiple sources.  Changes are
    34  // pushed via channels and sent to the merge function.
    35  type mux struct {
    36  	// Invoked when an update is sent to a source.
    37  	merger merger
    38  
    39  	// Sources and their lock.
    40  	sourceLock sync.RWMutex
    41  	// Maps source names to channels
    42  	sources map[string]chan interface{}
    43  }
    44  
    45  // newMux creates a new mux that can merge changes from multiple sources.
    46  func newMux(merger merger) *mux {
    47  	mux := &mux{
    48  		sources: make(map[string]chan interface{}),
    49  		merger:  merger,
    50  	}
    51  	return mux
    52  }
    53  
    54  // ChannelWithContext returns a channel where a configuration source
    55  // can send updates of new configurations. Multiple calls with the same
    56  // source will return the same channel. This allows change and state based sources
    57  // to use the same channel. Different source names however will be treated as a
    58  // union.
    59  func (m *mux) ChannelWithContext(ctx context.Context, source string) chan interface{} {
    60  	if len(source) == 0 {
    61  		panic("Channel given an empty name")
    62  	}
    63  	m.sourceLock.Lock()
    64  	defer m.sourceLock.Unlock()
    65  	channel, exists := m.sources[source]
    66  	if exists {
    67  		return channel
    68  	}
    69  	newChannel := make(chan interface{})
    70  	m.sources[source] = newChannel
    71  
    72  	go wait.Until(func() { m.listen(source, newChannel) }, 0, ctx.Done())
    73  	return newChannel
    74  }
    75  
    76  func (m *mux) listen(source string, listenChannel <-chan interface{}) {
    77  	for update := range listenChannel {
    78  		m.merger.Merge(source, update)
    79  	}
    80  }
    81  

View as plain text