meta.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package api
  14. import (
  15. "k8s.io/kubernetes/pkg/api/meta"
  16. "k8s.io/kubernetes/pkg/api/meta/metatypes"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/conversion"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. "k8s.io/kubernetes/pkg/types"
  21. "k8s.io/kubernetes/pkg/util/uuid"
  22. )
  23. // FillObjectMetaSystemFields populates fields that are managed by the system on ObjectMeta.
  24. func FillObjectMetaSystemFields(ctx Context, meta *ObjectMeta) {
  25. meta.CreationTimestamp = unversioned.Now()
  26. // allows admission controllers to assign a UID earlier in the request processing
  27. // to support tracking resources pending creation.
  28. uid, found := UIDFrom(ctx)
  29. if !found {
  30. uid = uuid.NewUUID()
  31. }
  32. meta.UID = uid
  33. meta.SelfLink = ""
  34. }
  35. // HasObjectMetaSystemFieldValues returns true if fields that are managed by the system on ObjectMeta have values.
  36. func HasObjectMetaSystemFieldValues(meta *ObjectMeta) bool {
  37. return !meta.CreationTimestamp.Time.IsZero() ||
  38. len(meta.UID) != 0
  39. }
  40. // ObjectMetaFor returns a pointer to a provided object's ObjectMeta.
  41. // TODO: allow runtime.Unknown to extract this object
  42. // TODO: Remove this function and use meta.Accessor() instead.
  43. func ObjectMetaFor(obj runtime.Object) (*ObjectMeta, error) {
  44. v, err := conversion.EnforcePtr(obj)
  45. if err != nil {
  46. return nil, err
  47. }
  48. var meta *ObjectMeta
  49. err = runtime.FieldPtr(v, "ObjectMeta", &meta)
  50. return meta, err
  51. }
  52. // ListMetaFor returns a pointer to a provided object's ListMeta,
  53. // or an error if the object does not have that pointer.
  54. // TODO: allow runtime.Unknown to extract this object
  55. func ListMetaFor(obj runtime.Object) (*unversioned.ListMeta, error) {
  56. v, err := conversion.EnforcePtr(obj)
  57. if err != nil {
  58. return nil, err
  59. }
  60. var meta *unversioned.ListMeta
  61. err = runtime.FieldPtr(v, "ListMeta", &meta)
  62. return meta, err
  63. }
  64. func (obj *ObjectMeta) GetObjectMeta() meta.Object { return obj }
  65. // Namespace implements meta.Object for any object with an ObjectMeta typed field. Allows
  66. // fast, direct access to metadata fields for API objects.
  67. func (meta *ObjectMeta) GetNamespace() string { return meta.Namespace }
  68. func (meta *ObjectMeta) SetNamespace(namespace string) { meta.Namespace = namespace }
  69. func (meta *ObjectMeta) GetName() string { return meta.Name }
  70. func (meta *ObjectMeta) SetName(name string) { meta.Name = name }
  71. func (meta *ObjectMeta) GetGenerateName() string { return meta.GenerateName }
  72. func (meta *ObjectMeta) SetGenerateName(generateName string) { meta.GenerateName = generateName }
  73. func (meta *ObjectMeta) GetUID() types.UID { return meta.UID }
  74. func (meta *ObjectMeta) SetUID(uid types.UID) { meta.UID = uid }
  75. func (meta *ObjectMeta) GetResourceVersion() string { return meta.ResourceVersion }
  76. func (meta *ObjectMeta) SetResourceVersion(version string) { meta.ResourceVersion = version }
  77. func (meta *ObjectMeta) GetSelfLink() string { return meta.SelfLink }
  78. func (meta *ObjectMeta) SetSelfLink(selfLink string) { meta.SelfLink = selfLink }
  79. func (meta *ObjectMeta) GetCreationTimestamp() unversioned.Time { return meta.CreationTimestamp }
  80. func (meta *ObjectMeta) SetCreationTimestamp(creationTimestamp unversioned.Time) {
  81. meta.CreationTimestamp = creationTimestamp
  82. }
  83. func (meta *ObjectMeta) GetDeletionTimestamp() *unversioned.Time { return meta.DeletionTimestamp }
  84. func (meta *ObjectMeta) SetDeletionTimestamp(deletionTimestamp *unversioned.Time) {
  85. meta.DeletionTimestamp = deletionTimestamp
  86. }
  87. func (meta *ObjectMeta) GetLabels() map[string]string { return meta.Labels }
  88. func (meta *ObjectMeta) SetLabels(labels map[string]string) { meta.Labels = labels }
  89. func (meta *ObjectMeta) GetAnnotations() map[string]string { return meta.Annotations }
  90. func (meta *ObjectMeta) SetAnnotations(annotations map[string]string) { meta.Annotations = annotations }
  91. func (meta *ObjectMeta) GetFinalizers() []string { return meta.Finalizers }
  92. func (meta *ObjectMeta) SetFinalizers(finalizers []string) { meta.Finalizers = finalizers }
  93. func (meta *ObjectMeta) GetOwnerReferences() []metatypes.OwnerReference {
  94. ret := make([]metatypes.OwnerReference, len(meta.OwnerReferences))
  95. for i := 0; i < len(meta.OwnerReferences); i++ {
  96. ret[i].Kind = meta.OwnerReferences[i].Kind
  97. ret[i].Name = meta.OwnerReferences[i].Name
  98. ret[i].UID = meta.OwnerReferences[i].UID
  99. ret[i].APIVersion = meta.OwnerReferences[i].APIVersion
  100. if meta.OwnerReferences[i].Controller != nil {
  101. value := *meta.OwnerReferences[i].Controller
  102. ret[i].Controller = &value
  103. }
  104. }
  105. return ret
  106. }
  107. func (meta *ObjectMeta) SetOwnerReferences(references []metatypes.OwnerReference) {
  108. newReferences := make([]OwnerReference, len(references))
  109. for i := 0; i < len(references); i++ {
  110. newReferences[i].Kind = references[i].Kind
  111. newReferences[i].Name = references[i].Name
  112. newReferences[i].UID = references[i].UID
  113. newReferences[i].APIVersion = references[i].APIVersion
  114. if references[i].Controller != nil {
  115. value := *references[i].Controller
  116. newReferences[i].Controller = &value
  117. }
  118. }
  119. meta.OwnerReferences = newReferences
  120. }
  121. func (meta *ObjectMeta) GetClusterName() string {
  122. return meta.ClusterName
  123. }
  124. func (meta *ObjectMeta) SetClusterName(clusterName string) {
  125. meta.ClusterName = clusterName
  126. }