sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/config/secret/reloader.go (about)

     1  /*
     2  Copyright 2022 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 secret
    18  
    19  import (
    20  	"os"
    21  	"sync"
    22  	"time"
    23  
    24  	"github.com/sirupsen/logrus"
    25  )
    26  
    27  type parsingSecretReloader[T any] struct {
    28  	lock      sync.RWMutex
    29  	path      string
    30  	rawValue  []byte
    31  	parsed    T
    32  	parsingFN func([]byte) (T, error)
    33  }
    34  
    35  func (p *parsingSecretReloader[T]) start(reloadCensor func()) error {
    36  	raw, parsed, err := loadSingleSecretWithParser(p.path, p.parsingFN)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	p.lock.Lock()
    41  	p.rawValue = raw
    42  	p.parsed = parsed
    43  	p.lock.Unlock()
    44  	reloadCensor()
    45  
    46  	go p.reloadSecret(reloadCensor)
    47  	return nil
    48  }
    49  
    50  func (p *parsingSecretReloader[T]) reloadSecret(reloadCensor func()) {
    51  	var lastModTime time.Time
    52  	logger := logrus.NewEntry(logrus.StandardLogger())
    53  
    54  	skips := 0
    55  	for range time.Tick(1 * time.Second) {
    56  		if skips < 600 {
    57  			// Check if the file changed to see if it needs to be re-read.
    58  			secretStat, err := os.Stat(p.path)
    59  			if err != nil {
    60  				logger.WithField("secret-path", p.path).WithError(err).Warn("Error loading secret file.")
    61  				continue
    62  			}
    63  
    64  			recentModTime := secretStat.ModTime()
    65  			if !recentModTime.After(lastModTime) {
    66  				skips++
    67  				continue // file hasn't been modified
    68  			}
    69  			lastModTime = recentModTime
    70  		}
    71  
    72  		raw, parsed, err := loadSingleSecretWithParser(p.path, p.parsingFN)
    73  		if err != nil {
    74  			logger.WithField("secret-path", p.path).WithError(err).Error("Error loading secret.")
    75  			continue
    76  		}
    77  
    78  		p.lock.Lock()
    79  		p.rawValue = raw
    80  		p.parsed = parsed
    81  		p.lock.Unlock()
    82  		reloadCensor()
    83  
    84  		skips = 0
    85  	}
    86  
    87  }
    88  
    89  func (p *parsingSecretReloader[T]) getRaw() []byte {
    90  	p.lock.RLock()
    91  	defer p.lock.RUnlock()
    92  	return p.rawValue
    93  }
    94  
    95  func (p *parsingSecretReloader[T]) get() T {
    96  	p.lock.RLock()
    97  	defer p.lock.RUnlock()
    98  	return p.parsed
    99  }