1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1
18
19import (
20	"k8s.io/apimachinery/pkg/runtime/schema"
21	"k8s.io/apimachinery/pkg/types"
22)
23
24// TODO: move this, Object, List, and Type to a different package
25type ObjectMetaAccessor interface {
26	GetObjectMeta() Object
27}
28
29// Object lets you work with object metadata from any of the versioned or
30// internal API objects. Attempting to set or retrieve a field on an object that does
31// not support that field (Name, UID, Namespace on lists) will be a no-op and return
32// a default value.
33type Object interface {
34	GetNamespace() string
35	SetNamespace(namespace string)
36	GetName() string
37	SetName(name string)
38	GetGenerateName() string
39	SetGenerateName(name string)
40	GetUID() types.UID
41	SetUID(uid types.UID)
42	GetResourceVersion() string
43	SetResourceVersion(version string)
44	GetGeneration() int64
45	SetGeneration(generation int64)
46	GetSelfLink() string
47	SetSelfLink(selfLink string)
48	GetCreationTimestamp() Time
49	SetCreationTimestamp(timestamp Time)
50	GetDeletionTimestamp() *Time
51	SetDeletionTimestamp(timestamp *Time)
52	GetDeletionGracePeriodSeconds() *int64
53	SetDeletionGracePeriodSeconds(*int64)
54	GetLabels() map[string]string
55	SetLabels(labels map[string]string)
56	GetAnnotations() map[string]string
57	SetAnnotations(annotations map[string]string)
58	GetInitializers() *Initializers
59	SetInitializers(initializers *Initializers)
60	GetFinalizers() []string
61	SetFinalizers(finalizers []string)
62	GetOwnerReferences() []OwnerReference
63	SetOwnerReferences([]OwnerReference)
64	GetClusterName() string
65	SetClusterName(clusterName string)
66}
67
68// ListMetaAccessor retrieves the list interface from an object
69type ListMetaAccessor interface {
70	GetListMeta() ListInterface
71}
72
73// Common lets you work with core metadata from any of the versioned or
74// internal API objects. Attempting to set or retrieve a field on an object that does
75// not support that field will be a no-op and return a default value.
76// TODO: move this, and TypeMeta and ListMeta, to a different package
77type Common interface {
78	GetResourceVersion() string
79	SetResourceVersion(version string)
80	GetSelfLink() string
81	SetSelfLink(selfLink string)
82}
83
84// ListInterface lets you work with list metadata from any of the versioned or
85// internal API objects. Attempting to set or retrieve a field on an object that does
86// not support that field will be a no-op and return a default value.
87// TODO: move this, and TypeMeta and ListMeta, to a different package
88type ListInterface interface {
89	GetResourceVersion() string
90	SetResourceVersion(version string)
91	GetSelfLink() string
92	SetSelfLink(selfLink string)
93	GetContinue() string
94	SetContinue(c string)
95}
96
97// Type exposes the type and APIVersion of versioned or internal API objects.
98// TODO: move this, and TypeMeta and ListMeta, to a different package
99type Type interface {
100	GetAPIVersion() string
101	SetAPIVersion(version string)
102	GetKind() string
103	SetKind(kind string)
104}
105
106func (meta *ListMeta) GetResourceVersion() string        { return meta.ResourceVersion }
107func (meta *ListMeta) SetResourceVersion(version string) { meta.ResourceVersion = version }
108func (meta *ListMeta) GetSelfLink() string               { return meta.SelfLink }
109func (meta *ListMeta) SetSelfLink(selfLink string)       { meta.SelfLink = selfLink }
110func (meta *ListMeta) GetContinue() string               { return meta.Continue }
111func (meta *ListMeta) SetContinue(c string)              { meta.Continue = c }
112
113func (obj *TypeMeta) GetObjectKind() schema.ObjectKind { return obj }
114
115// SetGroupVersionKind satisfies the ObjectKind interface for all objects that embed TypeMeta
116func (obj *TypeMeta) SetGroupVersionKind(gvk schema.GroupVersionKind) {
117	obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
118}
119
120// GroupVersionKind satisfies the ObjectKind interface for all objects that embed TypeMeta
121func (obj *TypeMeta) GroupVersionKind() schema.GroupVersionKind {
122	return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
123}
124
125func (obj *ListMeta) GetListMeta() ListInterface { return obj }
126
127func (obj *ObjectMeta) GetObjectMeta() Object { return obj }
128
129// Namespace implements metav1.Object for any object with an ObjectMeta typed field. Allows
130// fast, direct access to metadata fields for API objects.
131func (meta *ObjectMeta) GetNamespace() string                { return meta.Namespace }
132func (meta *ObjectMeta) SetNamespace(namespace string)       { meta.Namespace = namespace }
133func (meta *ObjectMeta) GetName() string                     { return meta.Name }
134func (meta *ObjectMeta) SetName(name string)                 { meta.Name = name }
135func (meta *ObjectMeta) GetGenerateName() string             { return meta.GenerateName }
136func (meta *ObjectMeta) SetGenerateName(generateName string) { meta.GenerateName = generateName }
137func (meta *ObjectMeta) GetUID() types.UID                   { return meta.UID }
138func (meta *ObjectMeta) SetUID(uid types.UID)                { meta.UID = uid }
139func (meta *ObjectMeta) GetResourceVersion() string          { return meta.ResourceVersion }
140func (meta *ObjectMeta) SetResourceVersion(version string)   { meta.ResourceVersion = version }
141func (meta *ObjectMeta) GetGeneration() int64                { return meta.Generation }
142func (meta *ObjectMeta) SetGeneration(generation int64)      { meta.Generation = generation }
143func (meta *ObjectMeta) GetSelfLink() string                 { return meta.SelfLink }
144func (meta *ObjectMeta) SetSelfLink(selfLink string)         { meta.SelfLink = selfLink }
145func (meta *ObjectMeta) GetCreationTimestamp() Time          { return meta.CreationTimestamp }
146func (meta *ObjectMeta) SetCreationTimestamp(creationTimestamp Time) {
147	meta.CreationTimestamp = creationTimestamp
148}
149func (meta *ObjectMeta) GetDeletionTimestamp() *Time { return meta.DeletionTimestamp }
150func (meta *ObjectMeta) SetDeletionTimestamp(deletionTimestamp *Time) {
151	meta.DeletionTimestamp = deletionTimestamp
152}
153func (meta *ObjectMeta) GetDeletionGracePeriodSeconds() *int64 { return meta.DeletionGracePeriodSeconds }
154func (meta *ObjectMeta) SetDeletionGracePeriodSeconds(deletionGracePeriodSeconds *int64) {
155	meta.DeletionGracePeriodSeconds = deletionGracePeriodSeconds
156}
157func (meta *ObjectMeta) GetLabels() map[string]string                 { return meta.Labels }
158func (meta *ObjectMeta) SetLabels(labels map[string]string)           { meta.Labels = labels }
159func (meta *ObjectMeta) GetAnnotations() map[string]string            { return meta.Annotations }
160func (meta *ObjectMeta) SetAnnotations(annotations map[string]string) { meta.Annotations = annotations }
161func (meta *ObjectMeta) GetInitializers() *Initializers               { return meta.Initializers }
162func (meta *ObjectMeta) SetInitializers(initializers *Initializers)   { meta.Initializers = initializers }
163func (meta *ObjectMeta) GetFinalizers() []string                      { return meta.Finalizers }
164func (meta *ObjectMeta) SetFinalizers(finalizers []string)            { meta.Finalizers = finalizers }
165
166func (meta *ObjectMeta) GetOwnerReferences() []OwnerReference {
167	if meta.OwnerReferences == nil {
168		return nil
169	}
170	ret := make([]OwnerReference, len(meta.OwnerReferences))
171	for i := 0; i < len(meta.OwnerReferences); i++ {
172		ret[i].Kind = meta.OwnerReferences[i].Kind
173		ret[i].Name = meta.OwnerReferences[i].Name
174		ret[i].UID = meta.OwnerReferences[i].UID
175		ret[i].APIVersion = meta.OwnerReferences[i].APIVersion
176		if meta.OwnerReferences[i].Controller != nil {
177			value := *meta.OwnerReferences[i].Controller
178			ret[i].Controller = &value
179		}
180		if meta.OwnerReferences[i].BlockOwnerDeletion != nil {
181			value := *meta.OwnerReferences[i].BlockOwnerDeletion
182			ret[i].BlockOwnerDeletion = &value
183		}
184	}
185	return ret
186}
187
188func (meta *ObjectMeta) SetOwnerReferences(references []OwnerReference) {
189	if references == nil {
190		meta.OwnerReferences = nil
191		return
192	}
193	newReferences := make([]OwnerReference, len(references))
194	for i := 0; i < len(references); i++ {
195		newReferences[i].Kind = references[i].Kind
196		newReferences[i].Name = references[i].Name
197		newReferences[i].UID = references[i].UID
198		newReferences[i].APIVersion = references[i].APIVersion
199		if references[i].Controller != nil {
200			value := *references[i].Controller
201			newReferences[i].Controller = &value
202		}
203		if references[i].BlockOwnerDeletion != nil {
204			value := *references[i].BlockOwnerDeletion
205			newReferences[i].BlockOwnerDeletion = &value
206		}
207	}
208	meta.OwnerReferences = newReferences
209}
210
211func (meta *ObjectMeta) GetClusterName() string {
212	return meta.ClusterName
213}
214func (meta *ObjectMeta) SetClusterName(clusterName string) {
215	meta.ClusterName = clusterName
216}
217