Skip to content

Commit

Permalink
operator/api: Modularize api server
Browse files Browse the repository at this point in the history
Modularize the operator api server into its own independent cell.  The
refactoring aims to keep the same behavior of the previous
implementation. Specifically, the refactored server:

- adds a custom handler for /healthz as an alias to /v1/healthz
- starts two HTTP servers, one listening on "127.0.0.1:0" and the other
  on "[::1]:0", in case the user is running ipv4 or ipv6 only

Signed-off-by: Fabio Falzoi <fabio.falzoi@isovalent.com>
  • Loading branch information
pippolo84 authored and dylandreimerink committed Apr 13, 2023
1 parent 0eb936e commit 11d5856
Show file tree
Hide file tree
Showing 18 changed files with 350 additions and 229 deletions.
1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-operator-alibabacloud_hive.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-operator-aws_hive.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-operator-aws_hive_dot-graph.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-operator-azure_hive.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-operator-generic_hive.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-operator_hive.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-operator_hive_dot-graph.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions operator/api/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,43 @@

package api

import (
"github.com/spf13/pflag"

"github.com/cilium/cilium/pkg/hive/cell"
)

const (
// OperatorAPIServeAddr is the "<ip>:<port>" on which to serve api requests
// from the operator.
// Use ":<port>" to bind on all interfaces.
// Use an empty string to bind on both "127.0.0.1:0" and "[::1]:0".
OperatorAPIServeAddr = "operator-api-serve-addr"
)

const (
// OperatorAPIServeAddrDefault is the default "<ip>:<port>" value on which to serve
// api requests from the operator.
OperatorAPIServeAddrDefault = "localhost:9234"
)

var ServerCell = cell.Module(
"cilium-operator-api",
"Cilium Operator API Server",

cell.Config(Config{}),
cell.Provide(newServer),
cell.Invoke(func(Server) {}),
)

type Config struct {
OperatorAPIServeAddr string
}

func (def Config) Flags(flags *pflag.FlagSet) {
flags.String(OperatorAPIServeAddr, OperatorAPIServeAddrDefault, "Address to serve API requests")
}

// SharedConfig contains the configuration that is shared between
// this module and others.
// This is done to avoid polluting this module with a direct dependency
Expand Down
25 changes: 0 additions & 25 deletions operator/api/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,6 @@ import (
"github.com/cilium/cilium/pkg/kvstore"
)

type getHealthz struct {
*Server
}

// NewGetHealthzHandler handles health requests.
func NewGetHealthzHandler(s *Server) operator.GetHealthzHandler {
return &getHealthz{Server: s}
}

// Handle handles GET requests for /healthz .
func (h *getHealthz) Handle(params operator.GetHealthzParams) middleware.Responder {
select {
// only start serving the real health check once all systems all up and running
case <-h.Server.allSystemsGo:
if err := h.Server.checkStatus(); err != nil {
log.WithError(err).Warn("Health check status")

return operator.NewGetHealthzInternalServerError().WithPayload(err.Error())
}
default:
}

return operator.NewGetHealthzOK().WithPayload("ok")
}

type kvstoreEnabledFunc func() bool

func HealthHandlerCell(kvstoreEnabled kvstoreEnabledFunc) cell.Cell {
Expand Down
19 changes: 0 additions & 19 deletions operator/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,6 @@ import (
"github.com/cilium/cilium/pkg/hive/cell"
)

type getMetrics struct {
*Server
}

// NewGetMetricsHandler handles metrics requests.
func NewGetMetricsHandler(s *Server) metrics.GetMetricsHandler {
return &getMetrics{Server: s}
}

// Handle handles GET requests for /metrics/ .
func (h *getMetrics) Handle(params metrics.GetMetricsParams) middleware.Responder {
m, err := opMetrics.DumpMetrics()
if err != nil {
return metrics.NewGetMetricsFailed()
}

return metrics.NewGetMetricsOK().WithPayload(m)
}

var MetricsHandlerCell = cell.Module(
"metrics-handler",
"Operator metrics HTTP handler",
Expand Down

0 comments on commit 11d5856

Please sign in to comment.