Skip to content

Commit

Permalink
Add controller-runtime args to agent
Browse files Browse the repository at this point in the history
* zap logging config
* kubeconfig
  • Loading branch information
Mario Manno committed Nov 24, 2023
1 parent 278fa1b commit eeb3c76
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 29 deletions.
23 changes: 2 additions & 21 deletions internal/cmd/agent/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (

"helm.sh/helm/v3/pkg/cli"

"github.com/rancher/wrangler/v2/pkg/kubeconfig"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -28,7 +26,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
)
Expand All @@ -51,24 +48,8 @@ func init() {

// start the fleet agent
// systemNamespace is the namespace the agent is running in, e.g. cattle-fleet-system
func start(ctx context.Context, kubeConfig, systemNamespace, agentScope string) error {
// TODO(manno): needs wrapper or switch to zap, TODO structured logging
// TODO(manno): parse commandline flags for zap
zopts := zap.Options{
Development: true,
}

// TODO(manno): switch to controller-runtime funcs
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))

// TODO(manno): needs RegisterFlags,
//localconfig := ctrl.GetConfigOrDie()
clientConfig := kubeconfig.GetNonInteractiveClientConfig(kubeConfig)
// localConfig is the kubeconfig for the cluster the agent is running on.
localConfig, err := clientConfig.ClientConfig()
if err != nil {
return err
}
func start(ctx context.Context, systemNamespace, agentScope string) error {
localConfig := ctrl.GetConfigOrDie()

// Registration is done in an init container. If we are here, we are already registered.
// Retrieve the existing config from the registration.
Expand Down
30 changes: 22 additions & 8 deletions internal/cmd/agent/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package agent

import (
"flag"
"fmt"
"log"
"net/http"
Expand All @@ -22,26 +23,32 @@ type UpstreamOptions struct {

type FleetAgent struct {
command.DebugConfig
UpstreamOptions
Namespace string `usage:"system namespace is the namespace, the agent runs in, e.g. cattle-fleet-system" env:"NAMESPACE"`
AgentScope string `usage:"An identifier used to scope the agent bundleID names, typically the same as namespace" env:"AGENT_SCOPE"`
}

var setupLog = ctrl.Log.WithName("setup")
var (
setupLog = ctrl.Log.WithName("setup")
zopts = zap.Options{
Development: true,
}
)

func (a *FleetAgent) PersistentPre(_ *cobra.Command, _ []string) error {
func (a *FleetAgent) PersistentPre(cmd *cobra.Command, _ []string) error {
if err := a.SetupDebug(); err != nil {
return fmt.Errorf("failed to setup debug logging: %w", err)
}
return nil
}

func (a *FleetAgent) Run(cmd *cobra.Command, args []string) error {
zopts := zap.Options{
Development: true,
}
ctx := clog.IntoContext(cmd.Context(), ctrl.Log)

// for compatibility, override zap opts with legacy debug opts. remove once manifests are updated.
zopts.Development = a.Debug
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))

ctx := clog.IntoContext(cmd.Context(), ctrl.Log)
localConfig := ctrl.GetConfigOrDie()

go func() {
log.Println(http.ListenAndServe("localhost:6060", nil)) // nolint:gosec // Debugging only
Expand All @@ -50,7 +57,7 @@ func (a *FleetAgent) Run(cmd *cobra.Command, args []string) error {
if a.Namespace == "" {
return fmt.Errorf("--namespace or env NAMESPACE is required to be set")
}
if err := start(ctx, a.Kubeconfig, a.Namespace, a.AgentScope); err != nil {
if err := start(ctx, localConfig, a.Namespace, a.AgentScope); err != nil {
return err
}

Expand All @@ -62,6 +69,13 @@ func App() *cobra.Command {
root := command.Command(&FleetAgent{}, cobra.Command{
Version: version.FriendlyVersion(),
})
// add command line flags from zap and controller-runtime, which use
// goflags and convert them to pflags
fs := flag.NewFlagSet("", flag.ExitOnError)
zopts.BindFlags(fs)
ctrl.RegisterFlags(fs)
root.Flags().AddGoFlagSet(fs)

root.AddCommand(NewClusterStatus())
root.AddCommand(NewRegister())
return root
Expand Down

0 comments on commit eeb3c76

Please sign in to comment.