Skip to content

Commit

Permalink
Add service to Gitops controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy12789 committed Jun 19, 2024
1 parent 3ee97c0 commit 66a3cb8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
31 changes: 31 additions & 0 deletions charts/fleet/templates/service_gitops_metrics.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
20 changes: 16 additions & 4 deletions internal/cmd/controller/gitops/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"os"
"time"

command "github.com/rancher/fleet/internal/cmd"
Expand Down Expand Up @@ -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."`
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 66a3cb8

Please sign in to comment.