Skip to content

Commit 682d413

Browse files
committed
enhance: iptables support
1 parent 7e0ecd4 commit 682d413

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

pkg/apis/ctrlmesh/types.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ const (
3131

3232
// Labels
3333
const (
34-
CtrlmeshControlPrefix = "ctrlmesh.kusionstack.io/"
35-
CtrlmeshIgnoreWebhookLabel = "ctrlmesh.kusionstack.io/ignore-webhook"
36-
CtrlmeshIgnoreValidateLabel = "ctrlmesh.kusionstack.io/ignore-validate"
37-
CtrlmeshDefaultReplicasLabel = "ctrlmesh.kusionstack.io/default-replicas"
38-
CtrlmeshEnableProxyLabel = "ctrlmesh.kusionstack.io/enable-proxy"
34+
CtrlmeshControlPrefix = "ctrlmesh.kusionstack.io/"
35+
CtrlmeshIgnoreWebhookLabel = "ctrlmesh.kusionstack.io/ignore-webhook"
36+
CtrlmeshIgnoreValidateLabel = "ctrlmesh.kusionstack.io/ignore-validate"
37+
CtrlmeshDefaultReplicasLabel = "ctrlmesh.kusionstack.io/default-replicas"
38+
CtrlmeshEnableProxyLabel = "ctrlmesh.kusionstack.io/enable-proxy"
39+
CtrlmeshEnableIptableMode = "ctrlmesh.kusionstack.io/enable-iptables"
40+
3941
CtrlmeshAutoShardingRootLabel = "ctrlmesh.kusionstack.io/auto-sharding-root"
4042
CtrlmeshInRollingLabel = "ctrlmesh.kusionstack.io/rolling"
4143
CtrlmeshDisableFakeKubeconfigArgLabel = "ctrlmesh.kusionstack.io/disable-fake-kubeconfig-arg"

pkg/cmd/proxy/main.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -56,6 +56,8 @@ var (
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

6163
func 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+
}

pkg/webhook/pod/injector.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
131131
if *proxyImage == "" {
132132
return fmt.Errorf("the images for ControllerMesh init or proxy container have not set in args")
133133
}
134-
134+
enableIpTable := pod.Labels[ctrlmesh.CtrlmeshEnableIptableMode] == "true"
135135
imagePullPolicy := v1.PullAlways
136136
if *proxyImagePullPolicy == string(v1.PullIfNotPresent) {
137137
imagePullPolicy = v1.PullIfNotPresent
@@ -177,6 +177,13 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
177177
},
178178
}
179179

180+
if enableIpTable {
181+
proxyContainer.Env = append(proxyContainer.Env, v1.EnvVar{
182+
Name: constants.EnvIPTable,
183+
Value: "true",
184+
})
185+
}
186+
180187
if val, ok := pod.Annotations[ctrlmesh.CtrlmeshProxyContainerResourceAnno]; ok {
181188
req := &v1.ResourceRequirements{}
182189
if err := json.Unmarshal([]byte(val), req); err != nil {
@@ -213,15 +220,6 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
213220
proxyContainer.Env = append(proxyContainer.Env, apiserverHostPortEnvs...)
214221
}
215222

216-
ipTableEnvs := getEnv(pod, constants.EnvIPTable)
217-
enableIpTable := false
218-
if len(ipTableEnvs) > 0 {
219-
initContainer.Env = append(initContainer.Env, ipTableEnvs...)
220-
//proxyContainer.Env = append(proxyContainer.Env, ipTableEnvs...)
221-
if ipTableEnvs[0].Value == "true" {
222-
enableIpTable = true
223-
}
224-
}
225223
if !enableIpTable {
226224
if err := h.applyFakeConfigMap(pod); err != nil {
227225
return err
@@ -271,7 +269,7 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
271269
proxyContainer.VolumeMounts = append(proxyContainer.VolumeMounts, certVolumeMounts[0])
272270
}
273271
}
274-
if *initImage != "" {
272+
if enableIpTable && *initImage != "" {
275273
pod.Spec.InitContainers = append([]v1.Container{*initContainer}, pod.Spec.InitContainers...)
276274
}
277275
if pod.Labels == nil {

0 commit comments

Comments
 (0)