detail.go 5.4 KB
Newer Older
1
// Copyright 2017 The Kubernetes Authors.
S
Sebastian Florek 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// 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.

15 16 17 18 19
package deployment

import (
	"log"

20
	"github.com/kubernetes/dashboard/src/app/backend/errors"
21
	"github.com/kubernetes/dashboard/src/app/backend/resource/common"
22
	apps "k8s.io/api/apps/v1"
M
Marcin Maciaszczyk 已提交
23
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
	"k8s.io/apimachinery/pkg/util/intstr"
S
Sebastian Florek 已提交
25
	client "k8s.io/client-go/kubernetes"
26 27
)

B
bryk 已提交
28
// RollingUpdateStrategy is behavior of a rolling update. See RollingUpdateDeployment K8s object.
29
type RollingUpdateStrategy struct {
30 31
	MaxSurge       *intstr.IntOrString `json:"maxSurge"`
	MaxUnavailable *intstr.IntOrString `json:"maxUnavailable"`
32 33
}

34 35
type StatusInfo struct {
	// Total number of desired replicas on the deployment
36
	Replicas int32 `json:"replicas"`
37 38

	// Number of non-terminated pods that have the desired template spec
39
	Updated int32 `json:"updated"`
40 41 42

	// Number of available pods (ready for at least minReadySeconds)
	// targeted by this deployment
43
	Available int32 `json:"available"`
44 45

	// Total number of unavailable pods targeted by this deployment.
46
	Unavailable int32 `json:"unavailable"`
47 48
}

B
bryk 已提交
49
// DeploymentDetail is a presentation layer view of Kubernetes Deployment resource.
50
type DeploymentDetail struct {
51 52
	// Extends list item structure.
	Deployment `json:",inline"`
53

54 55 56
	// Label selector of the service.
	Selector map[string]string `json:"selector"`

57
	// Status information on the deployment
58
	StatusInfo `json:"statusInfo"`
59

60 61 62
	// Conditions describe the state of a deployment at a certain point.
	Conditions []common.Condition `json:"conditions"`

L
Luiz Felipe G. Pereira 已提交
63 64
	// The deployment strategy to use to replace existing pods with new ones.
	// Valid options: Recreate, RollingUpdate
M
Marcin Maciaszczyk 已提交
65
	Strategy apps.DeploymentStrategyType `json:"strategy"`
66 67

	// Min ready seconds
68
	MinReadySeconds int32 `json:"minReadySeconds"`
69

L
Luiz Felipe G. Pereira 已提交
70
	// Rolling update strategy containing maxSurge and maxUnavailable
71
	RollingUpdateStrategy *RollingUpdateStrategy `json:"rollingUpdateStrategy,omitempty"`
72

73 74 75
	// Optional field that specifies the number of old Replica Sets to retain to allow rollback.
	RevisionHistoryLimit *int32 `json:"revisionHistoryLimit"`

76 77
	// List of non-critical errors, that occurred during resource retrieval.
	Errors []error `json:"errors"`
78 79
}

B
bryk 已提交
80
// GetDeploymentDetail returns model object of deployment and error, if any.
81
func GetDeploymentDetail(client client.Interface, namespace string, deploymentName string) (*DeploymentDetail, error) {
82

83
	log.Printf("Getting details of %s deployment in %s namespace", deploymentName, namespace)
84

85
	deployment, err := client.AppsV1().Deployments(namespace).Get(deploymentName, metaV1.GetOptions{})
86 87 88 89
	if err != nil {
		return nil, err
	}

S
Sebastian Florek 已提交
90
	selector, err := metaV1.LabelSelectorAsSelector(deployment.Spec.Selector)
91 92 93
	if err != nil {
		return nil, err
	}
S
Sebastian Florek 已提交
94
	options := metaV1.ListOptions{LabelSelector: selector.String()}
95

L
Luiz Felipe G. Pereira 已提交
96
	channels := &common.ResourceChannels{
97
		ReplicaSetList: common.GetReplicaSetListChannelWithOptions(client,
98 99 100
			common.NewSameNamespaceQuery(namespace), options, 1),
		PodList: common.GetPodListChannelWithOptions(client,
			common.NewSameNamespaceQuery(namespace), options, 1),
101 102
		EventList: common.GetEventListChannelWithOptions(client,
			common.NewSameNamespaceQuery(namespace), options, 1),
L
Luiz Felipe G. Pereira 已提交
103 104
	}

105
	rawRs := <-channels.ReplicaSetList.List
106 107 108 109
	err = <-channels.ReplicaSetList.Error
	nonCriticalErrors, criticalError := errors.HandleError(err)
	if criticalError != nil {
		return nil, criticalError
L
Luiz Felipe G. Pereira 已提交
110
	}
111

112
	rawPods := <-channels.PodList.List
113 114 115 116
	err = <-channels.PodList.Error
	nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
	if criticalError != nil {
		return nil, criticalError
L
Luiz Felipe G. Pereira 已提交
117 118
	}

119 120
	rawEvents := <-channels.EventList.List
	err = <-channels.EventList.Error
121 122 123
	nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
	if criticalError != nil {
		return nil, criticalError
124
	}
L
Luiz Felipe G. Pereira 已提交
125

126
	// Extra Info
127 128 129
	var rollingUpdateStrategy *RollingUpdateStrategy
	if deployment.Spec.Strategy.RollingUpdate != nil {
		rollingUpdateStrategy = &RollingUpdateStrategy{
130 131
			MaxSurge:       deployment.Spec.Strategy.RollingUpdate.MaxSurge,
			MaxUnavailable: deployment.Spec.Strategy.RollingUpdate.MaxUnavailable,
132 133 134 135
		}
	}

	return &DeploymentDetail{
136 137 138 139 140 141 142 143 144
		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,
145
	}, nil
L
Luiz Felipe G. Pereira 已提交
146 147
}

M
Marcin Maciaszczyk 已提交
148
func GetStatusInfo(deploymentStatus *apps.DeploymentStatus) StatusInfo {
149 150 151 152 153 154 155
	return StatusInfo{
		Replicas:    deploymentStatus.Replicas,
		Updated:     deploymentStatus.UpdatedReplicas,
		Available:   deploymentStatus.AvailableReplicas,
		Unavailable: deploymentStatus.UnavailableReplicas,
	}
}