From bcbdc7c73305220166fdd89a9262db2d3061f503 Mon Sep 17 00:00:00 2001 From: Tamal Saha Date: Wed, 20 Sep 2023 09:04:22 -0700 Subject: [PATCH] Implement project api (#245) Signed-off-by: Tamal Saha --- go.mod | 2 +- go.sum | 4 +- pkg/apiserver/apiserver.go | 13 +++ pkg/cmds/server/start.go | 5 + pkg/registry/management/projects/storage.go | 106 ++++++++++++++++++ .../apis/management/v1alpha1/project_types.go | 6 + vendor/modules.txt | 2 +- 7 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 pkg/registry/management/projects/storage.go diff --git a/go.mod b/go.mod index 78dee9cae..3c041a97e 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( k8s.io/kube-state-metrics/v2 v2.7.0 kmodules.xyz/apiversion v0.2.0 kmodules.xyz/authorizer v0.25.1 - kmodules.xyz/client-go v0.25.34-0.20230920051128-f758ec0276ab + kmodules.xyz/client-go v0.25.34-0.20230920151620-7cfdfeb7468e kmodules.xyz/custom-resources v0.25.1 kmodules.xyz/go-containerregistry v0.0.11 kmodules.xyz/monitoring-agent-api v0.25.1 diff --git a/go.sum b/go.sum index 641fe2c27..d15103d23 100644 --- a/go.sum +++ b/go.sum @@ -2118,8 +2118,8 @@ kmodules.xyz/apiversion v0.2.0 h1:vAQYqZFm4xu4pbB1cAdHbFEPES6EQkcR4wc06xdTOWk= kmodules.xyz/apiversion v0.2.0/go.mod h1:oPX8g8LvlPdPX3Yc5YvCzJHQnw3YF/X4/jdW0b1am80= kmodules.xyz/authorizer v0.25.1 h1:W19AtlPD2A1+Q4UqDmNCJKfX9bKIgj+J6bQmkYwsHwY= kmodules.xyz/authorizer v0.25.1/go.mod h1:hKAbHpRkbxZJjc+cMTUiyxQxp7amKUVDiN145IrpnhA= -kmodules.xyz/client-go v0.25.34-0.20230920051128-f758ec0276ab h1:Al8RDDn7TNBozXvqApo2BAU2lUxDkYAPDRC1FBA2DQ8= -kmodules.xyz/client-go v0.25.34-0.20230920051128-f758ec0276ab/go.mod h1:r/Va2Y6t1G8X1sPRjrQC6FWB3oh/i6rjssmlfJnbCmg= +kmodules.xyz/client-go v0.25.34-0.20230920151620-7cfdfeb7468e h1:dI7raCj36RBN4ITLJ6k1Y5U1Q0fzD88xVGjy3BnSesY= +kmodules.xyz/client-go v0.25.34-0.20230920151620-7cfdfeb7468e/go.mod h1:r/Va2Y6t1G8X1sPRjrQC6FWB3oh/i6rjssmlfJnbCmg= kmodules.xyz/crd-schema-fuzz v0.25.0 h1:c5ZxNRqJak1bkGhECmyrKpzKGThFMB4088Kynyvngbc= kmodules.xyz/custom-resources v0.25.1 h1:0qHPTxbT/q0afl2GCOnwPFaoxKziRIPXgVu77YwrCa4= kmodules.xyz/custom-resources v0.25.1/go.mod h1:ULwzvLmOqZJcPSXKI7iLclYL5eYRlKx8Nbex28Ht19E= diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index bad0c4da3..5ae0c2ca9 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -41,6 +41,7 @@ import ( resourcesummarystorage "kubeops.dev/ui-server/pkg/registry/core/resourcesummary" coststorage "kubeops.dev/ui-server/pkg/registry/cost/reports" whoamistorage "kubeops.dev/ui-server/pkg/registry/identity/whoami" + projecttorage "kubeops.dev/ui-server/pkg/registry/management/projects" "kubeops.dev/ui-server/pkg/registry/meta/chartpresetquery" "kubeops.dev/ui-server/pkg/registry/meta/render" "kubeops.dev/ui-server/pkg/registry/meta/renderdashboard" @@ -78,6 +79,7 @@ import ( "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" "kmodules.xyz/authorizer" + mgmtapi "kmodules.xyz/client-go/apis/management/v1alpha1" cu "kmodules.xyz/client-go/client" clustermeta "kmodules.xyz/client-go/cluster" "kmodules.xyz/client-go/meta" @@ -357,6 +359,17 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) { return nil, err } } + { + apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(mgmtapi.GroupVersion.Group, Scheme, metav1.ParameterCodec, Codecs) + + v1alpha1storage := map[string]rest.Storage{} + v1alpha1storage[mgmtapi.ResourceProjects] = projecttorage.NewStorage(ctrlClient) + apiGroupInfo.VersionedResourcesStorageMap["v1alpha1"] = v1alpha1storage + + if err := s.GenericAPIServer.InstallAPIGroup(&apiGroupInfo); err != nil { + return nil, err + } + } { // Create metrics handler and fill the stores with metrics store // containing Help and Type headers of metrics diff --git a/pkg/cmds/server/start.go b/pkg/cmds/server/start.go index aeb73d8fd..410947385 100644 --- a/pkg/cmds/server/start.go +++ b/pkg/cmds/server/start.go @@ -46,6 +46,7 @@ import ( "k8s.io/apiserver/pkg/util/feature" "k8s.io/component-base/metrics/legacyregistry" "k8s.io/klog/v2" + mgmtapi "kmodules.xyz/client-go/apis/management/v1alpha1" ou "kmodules.xyz/client-go/openapi" "kmodules.xyz/client-go/tools/clientcmd" auditorv1alpha1 "kmodules.xyz/custom-resources/apis/auditor/v1alpha1" @@ -157,9 +158,13 @@ func (o *UIServerOptions) Config() (*apiserver.Config, error) { fmt.Sprintf("/apis/%s/%s", reportsapi.SchemeGroupVersion, reportsapi.ResourceImages), fmt.Sprintf("/apis/%s/%s", reportsapi.SchemeGroupVersion, reportsapi.ResourceCVEReports), + fmt.Sprintf("/apis/%s/%s", policyapi.SchemeGroupVersion, policyapi.ResourcePolicyReports), + fmt.Sprintf("/apis/%s/%s", costapi.SchemeGroupVersion, costapi.ResourceCostReports), + fmt.Sprintf("/apis/%s/%s", mgmtapi.GroupVersion, mgmtapi.ResourceProjects), + fmt.Sprintf("/apis/%s/%s", auditorv1alpha1.SchemeGroupVersion, auditorv1alpha1.ResourceSiteInfos), fmt.Sprintf("/apis/%s/%s", ui.SchemeGroupVersion, ui.ResourceFeatures), diff --git a/pkg/registry/management/projects/storage.go b/pkg/registry/management/projects/storage.go new file mode 100644 index 000000000..35b2ecae4 --- /dev/null +++ b/pkg/registry/management/projects/storage.go @@ -0,0 +1,106 @@ +/* +Copyright AppsCode Inc. and Contributors. + +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 podview + +import ( + "context" + + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/apis/meta/internalversion" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apiserver/pkg/registry/rest" + mgmtapi "kmodules.xyz/client-go/apis/management/v1alpha1" + clustermeta "kmodules.xyz/client-go/cluster" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type Storage struct { + kc client.Client + convertor rest.TableConvertor +} + +var ( + _ rest.GroupVersionKindProvider = &Storage{} + _ rest.Scoper = &Storage{} + _ rest.Storage = &Storage{} + _ rest.Lister = &Storage{} + _ rest.Getter = &Storage{} +) + +func NewStorage(kc client.Client) *Storage { + s := &Storage{ + kc: kc, + convertor: rest.NewDefaultTableConvertor(schema.GroupResource{ + Group: mgmtapi.GroupVersion.Group, + Resource: mgmtapi.ResourceProjects, + }), + } + return s +} + +func (r *Storage) GroupVersionKind(_ schema.GroupVersion) schema.GroupVersionKind { + return mgmtapi.GroupVersion.WithKind(mgmtapi.ResourceKindProject) +} + +func (r *Storage) NamespaceScoped() bool { + return false +} + +func (r *Storage) New() runtime.Object { + return &mgmtapi.Project{} +} + +func (r *Storage) Destroy() {} + +func (r *Storage) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) { + if clustermeta.IsRancherManaged(r.kc.RESTMapper()) { + return clustermeta.GetRancherProject(r.kc, name) + } + return nil, apierrors.NewNotFound(schema.GroupResource{ + Group: mgmtapi.GroupVersion.Group, + Resource: mgmtapi.ResourceProjects, + }, name) +} + +func (r *Storage) NewList() runtime.Object { + return &mgmtapi.ProjectList{} +} + +func (r *Storage) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) { + var projects []mgmtapi.Project + var err error + if clustermeta.IsRancherManaged(r.kc.RESTMapper()) { + projects, err = clustermeta.ListRancherProjects(r.kc) + if err != nil { + return nil, err + } + } + + result := mgmtapi.ProjectList{ + TypeMeta: metav1.TypeMeta{}, + // ListMeta: nil, + Items: projects, + } + + return &result, err +} + +func (r *Storage) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) { + return r.convertor.ConvertToTable(ctx, object, tableOptions) +} diff --git a/vendor/kmodules.xyz/client-go/apis/management/v1alpha1/project_types.go b/vendor/kmodules.xyz/client-go/apis/management/v1alpha1/project_types.go index b25a38044..4db8dc067 100644 --- a/vendor/kmodules.xyz/client-go/apis/management/v1alpha1/project_types.go +++ b/vendor/kmodules.xyz/client-go/apis/management/v1alpha1/project_types.go @@ -20,6 +20,12 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) +const ( + ResourceKindProject = "Project" + ResourceProject = "project" + ResourceProjects = "projects" +) + // ProjectSpec defines the desired state of Project type ProjectSpec struct { // +kubebuilder:default=User diff --git a/vendor/modules.txt b/vendor/modules.txt index 8a6cbe437..490bbe19f 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1764,7 +1764,7 @@ kmodules.xyz/authorizer/apiserver kmodules.xyz/authorizer/rbac kmodules.xyz/authorizer/rbac/helpers kmodules.xyz/authorizer/rbac/validation -# kmodules.xyz/client-go v0.25.34-0.20230920051128-f758ec0276ab +# kmodules.xyz/client-go v0.25.34-0.20230920151620-7cfdfeb7468e ## explicit; go 1.18 kmodules.xyz/client-go kmodules.xyz/client-go/api/v1