k8s.io/kubernetes@v1.29.3/pkg/kubelet/util/store/store.go (about)

     1  /*
     2  Copyright 2017 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 store
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  )
    23  
    24  const (
    25  	keyMaxLength = 250
    26  
    27  	keyCharFmt      string = "[A-Za-z0-9]"
    28  	keyExtCharFmt   string = "[-A-Za-z0-9_.]"
    29  	qualifiedKeyFmt string = "(" + keyCharFmt + keyExtCharFmt + "*)?" + keyCharFmt
    30  )
    31  
    32  var (
    33  	// Key must consist of alphanumeric characters, '-', '_' or '.', and must start
    34  	// and end with an alphanumeric character.
    35  	keyRegex = regexp.MustCompile("^" + qualifiedKeyFmt + "$")
    36  
    37  	// ErrKeyNotFound is the error returned if key is not found in Store.
    38  	ErrKeyNotFound = fmt.Errorf("key is not found")
    39  )
    40  
    41  // Store provides the interface for storing keyed data.
    42  // Store must be thread-safe
    43  type Store interface {
    44  	// key must contain one or more characters in [A-Za-z0-9]
    45  	// Write writes data with key.
    46  	Write(key string, data []byte) error
    47  	// Read retrieves data with key
    48  	// Read must return ErrKeyNotFound if key is not found.
    49  	Read(key string) ([]byte, error)
    50  	// Delete deletes data by key
    51  	// Delete must not return error if key does not exist
    52  	Delete(key string) error
    53  	// List lists all existing keys.
    54  	List() ([]string, error)
    55  }
    56  
    57  // ValidateKey returns an error if the given key does not meet the requirement
    58  // of the key format and length.
    59  func ValidateKey(key string) error {
    60  	if len(key) <= keyMaxLength && keyRegex.MatchString(key) {
    61  		return nil
    62  	}
    63  	return fmt.Errorf("invalid key: %q", key)
    64  }