Skip to content

Commit 6089dcd

Browse files
author
calvin0327
committed
add tansport for rest config
Signed-off-by: calvin0327 <[email protected]>
1 parent ba14329 commit 6089dcd

File tree

5 files changed

+240
-79
lines changed

5 files changed

+240
-79
lines changed

client/client.go

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package client
1818

1919
import (
20+
"net/http"
2021
"time"
2122

2223
"k8s.io/apimachinery/pkg/runtime"
@@ -28,7 +29,7 @@ import (
2829
"sigs.k8s.io/controller-runtime/pkg/client"
2930

3031
clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2"
31-
"github.com/clusterpedia-io/client-go/constants"
32+
"github.com/clusterpedia-io/client-go/tools/transport"
3233
)
3334

3435
const (
@@ -37,46 +38,48 @@ const (
3738
DefaultTimeoutSeconds = 10
3839
)
3940

41+
var Scheme = runtime.NewScheme()
42+
43+
func init() {
44+
utilruntime.Must(clientgoscheme.AddToScheme(Scheme))
45+
utilruntime.Must(clusterv1alpha2.AddToScheme(Scheme))
46+
}
47+
4048
func Client() (client.Client, error) {
41-
restConfig, err := ctrl.GetConfig()
49+
config, err := ctrl.GetConfig()
4250
if err != nil {
4351
return nil, err
4452
}
4553

46-
return newClient(restConfig)
54+
return newClient(config)
4755
}
4856

4957
func ClusterClient(cluster string) (client.Client, error) {
50-
restConfig, err := ctrl.GetConfig()
58+
config, err := ctrl.GetConfig()
5159
if err != nil {
5260
return nil, err
5361
}
5462

55-
return newClient(restConfig, cluster)
63+
return newClient(config, cluster)
5664
}
5765

58-
func GetClient(restConfig *rest.Config, cluster ...string) (client.Client, error) {
59-
return newClient(restConfig, cluster...)
66+
func GetClient(config *rest.Config, cluster ...string) (client.Client, error) {
67+
return newClient(config, cluster...)
6068
}
6169

62-
func newClient(restConfig *rest.Config, cluster ...string) (client.Client, error) {
70+
func newClient(config *rest.Config, cluster ...string) (client.Client, error) {
6371
var err error
64-
6572
if len(cluster) == 1 {
66-
restConfig, err = ClusterConfigFor(restConfig, cluster[0])
73+
config, err = ClusterConfigFor(config, cluster[0])
6774
} else {
68-
restConfig, err = ConfigFor(restConfig)
75+
config, err = ConfigFor(config)
6976
}
7077
if err != nil {
7178
return nil, err
7279
}
7380

74-
scheme := runtime.NewScheme()
75-
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
76-
utilruntime.Must(clusterv1alpha2.AddToScheme(scheme))
77-
78-
c, err := client.New(restConfig, client.Options{
79-
Scheme: scheme,
81+
c, err := client.New(config, client.Options{
82+
Scheme: Scheme,
8083
})
8184
if err != nil {
8285
return nil, err
@@ -85,56 +88,63 @@ func newClient(restConfig *rest.Config, cluster ...string) (client.Client, error
8588
return c, nil
8689
}
8790

88-
func ConfigFor(cfg *rest.Config) (*rest.Config, error) {
89-
configShallowCopy := *cfg
90-
91-
// reset clusterpedia api path
92-
if err := SetConfigDefaults(&configShallowCopy); err != nil {
91+
func NewForConfig(cfg *rest.Config) (kubernetes.Interface, error) {
92+
config, err := ConfigFor(cfg)
93+
if err != nil {
9394
return nil, err
9495
}
9596

96-
return &configShallowCopy, nil
97-
}
98-
99-
func ClusterConfigFor(cfg *rest.Config, cluster string) (*rest.Config, error) {
100-
configShallowCopy, err := ConfigFor(cfg)
97+
kubeClient, err := kubernetes.NewForConfig(config)
10198
if err != nil {
10299
return nil, err
103100
}
104-
configShallowCopy.Host += constants.ClusterAPIPath + cluster
105-
return configShallowCopy, nil
101+
102+
return kubeClient, nil
106103
}
107104

108-
func NewForConfig(cfg *rest.Config) (kubernetes.Interface, error) {
109-
clientConfig, err := ConfigFor(cfg)
105+
func NewClusterForConfig(cfg *rest.Config, cluster string) (kubernetes.Interface, error) {
106+
config, err := ClusterConfigFor(cfg, cluster)
110107
if err != nil {
111108
return nil, err
112109
}
113110

114-
kubeClient, err := kubernetes.NewForConfig(clientConfig)
111+
kubeClient, err := kubernetes.NewForConfig(config)
115112
if err != nil {
116113
return nil, err
117114
}
118115

119116
return kubeClient, nil
120117
}
121118

122-
func NewClusterForConfig(cfg *rest.Config, cluster string) (kubernetes.Interface, error) {
123-
clientConfig, err := ClusterConfigFor(cfg, cluster)
124-
if err != nil {
119+
func ConfigFor(cfg *rest.Config) (*rest.Config, error) {
120+
configShallowCopy := *cfg
121+
if err := SetConfigDefaults(&configShallowCopy); err != nil {
125122
return nil, err
126123
}
127124

128-
kubeClient, err := kubernetes.NewForConfig(clientConfig)
129-
if err != nil {
125+
// wrap a transport to rest client config
126+
configShallowCopy.Wrap(func(rt http.RoundTripper) http.RoundTripper {
127+
return transport.NewTransport(configShallowCopy.Host, rt)
128+
})
129+
130+
return &configShallowCopy, nil
131+
}
132+
133+
func ClusterConfigFor(cfg *rest.Config, cluster string) (*rest.Config, error) {
134+
configShallowCopy := *cfg
135+
if err := SetConfigDefaults(&configShallowCopy); err != nil {
130136
return nil, err
131137
}
132138

133-
return kubeClient, nil
139+
// wrap a cluster transport to rest client config
140+
configShallowCopy.Wrap(func(rt http.RoundTripper) http.RoundTripper {
141+
return transport.NewTransportWithCluster(configShallowCopy.Host, cluster, rt)
142+
})
143+
144+
return &configShallowCopy, nil
134145
}
135146

136147
func SetConfigDefaults(config *rest.Config) error {
137-
config.Host += constants.ClusterPediaAPIPath
138148
if config.Timeout == 0 {
139149
config.Timeout = DefaultTimeoutSeconds * time.Second
140150
}
Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,28 @@
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-
171
package main
182

193
import (
204
"context"
215
"fmt"
226

23-
clusterpediaclient "github.com/clusterpedia-io/client-go/clusterpediaclient"
24-
"github.com/clusterpedia-io/client-go/tools/builder"
257
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26-
278
ctrl "sigs.k8s.io/controller-runtime"
9+
10+
"github.com/clusterpedia-io/client-go/client"
2811
)
2912

3013
func main() {
31-
restConfig, err := ctrl.GetConfig()
14+
config, err := ctrl.GetConfig()
3215
if err != nil {
3316
panic(err)
3417
}
35-
cc, err := clusterpediaclient.NewForConfig(restConfig)
18+
client, err := client.NewClusterForConfig(config, "cluster1")
3619
if err != nil {
3720
panic(err)
3821
}
3922

40-
collectionResource, err := cc.PediaClusterV1beta1().CollectionResource().List(context.TODO(), metav1.ListOptions{})
23+
pod, err := client.CoreV1().Pods("default").Get(context.TODO(), "pod1", metav1.GetOptions{})
4124
if err != nil {
4225
panic(err)
4326
}
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-
}
27+
fmt.Println(pod)
6228
}
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+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
ctrl "sigs.k8s.io/controller-runtime"
25+
26+
clusterpediaclient "github.com/clusterpedia-io/client-go/clusterpediaclient"
27+
"github.com/clusterpedia-io/client-go/tools/builder"
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+
}

examples/transport/main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"net/http"
8+
9+
"github.com/clusterpedia-io/client-go/tools/transport"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
clientset "k8s.io/client-go/kubernetes"
12+
ctrl "sigs.k8s.io/controller-runtime"
13+
)
14+
15+
func main() {
16+
config, err := ctrl.GetConfig()
17+
if err != nil {
18+
log.Fatalf("failed to init config: %v", err)
19+
}
20+
config.Wrap(func(rt http.RoundTripper) http.RoundTripper {
21+
return transport.NewTransportWithCluster(config.Host, "cluster", rt)
22+
})
23+
24+
client, err := clientset.NewForConfig(config)
25+
if err != nil {
26+
log.Fatalf("failed to init clientset: %v", err)
27+
}
28+
29+
pod, err := client.CoreV1().Pods("demo-system").Get(context.TODO(), "pod1", metav1.GetOptions{})
30+
if err != nil {
31+
log.Fatalf("failed to list pods: %v", err)
32+
}
33+
fmt.Println(pod)
34+
}

0 commit comments

Comments
 (0)