Skip to content

Commit

Permalink
enhance: iptables support
Browse files Browse the repository at this point in the history
  • Loading branch information
Eikykun committed Apr 18, 2024
1 parent 7e0ecd4 commit 682d413
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 19 deletions.
12 changes: 7 additions & 5 deletions pkg/apis/ctrlmesh/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ const (

// Labels
const (
CtrlmeshControlPrefix = "ctrlmesh.kusionstack.io/"
CtrlmeshIgnoreWebhookLabel = "ctrlmesh.kusionstack.io/ignore-webhook"
CtrlmeshIgnoreValidateLabel = "ctrlmesh.kusionstack.io/ignore-validate"
CtrlmeshDefaultReplicasLabel = "ctrlmesh.kusionstack.io/default-replicas"
CtrlmeshEnableProxyLabel = "ctrlmesh.kusionstack.io/enable-proxy"
CtrlmeshControlPrefix = "ctrlmesh.kusionstack.io/"
CtrlmeshIgnoreWebhookLabel = "ctrlmesh.kusionstack.io/ignore-webhook"
CtrlmeshIgnoreValidateLabel = "ctrlmesh.kusionstack.io/ignore-validate"
CtrlmeshDefaultReplicasLabel = "ctrlmesh.kusionstack.io/default-replicas"
CtrlmeshEnableProxyLabel = "ctrlmesh.kusionstack.io/enable-proxy"
CtrlmeshEnableIptableMode = "ctrlmesh.kusionstack.io/enable-iptables"

CtrlmeshAutoShardingRootLabel = "ctrlmesh.kusionstack.io/auto-sharding-root"
CtrlmeshInRollingLabel = "ctrlmesh.kusionstack.io/rolling"
CtrlmeshDisableFakeKubeconfigArgLabel = "ctrlmesh.kusionstack.io/disable-fake-kubeconfig-arg"
Expand Down
56 changes: 53 additions & 3 deletions pkg/cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"fmt"
"io/ioutil"

Check failure on line 23 in pkg/cmd/proxy/main.go

View workflow job for this annotation

GitHub Actions / Golang Lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package io or package os, and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"net"
"net/http"
"os"
Expand All @@ -35,7 +36,6 @@ import (

"github.com/KusionStack/controller-mesh/pkg/apis/ctrlmesh/constants"
"github.com/KusionStack/controller-mesh/pkg/client"

proxyapiserver "github.com/KusionStack/controller-mesh/pkg/proxy/apiserver"
proxycache "github.com/KusionStack/controller-mesh/pkg/proxy/cache"
"github.com/KusionStack/controller-mesh/pkg/proxy/circuitbreaker"
Expand All @@ -56,6 +56,8 @@ var (
webhookCertDir = flag.String(constants.ProxyWebhookCertDirFlag, "", "The directory where the webhook certs generated or mounted.")

proxyIptablePort = flag.Int(constants.ProxyIptablesFlag, constants.ProxyIptablesPort, "port that http-tproxy listens on")

enableIpTable = os.Getenv(constants.EnvIPTable) == "true"
)

func main() {
Expand All @@ -66,8 +68,18 @@ func main() {
klog.Fatalf("Environment %s=%s %s=%s not exist.",
constants.EnvPodNamespace, os.Getenv(constants.EnvPodNamespace), constants.EnvPodName, os.Getenv(constants.EnvPodName))
}
cfg := ctrl.GetConfigOrDie()
cfg.UserAgent = "ctrlmesh"
var cfg *rest.Config

if enableIpTable {
var err error
cfg, err = getRestConfig()
if err != nil {
klog.Fatalf("Failed to get rest config: %v", err)
}
} else {
cfg = ctrl.GetConfigOrDie()
}
//cfg.UserAgent = "ctrlmesh"
if err := client.NewRegistry(cfg); err != nil {
klog.Fatalf("Failed to new client registry: %v", err)
}
Expand Down Expand Up @@ -165,3 +177,41 @@ func serveHTTP(ctx context.Context, readyHandler *healthz.Handler) {
klog.Fatalf("Serve HTTP shutting down on :%d: %v", *metricsHealthPort, err)
}
}

func getRestConfig() (*rest.Config, error) {
const (
tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token"
//rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/..data/ca.crt"
)
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
if len(host) == 0 || len(port) == 0 {
return nil, rest.ErrNotInCluster
}

token, err := ioutil.ReadFile(tokenFile)
if err != nil {
return nil, err
}

tlsClientConfig := rest.TLSClientConfig{Insecure: true}

//if _, err := certutil.NewPool(rootCAFile); err != nil {
// klog.Errorf("Expected to load root CA config from %s, but got err: %v", rootCAFile, err)
//} else {
// tlsClientConfig.CAFile = rootCAFile
//}

cfg := &rest.Config{
// TODO: switch to using cluster DNS.
Host: "https://" + net.JoinHostPort(host, port),
TLSClientConfig: tlsClientConfig,
BearerToken: string(token),
BearerTokenFile: tokenFile,

Burst: 3000,
QPS: 2000.0,
}
klog.V(3).Infof("Starting with rest config: %v", utils.DumpJSON(cfg))

return cfg, nil
}
20 changes: 9 additions & 11 deletions pkg/webhook/pod/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
if *proxyImage == "" {
return fmt.Errorf("the images for ControllerMesh init or proxy container have not set in args")
}

enableIpTable := pod.Labels[ctrlmesh.CtrlmeshEnableIptableMode] == "true"
imagePullPolicy := v1.PullAlways
if *proxyImagePullPolicy == string(v1.PullIfNotPresent) {
imagePullPolicy = v1.PullIfNotPresent
Expand Down Expand Up @@ -177,6 +177,13 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
},
}

if enableIpTable {
proxyContainer.Env = append(proxyContainer.Env, v1.EnvVar{
Name: constants.EnvIPTable,
Value: "true",
})
}

if val, ok := pod.Annotations[ctrlmesh.CtrlmeshProxyContainerResourceAnno]; ok {
req := &v1.ResourceRequirements{}
if err := json.Unmarshal([]byte(val), req); err != nil {
Expand Down Expand Up @@ -213,15 +220,6 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
proxyContainer.Env = append(proxyContainer.Env, apiserverHostPortEnvs...)
}

ipTableEnvs := getEnv(pod, constants.EnvIPTable)
enableIpTable := false
if len(ipTableEnvs) > 0 {
initContainer.Env = append(initContainer.Env, ipTableEnvs...)
//proxyContainer.Env = append(proxyContainer.Env, ipTableEnvs...)
if ipTableEnvs[0].Value == "true" {
enableIpTable = true
}
}
if !enableIpTable {
if err := h.applyFakeConfigMap(pod); err != nil {
return err
Expand Down Expand Up @@ -271,7 +269,7 @@ func (h *MutatingHandler) injectByShardingConfig(ctx context.Context, pod *v1.Po
proxyContainer.VolumeMounts = append(proxyContainer.VolumeMounts, certVolumeMounts[0])
}
}
if *initImage != "" {
if enableIpTable && *initImage != "" {
pod.Spec.InitContainers = append([]v1.Container{*initContainer}, pod.Spec.InitContainers...)
}
if pod.Labels == nil {
Expand Down

0 comments on commit 682d413

Please sign in to comment.