Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/backend/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func DeployApp(spec *AppDeploymentSpec, client client.Interface) error {
Containers: []api.Container{containerSpec},
}
if spec.ImagePullSecret != nil {
podSpec.ImagePullSecrets = []api.LocalObjectReference{api.LocalObjectReference{Name: *spec.ImagePullSecret}}
podSpec.ImagePullSecrets = []api.LocalObjectReference{{Name: *spec.ImagePullSecret}}
}

podTemplate := &api.PodTemplateSpec{
Expand Down
43 changes: 33 additions & 10 deletions src/app/backend/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,22 @@ func GetReplicationControllerPodsEvents(client *client.Client, namespace, replic
return nil, err
}

events := make([]api.Event, 0, 0)
events, err := GetPodsEvents(client, pods)

for _, pod := range pods.Items {
fieldSelector, err := fields.ParseSelector("involvedObject.name=" + pod.Name)
if err != nil {
return nil, err
}

if err != nil {
return nil, err
}
return events, nil
}

list, err := client.Events(namespace).List(unversioned.ListOptions{
LabelSelector: unversioned.LabelSelector{labels.Everything()},
FieldSelector: unversioned.FieldSelector{fieldSelector},
})
// Gets events associated to given list of pods
// TODO(floreks): refactor this to make single API call instead of N api calls
func GetPodsEvents(client *client.Client, pods *api.PodList) ([]api.Event, error) {
events := make([]api.Event, 0, 0)

for _, pod := range pods.Items {
list, err := GetPodEvents(client, pod)

if err != nil {
return nil, err
Expand All @@ -164,6 +167,26 @@ func GetReplicationControllerPodsEvents(client *client.Client, namespace, replic
return events, nil
}

// Gets events associated to given pod
func GetPodEvents(client client.Interface, pod api.Pod) (*api.EventList, error) {
fieldSelector, err := fields.ParseSelector("involvedObject.name=" + pod.Name)

if err != nil {
return nil, err
}

list, err := client.Events(pod.Namespace).List(unversioned.ListOptions{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, now instead of one api call we have N api calls. Please fix :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are N api calls even now. I've only refactored it and extracted function.

https://github.com/kubernetes/dashboard/blob/master/src/app/backend/events.go#L149

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. Sorry. Can you add a todo so that we know what to fix?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. This PR is quite big so i will do that in next PR.

LabelSelector: unversioned.LabelSelector{labels.Everything()},
FieldSelector: unversioned.FieldSelector{fieldSelector},
})

if err != nil {
return nil, err
}

return list, nil
}

// Appends events from source slice to target events representation.
func AppendEvents(source []api.Event, target Events) Events {
for _, event := range source {
Expand Down
152 changes: 152 additions & 0 deletions src/app/backend/eventscommon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 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 main

import (
"k8s.io/kubernetes/pkg/api"
client "k8s.io/kubernetes/pkg/client/unversioned"
"log"
"strings"
)

// Partial string to correctly filter warning events.
// Has to be lower case for correct case insensitive comparison.
const FAILED_REASON_PARTIAL = "failed"

// Contains basic information about event related to a pod
type PodEvent struct {
// Short, machine understandable string that gives the reason
// for this event being generated.
Reason string `json:"reason"`

// A human-readable description of the status of related pod.
Message string `json:"message"`
}

// Returns warning pod events based on given list of pods.
// TODO(floreks) : Import and use Set instead of custom function to get rid of duplicates
func GetPodsEventWarnings(client client.Interface, pods []api.Pod) (result []PodEvent, err error) {
for _, pod := range pods {
if !isRunningOrSucceeded(pod) {
log.Printf("Getting warning events from pod: %s", pod.Name)
events, err := GetPodEvents(client, pod)

if err != nil {
return nil, err
}

result = getPodsEventWarnings(events)
}
}

return removeDuplicates(result), nil
}

// Returns list of Pod Event model objects based on kubernetes API event list object
// Event list object is filtered to get only warning events.
func getPodsEventWarnings(eventList *api.EventList) []PodEvent {
result := make([]PodEvent, 0)

var events []api.Event
if isTypeFilled(eventList.Items) {
events = filterEventsByType(eventList.Items, api.EventTypeWarning)
} else {
events = filterEventsByReason(eventList.Items, FAILED_REASON_PARTIAL)
}

for _, event := range events {
result = append(result, PodEvent{
Message: event.Message,
Reason: event.Reason,
})
}

return result
}

// Filters kubernetes API event objects based on event type.
// Empty string will return all events.
func filterEventsByType(events []api.Event, eventType string) []api.Event {
if len(eventType) == 0 || len(events) == 0 {
return events
}

result := make([]api.Event, 0)
for _, event := range events {
if event.Type == eventType {
result = append(result, event)
}
}

return result
}

// Filters kubernetes API event objects based on reason property.
// Empty string will return all events.
func filterEventsByReason(events []api.Event, partial string) []api.Event {
if len(partial) == 0 || len(events) == 0 {
return events
}

result := make([]api.Event, 0)
for _, event := range events {
if strings.Contains(strings.ToLower(event.Reason), partial) {
result = append(result, event)
}
}

return result
}

// Returns true if all given events type is filled, false otherwise.
// This is needed as some older versions of kubernetes do not have Type property filled.
func isTypeFilled(events []api.Event) bool {
if len(events) == 0 {
return false
}

for _, event := range events {
if len(event.Type) == 0 {
return false
}
}

return true
}

// Removes duplicate strings from the slice
func removeDuplicates(slice []PodEvent) []PodEvent {
visited := make(map[string]bool, 0)
result := make([]PodEvent, 0)

for _, elem := range slice {
if !visited[elem.Reason] {
visited[elem.Reason] = true
result = append(result, elem)
}
}

return result
}

// Returns true if given pod is in state running or succeeded, false otherwise
func isRunningOrSucceeded(pod api.Pod) bool {
switch pod.Status.Phase {
case api.PodRunning, api.PodSucceeded:
return true
}

return false
}
7 changes: 5 additions & 2 deletions src/app/backend/replicationcontrollercommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

type ReplicationControllerWithPods struct {
ReplicationController *api.ReplicationController
Pods *api.PodList
Pods *api.PodList
}

// ReplicationControllerPodInfo represents aggregate information about replication controller pods.
Expand All @@ -43,6 +43,9 @@ type ReplicationControllerPodInfo struct {

// Number of pods that are failed.
Failed int `json:"failed"`

// Unique warning messages related to pods in this Replication Controller.
Warnings []PodEvent `json:"warnings"`
}

// Returns structure containing ReplicationController and Pods for the given replication controller.
Expand All @@ -66,7 +69,7 @@ func getRawReplicationControllerWithPods(client client.Interface, namespace, nam

replicationControllerAndPods := &ReplicationControllerWithPods{
ReplicationController: replicationController,
Pods: pods,
Pods: pods,
}
return replicationControllerAndPods, nil
}
Expand Down
40 changes: 35 additions & 5 deletions src/app/backend/replicationcontrollerlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import (
"k8s.io/kubernetes/pkg/labels"
)

// Callback function in order to get the pod status errors
type GetPodsEventWarningsFunc func(pods []api.Pod) ([]PodEvent, error)

// ReplicationControllerList contains a list of Replication Controllers in the cluster.
type ReplicationControllerList struct {
// Unordered list of Replication Controllers.
Expand All @@ -45,7 +48,7 @@ type ReplicationController struct {
// Label of this Replication Controller.
Labels map[string]string `json:"labels"`

// Aggergate information about pods belonging to this repolica set.
// Aggregate information about pods belonging to this repolica set.
Pods ReplicationControllerPodInfo `json:"pods"`

// Container images of the Replication Controller.
Expand Down Expand Up @@ -88,14 +91,34 @@ func GetReplicationControllerList(client *client.Client) (*ReplicationController
return nil, err
}

return getReplicationControllerList(replicationControllers.Items, services.Items, pods.Items), nil
// Anonymous callback function to get pods warnings.
// Function fulfils GetPodsEventWarningsFunc type contract.
// Based on list of api pods returns list of pod related warning events
getPodsEventWarningsFn := func(pods []api.Pod) ([]PodEvent, error) {
errors, err := GetPodsEventWarnings(client, pods)

if err != nil {
return nil, err
}

return errors, nil
}

result, err := getReplicationControllerList(replicationControllers.Items, services.Items, pods.Items, getPodsEventWarningsFn)

if err != nil {
return nil, err
}

return result, nil
}

// Returns a list of all Replication Controller model objects in the cluster, based on all Kubernetes
// Replication Controller and Service API objects.
// The function processes all Replication Controllers API objects and finds matching Services for them.
func getReplicationControllerList(replicationControllers []api.ReplicationController, services []api.Service,
pods []api.Pod) *ReplicationControllerList {
func getReplicationControllerList(replicationControllers []api.ReplicationController,
services []api.Service, pods []api.Pod, getPodsEventWarningsFn GetPodsEventWarningsFunc) (
*ReplicationControllerList, error) {

replicationControllerList := &ReplicationControllerList{ReplicationControllers: make([]ReplicationController, 0)}

Expand Down Expand Up @@ -125,6 +148,13 @@ func getReplicationControllerList(replicationControllers []api.ReplicationContro
}
}
podInfo := getReplicationControllerPodInfo(&replicationController, matchingPods)
podErrors, err := getPodsEventWarningsFn(matchingPods)

if err != nil {
return nil, err
}

podInfo.Warnings = podErrors

replicationControllerList.ReplicationControllers = append(replicationControllerList.ReplicationControllers, ReplicationController{
Name: replicationController.ObjectMeta.Name,
Expand All @@ -139,7 +169,7 @@ func getReplicationControllerList(replicationControllers []api.ReplicationContro
})
}

return replicationControllerList
return replicationControllerList, nil
}

// Returns all services that target the same Pods (or subset) as the given Replication Controller.
Expand Down
11 changes: 10 additions & 1 deletion src/app/externs/backendapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,22 @@ backendApi.Event;
*/
backendApi.ReplicationControllerList;

/**
* @typedef {{
* reason: string,
* message: string
* }}
*/
backendApi.PodEvent;

/**
* @typedef {{
* current: number,
* desired: number,
* running: number,
* pending: number,
* failed: number
* failed: number,
* warnings: !Array<!backendApi.PodEvent>
* }}
*/
backendApi.ReplicationControllerPodInfo;
Expand Down
Loading