Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

acquire leader lease before serving #1439

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions cmd/admission/admission.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package main

import (
"context"
"fmt"
"github.com/hwameistor/hwameistor/pkg/local-storage/utils"
hookcfg "github.com/hwameistor/hwameistor/pkg/webhook/config"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -31,15 +35,29 @@ func main() {
log.Fatal("--cert-dir, --tls-cert-file, --tls-private-key-file is required")
}

mux := http.NewServeMux()
mux.Handle("/mutate", app.RegisterHwameiStorMutateWebhooks(*options))
server := &http.Server{
Addr: ":18443",
Handler: mux,
runServer := func(_ context.Context) {
// initialize webhook config first
if err := hookcfg.CreateOrUpdateWebHookConfig(); err != nil {
log.WithError(err).Fatal("failed to init webhook config")
}

mux := http.NewServeMux()
mux.Handle("/mutate", app.RegisterHwameiStorMutateWebhooks(*options))
mux.Handle("/healthz", app.RegisterHwameiStorHealthz(*options))
server := &http.Server{
Addr: ":18443",
Handler: mux,
}

log.Infof("admission server at %v, using tls-cert-file %s, tls-private-key-file %s", server.Addr, certPath, keyPath)
log.Fatal(server.ListenAndServeTLS(certPath, keyPath))
}

log.Infof("admission server at %v, using tls-cert-file %s, tls-private-key-file %s", server.Addr, certPath, keyPath)
log.Fatal(server.ListenAndServeTLS(certPath, keyPath))
// Acquired leader lease before proceeding
if err := utils.RunWithLease("" /*namespace*/, "" /*podName*/, fmt.Sprintf("hwameistor-admission-controller-master"), runServer); err != nil {
log.Error(err, "Failed to init cluster lease election")
os.Exit(1)
}
}

func setupLogging() {
Expand All @@ -54,6 +72,7 @@ func setupLogging() {
}

log.SetLevel(level)
log.Infof("log level set to %s", level.String())
log.SetFormatter(&log.JSONFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
Expand Down
11 changes: 11 additions & 0 deletions cmd/admission/app/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,23 @@ func serverHwameiStorMutateFunc(w http.ResponseWriter, r *http.Request, o webhoo
}
}

func healthz(w http.ResponseWriter, r *http.Request, o webhook.ServerOption) {
log.WithField("request.path", r.URL.Path).Info("health check ok")
_, _ = w.Write([]byte("ok"))
}

func RegisterHwameiStorMutateWebhooks(o webhook.ServerOption) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
serverHwameiStorMutateFunc(w, r, o)
})
}

func RegisterHwameiStorHealthz(o webhook.ServerOption) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
healthz(w, r, o)
})
}

func init() {
webhook.AddToMutateHooks(scheduler.NewPatchSchedulerWebHook())
if err := hookcfg.CreateOrUpdateWebHookConfig(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ require (
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.13.1
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions helm/hwameistor/templates/admission-controller.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ spec:
ports:
- containerPort: 18443
name: admission-api
readinessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 18443
scheme: HTTPS
volumeMounts:
- name: admission-controller-tls-certs
mountPath: /etc/webhook/certs
Expand Down
Loading