Debian Codegraph @

pkg/registry/rest/meta.go

raw
1 /*
2 Copyright 2017 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 rest
18
19 import (
20 "k8s.io/apimachinery/pkg/api/errors"
21 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22 "k8s.io/apimachinery/pkg/runtime/schema"
23 "k8s.io/apimachinery/pkg/util/uuid"
24 )
25
26 // metav1Now returns metav1.Now(), but allows override for unit testing
27 var metav1Now = func() metav1.Time { return metav1.Now() }
28
29 // WipeObjectMetaSystemFields erases fields that are managed by the system on ObjectMeta.
30 func WipeObjectMetaSystemFields(meta metav1.Object) {
31 meta.SetCreationTimestamp(metav1.Time{})
32 meta.SetUID("")
33 meta.SetDeletionTimestamp(nil)
34 meta.SetDeletionGracePeriodSeconds(nil)
35 meta.SetSelfLink("")
36 }
37
38 // FillObjectMetaSystemFields populates fields that are managed by the system on ObjectMeta.
39 func FillObjectMetaSystemFields(meta metav1.Object) {
40 meta.SetCreationTimestamp(metav1Now())
41 meta.SetUID(uuid.NewUUID())
42 }
43
44 // EnsureObjectNamespaceMatchesRequestNamespace returns an error if obj.Namespace and requestNamespace
45 // are both populated and do not match. If either is unpopulated, it modifies obj as needed to ensure
46 // obj.GetNamespace() == requestNamespace.
47 func EnsureObjectNamespaceMatchesRequestNamespace(requestNamespace string, obj metav1.Object) error {
48 objNamespace := obj.GetNamespace()
49 switch {
50 case objNamespace == requestNamespace:
51 // already matches, no-op
52 return nil
53
54 case objNamespace == metav1.NamespaceNone:
55 // unset, default to request namespace
56 obj.SetNamespace(requestNamespace)
57 return nil
58
59 case requestNamespace == metav1.NamespaceNone:
60 // cluster-scoped, clear namespace
61 obj.SetNamespace(metav1.NamespaceNone)
62 return nil
63
64 default:
65 // mismatch, error
66 return errors.NewBadRequest("the namespace of the provided object does not match the namespace sent on the request")
67 }
68 }
69
70 // ExpectedNamespaceForScope returns the expected namespace for a resource, given the request namespace and resource scope.
71 func ExpectedNamespaceForScope(requestNamespace string, namespaceScoped bool) string {
72 if namespaceScoped {
73 return requestNamespace
74 }
75 return ""
76 }
77
78 // ExpectedNamespaceForResource returns the expected namespace for a resource, given the request namespace.
79 func ExpectedNamespaceForResource(requestNamespace string, resource schema.GroupVersionResource) string {
80 if resource.Resource == "namespaces" && resource.Group == "" {
81 return ""
82 }
83 return requestNamespace
84 }