@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "flag"
2222 "fmt"
23+ "io/ioutil"
2324 "net"
2425 "net/http"
2526 "os"
@@ -35,7 +36,6 @@ import (
3536
3637 "github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/constants"
3738 "github.com/KusionStack/controller-mesh/pkg/client"
38-
3939 proxyapiserver "github.com/KusionStack/controller-mesh/pkg/proxy/apiserver"
4040 proxycache "github.com/KusionStack/controller-mesh/pkg/proxy/cache"
4141 "github.com/KusionStack/controller-mesh/pkg/proxy/circuitbreaker"
5656 webhookCertDir = flag .String (constants .ProxyWebhookCertDirFlag , "" , "The directory where the webhook certs generated or mounted." )
5757
5858 proxyIptablePort = flag .Int (constants .ProxyIptablesFlag , constants .ProxyIptablesPort , "port that http-tproxy listens on" )
59+
60+ enableIpTable = os .Getenv (constants .EnvIPTable ) == "true"
5961)
6062
6163func main () {
@@ -66,8 +68,18 @@ func main() {
6668 klog .Fatalf ("Environment %s=%s %s=%s not exist." ,
6769 constants .EnvPodNamespace , os .Getenv (constants .EnvPodNamespace ), constants .EnvPodName , os .Getenv (constants .EnvPodName ))
6870 }
69- cfg := ctrl .GetConfigOrDie ()
70- cfg .UserAgent = "ctrlmesh"
71+ var cfg * rest.Config
72+
73+ if enableIpTable {
74+ var err error
75+ cfg , err = getRestConfig ()
76+ if err != nil {
77+ klog .Fatalf ("Failed to get rest config: %v" , err )
78+ }
79+ } else {
80+ cfg = ctrl .GetConfigOrDie ()
81+ }
82+ //cfg.UserAgent = "ctrlmesh"
7183 if err := client .NewRegistry (cfg ); err != nil {
7284 klog .Fatalf ("Failed to new client registry: %v" , err )
7385 }
@@ -165,3 +177,41 @@ func serveHTTP(ctx context.Context, readyHandler *healthz.Handler) {
165177 klog .Fatalf ("Serve HTTP shutting down on :%d: %v" , * metricsHealthPort , err )
166178 }
167179}
180+
181+ func getRestConfig () (* rest.Config , error ) {
182+ const (
183+ tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
184+ //rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/..data/ca.crt"
185+ )
186+ host , port := os .Getenv ("KUBERNETES_SERVICE_HOST" ), os .Getenv ("KUBERNETES_SERVICE_PORT" )
187+ if len (host ) == 0 || len (port ) == 0 {
188+ return nil , rest .ErrNotInCluster
189+ }
190+
191+ token , err := ioutil .ReadFile (tokenFile )
192+ if err != nil {
193+ return nil , err
194+ }
195+
196+ tlsClientConfig := rest.TLSClientConfig {Insecure : true }
197+
198+ //if _, err := certutil.NewPool(rootCAFile); err != nil {
199+ // klog.Errorf("Expected to load root CA config from %s, but got err: %v", rootCAFile, err)
200+ //} else {
201+ // tlsClientConfig.CAFile = rootCAFile
202+ //}
203+
204+ cfg := & rest.Config {
205+ // TODO: switch to using cluster DNS.
206+ Host : "https://" + net .JoinHostPort (host , port ),
207+ TLSClientConfig : tlsClientConfig ,
208+ BearerToken : string (token ),
209+ BearerTokenFile : tokenFile ,
210+
211+ Burst : 3000 ,
212+ QPS : 2000.0 ,
213+ }
214+ klog .V (3 ).Infof ("Starting with rest config: %v" , utils .DumpJSON (cfg ))
215+
216+ return cfg , nil
217+ }
0 commit comments