Skip to content

Commit d877935

Browse files
authored
Merge pull request #43 from calvin0327/add-collectionresource-client
add collection resource client
2 parents 50d1a1a + 03ac625 commit d877935

File tree

11 files changed

+407
-21
lines changed

11 files changed

+407
-21
lines changed

client/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727
ctrl "sigs.k8s.io/controller-runtime"
2828
"sigs.k8s.io/controller-runtime/pkg/client"
2929

30-
"github.com/clusterpedia-io/client-go/constants"
3130
clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2"
31+
"github.com/clusterpedia-io/client-go/constants"
3232
)
3333

3434
const (
@@ -89,7 +89,7 @@ func ConfigFor(cfg *rest.Config) (*rest.Config, error) {
8989
configShallowCopy := *cfg
9090

9191
// reset clusterpedia api path
92-
if err := setConfigDefaults(&configShallowCopy); err != nil {
92+
if err := SetConfigDefaults(&configShallowCopy); err != nil {
9393
return nil, err
9494
}
9595

@@ -133,7 +133,7 @@ func NewClusterForConfig(cfg *rest.Config, cluster string) (kubernetes.Interface
133133
return kubeClient, nil
134134
}
135135

136-
func setConfigDefaults(config *rest.Config) error {
136+
func SetConfigDefaults(config *rest.Config) error {
137137
config.Host += constants.ClusterPediaAPIPath
138138
if config.Timeout == 0 {
139139
config.Timeout = DefaultTimeoutSeconds * time.Second
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2021 clusterpedia Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package scheme
18+
19+
import (
20+
clusterpediav1beta1 "github.com/clusterpedia-io/api/clusterpedia/v1beta1"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/runtime/serializer"
25+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
26+
)
27+
28+
var Scheme = runtime.NewScheme()
29+
var Codecs = serializer.NewCodecFactory(Scheme)
30+
var ParameterCodec = runtime.NewParameterCodec(Scheme)
31+
var localSchemeBuilder = runtime.NewSchemeBuilder()
32+
33+
func init() {
34+
localSchemeBuilder.Register(addKnownTypes)
35+
utilruntime.Must(localSchemeBuilder.AddToScheme(Scheme))
36+
}
37+
38+
// scheme clusterpediav1beta1 miss metav1.ListOption.
39+
func addKnownTypes(scheme *runtime.Scheme) error {
40+
scheme.AddKnownTypes(clusterpediav1beta1.SchemeGroupVersion,
41+
&clusterpediav1beta1.CollectionResource{},
42+
&clusterpediav1beta1.CollectionResourceList{},
43+
)
44+
metav1.AddToGroupVersion(scheme, clusterpediav1beta1.SchemeGroupVersion)
45+
return nil
46+
}

clusterpediaclient/simple.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2021 clusterpedia Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package clusterpediaclient
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/clusterpedia-io/client-go/clusterpediaclient/v1beta1"
23+
24+
"k8s.io/client-go/rest"
25+
"k8s.io/client-go/util/flowcontrol"
26+
)
27+
28+
type clusterpediaClient struct {
29+
pediaClusterClient *v1beta1.ClusterPediaV1beta1Client
30+
}
31+
32+
// AppsV1beta1 retrieves the PediaClusterV1beta1
33+
func (c *clusterpediaClient) PediaClusterV1beta1() v1beta1.ClusterPediaV1beta1 {
34+
return c.pediaClusterClient
35+
}
36+
37+
func NewForConfig(cfg *rest.Config) (*clusterpediaClient, error) {
38+
configShallowCopy := cfg
39+
if configShallowCopy.RateLimiter == nil && configShallowCopy.QPS > 0 {
40+
if configShallowCopy.Burst <= 0 {
41+
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")
42+
}
43+
configShallowCopy.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(configShallowCopy.QPS, configShallowCopy.Burst)
44+
}
45+
46+
var err error
47+
var cc clusterpediaClient
48+
cc.pediaClusterClient, err = v1beta1.NewForConfig(configShallowCopy)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
return &cc, nil
54+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
Copyright 2021 clusterpedia Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"context"
21+
"net/http"
22+
"time"
23+
24+
clusterpediav1beta1 "github.com/clusterpedia-io/api/clusterpedia/v1beta1"
25+
scheme "github.com/clusterpedia-io/client-go/clusterpediaclient/scheme"
26+
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/client-go/rest"
29+
)
30+
31+
type ClusterPediaV1beta1 interface {
32+
CollectionResource() CollectionResourceInterface
33+
}
34+
35+
type ClusterPediaV1beta1Client struct {
36+
restClient rest.Interface
37+
}
38+
39+
// New creates a new CoreV1Client for the given RESTClient.
40+
func NewForConfig(c *rest.Config) (*ClusterPediaV1beta1Client, error) {
41+
config := *c
42+
if err := setConfigDefaults(&config); err != nil {
43+
return nil, err
44+
}
45+
httpClient, err := rest.HTTPClientFor(&config)
46+
if err != nil {
47+
return nil, err
48+
}
49+
return NewForConfigAndClient(&config, httpClient)
50+
}
51+
52+
func NewForConfigAndClient(c *rest.Config, h *http.Client) (*ClusterPediaV1beta1Client, error) {
53+
config := *c
54+
if err := setConfigDefaults(&config); err != nil {
55+
return nil, err
56+
}
57+
client, err := rest.RESTClientForConfigAndClient(&config, h)
58+
if err != nil {
59+
return nil, err
60+
}
61+
return &ClusterPediaV1beta1Client{client}, nil
62+
}
63+
64+
func setConfigDefaults(config *rest.Config) error {
65+
gv := clusterpediav1beta1.SchemeGroupVersion
66+
config.GroupVersion = &gv
67+
config.APIPath = "/apis"
68+
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
69+
70+
if config.UserAgent == "" {
71+
config.UserAgent = rest.DefaultKubernetesUserAgent()
72+
}
73+
74+
return nil
75+
}
76+
77+
func (c *ClusterPediaV1beta1Client) CollectionResource() CollectionResourceInterface {
78+
return &CollectionResource{c.restClient}
79+
}
80+
81+
type CollectionResourceInterface interface {
82+
Get(ctx context.Context, name string, opts metav1.GetOptions) (*clusterpediav1beta1.CollectionResource, error)
83+
List(ctx context.Context, opts metav1.ListOptions) (*clusterpediav1beta1.CollectionResourceList, error)
84+
Fetch(ctx context.Context, name string, opts metav1.ListOptions, params map[string]string) (*clusterpediav1beta1.CollectionResource, error)
85+
}
86+
87+
type CollectionResource struct {
88+
client rest.Interface
89+
}
90+
91+
func (c *CollectionResource) Get(ctx context.Context, name string, opts metav1.GetOptions) (result *clusterpediav1beta1.CollectionResource, err error) {
92+
result = &clusterpediav1beta1.CollectionResource{}
93+
err = c.client.Get().
94+
Resource("collectionresources").
95+
Name(name).
96+
VersionedParams(&opts, scheme.ParameterCodec).
97+
Do(ctx).
98+
Into(result)
99+
return
100+
}
101+
102+
func (c *CollectionResource) List(ctx context.Context, opts metav1.ListOptions) (result *clusterpediav1beta1.CollectionResourceList, err error) {
103+
var timeout time.Duration
104+
if opts.TimeoutSeconds != nil {
105+
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
106+
}
107+
result = &clusterpediav1beta1.CollectionResourceList{}
108+
err = c.client.Get().
109+
Resource("collectionresources").
110+
VersionedParams(&opts, scheme.ParameterCodec).
111+
Timeout(timeout).
112+
Do(ctx).
113+
Into(result)
114+
return
115+
}
116+
117+
func (c *CollectionResource) Fetch(ctx context.Context, name string, opts metav1.ListOptions, params map[string]string) (result *clusterpediav1beta1.CollectionResource, err error) {
118+
request := c.client.Get().
119+
Resource("collectionresources").
120+
Name(name).
121+
VersionedParams(&opts, scheme.ParameterCodec)
122+
123+
for p, v := range params {
124+
request.Param(p, v)
125+
}
126+
127+
result = &clusterpediav1beta1.CollectionResource{}
128+
request.Do(ctx).Into(result)
129+
return
130+
}

customclient/interface.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2021 clusterpedia Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package customclient
218

319
import (

customclient/scheme.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2021 clusterpedia Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package customclient
218

319
import (

customclient/simple.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2021 clusterpedia Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package customclient
218

319
import (
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2021 clusterpedia Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
clusterpediaclient "github.com/clusterpedia-io/client-go/clusterpediaclient"
24+
"github.com/clusterpedia-io/client-go/tools/builder"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
27+
ctrl "sigs.k8s.io/controller-runtime"
28+
)
29+
30+
func main() {
31+
restConfig, err := ctrl.GetConfig()
32+
if err != nil {
33+
panic(err)
34+
}
35+
cc, err := clusterpediaclient.NewForConfig(restConfig)
36+
if err != nil {
37+
panic(err)
38+
}
39+
40+
collectionResource, err := cc.PediaClusterV1beta1().CollectionResource().List(context.TODO(), metav1.ListOptions{})
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
for _, item := range collectionResource.Items {
46+
fmt.Printf("resource info: %v\n", item)
47+
}
48+
49+
// build listOptions
50+
options := builder.ListOptionsBuilder().
51+
Namespaces("kube-system").
52+
Options()
53+
54+
resources, err := cc.PediaClusterV1beta1().CollectionResource().Fetch(context.TODO(), "workflows", options, nil)
55+
if err != nil {
56+
panic(err)
57+
}
58+
59+
for _, item := range resources.Items {
60+
fmt.Printf("resource info: %v\n", item)
61+
}
62+
}

0 commit comments

Comments
 (0)