Skip to content

Commit

Permalink
Merge branch 'main' into prepare-v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Mar 19, 2024
2 parents e6d2fd4 + b6bb7a3 commit de86073
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 26 deletions.
9 changes: 5 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,15 @@ run: webhook-certs-dir manifests generate install-gateway-api-crds install _ensu
# etc didn't change in between the runs.
.PHONY: _run
_run:
CONTROLLER_DEVELOPMENT_MODE=true go run ./main.go \
GATEWAY_OPERATOR_DEVELOPMENT_MODE=true go run ./main.go \
--no-leader-election \
-cluster-ca-secret-namespace kong-system \
-zap-time-encoding iso8601 \
-enable-controller-controlplane \
-enable-controller-gateway \
-enable-controller-aigateway \
-zap-log-level 2
-zap-time-encoding iso8601 \
-zap-log-level 2 \
-zap-devel true

SKAFFOLD_RUN_PROFILE ?= dev

Expand All @@ -458,7 +459,7 @@ run.skaffold:

.PHONY: debug
debug: webhook-certs-dir manifests generate install _ensure-kong-system-namespace
CONTROLLER_DEVELOPMENT_MODE=true dlv debug ./main.go -- \
GATEWAY_OPERATOR_DEVELOPMENT_MODE=true dlv debug ./main.go -- \
--no-leader-election \
-cluster-ca-secret-namespace kong-system \
--enable-controller-aigateway \
Expand Down
2 changes: 1 addition & 1 deletion config/debug/manager_debug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ spec:
- -zap-log-level=debug
name: manager
env:
- name: CONTROLLER_DEVELOPMENT_MODE
- name: GATEWAY_OPERATOR_DEVELOPMENT_MODE
value: "true"
resources:
limits:
Expand Down
2 changes: 1 addition & 1 deletion config/dev/manager_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spec:
- -enable-validating-webhook=true
name: manager
env:
- name: CONTROLLER_DEVELOPMENT_MODE
- name: GATEWAY_OPERATOR_DEVELOPMENT_MODE
value: "true"
resources:
limits:
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
cli := cli.New()
cfg := cli.Parse(os.Args[1:])

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&cfg.LoggerOpts)))
ctrl.SetLogger(zap.New(zap.UseFlagOptions(cfg.LoggerOpts)))

if err := manager.Run(cfg, scheme.Get(), manager.SetupControllersShim, admission.NewRequestHandler, nil); err != nil {
ctrl.Log.Error(err, "failed to run manager")
Expand Down
35 changes: 22 additions & 13 deletions modules/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"os"
"strings"

"github.com/samber/lo"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/kong/gateway-operator/modules/manager"
"github.com/kong/gateway-operator/modules/manager/logging"
"github.com/kong/gateway-operator/modules/manager/metadata"
Expand All @@ -19,7 +22,7 @@ func New() *CLI {
var cfg manager.Config
var deferCfg flagsForFurtherEvaluation

flagSet.BoolVar(&cfg.AnonymousReports, "anonymous-reports", true, "Send anonymized usage data to help improve Kong")
flagSet.BoolVar(&cfg.AnonymousReports, "anonymous-reports", true, "Send anonymized usage data to help improve Kong.")
flagSet.StringVar(&cfg.APIServerPath, "apiserver-host", "", "The Kubernetes API server URL. If not set, the operator will use cluster config discovery.")
flagSet.StringVar(&cfg.KubeconfigPath, "kubeconfig", "", "Path to the kubeconfig file.")

Expand All @@ -28,9 +31,9 @@ func New() *CLI {
flagSet.BoolVar(&deferCfg.DisableLeaderElection, "no-leader-election", false,
"Disable leader election for controller manager. Disabling this will not ensure there is only one active controller manager.")

flagSet.StringVar(&cfg.ControllerName, "controller-name", "", "a controller name to use if other than the default, only needed for multi-tenancy")
flagSet.StringVar(&cfg.ClusterCASecretName, "cluster-ca-secret", "kong-operator-ca", "name of the Secret containing the cluster CA certificate")
flagSet.StringVar(&deferCfg.ClusterCASecretNamespace, "cluster-ca-secret-namespace", "", "name of the namespace for Secret containing the cluster CA certificate")
flagSet.StringVar(&cfg.ControllerName, "controller-name", "", "Controller name to use if other than the default, only needed for multi-tenancy.")
flagSet.StringVar(&cfg.ClusterCASecretName, "cluster-ca-secret", "kong-operator-ca", "Name of the Secret containing the cluster CA certificate.")
flagSet.StringVar(&deferCfg.ClusterCASecretNamespace, "cluster-ca-secret-namespace", "", "Name of the namespace for Secret containing the cluster CA certificate.")

// controllers for standard APIs and features
flagSet.BoolVar(&cfg.GatewayControllerEnabled, "enable-controller-gateway", true, "Enable the Gateway controller.")
Expand All @@ -39,23 +42,33 @@ func New() *CLI {
flagSet.BoolVar(&cfg.DataPlaneBlueGreenControllerEnabled, "enable-controller-dataplane-bluegreen", true, "Enable the DataPlane BlueGreen controller. Mutually exclusive with DataPlane controller.")

// controllers for specialized APIs and features
flagSet.BoolVar(&cfg.AIGatewayControllerEnabled, "enable-controller-aigateway", false, "Enable the AIGateway controller. (Experimental)")
flagSet.BoolVar(&cfg.AIGatewayControllerEnabled, "enable-controller-aigateway", false, "Enable the AIGateway controller. (Experimental).")

// webhook and validation options
flagSet.BoolVar(&deferCfg.ValidatingWebhookEnabled, "enable-validating-webhook", true, "Enable the validating webhook.")

flagSet.BoolVar(&deferCfg.Version, "version", false, "Print version information")
flagSet.BoolVar(&deferCfg.Version, "version", false, "Print version information.")

developmentModeEnabled := manager.DefaultConfig().DevelopmentMode
if v := os.Getenv(envVarFlagPrefix + "DEVELOPMENT_MODE"); v == "true" { // TODO: clean env handling https://github.com/Kong/gateway-operator/issues/19
developmentModeEnabled = true
}
loggerOpts := lo.ToPtr(*manager.DefaultConfig().LoggerOpts)
loggerOpts.Development = developmentModeEnabled
loggerOpts.BindFlags(flagSet)

return &CLI{
flagSet: flagSet,
cfg: &cfg,
loggerOpts: loggerOpts,
deferFlagValues: &deferCfg,
}
}

// CLI represents command line interface for the operator.
type CLI struct {
flagSet *flag.FlagSet
flagSet *flag.FlagSet
loggerOpts *zap.Options

// deferFlagValues contains values of flags that require additional
// logic after parsing flagSet to determine desired configuration.
Expand Down Expand Up @@ -103,7 +116,7 @@ func (c *CLI) bindEnvVarsToFlags() (err error) {
// by the program. It returns config for controller manager.
func (c *CLI) Parse(arguments []string) manager.Config {
developmentModeEnabled := manager.DefaultConfig().DevelopmentMode
if v := os.Getenv("CONTROLLER_DEVELOPMENT_MODE"); v == "true" { // TODO: clean env handling https://github.com/Kong/gateway-operator/issues/19
if v := os.Getenv(envVarFlagPrefix + "DEVELOPMENT_MODE"); v == "true" { // TODO: clean env handling https://github.com/Kong/gateway-operator/issues/19
developmentModeEnabled = true
}

Expand All @@ -112,10 +125,6 @@ func (c *CLI) Parse(arguments []string) manager.Config {
webhookCertDir = certDir
}

loggerOpts := manager.DefaultConfig().LoggerOpts
loggerOpts.Development = developmentModeEnabled
loggerOpts.BindFlags(c.flagSet)

// Flags take precedence over environment variables,
// so we bind env vars first then parse aruments to override the values from flags.
if err := c.bindEnvVarsToFlags(); err != nil {
Expand Down Expand Up @@ -186,7 +195,7 @@ func (c *CLI) Parse(arguments []string) manager.Config {
c.cfg.ClusterCASecretNamespace = clusterCASecretNamespace
c.cfg.WebhookCertDir = webhookCertDir
c.cfg.ValidatingWebhookEnabled = validatingWebhookEnabled
c.cfg.LoggerOpts = logging.SetupLogEncoder(c.cfg.DevelopmentMode, loggerOpts)
c.cfg.LoggerOpts = logging.SetupLogEncoder(c.cfg.DevelopmentMode || c.loggerOpts.Development, c.loggerOpts)
c.cfg.WebhookPort = manager.DefaultConfig().WebhookPort
c.cfg.LeaderElectionNamespace = controllerNamespace
c.cfg.AnonymousReports = anonymousReportsEnabled
Expand Down
8 changes: 5 additions & 3 deletions modules/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/stretchr/testify/require"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/kong/gateway-operator/modules/manager"
"github.com/kong/gateway-operator/modules/manager/logging"
Expand Down Expand Up @@ -41,15 +42,15 @@ func TestParse(t *testing.T) {
name: "no command line arguments, many environment variables",
args: []string{},
envVars: map[string]string{
"POD_NAMESPACE": "test",
"CONTROLLER_DEVELOPMENT_MODE": "true",
"POD_NAMESPACE": "test",
"GATEWAY_OPERATOR_DEVELOPMENT_MODE": "true",
},
expectedCfg: func() manager.Config {
cfg := expectedDefaultCfg()
cfg.LeaderElectionNamespace = "test"
cfg.ClusterCASecretNamespace = "test"
cfg.ControllerNamespace = "test"
// All the below config changes are the result of CONTROLLER_DEVELOPMENT_MODE=true.
// All the below config changes are the result of GATEWAY_OPERATOR_DEVELOPMENT_MODE=true.
cfg.DevelopmentMode = true
cfg.ValidatingWebhookEnabled = false
loggerOpts := manager.DefaultConfig().LoggerOpts
Expand Down Expand Up @@ -147,5 +148,6 @@ func expectedDefaultCfg() manager.Config {
DataPlaneControllerEnabled: true,
DataPlaneBlueGreenControllerEnabled: true,
ValidatingWebhookEnabled: true,
LoggerOpts: &zap.Options{},
}
}
2 changes: 1 addition & 1 deletion modules/manager/logging/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var (
// SetupLogEncoder sets additional logger configuration options when development mode is enabled.
// In this way, the log structure is lighter and more human-friendly when the development mode
// is enabled.
func SetupLogEncoder(developmentMode bool, options zap.Options) zap.Options {
func SetupLogEncoder(developmentMode bool, options *zap.Options) *zap.Options {
if developmentMode {
options.TimeEncoder = defaultDevTimeEncoder
options.EncoderConfigOptions = []zap.EncoderConfigOption{
Expand Down
4 changes: 2 additions & 2 deletions modules/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Config struct {
KubeconfigPath string
ClusterCASecretName string
ClusterCASecretNamespace string
LoggerOpts zap.Options
LoggerOpts *zap.Options

// controllers for standard APIs and features
GatewayControllerEnabled bool
Expand Down Expand Up @@ -107,7 +107,7 @@ func DefaultConfig() Config {
ClusterCASecretName: "kong-operator-ca",
ClusterCASecretNamespace: defaultNamespace,
ControllerNamespace: defaultNamespace,
LoggerOpts: zap.Options{},
LoggerOpts: &zap.Options{},
GatewayControllerEnabled: true,
ControlPlaneControllerEnabled: true,
DataPlaneControllerEnabled: true,
Expand Down

0 comments on commit de86073

Please sign in to comment.