Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: feat: add ability to retrieve cluster yaml #93

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions pkg/apis/cluster/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ const (

// Cluster is an extension type to access a cluster
type Cluster struct {
metav1.TypeMeta
metav1.ObjectMeta
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ClusterSpec
Status ClusterStatus
Spec ClusterSpec `json:"spec"`
Status ClusterStatus `json:"status,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down Expand Up @@ -65,13 +65,14 @@ type ClusterTopology struct {
}

type ClusterStatus struct {
Healthy bool
Graph map[string]ClusterTopology
Healthy bool
Graph map[string]ClusterTopology
YAMLString string `json:"yaml,omitempty"`
}

type ClusterAccess struct {
Endpoint string
CABundle []byte
CABundle []byte `json:"caBundle,omitempty"`
Insecure *bool
Credential *ClusterAccessCredential
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/cluster/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ type ClusterTopology struct {

type ClusterStatus struct {
// +optional
Healthy bool `json:"healthy,omitempty"`
Graph map[string]ClusterTopology `json:"graph,omitempty"`
Healthy bool `json:"healthy,omitempty"`
Graph map[string]ClusterTopology `json:"graph,omitempty"`
YAMLString string `json:"yaml,omitempty"`
}

type ClusterAccess struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/cluster/v1beta1/zz_generated.conversion.go

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

6 changes: 6 additions & 0 deletions pkg/generated/openapi/zz_generated.openapi.go

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

2 changes: 2 additions & 0 deletions pkg/registry/cluster/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Storage struct {
Cluster *REST
Status *StatusREST
Proxy *ProxyREST
YAML *YAMLRest
}

// NewREST returns a RESTStorage object that will work against API services.
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewREST(optsGetter generic.RESTOptionsGetter) (*Storage, error) {
Cluster: &REST{store},
Status: &StatusREST{&statusStore},
Proxy: &ProxyREST{store},
YAML: &YAMLRest{store},
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/registry/cluster/storage_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (p RESTStorageProvider) NewRESTStorage(restOptionsGetter generic.RESTOption
v1beta1["clusters"] = clusterStorage.Cluster
v1beta1["clusters/status"] = clusterStorage.Status
v1beta1["clusters/proxy"] = clusterStorage.Proxy
v1beta1["clusters/yaml"] = clusterStorage.YAML

apiGroupInfo.VersionedResourcesStorageMap["v1beta1"] = v1beta1
return apiGroupInfo, nil
Expand Down
90 changes: 90 additions & 0 deletions pkg/registry/cluster/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
Copyright The Karbour 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.
*/

package cluster

import (
"context"
"fmt"

"github.com/KusionStack/karbour/pkg/apis/cluster"
clusterv1beta1 "github.com/KusionStack/karbour/pkg/apis/cluster/v1beta1"
yaml "gopkg.in/yaml.v3"

"k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/endpoints/request"
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/klog/v2"
)

var _ rest.Lister = &YAMLRest{}

type YAMLRest struct {
Store *genericregistry.Store
}

// New returns empty Cluster object.
func (r *YAMLRest) New() runtime.Object {
return &cluster.Cluster{}
}

func (r *YAMLRest) NewList() runtime.Object {
return &cluster.ClusterList{}
}

// Destroy cleans up resources on shutdown.
func (r *YAMLRest) Destroy() {
// Given that underlying store is shared with REST,
// we don't destroy it here explicitly.
}

// Get retrieves the object from the storage. It is required to support Patch.
func (r *YAMLRest) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return r.Store.Get(ctx, name, options)
}

func (r *YAMLRest) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
rt := &cluster.Cluster{}
info, ok := request.RequestInfoFrom(ctx)
if !ok {
return nil, fmt.Errorf("could not retrieve request info from context")
}
klog.Infof("Getting YAML for cluster %s...", info.Name)

// Locate the cluster resource based on the name in the request
obj, err := r.Store.Get(ctx, info.Name, &metav1.GetOptions{})
if err != nil {
return nil, err
}
obj.GetObjectKind().SetGroupVersionKind(clusterv1beta1.SchemeGroupVersion.WithKind("Cluster"))
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}
objYAML, err := yaml.Marshal(unstructuredObj)
if err != nil {
return nil, err
}
rt.Status.YAMLString = string(objYAML)
return rt, nil
}

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