Revision 8efbb7197edf027decaf02447430da8f533c5507 authored by Tam Mach on 06 June 2024, 05:43:36 UTC, committed by Tam Mach on 13 June 2024, 04:03:39 UTC
This is to avoid any unnecessary reconciliation for non-GAMMA HTTPRoute:

- Explicitly check if Kind and Group are not nil, as per the Gateway API
  spec, the nil values is meant for Gateway.
- Add GAMMA check for backend services and listening service.

Additionally, one small correction on Reason status is added to make
sure that the space character is not used.

```
2024-06-06T05:34:31.583996151Z time="2024-06-06T05:34:31Z" level=error msg="Reconciler error" HTTPRoute="{attaches-to-wildcard-example-com-with-hostname-intersection gateway-conformance-infra}" controller=httproute controllerGroup=gateway.networking.k8s.io controllerKind=HTTPRoute error="failed to update HTTPRoute status: HTTPRoute.gateway.networking.k8s.io \"attaches-to-wildcard-example-com-with-hostname-intersection\" is invalid: parents[0].conditions[0].reason: Invalid value: \"Invalid HTTPRoute\": parents[0].conditions[0].reason in body should match '^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$'" name=attaches-to-wildcard-example-com-with-hostname-intersection namespace=gateway-conformance-infra reconcileID="\"2c43d9eb-52ad-4344-b0ff-e58c227221fb\"" subsys=controller-runtime
```

Relates: 363fdd4ff951e02ebf666b1dccf17d0dfb5a0f47
Signed-off-by: Tam Mach <tam.mach@cilium.io>
1 parent 4d6bee1
Raw File
cell.go
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package pprof

import (
	"errors"
	"net"
	"net/http"
	"net/http/pprof"
	"strconv"

	"github.com/cilium/hive/cell"
	"github.com/sirupsen/logrus"
	"github.com/spf13/pflag"
)

const (
	// Pprof is the flag to enable the registration of pprof HTTP handlers
	Pprof = "pprof"

	// PprofAddress is the flag to set the address that pprof listens on
	PprofAddress = "pprof-address"

	// PprofPort is the flag to set the port that pprof listens on
	PprofPort = "pprof-port"
)

type Server interface {
	// Port returns the port at which the server is listening
	Port() int
}

// Cell creates the cell for pprof, that registers its HTTP handlers to serve
// profiling data in the format expected by the pprof visualization tool.
var Cell = cell.Module(
	"pprof",
	"pprof HTTP server to expose runtime profiling data",

	// We don't call cell.Config directly here, because the operator
	// uses different flags names for the same pprof config flags.
	// Therefore, to register each flag with the correct name, we leave
	// the call to cell.Config to the user of the cell.

	// Provide coupled with Invoke is used to improve cell testability,
	// namely to allow taking a reference to the Server and call Port() on it.
	cell.Provide(newServer),
	cell.Invoke(func(srv Server) {}),
)

// Config contains the configuration for the pprof cell.
type Config struct {
	Pprof        bool
	PprofAddress string
	PprofPort    uint16
}

func (def Config) Flags(flags *pflag.FlagSet) {
	flags.Bool(Pprof, def.Pprof, "Enable serving pprof debugging API")
	flags.String(PprofAddress, def.PprofAddress, "Address that pprof listens on")
	flags.Uint16(PprofPort, def.PprofPort, "Port that pprof listens on")
}

func newServer(lc cell.Lifecycle, log logrus.FieldLogger, cfg Config) Server {
	if !cfg.Pprof {
		return nil
	}

	srv := &server{
		logger:  log,
		address: cfg.PprofAddress,
		port:    cfg.PprofPort,
	}
	lc.Append(srv)

	return srv
}

type server struct {
	logger logrus.FieldLogger

	address string
	port    uint16

	httpSrv  *http.Server
	listener net.Listener
}

func (s *server) Start(ctx cell.HookContext) error {
	listener, err := net.Listen("tcp", net.JoinHostPort(s.address, strconv.FormatUint(uint64(s.port), 10)))
	if err != nil {
		return err
	}
	s.listener = listener

	s.logger = s.logger.WithFields(logrus.Fields{
		"ip":   s.listener.Addr().(*net.TCPAddr).IP,
		"port": s.listener.Addr().(*net.TCPAddr).Port,
	})

	mux := http.NewServeMux()
	mux.HandleFunc("/debug/pprof/", pprof.Index)
	mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
	mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
	mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
	mux.HandleFunc("/debug/pprof/trace", pprof.Trace)

	s.httpSrv = &http.Server{
		Handler: mux,
	}
	go func() {
		if err := s.httpSrv.Serve(s.listener); !errors.Is(err, http.ErrServerClosed) {
			s.logger.WithError(err).Error("server stopped unexpectedly")
		}
	}()
	s.logger.Info("Started pprof server")

	return nil
}

func (s *server) Stop(ctx cell.HookContext) error {
	s.logger.Info("Stopped pprof server")
	return s.httpSrv.Shutdown(ctx)
}

func (s *server) Port() int {
	return s.listener.Addr().(*net.TCPAddr).Port
}
back to top