forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
clustermesh-apiserver: Use hive and k8s-client
This refactors clustermesh-apiserver to use the k8s-client provided clientset. The health API server was refactored as a hive cell as it was a low-hanging fruit. Signed-off-by: Jussi Maki <[email protected]>
- Loading branch information
Showing
3 changed files
with
128 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/spf13/pflag" | ||
"go.uber.org/fx" | ||
|
||
"github.com/cilium/cilium/pkg/defaults" | ||
"github.com/cilium/cilium/pkg/hive" | ||
k8sClient "github.com/cilium/cilium/pkg/k8s/client" | ||
"github.com/cilium/cilium/pkg/option" | ||
) | ||
|
||
type HealthAPIServerConfig struct { | ||
ClusterMeshHealthPort int | ||
} | ||
|
||
func (HealthAPIServerConfig) CellFlags(flags *pflag.FlagSet) { | ||
flags.Int(option.ClusterMeshHealthPort, defaults.ClusterMeshHealthPort, "TCP port for ClusterMesh apiserver health API") | ||
} | ||
|
||
var healthAPIServerCell = hive.NewCellWithConfig[HealthAPIServerConfig]( | ||
"health-api-server", | ||
fx.Provide(newHealthAPIServer), | ||
fx.Invoke(func(HealthAPIServer) {}), // Always instantiate. | ||
) | ||
|
||
type HealthAPIServer struct { | ||
*http.Server | ||
} | ||
|
||
func newHealthAPIServer(lc fx.Lifecycle, clientset k8sClient.Clientset, cfg HealthAPIServerConfig) HealthAPIServer { | ||
mux := http.NewServeMux() | ||
|
||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { | ||
statusCode := http.StatusOK | ||
reply := "ok" | ||
|
||
if _, err := clientset.Discovery().ServerVersion(); err != nil { | ||
statusCode = http.StatusInternalServerError | ||
reply = err.Error() | ||
} | ||
w.WriteHeader(statusCode) | ||
if _, err := w.Write([]byte(reply)); err != nil { | ||
log.WithError(err).Error("Failed to respond to /healthz request") | ||
} | ||
}) | ||
|
||
srv := &http.Server{ | ||
Handler: mux, | ||
Addr: fmt.Sprintf(":%d", cfg.ClusterMeshHealthPort), | ||
} | ||
|
||
lc.Append(fx.Hook{ | ||
OnStart: func(context.Context) error { | ||
go func() { | ||
log.Info("Started health API") | ||
if err := srv.ListenAndServe(); err != nil { | ||
log.WithError(err).Fatalf("Unable to start health API") | ||
} | ||
}() | ||
return nil | ||
}, | ||
OnStop: srv.Shutdown, | ||
}) | ||
|
||
return HealthAPIServer{srv} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters