Skip to content

Commit

Permalink
Add resourceoutlinefilter api (#351)
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha authored Dec 15, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ee0980a commit 3065eec
Showing 20 changed files with 1,825 additions and 508 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ require (
kmodules.xyz/go-containerregistry v0.0.12
kmodules.xyz/monitoring-agent-api v0.30.4
kmodules.xyz/offshoot-api v0.30.1
kmodules.xyz/resource-metadata v0.23.1-0.20241211072838-2c167223c93e
kmodules.xyz/resource-metadata v0.23.1
kmodules.xyz/resource-metrics v0.30.5
kmodules.xyz/resource-metrics/utils v0.30.4
kmodules.xyz/sets v0.29.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -939,8 +939,8 @@ kmodules.xyz/monitoring-agent-api v0.30.4 h1:6CTKxYJKpWDsDYb0WRBHGFoW3xQof05d+W8
kmodules.xyz/monitoring-agent-api v0.30.4/go.mod h1:ZuTQ5uGi6H80QLsOTuuC7m58dfXDGUv0YB+s059gnr4=
kmodules.xyz/offshoot-api v0.30.1 h1:TrulAYO+oBsXe9sZZGTmNWIuI8qD2izMpgcTSPvgAmI=
kmodules.xyz/offshoot-api v0.30.1/go.mod h1:T3mpjR6fui0QzOcmQvIuANytW48fe9ytmy/1cgx6D4g=
kmodules.xyz/resource-metadata v0.23.1-0.20241211072838-2c167223c93e h1:WPdeqiYbIs7pXYzuKGIp0MF6psiIISjfMA74T4j9500=
kmodules.xyz/resource-metadata v0.23.1-0.20241211072838-2c167223c93e/go.mod h1:fnT2UJke3zXWJy8Vz+SnB8RKnilB6szX0v5nudD+oPg=
kmodules.xyz/resource-metadata v0.23.1 h1:uEs7E2fUCACbFEkIVe2BbFl+U9kotvE3Op0j9oNPzVY=
kmodules.xyz/resource-metadata v0.23.1/go.mod h1:k7oxVO2BbgXOXfPrf6v8uAki52MLQ01mo4g0PcEyebM=
kmodules.xyz/resource-metrics v0.30.5 h1:ZhpGeR9DCz1HTrKUg/mWhr95wlFzCPRdgVAqwaggy1o=
kmodules.xyz/resource-metrics v0.30.5/go.mod h1:w9+rz7/s/kGP1GWzYSuRdCn+l7EwpesmESSEHkLBnIQ=
kmodules.xyz/resource-metrics/utils v0.30.4 h1:bJS/x0Qr7N1FFdxugFbzZ/Es6HVs4ptsFlhkmgj3jac=
2 changes: 2 additions & 0 deletions pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
@@ -62,6 +62,7 @@ import (
"kubeops.dev/ui-server/pkg/registry/meta/resourcegraph"
"kubeops.dev/ui-server/pkg/registry/meta/resourcelayout"
"kubeops.dev/ui-server/pkg/registry/meta/resourceoutline"
"kubeops.dev/ui-server/pkg/registry/meta/resourceoutlinefilter"
"kubeops.dev/ui-server/pkg/registry/meta/resourcequery"
"kubeops.dev/ui-server/pkg/registry/meta/resourcetabledefinition"
"kubeops.dev/ui-server/pkg/registry/meta/usermenu"
@@ -335,6 +336,7 @@ func (c completedConfig) New(ctx context.Context) (*UIServer, error) {
v1alpha1storage[rsapi.ResourceResourceGraphs] = resourcegraph.NewStorage(ctrlClient)
v1alpha1storage[rsapi.ResourceResourceLayouts] = resourcelayout.NewStorage(ctrlClient)
v1alpha1storage[rsapi.ResourceResourceOutlines] = resourceoutline.NewStorage()
v1alpha1storage[rsapi.ResourceResourceOutlineFilters] = resourceoutlinefilter.NewStorage(ctrlClient)
v1alpha1storage[rsapi.ResourceGatewayInfos] = gatewayinfo.NewStorage(ctrlClient)
v1alpha1storage[uiapi.ResourceClusterProfiles] = clusterprofilestorage.NewStorage(ctrlClient)
v1alpha1storage[uiapi.ResourceResourceEditors] = resourceeditor.NewStorage(ctrlClient)
1 change: 1 addition & 0 deletions pkg/cmds/server/start.go
Original file line number Diff line number Diff line change
@@ -157,6 +157,7 @@ func (o *UIServerOptions) Config() (*apiserver.Config, error) {
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceResourceGraphs),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceResourceLayouts),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceResourceOutlines),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceResourceOutlineFilters),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceResourceQueries),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, rsapi.ResourceResourceTableDefinitions),
fmt.Sprintf("/apis/%s/%s", rsapi.SchemeGroupVersion, uiapi.ResourceResourceEditors),
149 changes: 149 additions & 0 deletions pkg/registry/meta/resourceoutlinefilter/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
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 resourceoutlinefilter

import (
"context"
"fmt"
"strconv"
"strings"

kerr "k8s.io/apimachinery/pkg/api/errors"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/rest"
"kmodules.xyz/resource-metadata/apis/meta"
rsapi "kmodules.xyz/resource-metadata/apis/meta/v1alpha1"
"kmodules.xyz/resource-metadata/hub/resourceoutlines"
"kmodules.xyz/resource-metadata/pkg/layouts"
"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.Getter = &Storage{}
_ rest.Lister = &Storage{}
_ rest.SingularNameProvider = &Storage{}
)

func NewStorage(kc client.Client) *Storage {
return &Storage{
kc: kc,
convertor: rest.NewDefaultTableConvertor(schema.GroupResource{
Group: rsapi.SchemeGroupVersion.Group,
Resource: rsapi.ResourceResourceOutlineFilters,
}),
}
}

func (r *Storage) GroupVersionKind(_ schema.GroupVersion) schema.GroupVersionKind {
return rsapi.SchemeGroupVersion.WithKind(rsapi.ResourceKindResourceOutlineFilter)
}

func (r *Storage) NamespaceScoped() bool {
return false
}

func (r *Storage) GetSingularName() string {
return strings.ToLower(rsapi.ResourceKindResourceOutlineFilter)
}

// Getter
func (r *Storage) New() runtime.Object {
return &rsapi.ResourceOutlineFilter{}
}

func (r *Storage) Destroy() {}

func (r *Storage) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
obj, err := resourceoutlines.LoadByName(name)
if err != nil {
return nil, kerr.NewNotFound(schema.GroupResource{Group: meta.GroupName, Resource: rsapi.ResourceKindResourceOutline}, name)
}
filter, err := layouts.GetResourceOutlineFilter(r.kc, obj)
if err != nil {
return nil, kerr.NewInternalError(err)
}
return &rsapi.ResourceOutlineFilter{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: filter.Name,
},
Spec: filter.Spec,
}, nil
}

// Lister
func (r *Storage) NewList() runtime.Object {
return &rsapi.ResourceOutlineFilterList{}
}

func (r *Storage) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
if options.FieldSelector != nil && !options.FieldSelector.Empty() {
return nil, kerr.NewBadRequest("fieldSelector is not a supported")
}

objs := resourceoutlines.List()

if options.Continue != "" {
start, err := strconv.Atoi(options.Continue)
if err != nil {
return nil, kerr.NewBadRequest(fmt.Sprintf("invalid continue option, err:%v", err))
}
if start > len(objs) {
return r.NewList(), nil
}
objs = objs[start:]
}
if options.Limit > 0 && int64(len(objs)) > options.Limit {
objs = objs[:options.Limit]
}

items := make([]rsapi.ResourceOutlineFilter, 0, len(objs))
for _, obj := range objs {
if options.LabelSelector != nil && !options.LabelSelector.Matches(labels.Set(obj.GetLabels())) {
continue
}
filter, err := layouts.GetResourceOutlineFilter(r.kc, &obj)
if err != nil {
return nil, kerr.NewInternalError(err)
}
items = append(items, rsapi.ResourceOutlineFilter{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: filter.Name,
},
Spec: filter.Spec,
})
}

return &rsapi.ResourceOutlineFilterList{Items: items}, nil
}

func (r *Storage) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
return r.convertor.ConvertToTable(ctx, object, tableOptions)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -65,6 +65,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&ResourceEditorList{},
&ResourceOutline{},
&ResourceOutlineList{},
&ResourceOutlineFilter{},
&ResourceOutlineFilterList{},
&ResourceLayout{},
&ResourceLayoutList{},
&Menu{},
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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 v1alpha1

import (
uiapi "kmodules.xyz/resource-metadata/apis/ui/v1alpha1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
ResourceKindResourceOutlineFilter = "ResourceOutlineFilter"
ResourceResourceOutlineFilter = "resourceoutlinefilter"
ResourceResourceOutlineFilters = "resourceoutlinefilters"
)

// +genclient
// +genclient:nonNamespaced
// +genclient:onlyVerbs=get
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// +kubebuilder:object:root=true
// +kubebuilder:resource:path=resourceoutlinefilters,singular=resourceoutlinefilter,scope=Cluster
type ResourceOutlineFilter struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec uiapi.ResourceOutlineFilterSpec `json:"spec,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root=true

type ResourceOutlineFilterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ResourceOutlineFilter `json:"items,omitempty"`
}
Loading

0 comments on commit 3065eec

Please sign in to comment.