github.com/datadog/cilium@v1.6.12/operator/kvstore_watchdog.go (about)

     1  // Copyright 2020 Authors of Cilium
     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 main
    16  
    17  import (
    18  	"strings"
    19  	"time"
    20  
    21  	"github.com/cilium/cilium/pkg/allocator"
    22  	"github.com/cilium/cilium/pkg/defaults"
    23  	"github.com/cilium/cilium/pkg/identity/cache"
    24  	"github.com/cilium/cilium/pkg/kvstore"
    25  	kvstoreallocator "github.com/cilium/cilium/pkg/kvstore/allocator"
    26  )
    27  
    28  // keyPathFromLockPath returns the path of the given key that contains a lease
    29  // prefixed to its path.
    30  func keyPathFromLockPath(k string) string {
    31  	// vendor/go.etcd.io/etcd/clientv3/concurrency/mutex.go:L46
    32  	i := strings.LastIndexByte(k, '/')
    33  	if i >= 0 {
    34  		return k[:i]
    35  	}
    36  	return k
    37  }
    38  
    39  // getOldestLeases returns the value that has the smaller revision for each
    40  // 'path'. A 'path' shares the same common prefix for different locks.
    41  func getOldestLeases(lockPaths map[string]kvstore.Value) map[string]kvstore.Value {
    42  	type LockValue struct {
    43  		kvstore.Value
    44  		keyPath string
    45  	}
    46  	oldestPaths := map[string]LockValue{}
    47  	for lockPath, v := range lockPaths {
    48  		keyPath := keyPathFromLockPath(lockPath)
    49  		oldestKeyPath, ok := oldestPaths[keyPath]
    50  		if !ok || v.ModRevision < oldestKeyPath.ModRevision {
    51  			// Store the oldest common path
    52  			oldestPaths[keyPath] = LockValue{
    53  				keyPath: lockPath,
    54  				Value:   v,
    55  			}
    56  		}
    57  	}
    58  	oldestLeases := map[string]kvstore.Value{}
    59  	for _, v := range oldestPaths {
    60  		// Retrieve the oldest lock path
    61  		oldestLeases[v.keyPath] = v.Value
    62  	}
    63  	return oldestLeases
    64  }
    65  
    66  func startKvstoreWatchdog() {
    67  	log.Infof("Starting kvstore watchdog with %s interval...", defaults.LockLeaseTTL)
    68  	backend, err := kvstoreallocator.NewKVStoreBackend(cache.IdentitiesPath, "", nil, kvstore.Client())
    69  	if err != nil {
    70  		log.WithError(err).Fatal("Unable to initialize kvstore backend for identity garbage collection")
    71  	}
    72  	a := allocator.NewAllocatorForGC(backend)
    73  
    74  	keysToDelete := map[string]kvstore.Value{}
    75  	go func() {
    76  		for {
    77  			keysToDelete = getOldestLeases(keysToDelete)
    78  			keysToDelete2, err := a.RunLocksGC(keysToDelete)
    79  			if err != nil {
    80  				log.WithError(err).Warning("Unable to run security identity garbage collector")
    81  			} else {
    82  				keysToDelete = keysToDelete2
    83  			}
    84  
    85  			<-time.After(defaults.LockLeaseTTL)
    86  		}
    87  	}()
    88  }