Extract the prepareKey function
Kubernetes-commit: 0b10735cd72fbf5416a20d2bb7e525b7d8a1e716
This commit is contained in:
		
							parent
							
								
									7ce88c0631
								
							
						
					
					
						commit
						ce9b4fddcb
					
				|  | @ -21,7 +21,6 @@ import ( | |||
| 	"fmt" | ||||
| 	"net/http" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
| 	"sync" | ||||
| 	"time" | ||||
| 
 | ||||
|  | @ -1170,29 +1169,7 @@ func (c *Cacher) Stop() { | |||
| } | ||||
| 
 | ||||
| func (c *Cacher) prepareKey(key string, recursive bool) (string, error) { | ||||
| 	if key == ".." || | ||||
| 		strings.HasPrefix(key, "../") || | ||||
| 		strings.HasSuffix(key, "/..") || | ||||
| 		strings.Contains(key, "/../") { | ||||
| 		return "", fmt.Errorf("invalid key: %q", key) | ||||
| 	} | ||||
| 	if key == "." || | ||||
| 		strings.HasPrefix(key, "./") || | ||||
| 		strings.HasSuffix(key, "/.") || | ||||
| 		strings.Contains(key, "/./") { | ||||
| 		return "", fmt.Errorf("invalid key: %q", key) | ||||
| 	} | ||||
| 	if key == "" || key == "/" { | ||||
| 		return "", fmt.Errorf("empty key: %q", key) | ||||
| 	} | ||||
| 	// For recursive lists, we need to make sure the key ended with "/" so that we only
 | ||||
| 	// get children "directories". e.g. if we have key "/a", "/a/b", "/ab", getting keys
 | ||||
| 	// with prefix "/a" will return all three, while with prefix "/a/" will return only
 | ||||
| 	// "/a/b" which is the correct answer.
 | ||||
| 	if recursive && !strings.HasSuffix(key, "/") { | ||||
| 		key += "/" | ||||
| 	} | ||||
| 	return key, nil | ||||
| 	return storage.PrepareKey(key, recursive) | ||||
| } | ||||
| 
 | ||||
| func forgetWatcher(c *Cacher, w *cacheWatcher, index int, scope namespacedName, triggerValue string, triggerSupported bool) func(bool) { | ||||
|  |  | |||
|  | @ -1105,33 +1105,15 @@ func (s *store) validateMinimumResourceVersion(minimumResourceVersion string, ac | |||
| } | ||||
| 
 | ||||
| func (s *store) prepareKey(key string, recursive bool) (string, error) { | ||||
| 	if key == ".." || | ||||
| 		strings.HasPrefix(key, "../") || | ||||
| 		strings.HasSuffix(key, "/..") || | ||||
| 		strings.Contains(key, "/../") { | ||||
| 		return "", fmt.Errorf("invalid key: %q", key) | ||||
| 	} | ||||
| 	if key == "." || | ||||
| 		strings.HasPrefix(key, "./") || | ||||
| 		strings.HasSuffix(key, "/.") || | ||||
| 		strings.Contains(key, "/./") { | ||||
| 		return "", fmt.Errorf("invalid key: %q", key) | ||||
| 	} | ||||
| 	if key == "" || key == "/" { | ||||
| 		return "", fmt.Errorf("empty key: %q", key) | ||||
| 	key, err := storage.PrepareKey(key, recursive) | ||||
| 	if err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	// We ensured that pathPrefix ends in '/' in construction, so skip any leading '/' in the key now.
 | ||||
| 	startIndex := 0 | ||||
| 	if key[0] == '/' { | ||||
| 		startIndex = 1 | ||||
| 	} | ||||
| 	// For recursive requests, we need to make sure the key ended with "/" so that we only
 | ||||
| 	// get children "directories". e.g. if we have key "/a", "/a/b", "/ab", getting keys
 | ||||
| 	// with prefix "/a" will return all three, while with prefix "/a/" will return only
 | ||||
| 	// "/a/b" which is the correct answer.
 | ||||
| 	if recursive && !strings.HasSuffix(key, "/") { | ||||
| 		key += "/" | ||||
| 	} | ||||
| 	return s.pathPrefix + key[startIndex:], nil | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -19,6 +19,7 @@ package storage | |||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
| 
 | ||||
| 	apierrors "k8s.io/apimachinery/pkg/api/errors" | ||||
| 	"k8s.io/apimachinery/pkg/api/meta" | ||||
|  | @ -390,3 +391,29 @@ type Stats struct { | |||
| 	// Value is an estimate, meaning it doesn't need to provide accurate nor consistent.
 | ||||
| 	EstimatedAverageObjectSizeBytes int64 | ||||
| } | ||||
| 
 | ||||
| func PrepareKey(key string, recursive bool) (string, error) { | ||||
| 	if key == ".." || | ||||
| 		strings.HasPrefix(key, "../") || | ||||
| 		strings.HasSuffix(key, "/..") || | ||||
| 		strings.Contains(key, "/../") { | ||||
| 		return "", fmt.Errorf("invalid key: %q", key) | ||||
| 	} | ||||
| 	if key == "." || | ||||
| 		strings.HasPrefix(key, "./") || | ||||
| 		strings.HasSuffix(key, "/.") || | ||||
| 		strings.Contains(key, "/./") { | ||||
| 		return "", fmt.Errorf("invalid key: %q", key) | ||||
| 	} | ||||
| 	if key == "" || key == "/" { | ||||
| 		return "", fmt.Errorf("empty key: %q", key) | ||||
| 	} | ||||
| 	// For recursive requests, we need to make sure the key ended with "/" so that we only
 | ||||
| 	// get children "directories". e.g. if we have key "/a", "/a/b", "/ab", getting keys
 | ||||
| 	// with prefix "/a" will return all three, while with prefix "/a/" will return only
 | ||||
| 	// "/a/b" which is the correct answer.
 | ||||
| 	if recursive && !strings.HasSuffix(key, "/") { | ||||
| 		key += "/" | ||||
| 	} | ||||
| 	return key, nil | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue