Skip to content

Commit 4104146

Browse files
committed
Introduce ObjectMeta and TypeMeta information to presentation models (#729)
With this we'll be able to do generic stuff, e.g., deletion or YAML edition.
1 parent 56308f8 commit 4104146

File tree

34 files changed

+262
-218
lines changed

34 files changed

+262
-218
lines changed

src/app/backend/resource/common/types.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,76 @@ package common
1616

1717
import (
1818
"bytes"
19+
1920
"k8s.io/kubernetes/pkg/api"
21+
"k8s.io/kubernetes/pkg/api/unversioned"
2022
)
2123

24+
type ResourceType string
25+
26+
const (
27+
ResourceTypeReplicaSet = "replicaset"
28+
ResourceTypeReplicationController = "replicationcontroller"
29+
)
30+
31+
func CreateObjectMeta(k8SObjectMeta api.ObjectMeta) ObjectMeta {
32+
return ObjectMeta{
33+
Name: k8SObjectMeta.Name,
34+
Namespace: k8SObjectMeta.Namespace,
35+
Labels: k8SObjectMeta.Labels,
36+
CreationTimestamp: k8SObjectMeta.CreationTimestamp,
37+
}
38+
}
39+
40+
func CreateTypeMeta(k8STypeMeta unversioned.TypeMeta) TypeMeta {
41+
return TypeMeta{
42+
Kind: k8STypeMeta.Kind,
43+
}
44+
}
45+
46+
type ObjectMeta struct {
47+
// Name is unique within a namespace. Name is primarily intended for creation
48+
// idempotence and configuration definition.
49+
Name string `json:"name,omitempty"`
50+
51+
// Namespace defines the space within which name must be unique. An empty namespace is
52+
// equivalent to the "default" namespace, but "default" is the canonical representation.
53+
// Not all objects are required to be scoped to a namespace - the value of this field for
54+
// those objects will be empty.
55+
Namespace string `json:"namespace,omitempty"`
56+
57+
// Labels are key value pairs that may be used to scope and select individual resources.
58+
// Label keys are of the form:
59+
// label-key ::= prefixed-name | name
60+
// prefixed-name ::= prefix '/' name
61+
// prefix ::= DNS_SUBDOMAIN
62+
// name ::= DNS_LABEL
63+
// The prefix is optional. If the prefix is not specified, the key is assumed to be private
64+
// to the user. Other system components that wish to use labels must specify a prefix.
65+
Labels map[string]string `json:"labels,omitempty"`
66+
67+
// Annotations are unstructured key value data stored with a resource that may be set by
68+
// external tooling. They are not queryable and should be preserved when modifying
69+
// objects. Annotation keys have the same formatting restrictions as Label keys. See the
70+
// comments on Labels for details.
71+
Annotations map[string]string `json:"annotations,omitempty"`
72+
73+
// CreationTimestamp is a timestamp representing the server time when this object was
74+
// created. It is not guaranteed to be set in happens-before order across separate operations.
75+
// Clients may not set this value. It is represented in RFC3339 form and is in UTC.
76+
CreationTimestamp unversioned.Time `json:"creationTimestamp,omitempty"`
77+
}
78+
79+
// TypeMeta describes an individual object in an API response or request
80+
// with strings representing the type of the object.
81+
type TypeMeta struct {
82+
// Kind is a string value representing the REST resource this object represents.
83+
// Servers may infer this from the endpoint the client submits requests to.
84+
// In smalllettercase.
85+
// More info: http://releases.k8s.io/HEAD/docs/devel/api-conventions.md#types-kinds
86+
Kind string `json:"kind,omitempty"`
87+
}
88+
2289
// Endpoint describes an endpoint that is host and a list of available ports for that host.
2390
type Endpoint struct {
2491
// Hostname, either as a domain name or IP address.

src/app/backend/resource/deployment/deploymentlist.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/kubernetes/dashboard/resource/replicationcontroller"
2222
"k8s.io/kubernetes/pkg/api"
2323
k8serrors "k8s.io/kubernetes/pkg/api/errors"
24-
"k8s.io/kubernetes/pkg/api/unversioned"
2524
"k8s.io/kubernetes/pkg/apis/extensions"
2625
client "k8s.io/kubernetes/pkg/client/unversioned"
2726
)
@@ -36,28 +35,19 @@ type DeploymentList struct {
3635
// it is Deployment plus additional augumented data we can get from other sources
3736
// (like services that target the same pods).
3837
type Deployment struct {
39-
// Name of the Deployment.
40-
Name string `json:"name"`
41-
42-
// Namespace this Deployment is in.
43-
Namespace string `json:"namespace"`
44-
45-
// Label of this Deployment.
46-
Labels map[string]string `json:"labels"`
38+
ObjectMeta common.ObjectMeta `json:"objectMeta"`
39+
TypeMeta common.TypeMeta `json:"typeMeta"`
4740

4841
// Aggregate information about pods belonging to this Deployment.
4942
Pods common.PodInfo `json:"pods"`
5043

5144
// Container images of the Deployment.
5245
ContainerImages []string `json:"containerImages"`
53-
54-
// Time the replication controller was created.
55-
CreationTime unversioned.Time `json:"creationTime"`
5646
}
5747

5848
// GetDeploymentList returns a list of all Deployments in the cluster.
5949
func GetDeploymentList(client client.Interface) (*DeploymentList, error) {
60-
log.Printf("Getting list of all replica sets in the cluster")
50+
log.Printf("Getting list of all deployments in the cluster")
6151

6252
channels := &common.ResourceChannels{
6353
DeploymentList: common.GetDeploymentListChannel(client.Extensions(), 1),
@@ -126,12 +116,10 @@ func getDeploymentList(deployments []extensions.Deployment,
126116

127117
deploymentList.Deployments = append(deploymentList.Deployments,
128118
Deployment{
129-
Name: deployment.ObjectMeta.Name,
130-
Namespace: deployment.ObjectMeta.Namespace,
131-
Labels: deployment.ObjectMeta.Labels,
119+
ObjectMeta: common.CreateObjectMeta(deployment.ObjectMeta),
120+
TypeMeta: common.CreateTypeMeta(deployment.TypeMeta),
132121
ContainerImages: replicationcontroller.GetContainerImages(&deployment.Spec.Template.Spec),
133122
Pods: podInfo,
134-
CreationTime: deployment.ObjectMeta.CreationTimestamp,
135123
})
136124
}
137125

src/app/backend/resource/pod/podlist.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/kubernetes/dashboard/client"
2121
"github.com/kubernetes/dashboard/resource/common"
2222
"k8s.io/kubernetes/pkg/api"
23-
"k8s.io/kubernetes/pkg/api/unversioned"
2423
k8sClient "k8s.io/kubernetes/pkg/client/unversioned"
2524
)
2625

@@ -34,21 +33,12 @@ type PodList struct {
3433
// it is Pod plus additional augumented data we can get from other sources
3534
// (like services that target it).
3635
type Pod struct {
37-
// Name of the Pod.
38-
Name string `json:"name"`
39-
40-
// Namespace this Pod is in.
41-
Namespace string `json:"namespace"`
42-
43-
// Label of this Pod.
44-
Labels map[string]string `json:"labels"`
36+
ObjectMeta common.ObjectMeta `json:"objectMeta"`
37+
TypeMeta common.TypeMeta `json:"typeMeta"`
4538

4639
// Container images of the Pod.
4740
ContainerImages []string `json:"containerImages"`
4841

49-
// Time the replication controller was created.
50-
CreationTime unversioned.Time `json:"creationTime"`
51-
5242
// Status of the Pod. See Kubernetes API for reference.
5343
PodPhase api.PodPhase `json:"podPhase"`
5444

@@ -102,11 +92,10 @@ func CreatePodList(pods []api.Pod, heapsterClient client.HeapsterClient) PodList
10292

10393
for _, pod := range pods {
10494
podDetail := Pod{
105-
Name: pod.Name,
95+
ObjectMeta: common.CreateObjectMeta(pod.ObjectMeta),
96+
TypeMeta: common.CreateTypeMeta(pod.TypeMeta),
10697
PodPhase: pod.Status.Phase,
107-
CreationTime: pod.ObjectMeta.CreationTimestamp,
10898
PodIP: pod.Status.PodIP,
109-
NodeName: pod.Spec.NodeName,
11099
RestartCount: getRestartCount(pod),
111100
}
112101
if metrics != nil && metrics.MetricsMap[pod.Namespace] != nil {

src/app/backend/resource/replicaset/replicasetlist.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/kubernetes/dashboard/resource/replicationcontroller"
2222
"k8s.io/kubernetes/pkg/api"
2323
k8serrors "k8s.io/kubernetes/pkg/api/errors"
24-
"k8s.io/kubernetes/pkg/api/unversioned"
2524
"k8s.io/kubernetes/pkg/apis/extensions"
2625
client "k8s.io/kubernetes/pkg/client/unversioned"
2726
)
@@ -36,23 +35,14 @@ type ReplicaSetList struct {
3635
// it is Replica Set plus additional augumented data we can get from other sources
3736
// (like services that target the same pods).
3837
type ReplicaSet struct {
39-
// Name of the Replica Set.
40-
Name string `json:"name"`
41-
42-
// Namespace this Replica Set is in.
43-
Namespace string `json:"namespace"`
44-
45-
// Label of this Replica Set.
46-
Labels map[string]string `json:"labels"`
38+
ObjectMeta common.ObjectMeta `json:"objectMeta"`
39+
TypeMeta common.TypeMeta `json:"typeMeta"`
4740

4841
// Aggregate information about pods belonging to this Replica Set.
4942
Pods common.PodInfo `json:"pods"`
5043

5144
// Container images of the Replica Set.
5245
ContainerImages []string `json:"containerImages"`
53-
54-
// Time the replication controller was created.
55-
CreationTime unversioned.Time `json:"creationTime"`
5646
}
5747

5848
// GetReplicaSetList returns a list of all Replica Sets in the cluster.
@@ -126,12 +116,10 @@ func getReplicaSetList(replicaSets []extensions.ReplicaSet,
126116

127117
replicaSetList.ReplicaSets = append(replicaSetList.ReplicaSets,
128118
ReplicaSet{
129-
Name: replicaSet.ObjectMeta.Name,
130-
Namespace: replicaSet.ObjectMeta.Namespace,
131-
Labels: replicaSet.ObjectMeta.Labels,
119+
ObjectMeta: common.CreateObjectMeta(replicaSet.ObjectMeta),
120+
TypeMeta: common.CreateTypeMeta(replicaSet.TypeMeta),
132121
ContainerImages: replicationcontroller.GetContainerImages(&replicaSet.Spec.Template.Spec),
133122
Pods: podInfo,
134-
CreationTime: replicaSet.ObjectMeta.CreationTimestamp,
135123
})
136124
}
137125

src/app/backend/resource/replicationcontroller/replicationcontrollerdetail.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,8 @@ import (
2929

3030
// ReplicationControllerDetail represents detailed information about a Replication Controller.
3131
type ReplicationControllerDetail struct {
32-
// Name of the Replication Controller.
33-
Name string `json:"name"`
34-
35-
// Namespace the Replication Controller is in.
36-
Namespace string `json:"namespace"`
37-
38-
// Label mapping of the Replication Controller.
39-
Labels map[string]string `json:"labels"`
32+
ObjectMeta common.ObjectMeta `json:"objectMeta"`
33+
TypeMeta common.TypeMeta `json:"typeMeta"`
4034

4135
// Label selector of the Replication Controller.
4236
LabelSelector map[string]string `json:"labelSelector"`
@@ -91,9 +85,8 @@ func GetReplicationControllerDetail(client k8sClient.Interface, heapsterClient c
9185
}
9286

9387
replicationControllerDetail := &ReplicationControllerDetail{
94-
Name: replicationController.Name,
95-
Namespace: replicationController.Namespace,
96-
Labels: replicationController.ObjectMeta.Labels,
88+
ObjectMeta: common.CreateObjectMeta(replicationController.ObjectMeta),
89+
TypeMeta: common.CreateTypeMeta(replicationController.TypeMeta),
9790
LabelSelector: replicationController.Spec.Selector,
9891
PodInfo: getReplicationPodInfo(replicationController, pods.Items),
9992
}
@@ -102,7 +95,7 @@ func GetReplicationControllerDetail(client k8sClient.Interface, heapsterClient c
10295

10396
for _, service := range matchingServices {
10497
replicationControllerDetail.Services = append(replicationControllerDetail.Services,
105-
getServiceDetail(service, *replicationController, pods.Items, nodes.Items))
98+
getService(service, *replicationController, pods.Items, nodes.Items))
10699
}
107100

108101
for _, container := range replicationController.Spec.Template.Spec.Containers {
@@ -208,10 +201,11 @@ func UpdateReplicasCount(client k8sClient.Interface, namespace, name string,
208201
}
209202

210203
// Returns detailed information about service from given service
211-
func getServiceDetail(service api.Service, replicationController api.ReplicationController,
204+
func getService(service api.Service, replicationController api.ReplicationController,
212205
pods []api.Pod, nodes []api.Node) resourceService.Service {
213206
return resourceService.Service{
214-
Name: service.ObjectMeta.Name,
207+
ObjectMeta: common.CreateObjectMeta(service.ObjectMeta),
208+
TypeMeta: common.CreateTypeMeta(service.TypeMeta),
215209
InternalEndpoint: common.GetInternalEndpoint(service.Name, service.Namespace,
216210
service.Spec.Ports),
217211
ExternalEndpoints: getExternalEndpoints(replicationController, pods, service, nodes),

src/app/backend/resource/replicationcontroller/replicationcontrollerlist.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import (
1818
"log"
1919

2020
"github.com/kubernetes/dashboard/resource/common"
21-
// TODO(maciaszczykm): Avoid using dot-imports.
22-
. "github.com/kubernetes/dashboard/resource/event"
21+
"github.com/kubernetes/dashboard/resource/event"
2322
"k8s.io/kubernetes/pkg/api"
24-
"k8s.io/kubernetes/pkg/api/unversioned"
2523
client "k8s.io/kubernetes/pkg/client/unversioned"
2624
)
2725

@@ -34,27 +32,15 @@ type ReplicationControllerList struct {
3432
// ReplicationController (aka. Replication Controller) plus zero or more Kubernetes services that
3533
// target the Replication Controller.
3634
type ReplicationController struct {
37-
// Name of the Replication Controller.
38-
Name string `json:"name"`
39-
40-
// Namespace this Replication Controller is in.
41-
Namespace string `json:"namespace"`
42-
43-
// Human readable description of this Replication Controller.
44-
Description string `json:"description"`
45-
46-
// Label of this Replication Controller.
47-
Labels map[string]string `json:"labels"`
35+
ObjectMeta common.ObjectMeta `json:"objectMeta"`
36+
TypeMeta common.TypeMeta `json:"typeMeta"`
4837

4938
// Aggregate information about pods belonging to this Replication Controller.
5039
Pods common.PodInfo `json:"pods"`
5140

5241
// Container images of the Replication Controller.
5342
ContainerImages []string `json:"containerImages"`
5443

55-
// Time the replication controller was created.
56-
CreationTime unversioned.Time `json:"creationTime"`
57-
5844
// Internal endpoints of all Kubernetes services have the same label selector as this Replication Controller.
5945
InternalEndpoints []common.Endpoint `json:"internalEndpoints"`
6046

@@ -143,19 +129,16 @@ func getReplicationControllerList(replicationControllers []api.ReplicationContro
143129
}
144130
}
145131
podInfo := getReplicationPodInfo(&replicationController, matchingPods)
146-
podErrors := GetPodsEventWarnings(events, matchingPods)
132+
podErrors := event.GetPodsEventWarnings(events, matchingPods)
147133

148134
podInfo.Warnings = podErrors
149135

150136
replicationControllerList.ReplicationControllers = append(replicationControllerList.ReplicationControllers,
151137
ReplicationController{
152-
Name: replicationController.ObjectMeta.Name,
153-
Namespace: replicationController.ObjectMeta.Namespace,
154-
Description: replicationController.Annotations[DescriptionAnnotationKey],
155-
Labels: replicationController.ObjectMeta.Labels,
138+
ObjectMeta: common.CreateObjectMeta(replicationController.ObjectMeta),
139+
TypeMeta: common.CreateTypeMeta(replicationController.TypeMeta),
156140
Pods: podInfo,
157141
ContainerImages: GetContainerImages(&replicationController.Spec.Template.Spec),
158-
CreationTime: replicationController.ObjectMeta.CreationTimestamp,
159142
InternalEndpoints: internalEndpoints,
160143
ExternalEndpoints: externalEndpoints,
161144
})

0 commit comments

Comments
 (0)