feat: support connect one namespace but proxy workload in another namespace (#496)

This commit is contained in:
naison
2025-03-30 11:50:11 +08:00
committed by GitHub
parent 08bcbe1611
commit a030dc582b
31 changed files with 545 additions and 615 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net/http"
"os"
@@ -28,8 +29,13 @@ func Main(f util.Factory) error {
if err != nil {
return err
}
var ns string
_, err = clientset.CoreV1().Services(config.KubevpnNamespace).Get(context.Background(), config.ConfigMapPodTrafficManager, metav1.GetOptions{})
if err == nil {
ns = config.KubevpnNamespace
}
h := &admissionReviewHandler{f: f, clientset: clientset}
h := &admissionReviewHandler{f: f, clientset: clientset, ns: ns}
http.HandleFunc("/pods", func(w http.ResponseWriter, r *http.Request) {
serve(w, r, newDelegateToV1AdmitHandler(h.admitPods))
})

View File

@@ -23,6 +23,7 @@ import (
type admissionReviewHandler struct {
sync.Mutex
f cmdutil.Factory
ns string
clientset *kubernetes.Clientset
}

View File

@@ -80,8 +80,8 @@ func (h *admissionReviewHandler) handleCreate(ar v1.AdmissionReview) *v1.Admissi
// 2) release old ip
h.Lock()
defer h.Unlock()
cmi := h.clientset.CoreV1().ConfigMaps(ar.Request.Namespace)
manager := dhcp.NewDHCPManager(cmi, ar.Request.Namespace)
mapInterface := h.clientset.CoreV1().ConfigMaps(util.If(h.ns != "", h.ns, ar.Request.Namespace))
manager := dhcp.NewDHCPManager(mapInterface, util.If(h.ns != "", h.ns, ar.Request.Namespace))
var ips []net.IP
for k := 0; k < len(container.Env); k++ {
envVar := container.Env[k]
@@ -180,8 +180,8 @@ func (h *admissionReviewHandler) handleDelete(ar v1.AdmissionReview) *v1.Admissi
if len(ips) != 0 {
h.Lock()
defer h.Unlock()
cmi := h.clientset.CoreV1().ConfigMaps(ar.Request.Namespace)
err := dhcp.NewDHCPManager(cmi, ar.Request.Namespace).ReleaseIP(context.Background(), ips...)
mapInterface := h.clientset.CoreV1().ConfigMaps(util.If(h.ns != "", h.ns, ar.Request.Namespace))
err := dhcp.NewDHCPManager(mapInterface, util.If(h.ns != "", h.ns, ar.Request.Namespace)).ReleaseIP(context.Background(), ips...)
if err != nil {
plog.G(context.Background()).Errorf("Failed to release IP %v to DHCP server: %v", ips, err)
} else {