2015-07-30 11:27:18 +00:00
|
|
|
/*
|
2016-06-03 00:25:58 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2015-07-30 11:27:18 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-10-20 21:36:45 +00:00
|
|
|
"fmt"
|
2015-07-30 11:27:18 +00:00
|
|
|
"strconv"
|
2016-07-14 02:21:25 +00:00
|
|
|
"strings"
|
2015-07-30 11:27:18 +00:00
|
|
|
|
2015-07-28 06:26:53 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/meta"
|
2015-10-20 21:36:45 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/validation"
|
2015-08-05 22:03:47 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2015-11-06 23:30:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/util/validation/field"
|
2015-07-30 11:27:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SimpleUpdateFunc func(runtime.Object) (runtime.Object, error)
|
|
|
|
|
|
|
|
// SimpleUpdateFunc converts SimpleUpdateFunc into UpdateFunc
|
|
|
|
func SimpleUpdate(fn SimpleUpdateFunc) UpdateFunc {
|
|
|
|
return func(input runtime.Object, _ ResponseMeta) (runtime.Object, *uint64, error) {
|
|
|
|
out, err := fn(input)
|
|
|
|
return out, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 12:09:16 +00:00
|
|
|
// SimpleFilter implements Filter interface.
|
|
|
|
type SimpleFilter struct {
|
2016-06-03 13:23:26 +00:00
|
|
|
filterFunc func(runtime.Object) bool
|
|
|
|
triggerFunc func() []MatchValue
|
2016-06-03 12:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SimpleFilter) Filter(obj runtime.Object) bool {
|
|
|
|
return s.filterFunc(obj)
|
|
|
|
}
|
|
|
|
|
2016-06-03 13:23:26 +00:00
|
|
|
func (s *SimpleFilter) Trigger() []MatchValue {
|
|
|
|
return s.triggerFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSimpleFilter(
|
|
|
|
filterFunc func(runtime.Object) bool,
|
|
|
|
triggerFunc func() []MatchValue) Filter {
|
2016-06-03 12:09:16 +00:00
|
|
|
return &SimpleFilter{
|
2016-06-03 13:23:26 +00:00
|
|
|
filterFunc: filterFunc,
|
|
|
|
triggerFunc: triggerFunc,
|
2016-06-03 12:09:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func EverythingFunc(runtime.Object) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-06-03 13:23:26 +00:00
|
|
|
func NoTriggerFunc() []MatchValue {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NoTriggerPublisher(runtime.Object) []MatchValue {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-30 11:27:18 +00:00
|
|
|
// ParseWatchResourceVersion takes a resource version argument and converts it to
|
|
|
|
// the etcd version we should pass to helper.Watch(). Because resourceVersion is
|
|
|
|
// an opaque value, the default watch behavior for non-zero watch is to watch
|
|
|
|
// the next value (if you pass "1", you will see updates from "2" onwards).
|
2015-12-04 08:58:24 +00:00
|
|
|
func ParseWatchResourceVersion(resourceVersion string) (uint64, error) {
|
2015-07-30 11:27:18 +00:00
|
|
|
if resourceVersion == "" || resourceVersion == "0" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
version, err := strconv.ParseUint(resourceVersion, 10, 64)
|
|
|
|
if err != nil {
|
2016-03-21 06:15:00 +00:00
|
|
|
return 0, NewInvalidError(field.ErrorList{
|
2015-11-04 21:52:14 +00:00
|
|
|
// Validation errors are supposed to return version-specific field
|
|
|
|
// paths, but this is probably close enough.
|
2015-11-10 20:59:41 +00:00
|
|
|
field.Invalid(field.NewPath("resourceVersion"), resourceVersion, err.Error()),
|
2015-11-04 21:52:14 +00:00
|
|
|
})
|
2015-07-30 11:27:18 +00:00
|
|
|
}
|
2015-12-10 14:03:59 +00:00
|
|
|
return version, nil
|
2015-07-30 11:27:18 +00:00
|
|
|
}
|
2015-07-28 06:26:53 +00:00
|
|
|
|
2015-12-04 08:58:24 +00:00
|
|
|
// ParseListResourceVersion takes a resource version argument and converts it to
|
|
|
|
// the etcd version.
|
|
|
|
func ParseListResourceVersion(resourceVersion string) (uint64, error) {
|
|
|
|
if resourceVersion == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
version, err := strconv.ParseUint(resourceVersion, 10, 64)
|
|
|
|
return version, err
|
|
|
|
}
|
|
|
|
|
2015-07-28 06:26:53 +00:00
|
|
|
func NamespaceKeyFunc(prefix string, obj runtime.Object) (string, error) {
|
|
|
|
meta, err := meta.Accessor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 03:01:12 +00:00
|
|
|
name := meta.GetName()
|
2015-12-16 06:03:20 +00:00
|
|
|
if msgs := validation.IsValidPathSegmentName(name); len(msgs) != 0 {
|
|
|
|
return "", fmt.Errorf("invalid name: %v", msgs)
|
2015-10-20 21:36:45 +00:00
|
|
|
}
|
2015-12-08 03:01:12 +00:00
|
|
|
return prefix + "/" + meta.GetNamespace() + "/" + name, nil
|
2015-07-28 06:26:53 +00:00
|
|
|
}
|
2015-08-17 07:59:12 +00:00
|
|
|
|
|
|
|
func NoNamespaceKeyFunc(prefix string, obj runtime.Object) (string, error) {
|
|
|
|
meta, err := meta.Accessor(obj)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2015-12-08 03:01:12 +00:00
|
|
|
name := meta.GetName()
|
2015-12-16 06:03:20 +00:00
|
|
|
if msgs := validation.IsValidPathSegmentName(name); len(msgs) != 0 {
|
|
|
|
return "", fmt.Errorf("invalid name: %v", msgs)
|
2015-10-20 21:36:45 +00:00
|
|
|
}
|
2015-12-08 03:01:12 +00:00
|
|
|
return prefix + "/" + name, nil
|
2015-08-17 07:59:12 +00:00
|
|
|
}
|
2016-07-14 02:21:25 +00:00
|
|
|
|
|
|
|
// hasPathPrefix returns true if the string matches pathPrefix exactly, or if is prefixed with pathPrefix at a path segment boundary
|
|
|
|
func hasPathPrefix(s, pathPrefix string) bool {
|
|
|
|
// Short circuit if s doesn't contain the prefix at all
|
|
|
|
if !strings.HasPrefix(s, pathPrefix) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
pathPrefixLength := len(pathPrefix)
|
|
|
|
|
|
|
|
if len(s) == pathPrefixLength {
|
|
|
|
// Exact match
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(pathPrefix, "/") {
|
|
|
|
// pathPrefix already ensured a path segment boundary
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if s[pathPrefixLength:pathPrefixLength+1] == "/" {
|
|
|
|
// The next character in s is a path segment boundary
|
|
|
|
// Check this instead of normalizing pathPrefix to avoid allocating on every call
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|