deployments.go 3.2 KB
Newer Older
Z
zryfish 已提交
1
/*
H
hongming 已提交
2
Copyright 2019 The KubeSphere Authors.
Z
zryfish 已提交
3

H
hongming 已提交
4 5 6
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
Z
zryfish 已提交
7

H
hongming 已提交
8
    http://www.apache.org/licenses/LICENSE-2.0
Z
zryfish 已提交
9

H
hongming 已提交
10 11 12 13 14
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.
Z
zryfish 已提交
15
*/
H
hongming 已提交
16

Z
zryfish 已提交
17 18 19
package deployment

import (
Z
zryfish 已提交
20
	"k8s.io/apimachinery/pkg/runtime"
Z
zryfish 已提交
21
	"k8s.io/client-go/informers"
Z
zryfish 已提交
22
	"kubesphere.io/kubesphere/pkg/api"
Z
zryfish 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	"kubesphere.io/kubesphere/pkg/apiserver/query"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
	"strings"
	"time"

	"k8s.io/api/apps/v1"
)

const (
	statusStopped  = "stopped"
	statusRunning  = "running"
	statusUpdating = "updating"
)

type deploymentsGetter struct {
	sharedInformers informers.SharedInformerFactory
}

func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface {
	return &deploymentsGetter{sharedInformers: sharedInformers}
}

Z
zryfish 已提交
45
func (d *deploymentsGetter) Get(namespace, name string) (runtime.Object, error) {
Z
zryfish 已提交
46 47 48
	return d.sharedInformers.Apps().V1().Deployments().Lister().Deployments(namespace).Get(name)
}

Z
zryfish 已提交
49
func (d *deploymentsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
Z
zryfish 已提交
50
	// first retrieves all deployments within given namespace
H
hongming 已提交
51
	deployments, err := d.sharedInformers.Apps().V1().Deployments().Lister().Deployments(namespace).List(query.Selector())
Z
zryfish 已提交
52 53 54 55
	if err != nil {
		return nil, err
	}

Z
zryfish 已提交
56
	var result []runtime.Object
H
hongming 已提交
57 58
	for _, deployment := range deployments {
		result = append(result, deployment)
Z
zryfish 已提交
59 60
	}

Z
zryfish 已提交
61
	return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
Z
zryfish 已提交
62 63
}

Z
zryfish 已提交
64
func (d *deploymentsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
Z
zryfish 已提交
65 66 67 68 69 70 71 72 73 74 75 76

	leftDeployment, ok := left.(*v1.Deployment)
	if !ok {
		return false
	}

	rightDeployment, ok := right.(*v1.Deployment)
	if !ok {
		return false
	}

	switch field {
H
hongming 已提交
77 78
	case query.FieldUpdateTime:
		fallthrough
Z
zryfish 已提交
79 80 81
	case query.FieldLastUpdateTimestamp:
		return lastUpdateTime(leftDeployment).After(lastUpdateTime(rightDeployment))
	default:
H
hongming 已提交
82
		return v1alpha3.DefaultObjectMetaCompare(leftDeployment.ObjectMeta, rightDeployment.ObjectMeta, field)
Z
zryfish 已提交
83 84 85
	}
}

Z
zryfish 已提交
86
func (d *deploymentsGetter) filter(object runtime.Object, filter query.Filter) bool {
Z
zryfish 已提交
87 88 89 90 91 92
	deployment, ok := object.(*v1.Deployment)
	if !ok {
		return false
	}

	switch filter.Field {
H
hongming 已提交
93

Z
zryfish 已提交
94
	case query.FieldStatus:
H
hongming 已提交
95
		return strings.Compare(deploymentStatus(deployment.Status), string(filter.Value)) == 0
Z
zryfish 已提交
96
	default:
H
hongming 已提交
97
		return v1alpha3.DefaultObjectMetaFilter(deployment.ObjectMeta, filter)
Z
zryfish 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	}
}

func deploymentStatus(status v1.DeploymentStatus) string {
	if status.ReadyReplicas == 0 && status.Replicas == 0 {
		return statusStopped
	} else if status.ReadyReplicas == status.Replicas {
		return statusRunning
	} else {
		return statusUpdating
	}
}

func lastUpdateTime(deployment *v1.Deployment) time.Time {
	lut := deployment.CreationTimestamp.Time
	for _, condition := range deployment.Status.Conditions {
		if condition.LastUpdateTime.After(lut) {
			lut = condition.LastUpdateTime.Time
		}
	}
	return lut
}