forked from kubernetes/dashboard
/
detail.go
158 lines (130 loc) · 5.62 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
// 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 (
"context"
"log"
"github.com/lixianyang/dashboard/v2/src/app/backend/errors"
"github.com/lixianyang/dashboard/v2/src/app/backend/resource/common"
apps "k8s.io/api/apps/v1"
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"`
}
// StatusInfo is the status information of the deployment
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 {
// Extends list item structure.
Deployment `json:",inline"`
// Label selector of the service.
Selector map[string]string `json:"selector"`
// Status information on the deployment
StatusInfo `json:"statusInfo"`
// Conditions describe the state of a deployment at a certain point.
Conditions []common.Condition `json:"conditions"`
// 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"`
// Optional field that specifies the number of old Replica Sets to retain to allow rollback.
RevisionHistoryLimit *int32 `json:"revisionHistoryLimit"`
// 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, namespace string, deploymentName string) (*DeploymentDetail, error) {
log.Printf("Getting details of %s deployment in %s namespace", deploymentName, namespace)
deployment, err := client.AppsV1().Deployments(namespace).Get(context.TODO(), 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),
EventList: common.GetEventListChannelWithOptions(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
}
rawEvents := <-channels.EventList.List
err = <-channels.EventList.Error
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
// 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{
Deployment: toDeployment(deployment, rawRs.Items, rawPods.Items, rawEvents.Items),
Selector: deployment.Spec.Selector.MatchLabels,
StatusInfo: GetStatusInfo(&deployment.Status),
Conditions: getConditions(deployment.Status.Conditions),
Strategy: deployment.Spec.Strategy.Type,
MinReadySeconds: deployment.Spec.MinReadySeconds,
RollingUpdateStrategy: rollingUpdateStrategy,
RevisionHistoryLimit: deployment.Spec.RevisionHistoryLimit,
Errors: nonCriticalErrors,
}, nil
}
// GetStatusInfo is used to get the status information from the *apps.DeploymentStatus
func GetStatusInfo(deploymentStatus *apps.DeploymentStatus) StatusInfo {
return StatusInfo{
Replicas: deploymentStatus.Replicas,
Updated: deploymentStatus.UpdatedReplicas,
Available: deploymentStatus.AvailableReplicas,
Unavailable: deploymentStatus.UnavailableReplicas,
}
}