| 1 |
/* |
| 2 |
Copyright 2016 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 |
"context" |
| 21 |
|
| 22 |
"k8s.io/apimachinery/pkg/api/meta" |
| 23 |
"k8s.io/apimachinery/pkg/fields" |
| 24 |
"k8s.io/apimachinery/pkg/labels" |
| 25 |
"k8s.io/apimachinery/pkg/runtime" |
| 26 |
"k8s.io/apiserver/pkg/endpoints/request" |
| 27 |
) |
| 28 |
|
| 29 |
// AttrFunc returns label and field sets and the uninitialized flag for List or Watch to match. |
| 30 |
// In any failure to parse given object, it returns error. |
| 31 |
type AttrFunc func(obj runtime.Object) (labels.Set, fields.Set, error) |
| 32 |
|
| 33 |
// FieldMutationFunc allows the mutation of the field selection fields. It is mutating to |
| 34 |
// avoid the extra allocation on this common path |
| 35 |
type FieldMutationFunc func(obj runtime.Object, fieldSet fields.Set) error |
| 36 |
|
| 37 |
func DefaultClusterScopedAttr(obj runtime.Object) (labels.Set, fields.Set, error) { |
| 38 |
metadata, err := meta.Accessor(obj) |
| 39 |
if err != nil { |
| 40 |
return nil, nil, err |
| 41 |
} |
| 42 |
fieldSet := fields.Set{ |
| 43 |
"metadata.name": metadata.GetName(), |
| 44 |
} |
| 45 |
|
| 46 |
return labels.Set(metadata.GetLabels()), fieldSet, nil |
| 47 |
} |
| 48 |
|
| 49 |
func DefaultNamespaceScopedAttr(obj runtime.Object) (labels.Set, fields.Set, error) { |
| 50 |
metadata, err := meta.Accessor(obj) |
| 51 |
if err != nil { |
| 52 |
return nil, nil, err |
| 53 |
} |
| 54 |
fieldSet := fields.Set{ |
| 55 |
"metadata.name": metadata.GetName(), |
| 56 |
"metadata.namespace": metadata.GetNamespace(), |
| 57 |
} |
| 58 |
|
| 59 |
return labels.Set(metadata.GetLabels()), fieldSet, nil |
| 60 |
} |
| 61 |
|
| 62 |
func (f AttrFunc) WithFieldMutation(fieldMutator FieldMutationFunc) AttrFunc { |
| 63 |
return func(obj runtime.Object) (labels.Set, fields.Set, error) { |
| 64 |
labelSet, fieldSet, err := f(obj) |
| 65 |
if err != nil { |
| 66 |
return nil, nil, err |
| 67 |
} |
| 68 |
if err := fieldMutator(obj, fieldSet); err != nil { |
| 69 |
return nil, nil, err |
| 70 |
} |
| 71 |
return labelSet, fieldSet, nil |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// SelectionPredicate is used to represent the way to select objects from api storage. |
| 76 |
type SelectionPredicate struct { |
| 77 |
Label labels.Selector |
| 78 |
Field fields.Selector |
| 79 |
GetAttrs AttrFunc |
| 80 |
IndexLabels []string |
| 81 |
IndexFields []string |
| 82 |
Limit int64 |
| 83 |
Continue string |
| 84 |
AllowWatchBookmarks bool |
| 85 |
} |
| 86 |
|
| 87 |
// Matches returns true if the given object's labels and fields (as |
| 88 |
// returned by s.GetAttrs) match s.Label and s.Field. An error is |
| 89 |
// returned if s.GetAttrs fails. |
| 90 |
func (s *SelectionPredicate) Matches(obj runtime.Object) (bool, error) { |
| 91 |
if s.Empty() { |
| 92 |
return true, nil |
| 93 |
} |
| 94 |
labels, fields, err := s.GetAttrs(obj) |
| 95 |
if err != nil { |
| 96 |
return false, err |
| 97 |
} |
| 98 |
matched := s.Label.Matches(labels) |
| 99 |
if matched && s.Field != nil { |
| 100 |
matched = matched && s.Field.Matches(fields) |
| 101 |
} |
| 102 |
return matched, nil |
| 103 |
} |
| 104 |
|
| 105 |
// MatchesObjectAttributes returns true if the given labels and fields |
| 106 |
// match s.Label and s.Field. |
| 107 |
func (s *SelectionPredicate) MatchesObjectAttributes(l labels.Set, f fields.Set) bool { |
| 108 |
if s.Label.Empty() && s.Field.Empty() { |
| 109 |
return true |
| 110 |
} |
| 111 |
matched := s.Label.Matches(l) |
| 112 |
if matched && s.Field != nil { |
| 113 |
matched = (matched && s.Field.Matches(f)) |
| 114 |
} |
| 115 |
return matched |
| 116 |
} |
| 117 |
|
| 118 |
// MatchesSingleNamespace will return (namespace, true) if and only if s.Field matches on the object's |
| 119 |
// namespace. |
| 120 |
func (s *SelectionPredicate) MatchesSingleNamespace() (string, bool) { |
| 121 |
if len(s.Continue) > 0 || s.Field == nil { |
| 122 |
return "", false |
| 123 |
} |
| 124 |
if namespace, ok := s.Field.RequiresExactMatch("metadata.namespace"); ok { |
| 125 |
return namespace, true |
| 126 |
} |
| 127 |
return "", false |
| 128 |
} |
| 129 |
|
| 130 |
// MatchesSingle will return (name, true) if and only if s.Field matches on the object's |
| 131 |
// name. |
| 132 |
func (s *SelectionPredicate) MatchesSingle() (string, bool) { |
| 133 |
if len(s.Continue) > 0 || s.Field == nil { |
| 134 |
return "", false |
| 135 |
} |
| 136 |
// TODO: should be namespace.name |
| 137 |
if name, ok := s.Field.RequiresExactMatch("metadata.name"); ok { |
| 138 |
return name, true |
| 139 |
} |
| 140 |
return "", false |
| 141 |
} |
| 142 |
|
| 143 |
// Empty returns true if the predicate performs no filtering. |
| 144 |
func (s *SelectionPredicate) Empty() bool { |
| 145 |
return s.Label.Empty() && s.Field.Empty() |
| 146 |
} |
| 147 |
|
| 148 |
// For any index defined by IndexFields, if a matcher can match only (a subset) |
| 149 |
// of objects that return <value> for a given index, a pair (<index name>, <value>) |
| 150 |
// wil be returned. |
| 151 |
func (s *SelectionPredicate) MatcherIndex(ctx context.Context) []MatchValue { |
| 152 |
var result []MatchValue |
| 153 |
for _, field := range s.IndexFields { |
| 154 |
if value, ok := s.Field.RequiresExactMatch(field); ok { |
| 155 |
result = append(result, MatchValue{IndexName: FieldIndex(field), Value: value}) |
| 156 |
} else if field == "metadata.namespace" { |
| 157 |
// list pods in the namespace. i.e. /api/v1/namespaces/default/pods |
| 158 |
if namespace, isNamespaceScope := isNamespaceScopedRequest(ctx); isNamespaceScope { |
| 159 |
result = append(result, MatchValue{IndexName: FieldIndex(field), Value: namespace}) |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
for _, label := range s.IndexLabels { |
| 164 |
if value, ok := s.Label.RequiresExactMatch(label); ok { |
| 165 |
result = append(result, MatchValue{IndexName: LabelIndex(label), Value: value}) |
| 166 |
} |
| 167 |
} |
| 168 |
return result |
| 169 |
} |
| 170 |
|
| 171 |
func isNamespaceScopedRequest(ctx context.Context) (string, bool) { |
| 172 |
re, _ := request.RequestInfoFrom(ctx) |
| 173 |
if re == nil || len(re.Namespace) == 0 { |
| 174 |
return "", false |
| 175 |
} |
| 176 |
return re.Namespace, true |
| 177 |
} |
| 178 |
|
| 179 |
// LabelIndex add prefix for label index. |
| 180 |
func LabelIndex(label string) string { |
| 181 |
return "l:" + label |
| 182 |
} |
| 183 |
|
| 184 |
// FiledIndex add prefix for field index. |
| 185 |
func FieldIndex(field string) string { |
| 186 |
return "f:" + field |
| 187 |
} |