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

add collection resource client #43

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions client/client.go
Expand Up @@ -27,8 +27,8 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/clusterpedia-io/client-go/constants"
clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2"
"github.com/clusterpedia-io/client-go/constants"
)

const (
Expand Down Expand Up @@ -89,7 +89,7 @@ func ConfigFor(cfg *rest.Config) (*rest.Config, error) {
configShallowCopy := *cfg

// reset clusterpedia api path
if err := setConfigDefaults(&configShallowCopy); err != nil {
if err := SetConfigDefaults(&configShallowCopy); err != nil {
return nil, err
}

Expand Down Expand Up @@ -133,7 +133,7 @@ func NewClusterForConfig(cfg *rest.Config, cluster string) (kubernetes.Interface
return kubeClient, nil
}

func setConfigDefaults(config *rest.Config) error {
func SetConfigDefaults(config *rest.Config) error {
config.Host += constants.ClusterPediaAPIPath
if config.Timeout == 0 {
config.Timeout = DefaultTimeoutSeconds * time.Second
Expand Down
46 changes: 46 additions & 0 deletions clusterpediaclient/scheme/scheme.go
@@ -0,0 +1,46 @@
/*
Copyright 2021 clusterpedia 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 scheme

import (
clusterpediav1beta1 "github.com/clusterpedia-io/api/clusterpedia/v1beta1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
)

var Scheme = runtime.NewScheme()
var Codecs = serializer.NewCodecFactory(Scheme)
var ParameterCodec = runtime.NewParameterCodec(Scheme)
var localSchemeBuilder = runtime.NewSchemeBuilder()

func init() {
localSchemeBuilder.Register(addKnownTypes)
utilruntime.Must(localSchemeBuilder.AddToScheme(Scheme))
}

// scheme clusterpediav1beta1 miss metav1.ListOption.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(clusterpediav1beta1.SchemeGroupVersion,
&clusterpediav1beta1.CollectionResource{},
&clusterpediav1beta1.CollectionResourceList{},
)
metav1.AddToGroupVersion(scheme, clusterpediav1beta1.SchemeGroupVersion)
return nil
}
54 changes: 54 additions & 0 deletions clusterpediaclient/simple.go
@@ -0,0 +1,54 @@
/*
Copyright 2021 clusterpedia 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 clusterpediaclient

import (
"fmt"

"github.com/clusterpedia-io/client-go/clusterpediaclient/v1beta1"

"k8s.io/client-go/rest"
"k8s.io/client-go/util/flowcontrol"
)

type clusterpediaClient struct {
pediaClusterClient *v1beta1.ClusterPediaV1beta1Client
}

// AppsV1beta1 retrieves the PediaClusterV1beta1
func (c *clusterpediaClient) PediaClusterV1beta1() v1beta1.ClusterPediaV1beta1 {
return c.pediaClusterClient
}

func NewForConfig(cfg *rest.Config) (*clusterpediaClient, error) {
configShallowCopy := cfg
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
if configShallowCopy.Burst <= 0 {
return nil, fmt.Errorf("burst is required to be greater than 0 when RateLimiter is not set and QPS is set to greater than 0")
}
configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst)
}

var err error
var cc clusterpediaClient
cc.pediaClusterClient, err = v1beta1.NewForConfig(configShallowCopy)
if err != nil {
return nil, err
}

return &cc, nil
}
130 changes: 130 additions & 0 deletions clusterpediaclient/v1beta1/collectionresource.go
@@ -0,0 +1,130 @@
/*
Copyright 2021 clusterpedia 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 v1beta1

import (
"context"
"net/http"
"time"

clusterpediav1beta1 "github.com/clusterpedia-io/api/clusterpedia/v1beta1"
scheme "github.com/clusterpedia-io/client-go/clusterpediaclient/scheme"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
)

type ClusterPediaV1beta1 interface {
CollectionResource() CollectionResourceInterface
}

type ClusterPediaV1beta1Client struct {
restClient rest.Interface
}

// New creates a new CoreV1Client for the given RESTClient.
func NewForConfig(c *rest.Config) (*ClusterPediaV1beta1Client, error) {
config := *c
if err := setConfigDefaults(&config); err != nil {
return nil, err
}
httpClient, err := rest.HTTPClientFor(&config)
if err != nil {
return nil, err
}
return NewForConfigAndClient(&config, httpClient)
}

func NewForConfigAndClient(c *rest.Config, h *http.Client) (*ClusterPediaV1beta1Client, error) {
config := *c
if err := setConfigDefaults(&config); err != nil {
return nil, err
}
client, err := rest.RESTClientForConfigAndClient(&config, h)
if err != nil {
return nil, err
}
return &ClusterPediaV1beta1Client{client}, nil
}

func setConfigDefaults(config *rest.Config) error {
gv := clusterpediav1beta1.SchemeGroupVersion
config.GroupVersion = &gv
config.APIPath = "/apis"
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()

if config.UserAgent == "" {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}

return nil
}

func (c *ClusterPediaV1beta1Client) CollectionResource() CollectionResourceInterface {
return &CollectionResource{c.restClient}
}

type CollectionResourceInterface interface {
Get(ctx context.Context, name string, opts metav1.GetOptions) (*clusterpediav1beta1.CollectionResource, error)
List(ctx context.Context, opts metav1.ListOptions) (*clusterpediav1beta1.CollectionResourceList, error)
Fetch(ctx context.Context, name string, opts metav1.ListOptions, params map[string]string) (*clusterpediav1beta1.CollectionResource, error)
}

type CollectionResource struct {
client rest.Interface
}

func (c *CollectionResource) Get(ctx context.Context, name string, opts metav1.GetOptions) (result *clusterpediav1beta1.CollectionResource, err error) {
result = &clusterpediav1beta1.CollectionResource{}
err = c.client.Get().
Resource("collectionresources").
Name(name).
VersionedParams(&opts, scheme.ParameterCodec).
Do(ctx).
Into(result)
return
}

func (c *CollectionResource) List(ctx context.Context, opts metav1.ListOptions) (result *clusterpediav1beta1.CollectionResourceList, err error) {
var timeout time.Duration
if opts.TimeoutSeconds != nil {
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
}
result = &clusterpediav1beta1.CollectionResourceList{}
err = c.client.Get().
Resource("collectionresources").
VersionedParams(&opts, scheme.ParameterCodec).
Timeout(timeout).
Do(ctx).
Into(result)
return
}

func (c *CollectionResource) Fetch(ctx context.Context, name string, opts metav1.ListOptions, params map[string]string) (result *clusterpediav1beta1.CollectionResource, err error) {
request := c.client.Get().
Resource("collectionresources").
Name(name).
VersionedParams(&opts, scheme.ParameterCodec)

for p, v := range params {
request.Param(p, v)
}

result = &clusterpediav1beta1.CollectionResource{}
request.Do(ctx).Into(result)
return
}
16 changes: 16 additions & 0 deletions customclient/interface.go
@@ -1,3 +1,19 @@
/*
Copyright 2021 clusterpedia 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 customclient

import (
Expand Down
16 changes: 16 additions & 0 deletions customclient/scheme.go
@@ -1,3 +1,19 @@
/*
Copyright 2021 clusterpedia 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 customclient

import (
Expand Down
16 changes: 16 additions & 0 deletions customclient/simple.go
@@ -1,3 +1,19 @@
/*
Copyright 2021 clusterpedia 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 customclient

import (
Expand Down
62 changes: 62 additions & 0 deletions examples/clusterpedia-client/main.go
@@ -0,0 +1,62 @@
/*
Copyright 2021 clusterpedia 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 main

import (
"context"
"fmt"

clusterpediaclient "github.com/clusterpedia-io/client-go/clusterpediaclient"
"github.com/clusterpedia-io/client-go/tools/builder"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

ctrl "sigs.k8s.io/controller-runtime"
)

func main() {
restConfig, err := ctrl.GetConfig()
if err != nil {
panic(err)
}
cc, err := clusterpediaclient.NewForConfig(restConfig)
if err != nil {
panic(err)
}

collectionResource, err := cc.PediaClusterV1beta1().CollectionResource().List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err)
}

for _, item := range collectionResource.Items {
fmt.Printf("resource info: %v\n", item)
}

// build listOptions
options := builder.ListOptionsBuilder().
Namespaces("kube-system").
Options()

resources, err := cc.PediaClusterV1beta1().CollectionResource().Fetch(context.TODO(), "workflows", options, nil)
if err != nil {
panic(err)
}

for _, item := range resources.Items {
fmt.Printf("resource info: %v\n", item)
}
}