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

health: Add flag to set HTTP port #16926

Merged
merged 4 commits into from
Sep 29, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/cmdref/cilium-agent.md

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

4 changes: 2 additions & 2 deletions cilium-health/launch/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -20,7 +21,6 @@ import (
"github.com/cilium/cilium/pkg/defaults"
"github.com/cilium/cilium/pkg/endpoint"
"github.com/cilium/cilium/pkg/endpoint/regeneration"
healthDefaults "github.com/cilium/cilium/pkg/health/defaults"
"github.com/cilium/cilium/pkg/health/probe"
"github.com/cilium/cilium/pkg/identity/cache"
ipamOption "github.com/cilium/cilium/pkg/ipam/option"
Expand Down Expand Up @@ -360,7 +360,7 @@ func LaunchAsEndpoint(baseCtx context.Context,
ep.UpdateLabels(ctx, labels.LabelHealth, nil, true)

// Initialize the health client to talk to this instance.
client := &Client{host: "http://" + net.JoinHostPort(healthIP.String(), fmt.Sprintf("%d", healthDefaults.HTTPPathPort))}
client := &Client{host: "http://" + net.JoinHostPort(healthIP.String(), strconv.Itoa(option.Config.ClusterHealthPort))}
metrics.SubprocessStart.WithLabelValues(ciliumHealth).Inc()

return client, nil
Expand Down
1 change: 1 addition & 0 deletions cilium-health/launch/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func Launch() (*CiliumHealth, error) {
Debug: option.Config.Opts.IsEnabled(option.Debug),
ProbeInterval: serverProbeInterval,
ProbeDeadline: serverProbeDeadline,
HTTPPathPort: option.Config.ClusterHealthPort,
}

ch.server, err = server.NewServer(config)
Expand Down
3 changes: 2 additions & 1 deletion cilium-health/responder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"os/signal"

healthDefaults "github.com/cilium/cilium/pkg/health/defaults"
"github.com/cilium/cilium/pkg/health/probe/responder"
"github.com/cilium/cilium/pkg/pidfile"

Expand All @@ -31,7 +32,7 @@ func main() {
listen int
)
flag.StringVar(&pidfilePath, "pidfile", "", "Write pid to the specified file")
flag.IntVar(&listen, "listen", 4240, "Port on which the responder listens")
flag.IntVar(&listen, "listen", healthDefaults.HTTPPathPort, "Port on which the responder listens")
flag.Parse()

// Shutdown gracefully to halt server and remove pidfile
Expand Down
3 changes: 3 additions & 0 deletions daemon/cmd/daemon_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ func init() {
flags.Int(option.AgentHealthPort, defaults.AgentHealthPort, "TCP port for agent health status API")
option.BindEnv(option.AgentHealthPort)

flags.Int(option.ClusterHealthPort, defaults.ClusterHealthPort, "TCP port for cluster-wide network connectivity health API")
option.BindEnv(option.ClusterHealthPort)

flags.StringSlice(option.AgentLabels, []string{}, "Additional labels to identify this agent")
option.BindEnv(option.AgentLabels)

Expand Down
6 changes: 6 additions & 0 deletions install/kubernetes/cilium/templates/cilium-configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ data:
# for cilium-health.
agent-health-port: "{{ .Values.healthPort }}"
{{- end }}

{{- if hasKey .Values "clusterHealthPort" }}
joestringer marked this conversation as resolved.
Show resolved Hide resolved
# Set the TCP port for the agent health API. This port is used for cilium-health.
cluster-health-port: "{{ .Values.clusterHealthPort }}"
{{- end }}

{{- if hasKey .Values "policyEnforcementMode" }}
# The agent can be put into the following three policy enforcement modes
# default, always and never.
Expand Down
3 changes: 3 additions & 0 deletions pkg/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const (
// AgentHealthPort is the default value for option.AgentHealthPort
AgentHealthPort = 9876

// ClusterHealthPort is the default value for option.ClusterHealthPort
ClusterHealthPort = 4240

// GopsPortAgent is the default value for option.GopsPort in the agent
GopsPortAgent = 9890

Expand Down
11 changes: 1 addition & 10 deletions pkg/health/defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,5 @@ const (
SockPathEnv = "CILIUM_HEALTH_SOCK"

// HTTPPathPort is used for probing base HTTP path connectivity
HTTPPathPort = 4240

// L7PathPort is used for probing L7 path connectivity
L7PathPort = 4241

// ServicePathPort is used for probing service redirect path connectivity
ServicePathPort = 4242

// ServiceL7PathPort is used for probing service redirect path connectivity with L7
ServiceL7PathPort = 4243
HTTPPathPort = daemon.ClusterHealthPort
)
27 changes: 11 additions & 16 deletions pkg/health/server/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/cilium/cilium/api/v1/health/models"
ciliumModels "github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/health/defaults"
errordeveloper marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cilium/cilium/pkg/health/probe"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging/logfields"
Expand Down Expand Up @@ -204,15 +203,17 @@ func (p *prober) setNodes(added nodeMap, removed nodeMap) {
}
}

func (p *prober) httpProbe(node string, ip string, port int) *models.ConnectivityStatus {
const httpPathDescription = "Via L3"

func (p *prober) httpProbe(node string, ip string) *models.ConnectivityStatus {
result := &models.ConnectivityStatus{}

host := "http://" + net.JoinHostPort(ip, strconv.Itoa(port))
host := "http://" + net.JoinHostPort(ip, strconv.Itoa(p.server.Config.HTTPPathPort))
scopedLog := log.WithFields(logrus.Fields{
logfields.NodeName: node,
logfields.IPAddr: ip,
"host": host,
"path": PortToPaths[port],
"path": httpPathDescription,
})

scopedLog.Debug("Greeting host")
Expand Down Expand Up @@ -268,23 +269,17 @@ func (p *prober) runHTTPProbe() {
logfields.IPAddr: ip.String(),
})

status := &models.PathStatus{}
ports := map[int]**models.ConnectivityStatus{
defaults.HTTPPathPort: &status.HTTP,
}
for port, result := range ports {
*result = p.httpProbe(name, ip.String(), port)
if status.HTTP.Status != "" {
scopedLog.WithFields(logrus.Fields{
logfields.Port: port,
}).Debugf("Failed to probe: %s", status.HTTP.Status)
}
resp := p.httpProbe(name, ip.String())
if resp.Status != "" {
scopedLog.WithFields(logrus.Fields{
logfields.Port: p.server.Config.HTTPPathPort,
}).Debugf("Failed to probe: %s", resp.Status)
}

peer := ipString(ip.String())
p.Lock()
if _, ok := p.results[peer]; ok {
p.results[peer].HTTP = status.HTTP
p.results[peer].HTTP = resp
} else {
// While we weren't holding the lock, the
// pinger's OnIdle() callback fired and updated
Expand Down
32 changes: 9 additions & 23 deletions pkg/health/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ import (

var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "health-server")

// PortToPaths is a convenience map for access to the ports and their
// common string representations
PortToPaths = map[int]string{
defaults.HTTPPathPort: "Via L3",
}
)

// Config stores the configuration data for a cilium-health server.
Expand All @@ -38,6 +32,7 @@ type Config struct {
CiliumURI string
ProbeInterval time.Duration
ProbeDeadline time.Duration
HTTPPathPort int
}

// ipString is an IP address used as a more descriptive type name in maps.
Expand All @@ -58,8 +53,8 @@ type Server struct {
// a diff of the nodes added and removed based on this clientID.
clientID int64

tcpServers []*responder.Server // Servers for external pings
startTime time.Time
httpPathServer *responder.Server // HTTP server for external pings
startTime time.Time

// The lock protects against read and write access to the IP->Node map,
// the list of statuses as most recently seen, and the last time a
Expand Down Expand Up @@ -248,7 +243,7 @@ func (s *Server) runActiveServices() error {
}

// Serve spins up the following goroutines:
// * TCP API Server: Responders to the health API "/hello" message, one per path
// * HTTP API Server: Responder to the health API "/hello" message
// * Prober: Periodically run pings across the cluster at a configured interval
// and update the server's connectivity status cache.
// * Unix API Server: Handle all health API requests over a unix socket.
Expand All @@ -257,12 +252,9 @@ func (s *Server) runActiveServices() error {
func (s *Server) Serve() (err error) {
errors := make(chan error)

for i := range s.tcpServers {
srv := s.tcpServers[i]
go func() {
errors <- srv.Serve()
}()
}
go func() {
errors <- s.httpPathServer.Serve()
}()

go func() {
errors <- s.runActiveServices()
Expand All @@ -275,9 +267,7 @@ func (s *Server) Serve() (err error) {

// Shutdown server and clean up resources
func (s *Server) Shutdown() {
for i := range s.tcpServers {
s.tcpServers[i].Shutdown()
}
s.httpPathServer.Shutdown()
s.Server.Shutdown()
}

Expand Down Expand Up @@ -306,7 +296,6 @@ func NewServer(config Config) (*Server, error) {
server := &Server{
startTime: time.Now(),
Config: config,
tcpServers: []*responder.Server{},
connectivity: &healthReport{},
}

Expand All @@ -323,10 +312,7 @@ func NewServer(config Config) (*Server, error) {
server.Client = cl
server.Server = *server.newServer(swaggerSpec)

for port := range PortToPaths {
srv := responder.NewServer(port)
server.tcpServers = append(server.tcpServers, srv)
}
server.httpPathServer = responder.NewServer(config.HTTPPathPort)

return server, nil
}
11 changes: 9 additions & 2 deletions pkg/option/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ var (
)

const (
// AgentHealthPort is the TCP port for the agent health status API.
// AgentHealthPort is the TCP port for agent health status API
AgentHealthPort = "agent-health-port"

// ClusterHealthPort is the TCP port for cluster-wide network connectivity health API
ClusterHealthPort = "cluster-health-port"

// AgentLabels are additional labels to identify this agent
AgentLabels = "agent-labels"

Expand Down Expand Up @@ -1209,9 +1212,12 @@ type DaemonConfig struct {
// Monitor contains the configuration for the node monitor.
Monitor *models.MonitorStatus

// AgentHealthPort is the TCP port for the agent health status API.
// AgentHealthPort is the TCP port for agent health status API
AgentHealthPort int

// ClusterHealthPort is the TCP port for cluster-wide network connectivity health API
ClusterHealthPort int

// AgentLabels contains additional labels to identify this agent in monitor events.
AgentLabels []string

Expand Down Expand Up @@ -2364,6 +2370,7 @@ func (c *DaemonConfig) Populate() {
var err error

c.AgentHealthPort = viper.GetInt(AgentHealthPort)
c.ClusterHealthPort = viper.GetInt(ClusterHealthPort)
c.AgentLabels = viper.GetStringSlice(AgentLabels)
c.AllowICMPFragNeeded = viper.GetBool(AllowICMPFragNeeded)
c.AllowLocalhost = viper.GetString(AllowLocalhost)
Expand Down