Skip to content

Commit

Permalink
Implement project api (#245)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Sep 20, 2023
1 parent 898bf79 commit bcbdc7c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
13 changes: 13 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pkg/cmds/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
106 changes: 106 additions & 0 deletions pkg/registry/management/projects/storage.go
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bcbdc7c

Please sign in to comment.