| 1 |
/* |
| 2 |
Copyright 2015 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 storage |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"sync/atomic" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/api/meta" |
| 24 |
"k8s.io/apimachinery/pkg/api/validation/path" |
| 25 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 |
"k8s.io/apimachinery/pkg/runtime" |
| 27 |
) |
| 28 |
|
| 29 |
type SimpleUpdateFunc func(runtime.Object) (runtime.Object, error) |
| 30 |
|
| 31 |
// SimpleUpdateFunc converts SimpleUpdateFunc into UpdateFunc |
| 32 |
func 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 |
|
| 39 |
func NamespaceKeyFunc(prefix string, obj runtime.Object) (string, error) { |
| 40 |
meta, err := meta.Accessor(obj) |
| 41 |
if err != nil { |
| 42 |
return "", err |
| 43 |
} |
| 44 |
name := meta.GetName() |
| 45 |
if msgs := path.IsValidPathSegmentName(name); len(msgs) != 0 { |
| 46 |
return "", fmt.Errorf("invalid name: %v", msgs) |
| 47 |
} |
| 48 |
return prefix + "/" + meta.GetNamespace() + "/" + name, nil |
| 49 |
} |
| 50 |
|
| 51 |
func NoNamespaceKeyFunc(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 + "/" + name, nil |
| 61 |
} |
| 62 |
|
| 63 |
// HighWaterMark is a thread-safe object for tracking the maximum value seen |
| 64 |
// for some quantity. |
| 65 |
type HighWaterMark int64 |
| 66 |
|
| 67 |
// Update returns true if and only if 'current' is the highest value ever seen. |
| 68 |
func (hwm *HighWaterMark) Update(current int64) bool { |
| 69 |
for { |
| 70 |
old := atomic.LoadInt64((*int64)(hwm)) |
| 71 |
if current <= old { |
| 72 |
return false |
| 73 |
} |
| 74 |
if atomic.CompareAndSwapInt64((*int64)(hwm), old, current) { |
| 75 |
return true |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// AnnotateInitialEventsEndBookmark adds a special annotation to the given object |
| 81 |
// which indicates that the initial events have been sent. |
| 82 |
// |
| 83 |
// Note that this function assumes that the obj's annotation |
| 84 |
// field is a reference type (i.e. a map). |
| 85 |
func AnnotateInitialEventsEndBookmark(obj runtime.Object) error { |
| 86 |
objMeta, err := meta.Accessor(obj) |
| 87 |
if err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
objAnnotations := objMeta.GetAnnotations() |
| 91 |
if objAnnotations == nil { |
| 92 |
objAnnotations = map[string]string{} |
| 93 |
} |
| 94 |
objAnnotations[metav1.InitialEventsAnnotationKey] = "true" |
| 95 |
objMeta.SetAnnotations(objAnnotations) |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
// HasInitialEventsEndBookmarkAnnotation checks the presence of the |
| 100 |
// special annotation which marks that the initial events have been sent. |
| 101 |
func HasInitialEventsEndBookmarkAnnotation(obj runtime.Object) (bool, error) { |
| 102 |
objMeta, err := meta.Accessor(obj) |
| 103 |
if err != nil { |
| 104 |
return false, err |
| 105 |
} |
| 106 |
objAnnotations := objMeta.GetAnnotations() |
| 107 |
return objAnnotations[metav1.InitialEventsAnnotationKey] == "true", nil |
| 108 |
} |