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 988d3ff
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
15 changes: 15 additions & 0 deletions charts/fleet/templates/service_gitops_metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: "monitoring-gitops-controller"
labels:
app: gitjob
spec:
type: ClusterIP
ports:
- port: 8081
targetPort: 8081
protocol: TCP
name: metrics
selector:
app: gitjob
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 988d3ff

Please sign in to comment.