|
| 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 daemonset |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/kubernetes/dashboard/resource/common" |
| 19 | + // "github.com/kubernetes/dashboard/resource/event" |
| 20 | + "k8s.io/kubernetes/pkg/api" |
| 21 | + "k8s.io/kubernetes/pkg/api/unversioned" |
| 22 | + "k8s.io/kubernetes/pkg/apis/extensions" |
| 23 | + client "k8s.io/kubernetes/pkg/client/unversioned" |
| 24 | + "k8s.io/kubernetes/pkg/fields" |
| 25 | + "k8s.io/kubernetes/pkg/labels" |
| 26 | +) |
| 27 | + |
| 28 | +type DaemonSetWithPods struct { |
| 29 | + DaemonSet *extensions.DaemonSet |
| 30 | + Pods *api.PodList |
| 31 | +} |
| 32 | + |
| 33 | +// Returns structure containing DaemonSet and Pods for the given daemon set. |
| 34 | +func getRawDaemonSetWithPods(client client.Interface, namespace, name string) ( |
| 35 | + *DaemonSetWithPods, error) { |
| 36 | + daemonSet, err := client.Extensions().DaemonSets(namespace).Get(name) |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + labelSelector, err := unversioned.LabelSelectorAsSelector(daemonSet.Spec.Selector) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + pods, err := client.Pods(namespace).List( |
| 47 | + api.ListOptions{ |
| 48 | + LabelSelector: labelSelector, |
| 49 | + FieldSelector: fields.Everything(), |
| 50 | + }) |
| 51 | + |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + |
| 56 | + daemonSetAndPods := &DaemonSetWithPods{ |
| 57 | + DaemonSet: daemonSet, |
| 58 | + Pods: pods, |
| 59 | + } |
| 60 | + return daemonSetAndPods, nil |
| 61 | +} |
| 62 | + |
| 63 | +// Retrieves Pod list that belongs to a Daemon Set. |
| 64 | +func getRawDaemonSetPods(client client.Interface, namespace, name string) (*api.PodList, error) { |
| 65 | + daemonSetAndPods, err := getRawDaemonSetWithPods(client, namespace, name) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + return daemonSetAndPods.Pods, nil |
| 70 | +} |
| 71 | + |
| 72 | +// Returns aggregate information about daemon set pods. |
| 73 | +func getDaemonSetPodInfo(daemonSet *extensions.DaemonSet, pods []api.Pod) common.PodInfo { |
| 74 | + result := common.PodInfo{} |
| 75 | + for _, pod := range pods { |
| 76 | + switch pod.Status.Phase { |
| 77 | + case api.PodRunning: |
| 78 | + result.Running++ |
| 79 | + case api.PodPending: |
| 80 | + result.Pending++ |
| 81 | + case api.PodFailed: |
| 82 | + result.Failed++ |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return result |
| 87 | +} |
| 88 | + |
| 89 | +// Based on given selector returns list of services that are candidates for deletion. |
| 90 | +// Services are matched by daemon sets' label selector. They are deleted if given |
| 91 | +// label selector is targeting only 1 daemon set. |
| 92 | +func getServicesForDSDeletion(client client.Interface, labelSelector labels.Selector, |
| 93 | + namespace string) ([]api.Service, error) { |
| 94 | + |
| 95 | + daemonSet, err := client.Extensions().DaemonSets(namespace).List(api.ListOptions{ |
| 96 | + LabelSelector: labelSelector, |
| 97 | + FieldSelector: fields.Everything(), |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + // if label selector is targeting only 1 daemon set |
| 104 | + // then we can delete services targeted by this label selector, |
| 105 | + // otherwise we can not delete any services so just return empty list |
| 106 | + if len(daemonSet.Items) != 1 { |
| 107 | + return []api.Service{}, nil |
| 108 | + } |
| 109 | + |
| 110 | + services, err := client.Services(namespace).List(api.ListOptions{ |
| 111 | + LabelSelector: labelSelector, |
| 112 | + FieldSelector: fields.Everything(), |
| 113 | + }) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + return services.Items, nil |
| 119 | +} |
0 commit comments