Skip to content

Commit 8a6eb0d

Browse files
committed
Merge pull request #738 from floreks/refactor-backend-common
Refactor backend and extract globally used methods to common packages
2 parents 367904b + f7365d2 commit 8a6eb0d

23 files changed

+380
-281
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
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+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package common
16+
17+
import (
18+
"k8s.io/kubernetes/pkg/api/unversioned"
19+
)
20+
21+
// Events response structure.
22+
type EventList struct {
23+
// Namespace.
24+
Namespace string `json:"namespace"`
25+
26+
// List of events from given namespace.
27+
Events []Event `json:"events"`
28+
}
29+
30+
// Event is a single event representation.
31+
type Event struct {
32+
// A human-readable description of the status of related object.
33+
Message string `json:"message"`
34+
35+
// Component from which the event is generated.
36+
SourceComponent string `json:"sourceComponent"`
37+
38+
// Host name on which the event is generated.
39+
SourceHost string `json:"sourceHost"`
40+
41+
// Reference to a piece of an object, which triggered an event. For example
42+
// "spec.containers{name}" refers to container within pod with given name, if no container
43+
// name is specified, for example "spec.containers[2]", then it refers to container with
44+
// index 2 in this pod.
45+
SubObject string `json:"object"`
46+
47+
// The number of times this event has occurred.
48+
Count int `json:"count"`
49+
50+
// The time at which the event was first recorded.
51+
FirstSeen unversioned.Time `json:"firstSeen"`
52+
53+
// The time at which the most recent occurrence of this event was recorded.
54+
LastSeen unversioned.Time `json:"lastSeen"`
55+
56+
// Short, machine understandable string that gives the reason
57+
// for this event being generated.
58+
Reason string `json:"reason"`
59+
60+
// Event type (at the moment only normal and warning are supported).
61+
Type string `json:"type"`
62+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
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+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package common
16+
17+
import (
18+
"k8s.io/kubernetes/pkg/api"
19+
)
20+
21+
func FilterNamespacedPodsBySelector(pods []api.Pod, namespace string,
22+
resourceSelector map[string]string) []api.Pod {
23+
24+
var matchingPods []api.Pod
25+
for _, pod := range pods {
26+
if pod.ObjectMeta.Namespace == namespace &&
27+
IsLabelSelectorMatching(resourceSelector, pod.Labels) {
28+
matchingPods = append(matchingPods, pod)
29+
}
30+
}
31+
32+
return matchingPods
33+
}
34+
35+
// Returns pods targeted by given selector.
36+
func FilterPodsBySelector(pods []api.Pod, resourceSelector map[string]string) []api.Pod {
37+
38+
var matchingPods []api.Pod
39+
for _, pod := range pods {
40+
if IsLabelSelectorMatching(resourceSelector, pod.Labels) {
41+
matchingPods = append(matchingPods, pod)
42+
}
43+
}
44+
return matchingPods
45+
}
46+
47+
// GetContainerImages returns container image strings from the given pod spec.
48+
func GetContainerImages(podTemplate *api.PodSpec) []string {
49+
var containerImages []string
50+
for _, container := range podTemplate.Containers {
51+
containerImages = append(containerImages, container.Image)
52+
}
53+
return containerImages
54+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2015 Google Inc. All Rights Reserved.
2+
//
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+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package common
16+
17+
import (
18+
"k8s.io/kubernetes/pkg/api"
19+
)
20+
21+
// ServicePort is a pair of port and protocol, e.g. a service endpoint.
22+
type ServicePort struct {
23+
// Positive port number.
24+
Port int `json:"port"`
25+
26+
// Protocol name, e.g., TCP or UDP.
27+
Protocol api.Protocol `json:"protocol"`
28+
}
29+
30+
// GetServicePorts returns human readable name for the given service ports list.
31+
func GetServicePorts(apiPorts []api.ServicePort) []ServicePort {
32+
var ports []ServicePort
33+
for _, port := range apiPorts {
34+
ports = append(ports, ServicePort{port.Port, port.Protocol})
35+
}
36+
return ports
37+
}

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

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -63,58 +63,6 @@ type TypeMeta struct {
6363
Kind ResourceKind `json:"kind,omitempty"`
6464
}
6565

66-
// ServicePort is a pair of port and protocol, e.g. a service endpoint.
67-
type ServicePort struct {
68-
// Positive port number.
69-
Port int `json:"port"`
70-
71-
// Protocol name, e.g., TCP or UDP.
72-
Protocol api.Protocol `json:"protocol"`
73-
}
74-
75-
// Events response structure.
76-
type EventList struct {
77-
// Namespace.
78-
Namespace string `json:"namespace"`
79-
80-
// List of events from given namespace.
81-
Events []Event `json:"events"`
82-
}
83-
84-
// Event is a single event representation.
85-
type Event struct {
86-
// A human-readable description of the status of related object.
87-
Message string `json:"message"`
88-
89-
// Component from which the event is generated.
90-
SourceComponent string `json:"sourceComponent"`
91-
92-
// Host name on which the event is generated.
93-
SourceHost string `json:"sourceHost"`
94-
95-
// Reference to a piece of an object, which triggered an event. For example
96-
// "spec.containers{name}" refers to container within pod with given name, if no container
97-
// name is specified, for example "spec.containers[2]", then it refers to container with
98-
// index 2 in this pod.
99-
SubObject string `json:"object"`
100-
101-
// The number of times this event has occurred.
102-
Count int `json:"count"`
103-
104-
// The time at which the event was first recorded.
105-
FirstSeen unversioned.Time `json:"firstSeen"`
106-
107-
// The time at which the most recent occurrence of this event was recorded.
108-
LastSeen unversioned.Time `json:"lastSeen"`
109-
110-
// Short, machine understandable string that gives the reason
111-
// for this event being generated.
112-
Reason string `json:"reason"`
113-
114-
// Event type (at the moment only normal and warning are supported).
115-
Type string `json:"type"`
116-
}
117-
11866
// Returns internal endpoint name for the given service properties, e.g.,
11967
// NewObjectMeta creates a new instance of ObjectMeta struct based on K8s object meta.
12068
func NewObjectMeta(k8SObjectMeta api.ObjectMeta) ObjectMeta {
@@ -158,15 +106,6 @@ var kindToAPIPathMapping = map[string]string{
158106
ResourceKindReplicaSet: "replicasets",
159107
}
160108

161-
// GetServicePorts returns human readable name for the given service ports list.
162-
func GetServicePorts(apiPorts []api.ServicePort) []ServicePort {
163-
var ports []ServicePort
164-
for _, port := range apiPorts {
165-
ports = append(ports, ServicePort{port.Port, port.Protocol})
166-
}
167-
return ports
168-
}
169-
170109
// IsLabelSelectorMatching returns true when an object with the given
171110
// selector targets the same Resources (or subset) that
172111
// the tested object with the given selector.
@@ -186,29 +125,3 @@ func IsLabelSelectorMatching(labelSelector map[string]string,
186125

187126
return true
188127
}
189-
190-
func FilterNamespacedPodsBySelector(pods []api.Pod, namespace string,
191-
resourceSelector map[string]string) []api.Pod {
192-
193-
var matchingPods []api.Pod
194-
for _, pod := range pods {
195-
if pod.ObjectMeta.Namespace == namespace &&
196-
IsLabelSelectorMatching(resourceSelector, pod.Labels) {
197-
matchingPods = append(matchingPods, pod)
198-
}
199-
}
200-
201-
return matchingPods
202-
}
203-
204-
// Returns pods targeted by given selector.
205-
func FilterPodsBySelector(pods []api.Pod, resourceSelector map[string]string) []api.Pod {
206-
207-
var matchingPods []api.Pod
208-
for _, pod := range pods {
209-
if IsLabelSelectorMatching(resourceSelector, pod.Labels) {
210-
matchingPods = append(matchingPods, pod)
211-
}
212-
}
213-
return matchingPods
214-
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ package deployment
1616

1717
import (
1818
"github.com/kubernetes/dashboard/resource/common"
19+
1920
"k8s.io/kubernetes/pkg/api"
2021
"k8s.io/kubernetes/pkg/apis/extensions"
2122
)
2223

2324
// getPodInfo returns aggregate information about deployment pods.
24-
func getPodInfo(resource *extensions.Deployment,
25-
pods []api.Pod) common.PodInfo {
26-
25+
func getPodInfo(resource *extensions.Deployment, pods []api.Pod) common.PodInfo {
2726
return common.GetPodInfo(resource.Status.Replicas, resource.Spec.Replicas, pods)
2827
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"log"
1919

2020
"github.com/kubernetes/dashboard/resource/common"
21-
"github.com/kubernetes/dashboard/resource/replicationcontroller"
21+
2222
"k8s.io/kubernetes/pkg/api"
2323
k8serrors "k8s.io/kubernetes/pkg/api/errors"
2424
"k8s.io/kubernetes/pkg/apis/extensions"
@@ -118,7 +118,7 @@ func getDeploymentList(deployments []extensions.Deployment,
118118
Deployment{
119119
ObjectMeta: common.NewObjectMeta(deployment.ObjectMeta),
120120
TypeMeta: common.NewTypeMeta(common.ResourceKindDeployment),
121-
ContainerImages: replicationcontroller.GetContainerImages(&deployment.Spec.Template.Spec),
121+
ContainerImages: common.GetContainerImages(&deployment.Spec.Template.Spec),
122122
Pods: podInfo,
123123
})
124124
}

src/app/backend/resource/event/event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strings"
1919

2020
"github.com/kubernetes/dashboard/resource/common"
21+
2122
"k8s.io/kubernetes/pkg/api"
2223
"k8s.io/kubernetes/pkg/types"
2324
)

src/app/backend/resource/event/eventcommon.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package event
1616

1717
import (
1818
"github.com/kubernetes/dashboard/resource/common"
19+
1920
"k8s.io/kubernetes/pkg/api"
2021
client "k8s.io/kubernetes/pkg/client/unversioned"
2122
"k8s.io/kubernetes/pkg/fields"

src/app/backend/resource/namespace/namespace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package namespace
1717
import (
1818
"log"
1919

20-
api "k8s.io/kubernetes/pkg/api"
20+
"k8s.io/kubernetes/pkg/api"
2121
client "k8s.io/kubernetes/pkg/client/unversioned"
2222
"k8s.io/kubernetes/pkg/fields"
2323
"k8s.io/kubernetes/pkg/labels"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/kubernetes/dashboard/client"
2121
"github.com/kubernetes/dashboard/resource/common"
22+
2223
"k8s.io/kubernetes/pkg/api"
2324
k8sClient "k8s.io/kubernetes/pkg/client/unversioned"
2425
)

0 commit comments

Comments
 (0)