-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
190 lines (170 loc) · 7.9 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package common
import (
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
)
// ObjectMeta is metadata about an instance of a resource.
type ObjectMeta struct {
// Name is unique within a namespace. Name is primarily intended for creation
// idempotence and configuration definition.
Name string `json:"name,omitempty"`
// Namespace defines the space within which name must be unique. An empty namespace is
// equivalent to the "default" namespace, but "default" is the canonical representation.
// Not all objects are required to be scoped to a namespace - the value of this field for
// those objects will be empty.
Namespace string `json:"namespace,omitempty"`
// Labels are key value pairs that may be used to scope and select individual resources.
// Label keys are of the form:
// label-key ::= prefixed-name | name
// prefixed-name ::= prefix '/' name
// prefix ::= DNS_SUBDOMAIN
// name ::= DNS_LABEL
// The prefix is optional. If the prefix is not specified, the key is assumed to be private
// to the user. Other system components that wish to use labels must specify a prefix.
Labels map[string]string `json:"labels,omitempty"`
// Annotations are unstructured key value data stored with a resource that may be set by
// external tooling. They are not queryable and should be preserved when modifying
// objects. Annotation keys have the same formatting restrictions as Label keys. See the
// comments on Labels for details.
Annotations map[string]string `json:"annotations,omitempty"`
// CreationTimestamp is a timestamp representing the server time when this object was
// created. It is not guaranteed to be set in happens-before order across separate operations.
// Clients may not set this value. It is represented in RFC3339 form and is in UTC.
CreationTimestamp unversioned.Time `json:"creationTimestamp,omitempty"`
}
// TypeMeta describes an individual object in an API response or request with strings representing
// the type of the object.
type TypeMeta struct {
// Kind is a string value representing the REST resource this object represents.
// Servers may infer this from the endpoint the client submits requests to.
// In smalllettercase.
// More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#types-kinds
Kind ResourceKind `json:"kind,omitempty"`
}
// ListMeta describes list of objects, i.e. holds information about pagination options set for the
// list.
type ListMeta struct {
// Total number of items on the list. Used for pagination.
TotalItems int `json:"totalItems"`
}
// NewObjectMeta returns internal endpoint name for the given service properties, e.g.,
// NewObjectMeta creates a new instance of ObjectMeta struct based on K8s object meta.
func NewObjectMeta(k8SObjectMeta api.ObjectMeta) ObjectMeta {
return ObjectMeta{
Name: k8SObjectMeta.Name,
Namespace: k8SObjectMeta.Namespace,
Labels: k8SObjectMeta.Labels,
CreationTimestamp: k8SObjectMeta.CreationTimestamp,
Annotations: k8SObjectMeta.Annotations,
}
}
// NewTypeMeta creates new type mete for the resource kind.
func NewTypeMeta(kind ResourceKind) TypeMeta {
return TypeMeta{
Kind: kind,
}
}
// ResourceKind is an unique name for each resource. It can used for API discovery and generic
// code that does things based on the kind. For example, there may be a generic "deleter"
// that based on resource kind, name and namespace deletes it.
type ResourceKind string
// List of all resource kinds supported by the UI.
const (
ResourceKindReplicaSet = "replicaset"
ResourceKindService = "service"
ResourceKindIngress = "ingress"
ResourceKindDeployment = "deployment"
ResourceKindPod = "pod"
ResourceKindEvent = "event"
ResourceKindReplicationController = "replicationcontroller"
ResourceKindDaemonSet = "daemonset"
ResourceKindJob = "job"
ResourceKindPetSet = "petset"
ResourceKindNamespace = "namespace"
ResourceKindNode = "node"
ResourceKindSecret = "secret"
ResourceKindConfigMap = "configmap"
ResourceKindPersistentVolume = "persistentvolume"
ResourceKindPersistentVolumeClaim = "persistentvolumeclaim"
)
// ClientType represents type of client that is used to perform generic operations on resources.
// Different resources belong to different client, i.e. Deployments belongs to extension client
// and PetSets to apps client.
type ClientType string
// List of client types supported by the UI.
const (
ClientTypeDefault = "restclient"
ClientTypeExtensionClient = "extensionclient"
ClientTypeAppsClient = "appsclient"
ClientTypeBatchClient = "batchclient"
)
// Mapping from resource kind to K8s apiserver API path. This is mostly pluralization, because
// K8s apiserver uses plural paths and this project singular.
// Must be kept in sync with the list of supported kinds.
var kindToAPIMapping = map[string]struct {
// K8s resource name
Resource string
// Client type used by given resource, i.e. deployments are using extension client and pet
// sets apps client.
ClientType ClientType
}{
ResourceKindService: {"services", ClientTypeDefault},
ResourceKindIngress: {"ingresses", ClientTypeExtensionClient},
ResourceKindPod: {"pods", ClientTypeDefault},
ResourceKindEvent: {"events", ClientTypeDefault},
ResourceKindReplicationController: {"replicationcontrollers", ClientTypeDefault},
ResourceKindDeployment: {"deployments", ClientTypeExtensionClient},
ResourceKindReplicaSet: {"replicasets", ClientTypeExtensionClient},
ResourceKindDaemonSet: {"daemonsets", ClientTypeExtensionClient},
ResourceKindPetSet: {"petsets", ClientTypeAppsClient},
ResourceKindJob: {"jobs", ClientTypeBatchClient},
ResourceKindNamespace: {"namespaces", ClientTypeDefault},
ResourceKindNode: {"nodes", ClientTypeDefault},
ResourceKindSecret: {"secrets", ClientTypeDefault},
ResourceKindConfigMap: {"configmaps", ClientTypeDefault},
ResourceKindPersistentVolume: {"persistentvolumes", ClientTypeDefault},
}
// IsSelectorMatching returns true when an object with the given
// selector targets the same Resources (or subset) that
// the tested object with the given selector.
func IsSelectorMatching(labelSelector map[string]string,
testedObjectLabels map[string]string) bool {
// If service has no selectors, then assume it targets different Resource.
if len(labelSelector) == 0 {
return false
}
for label, value := range labelSelector {
if rsValue, ok := testedObjectLabels[label]; !ok || rsValue != value {
return false
}
}
return true
}
// IsLabelSelectorMatching returns true when a resource with the given selector targets the same
// Resources(or subset) that a tested object selector with the given selector.
func IsLabelSelectorMatching(selector map[string]string,
labelSelector *unversioned.LabelSelector) bool {
// If the resource has no selectors, then assume it targets different Pods.
if len(selector) == 0 {
return false
}
for label, value := range selector {
if rsValue, ok := labelSelector.MatchLabels[label]; !ok || rsValue != value {
return false
}
}
return true
}