blob: 9d437d06223c2d55012111afdb95e2cc58e9bc04 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package storage
18
19import (
20 "fmt"
21 "strings"
22 "sync/atomic"
23
24 "k8s.io/apimachinery/pkg/api/meta"
25 "k8s.io/apimachinery/pkg/api/validation/path"
26 "k8s.io/apimachinery/pkg/runtime"
27)
28
29type SimpleUpdateFunc func(runtime.Object) (runtime.Object, error)
30
31// SimpleUpdateFunc converts SimpleUpdateFunc into UpdateFunc
32func SimpleUpdate(fn SimpleUpdateFunc) UpdateFunc {
33 return func(input runtime.Object, _ ResponseMeta) (runtime.Object, *uint64, error) {
34 out, err := fn(input)
35 return out, nil, err
36 }
37}
38
39func EverythingFunc(runtime.Object) bool {
40 return true
41}
42
43func NoTriggerFunc() []MatchValue {
44 return nil
45}
46
47func NoTriggerPublisher(runtime.Object) []MatchValue {
48 return nil
49}
50
51func NamespaceKeyFunc(prefix string, obj runtime.Object) (string, error) {
52 meta, err := meta.Accessor(obj)
53 if err != nil {
54 return "", err
55 }
56 name := meta.GetName()
57 if msgs := path.IsValidPathSegmentName(name); len(msgs) != 0 {
58 return "", fmt.Errorf("invalid name: %v", msgs)
59 }
60 return prefix + "/" + meta.GetNamespace() + "/" + name, nil
61}
62
63func NoNamespaceKeyFunc(prefix string, obj runtime.Object) (string, error) {
64 meta, err := meta.Accessor(obj)
65 if err != nil {
66 return "", err
67 }
68 name := meta.GetName()
69 if msgs := path.IsValidPathSegmentName(name); len(msgs) != 0 {
70 return "", fmt.Errorf("invalid name: %v", msgs)
71 }
72 return prefix + "/" + name, nil
73}
74
75// hasPathPrefix returns true if the string matches pathPrefix exactly, or if is prefixed with pathPrefix at a path segment boundary
76func hasPathPrefix(s, pathPrefix string) bool {
77 // Short circuit if s doesn't contain the prefix at all
78 if !strings.HasPrefix(s, pathPrefix) {
79 return false
80 }
81
82 pathPrefixLength := len(pathPrefix)
83
84 if len(s) == pathPrefixLength {
85 // Exact match
86 return true
87 }
88 if strings.HasSuffix(pathPrefix, "/") {
89 // pathPrefix already ensured a path segment boundary
90 return true
91 }
92 if s[pathPrefixLength:pathPrefixLength+1] == "/" {
93 // The next character in s is a path segment boundary
94 // Check this instead of normalizing pathPrefix to avoid allocating on every call
95 return true
96 }
97 return false
98}
99
100// HighWaterMark is a thread-safe object for tracking the maximum value seen
101// for some quantity.
102type HighWaterMark int64
103
104// Update returns true if and only if 'current' is the highest value ever seen.
105func (hwm *HighWaterMark) Update(current int64) bool {
106 for {
107 old := atomic.LoadInt64((*int64)(hwm))
108 if current <= old {
109 return false
110 }
111 if atomic.CompareAndSwapInt64((*int64)(hwm), old, current) {
112 return true
113 }
114 }
115}