detail.go 3.8 KB
Newer Older
1
// Copyright 2017 The Kubernetes Authors.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//
// 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 namespace

import (
	"log"

20
	"github.com/kubernetes/dashboard/src/app/backend/api"
21
	"github.com/kubernetes/dashboard/src/app/backend/errors"
22
	"github.com/kubernetes/dashboard/src/app/backend/resource/limitrange"
23
	rq "github.com/kubernetes/dashboard/src/app/backend/resource/resourcequota"
24
	v1 "k8s.io/api/core/v1"
S
Sebastian Florek 已提交
25 26
	metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	k8sClient "k8s.io/client-go/kubernetes"
27 28 29 30 31
)

// NamespaceDetail is a presentation layer view of Kubernetes Namespace resource. This means it is Namespace plus
// additional augmented data we can get from other sources.
type NamespaceDetail struct {
32 33
	// Extends list item structure.
	Namespace `json:",inline"`
34 35

	// ResourceQuotaList is list of resource quotas associated to the namespace
36
	ResourceQuotaList *rq.ResourceQuotaDetailList `json:"resourceQuotaList"`
37 38 39

	// ResourceLimits is list of limit ranges associated to the namespace
	ResourceLimits []limitrange.LimitRangeItem `json:"resourceLimits"`
40 41 42

	// List of non-critical errors, that occurred during resource retrieval.
	Errors []error `json:"errors"`
43 44 45
}

// GetNamespaceDetail gets namespace details.
46 47
func GetNamespaceDetail(client k8sClient.Interface, name string) (*NamespaceDetail, error) {
	log.Printf("Getting details of %s namespace\n", name)
48

49
	namespace, err := client.CoreV1().Namespaces().Get(name, metaV1.GetOptions{})
50 51 52 53
	if err != nil {
		return nil, err
	}

54
	resourceQuotaList, err := getResourceQuotas(client, *namespace)
55
	nonCriticalErrors, criticalError := errors.HandleError(err)
56 57
	if criticalError != nil {
		return nil, criticalError
58 59
	}

60
	resourceLimits, err := getLimitRanges(client, *namespace)
61 62 63
	nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
	if criticalError != nil {
		return nil, criticalError
64 65
	}

66
	namespaceDetails := toNamespaceDetail(*namespace, resourceQuotaList, resourceLimits, nonCriticalErrors)
67 68 69
	return &namespaceDetails, nil
}

70
func toNamespaceDetail(namespace v1.Namespace, resourceQuotaList *rq.ResourceQuotaDetailList,
71
	resourceLimits []limitrange.LimitRangeItem, nonCriticalErrors []error) NamespaceDetail {
72 73

	return NamespaceDetail{
74
		Namespace:         toNamespace(namespace),
75
		ResourceQuotaList: resourceQuotaList,
76
		ResourceLimits:    resourceLimits,
77
		Errors:            nonCriticalErrors,
78 79
	}
}
80

81
func getResourceQuotas(client k8sClient.Interface, namespace v1.Namespace) (*rq.ResourceQuotaDetailList, error) {
M
Marcin Maciaszczyk 已提交
82
	list, err := client.CoreV1().ResourceQuotas(namespace.Name).List(api.ListEverything)
83

84 85
	result := &rq.ResourceQuotaDetailList{
		Items:    make([]rq.ResourceQuotaDetail, 0),
86
		ListMeta: api.ListMeta{TotalItems: len(list.Items)},
87 88 89
	}

	for _, item := range list.Items {
90
		detail := rq.ToResourceQuotaDetail(&item)
91 92 93 94 95 96
		result.Items = append(result.Items, *detail)
	}

	return result, err
}

97
func getLimitRanges(client k8sClient.Interface, namespace v1.Namespace) ([]limitrange.LimitRangeItem, error) {
M
Marcin Maciaszczyk 已提交
98
	list, err := client.CoreV1().LimitRanges(namespace.Name).List(api.ListEverything)
99 100 101 102 103 104 105 106 107 108 109
	if err != nil {
		return nil, err
	}

	resourceLimits := make([]limitrange.LimitRangeItem, 0)
	for _, item := range list.Items {
		list := limitrange.ToLimitRanges(&item)
		resourceLimits = append(resourceLimits, list...)
	}

	return resourceLimits, nil
110
}