k8s.io/apiserver@v0.31.1/pkg/registry/rest/meta.go (about)

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