diff --git a/charts/fleet/templates/service_gitops_metrics.yaml b/charts/fleet/templates/service_gitops_metrics.yaml new file mode 100644 index 0000000000..7a2ea5cdda --- /dev/null +++ b/charts/fleet/templates/service_gitops_metrics.yaml @@ -0,0 +1,31 @@ +{{- if .Values.gitops.enabled }} +{{- if .Values.metrics.enabled }} +{{ $shards := list "" }} +{{ if .Values.shards }} +{{ $shards = concat $shards .Values.shards | uniq }} +{{ end }} +{{ range $shards }} +apiVersion: v1 +kind: Service +metadata: + name: "monitoring-gitops-controller{{if . }}-shard-{{ . }}{{end}}" + labels: + app: gitjob +spec: + type: ClusterIP + ports: + - port: 8081 + targetPort: 8081 + protocol: TCP + name: metrics + selector: + app: gitjob + {{- if empty . }} + # fleet.cattle.io/shard-default: "true" + {{- else }} + fleet.cattle.io/shard-id: "{{ . }}" + {{- end }} +--- +{{- end }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/internal/cmd/controller/gitops/operator.go b/internal/cmd/controller/gitops/operator.go index 0548ea5ae5..b423c9e527 100644 --- a/internal/cmd/controller/gitops/operator.go +++ b/internal/cmd/controller/gitops/operator.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "os" "time" command "github.com/rancher/fleet/internal/cmd" @@ -43,6 +44,7 @@ type GitOperator struct { Kubeconfig string `usage:"Kubeconfig file"` Namespace string `usage:"namespace to watch" default:"cattle-fleet-system" env:"NAMESPACE"` MetricsAddr string `name:"metrics-bind-address" default:":8081" usage:"The address the metric endpoint binds to."` + DisableMetrics bool `name:"disable-metrics" usage:"Disable the metrics server."` EnableLeaderElection bool `name:"leader-elect" default:"true" usage:"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager."` Image string `name:"gitjob-image" default:"rancher/fleet:dev" usage:"The gitjob image that will be used in the generated job."` Listen string `default:":8080" usage:"The port the webhook listens."` @@ -79,11 +81,21 @@ func (g *GitOperator) Run(cmd *cobra.Command, args []string) error { ctx := clog.IntoContext(cmd.Context(), ctrl.Log.WithName("gitjob-reconciler")) namespace := g.Namespace + + if envMetricsAddr := os.Getenv("GITOPS_METRICS_BIND_ADDRESS"); envMetricsAddr != "" { + g.MetricsAddr = envMetricsAddr + } + + var metricServerOptions metricsserver.Options + if g.DisableMetrics { + metricServerOptions = metricsserver.Options{BindAddress: "0"} + } else { + metricServerOptions = metricsserver.Options{BindAddress: g.MetricsAddr} + } + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ - Scheme: scheme, - Metrics: metricsserver.Options{ - BindAddress: g.MetricsAddr, - }, + Scheme: scheme, + Metrics: metricServerOptions, LeaderElection: g.EnableLeaderElection, LeaderElectionID: "gitjob-leader", LeaderElectionNamespace: namespace,