customresourcedefinitions.go 2.2 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Copyright 2019 The KubeSphere 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.
*/

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
package customresourcedefinition

import (
	"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
	apiextensionsinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
	"k8s.io/apimachinery/pkg/runtime"
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/apiserver/query"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
)

type crdGetter struct {
	informers apiextensionsinformers.SharedInformerFactory
}

func New(informers apiextensionsinformers.SharedInformerFactory) v1alpha3.Interface {
	return &crdGetter{
		informers: informers,
	}
}

func (c crdGetter) Get(_, name string) (runtime.Object, error) {
	return c.informers.Apiextensions().V1().CustomResourceDefinitions().Lister().Get(name)
}

func (c crdGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
	crds, err := c.informers.Apiextensions().V1().CustomResourceDefinitions().Lister().List(query.Selector())
	if err != nil {
		return nil, err
	}

	var result []runtime.Object
H
hongming 已提交
49 50
	for _, crd := range crds {
		result = append(result, crd)
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
	}

	return v1alpha3.DefaultList(result, query, c.compare, c.filter), nil
}

func (c crdGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
	leftCRD, ok := left.(*v1.CustomResourceDefinition)
	if !ok {
		return false
	}

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

	return v1alpha3.DefaultObjectMetaCompare(leftCRD.ObjectMeta, rightCRD.ObjectMeta, field)
}

func (c crdGetter) filter(object runtime.Object, filter query.Filter) bool {
	crd, ok := object.(*v1.CustomResourceDefinition)
	if !ok {
		return false
	}

	return v1alpha3.DefaultObjectMetaFilter(crd.ObjectMeta, filter)
}