feat: use api to get and release ip

This commit is contained in:
fengcaiwen
2023-02-22 17:11:13 +08:00
parent 48c34d8512
commit 8b4b38d6c2
11 changed files with 141 additions and 102 deletions

View File

@@ -55,7 +55,7 @@ func (d *dhcpServer) releaseIP(w http.ResponseWriter, r *http.Request) {
return
}
log.Infof("handling rent ip request, pod name: %s, ns: %s", podName, namespace)
log.Infof("handling release ip request, pod name: %s, ns: %s", podName, namespace)
clientset, err := d.f.KubernetesClientSet()
if err != nil {
log.Error(err)

View File

@@ -11,9 +11,10 @@ import (
log "github.com/sirupsen/logrus"
v1 "k8s.io/api/admission/v1"
"k8s.io/api/admission/v1beta1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"github.com/wencaiwulue/kubevpn/pkg/config"
)
// admissionReviewHandler is a handler to handle business logic, holding an util.Factory
@@ -123,17 +124,17 @@ func serve(w http.ResponseWriter, r *http.Request, admit admitHandler) {
func Main(f cmdutil.Factory) error {
h := &admissionReviewHandler{f: f}
http.HandleFunc("/pods", func(w http.ResponseWriter, r *http.Request) { serve(w, r, newDelegateToV1AdmitHandler(h.admitPods)) })
http.HandleFunc("/readyz", func(w http.ResponseWriter, req *http.Request) { _, _ = w.Write([]byte("ok")) })
http.HandleFunc("/readyz", func(w http.ResponseWriter, req *http.Request) { w.Write([]byte("ok")) })
s := dhcpServer{f: f}
http.HandleFunc("/rent/ip", s.rentIP)
http.HandleFunc("/release/ip", s.releaseIP)
cert, ok := os.LookupEnv(corev1.TLSCertKey)
http.HandleFunc(config.APIRentIP, s.rentIP)
http.HandleFunc(config.APIReleaseIP, s.releaseIP)
cert, ok := os.LookupEnv(config.TLSCertKey)
if !ok {
return fmt.Errorf("can not get %s from env", corev1.TLSCertKey)
return fmt.Errorf("can not get %s from env", config.TLSCertKey)
}
key, ok := os.LookupEnv(corev1.TLSPrivateKeyKey)
key, ok := os.LookupEnv(config.TLSPrivateKeyKey)
if !ok {
return fmt.Errorf("can not get %s from env", corev1.TLSPrivateKeyKey)
return fmt.Errorf("can not get %s from env", config.TLSPrivateKeyKey)
}
pair, err := tls.X509KeyPair([]byte(cert), []byte(key))
if err != nil {