-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploymentdetail.go
175 lines (141 loc) · 5.88 KB
/
deploymentdetail.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
package deployment
import (
"log"
"github.com/kubernetes/dashboard/resource/common"
"github.com/kubernetes/dashboard/resource/replicaset"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions"
client "k8s.io/kubernetes/pkg/client/unversioned"
deploymentutil "k8s.io/kubernetes/pkg/util/deployment"
)
// RollingUpdateStrategy is behavior of a rolling update. See RollingUpdateDeployment K8s object.
type RollingUpdateStrategy struct {
MaxSurge int `json:"maxSurge"`
MaxUnavailable int `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 common.ObjectMeta `json:"objectMeta"`
TypeMeta common.TypeMeta `json:"typeMeta"`
// 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 extensions.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"`
}
// GetDeploymentDetail returns model object of deployment and error, if any.
func GetDeploymentDetail(client client.Interface, namespace string,
name string) (*DeploymentDetail, error) {
log.Printf("Getting details of %s deployment in %s namespace", name, namespace)
deployment, err := client.Extensions().Deployments(namespace).Get(name)
if err != nil {
return nil, err
}
selector, err := unversioned.LabelSelectorAsSelector(deployment.Spec.Selector)
if err != nil {
return nil, err
}
options := api.ListOptions{LabelSelector: selector}
channels := &common.ResourceChannels{
ReplicaSetList: common.GetReplicaSetListChannelWithOptions(client.Extensions(),
common.NewSameNamespaceQuery(namespace), options, 1),
PodList: common.GetPodListChannelWithOptions(client,
common.NewSameNamespaceQuery(namespace), options, 1),
EventList: common.GetEventListChannelWithOptions(client,
common.NewSameNamespaceQuery(namespace), options, 1),
}
rsList := <-channels.ReplicaSetList.List
if err := <-channels.ReplicaSetList.Error; err != nil {
return nil, err
}
podList := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
eventList := <-channels.EventList.List
if err := <-channels.EventList.Error; err != nil {
return nil, err
}
oldReplicaSets, _, err := deploymentutil.FindOldReplicaSets(deployment, rsList.Items, podList)
if err != nil {
return nil, err
}
newReplicaSet, err := deploymentutil.FindNewReplicaSet(deployment, rsList.Items)
if err != nil {
return nil, err
}
events, err := GetDeploymentEvents(eventList.Items, namespace, name)
if err != nil {
return nil, err
}
return getDeploymentDetail(deployment, oldReplicaSets, newReplicaSet,
podList.Items, events, eventList.Items), nil
}
func getDeploymentDetail(deployment *extensions.Deployment,
oldRs []*extensions.ReplicaSet, newRs *extensions.ReplicaSet,
pods []api.Pod, events *common.EventList, rawEvents []api.Event) *DeploymentDetail {
var newReplicaSet replicaset.ReplicaSet
if newRs != nil {
newRsPodInfo := common.GetPodInfo(newRs.Status.Replicas, newRs.Spec.Replicas, pods)
newReplicaSet = replicaset.ToReplicaSet(newRs, &newRsPodInfo)
}
oldReplicaSets := make([]extensions.ReplicaSet, len(oldRs))
for i, replicaSet := range oldRs {
oldReplicaSets[i] = *replicaSet
}
oldReplicaSetList := replicaset.ToReplicaSetList(oldReplicaSets, pods, rawEvents)
var rollingUpdateStrategy *RollingUpdateStrategy
if deployment.Spec.Strategy.RollingUpdate != nil {
rollingUpdateStrategy = &RollingUpdateStrategy{
MaxSurge: deployment.Spec.Strategy.RollingUpdate.MaxSurge.IntValue(),
MaxUnavailable: deployment.Spec.Strategy.RollingUpdate.MaxUnavailable.IntValue(),
}
}
return &DeploymentDetail{
ObjectMeta: common.NewObjectMeta(deployment.ObjectMeta),
TypeMeta: common.NewTypeMeta(common.ResourceKindDeployment),
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: *events,
}
}
func GetStatusInfo(deploymentStatus *extensions.DeploymentStatus) StatusInfo {
return StatusInfo{
Replicas: deploymentStatus.Replicas,
Updated: deploymentStatus.UpdatedReplicas,
Available: deploymentStatus.AvailableReplicas,
Unavailable: deploymentStatus.UnavailableReplicas,
}
}