@@ -17,6 +17,7 @@ limitations under the License.
17
17
package client
18
18
19
19
import (
20
+ "net/http"
20
21
"time"
21
22
22
23
"k8s.io/apimachinery/pkg/runtime"
@@ -28,7 +29,7 @@ import (
28
29
"sigs.k8s.io/controller-runtime/pkg/client"
29
30
30
31
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 "
32
33
)
33
34
34
35
const (
@@ -37,46 +38,48 @@ const (
37
38
DefaultTimeoutSeconds = 10
38
39
)
39
40
41
+ var Scheme = runtime .NewScheme ()
42
+
43
+ func init () {
44
+ utilruntime .Must (clientgoscheme .AddToScheme (Scheme ))
45
+ utilruntime .Must (clusterv1alpha2 .AddToScheme (Scheme ))
46
+ }
47
+
40
48
func Client () (client.Client , error ) {
41
- restConfig , err := ctrl .GetConfig ()
49
+ config , err := ctrl .GetConfig ()
42
50
if err != nil {
43
51
return nil , err
44
52
}
45
53
46
- return newClient (restConfig )
54
+ return newClient (config )
47
55
}
48
56
49
57
func ClusterClient (cluster string ) (client.Client , error ) {
50
- restConfig , err := ctrl .GetConfig ()
58
+ config , err := ctrl .GetConfig ()
51
59
if err != nil {
52
60
return nil , err
53
61
}
54
62
55
- return newClient (restConfig , cluster )
63
+ return newClient (config , cluster )
56
64
}
57
65
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 ... )
60
68
}
61
69
62
- func newClient (restConfig * rest.Config , cluster ... string ) (client.Client , error ) {
70
+ func newClient (config * rest.Config , cluster ... string ) (client.Client , error ) {
63
71
var err error
64
-
65
72
if len (cluster ) == 1 {
66
- restConfig , err = ClusterConfigFor (restConfig , cluster [0 ])
73
+ config , err = ClusterConfigFor (config , cluster [0 ])
67
74
} else {
68
- restConfig , err = ConfigFor (restConfig )
75
+ config , err = ConfigFor (config )
69
76
}
70
77
if err != nil {
71
78
return nil , err
72
79
}
73
80
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 ,
80
83
})
81
84
if err != nil {
82
85
return nil , err
@@ -85,56 +88,63 @@ func newClient(restConfig *rest.Config, cluster ...string) (client.Client, error
85
88
return c , nil
86
89
}
87
90
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 {
93
94
return nil , err
94
95
}
95
96
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 )
101
98
if err != nil {
102
99
return nil , err
103
100
}
104
- configShallowCopy . Host += constants . ClusterAPIPath + cluster
105
- return configShallowCopy , nil
101
+
102
+ return kubeClient , nil
106
103
}
107
104
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 )
110
107
if err != nil {
111
108
return nil , err
112
109
}
113
110
114
- kubeClient , err := kubernetes .NewForConfig (clientConfig )
111
+ kubeClient , err := kubernetes .NewForConfig (config )
115
112
if err != nil {
116
113
return nil , err
117
114
}
118
115
119
116
return kubeClient , nil
120
117
}
121
118
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 {
125
122
return nil , err
126
123
}
127
124
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 {
130
136
return nil , err
131
137
}
132
138
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
134
145
}
135
146
136
147
func SetConfigDefaults (config * rest.Config ) error {
137
- config .Host += constants .ClusterPediaAPIPath
138
148
if config .Timeout == 0 {
139
149
config .Timeout = DefaultTimeoutSeconds * time .Second
140
150
}
0 commit comments