-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetail.go
220 lines (181 loc) · 8.26 KB
/
detail.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2017 The Kubernetes Authors.
//
// 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 deployment
import (
"log"
"github.com/kubernetes/dashboard/src/app/backend/api"
"github.com/kubernetes/dashboard/src/app/backend/errors"
metricapi "github.com/kubernetes/dashboard/src/app/backend/integration/metric/api"
"github.com/kubernetes/dashboard/src/app/backend/resource/common"
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
"github.com/kubernetes/dashboard/src/app/backend/resource/event"
hpa "github.com/kubernetes/dashboard/src/app/backend/resource/horizontalpodautoscaler"
"github.com/kubernetes/dashboard/src/app/backend/resource/pod"
"github.com/kubernetes/dashboard/src/app/backend/resource/replicaset"
apps "k8s.io/api/apps/v1beta2"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
client "k8s.io/client-go/kubernetes"
)
// RollingUpdateStrategy is behavior of a rolling update. See RollingUpdateDeployment K8s object.
type RollingUpdateStrategy struct {
MaxSurge *intstr.IntOrString `json:"maxSurge"`
MaxUnavailable *intstr.IntOrString `json:"maxUnavailable"`
}
type StatusInfo struct {
// Total number of desired replicas on the deployment
Replicas int32 `json:"replicas"`
// Number of non-terminated pods that have the desired template spec
Updated int32 `json:"updated"`
// Number of available pods (ready for at least minReadySeconds)
// targeted by this deployment
Available int32 `json:"available"`
// Total number of unavailable pods targeted by this deployment.
Unavailable int32 `json:"unavailable"`
}
// DeploymentDetail is a presentation layer view of Kubernetes Deployment resource.
type DeploymentDetail struct {
ObjectMeta api.ObjectMeta `json:"objectMeta"`
TypeMeta api.TypeMeta `json:"typeMeta"`
// Detailed information about Pods belonging to this Deployment.
PodList pod.PodList `json:"podList"`
// Label selector of the service.
Selector map[string]string `json:"selector"`
// Status information on the deployment
StatusInfo `json:"statusInfo"`
// The deployment strategy to use to replace existing pods with new ones.
// Valid options: Recreate, RollingUpdate
Strategy apps.DeploymentStrategyType `json:"strategy"`
// Min ready seconds
MinReadySeconds int32 `json:"minReadySeconds"`
// Rolling update strategy containing maxSurge and maxUnavailable
RollingUpdateStrategy *RollingUpdateStrategy `json:"rollingUpdateStrategy,omitempty"`
// RepliaSetList containing old replica sets from the deployment
OldReplicaSetList replicaset.ReplicaSetList `json:"oldReplicaSetList"`
// New replica set used by this deployment
NewReplicaSet replicaset.ReplicaSet `json:"newReplicaSet"`
// Optional field that specifies the number of old Replica Sets to retain to allow rollback.
RevisionHistoryLimit *int32 `json:"revisionHistoryLimit"`
// List of events related to this Deployment
EventList common.EventList `json:"eventList"`
// List of Horizontal Pod AutoScalers targeting this Deployment
HorizontalPodAutoscalerList hpa.HorizontalPodAutoscalerList `json:"horizontalPodAutoscalerList"`
// List of non-critical errors, that occurred during resource retrieval.
Errors []error `json:"errors"`
}
// GetDeploymentDetail returns model object of deployment and error, if any.
func GetDeploymentDetail(client client.Interface, metricClient metricapi.MetricClient, namespace string,
deploymentName string) (*DeploymentDetail, error) {
log.Printf("Getting details of %s deployment in %s namespace", deploymentName, namespace)
deployment, err := client.AppsV1beta2().Deployments(namespace).Get(deploymentName, metaV1.GetOptions{})
if err != nil {
return nil, err
}
selector, err := metaV1.LabelSelectorAsSelector(deployment.Spec.Selector)
if err != nil {
return nil, err
}
options := metaV1.ListOptions{LabelSelector: selector.String()}
channels := &common.ResourceChannels{
ReplicaSetList: common.GetReplicaSetListChannelWithOptions(client,
common.NewSameNamespaceQuery(namespace), options, 1),
PodList: common.GetPodListChannelWithOptions(client,
common.NewSameNamespaceQuery(namespace), options, 1),
}
rawRs := <-channels.ReplicaSetList.List
err = <-channels.ReplicaSetList.Error
nonCriticalErrors, criticalError := errors.HandleError(err)
if criticalError != nil {
return nil, criticalError
}
rawPods := <-channels.PodList.List
err = <-channels.PodList.Error
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
podList, err := GetDeploymentPods(client, metricClient, dataselect.DefaultDataSelectWithMetrics, namespace, deploymentName)
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
eventList, err := event.GetResourceEvents(client, dataselect.DefaultDataSelect, namespace, deploymentName)
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
hpas, err := hpa.GetHorizontalPodAutoscalerListForResource(client, namespace, "Deployment", deploymentName)
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
oldReplicaSetList, err := GetDeploymentOldReplicaSets(client, dataselect.DefaultDataSelect, namespace, deploymentName)
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
rawRepSets := make([]*apps.ReplicaSet, 0)
for i := range rawRs.Items {
rawRepSets = append(rawRepSets, &rawRs.Items[i])
}
newRs, err := FindNewReplicaSet(deployment, rawRepSets)
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
var newReplicaSet replicaset.ReplicaSet
if newRs != nil {
matchingPods := common.FilterPodsByControllerRef(newRs, rawPods.Items)
newRsPodInfo := common.GetPodInfo(newRs.Status.Replicas, newRs.Spec.Replicas, matchingPods)
events, err := event.GetPodsEvents(client, namespace, matchingPods)
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
newRsPodInfo.Warnings = event.GetPodsEventWarnings(events, matchingPods)
newReplicaSet = replicaset.ToReplicaSet(newRs, &newRsPodInfo)
}
// Extra Info
var rollingUpdateStrategy *RollingUpdateStrategy
if deployment.Spec.Strategy.RollingUpdate != nil {
rollingUpdateStrategy = &RollingUpdateStrategy{
MaxSurge: deployment.Spec.Strategy.RollingUpdate.MaxSurge,
MaxUnavailable: deployment.Spec.Strategy.RollingUpdate.MaxUnavailable,
}
}
return &DeploymentDetail{
ObjectMeta: api.NewObjectMeta(deployment.ObjectMeta),
TypeMeta: api.NewTypeMeta(api.ResourceKindDeployment),
PodList: *podList,
Selector: deployment.Spec.Selector.MatchLabels,
StatusInfo: GetStatusInfo(&deployment.Status),
Strategy: deployment.Spec.Strategy.Type,
MinReadySeconds: deployment.Spec.MinReadySeconds,
RollingUpdateStrategy: rollingUpdateStrategy,
OldReplicaSetList: *oldReplicaSetList,
NewReplicaSet: newReplicaSet,
RevisionHistoryLimit: deployment.Spec.RevisionHistoryLimit,
EventList: *eventList,
HorizontalPodAutoscalerList: *hpas,
Errors: nonCriticalErrors,
}, nil
}
func GetStatusInfo(deploymentStatus *apps.DeploymentStatus) StatusInfo {
return StatusInfo{
Replicas: deploymentStatus.Replicas,
Updated: deploymentStatus.UpdatedReplicas,
Available: deploymentStatus.AvailableReplicas,
Unavailable: deploymentStatus.UnavailableReplicas,
}
}