Skip to content

Commit

Permalink
feat: add clusterName configuration
Browse files Browse the repository at this point in the history
Signed-off-by: lenglet-k <[email protected]>
  • Loading branch information
lenglet-k committed Dec 9, 2024
1 parent 88f8de7 commit f7d2ddf
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 10 deletions.
4 changes: 4 additions & 0 deletions api/v1/backup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type BackupSpec struct {
// +optional
PromURL string `json:"promURL,omitempty"`

// ClusterName sets athe kubernetes cluster name to send to pushgateway for grouping metrics
// +optional
ClusterName string `json:"clusterName,omitempty"`

// StatsURL sets an arbitrary URL where the restic container posts metrics and
// information about the snapshots to. This is in addition to the prometheus
// pushgateway.
Expand Down
4 changes: 4 additions & 0 deletions api/v1/check_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type CheckSpec struct {
// +optional
PromURL string `json:"promURL,omitempty"`

// ClusterName sets athe kubernetes cluster name to send to pushgateway for grouping metrics
// +optional
ClusterName string `json:"clusterName,omitempty"`

// KeepJobs amount of jobs to keep for later analysis.
//
// Deprecated: Use FailedJobsHistoryLimit and SuccessfulJobsHistoryLimit respectively.
Expand Down
2 changes: 1 addition & 1 deletion cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var (
&cli.StringFlag{Destination: &cfg.Config.GlobalStatsURL, Name: "globalstatsurl", EnvVars: []string{"BACKUP_GLOBALSTATSURL"}, Usage: "set the URL to post metrics globally"},
&cli.StringFlag{Destination: &cfg.Config.MetricsBindAddress, Name: "metrics-bindaddress", EnvVars: []string{"BACKUP_METRICS_BINDADDRESS"}, Value: ":8080", Usage: "set the bind address for the prometheus endpoint"},
&cli.StringFlag{Destination: &cfg.Config.PromURL, Name: "promurl", EnvVars: []string{"BACKUP_PROMURL"}, Value: "http://127.0.0.1/", Usage: "set the operator wide default prometheus push gateway"},

&cli.StringFlag{Destination: &cfg.Config.ClusterName, Name: "clusterName", EnvVars: []string{"CLUSTER_NAME"}, Value: "default", Usage: "set the operator wide kubernetes cluster name to send to push gateway for grouping metrics"},
&cli.StringFlag{Destination: &cfg.Config.RestartPolicy, Name: "restartpolicy", EnvVars: []string{"BACKUP_RESTARTPOLICY"}, Value: "OnFailure", Usage: "set the RestartPolicy for the backup jobs. According to https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/, this should be 'OnFailure' for jobs that terminate"},
&cli.StringFlag{Destination: &cfg.Config.PodFilter, Name: "podfilter", EnvVars: []string{"BACKUP_PODFILTER"}, Value: "backupPod=true", Usage: "the filter used to find the backup pods"},
&cli.StringFlag{Destination: &cfg.Config.ServiceAccount, Name: "podexecaccountname", Aliases: []string{"serviceaccount"}, EnvVars: []string{"BACKUP_PODEXECACCOUNTNAME"}, Value: "pod-executor", Usage: "set the service account name that should be used for the pod command execution"},
Expand Down
9 changes: 5 additions & 4 deletions cmd/restic/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ func initTest(t *testing.T) *testEnvironment {
ctx := context.Background()

cfg.Config = &cfg.Configuration{
Hostname: os.Getenv("HOSTNAME"),
PromURL: os.Getenv("PROM_URL"),
WebhookURL: os.Getenv("STATS_URL"),
Hostname: os.Getenv("HOSTNAME"),
PromURL: os.Getenv("PROM_URL"),
ClusterName: os.Getenv("CLUSTER_NAME"),
WebhookURL: os.Getenv("STATS_URL"),

RestoreS3Endpoint: os.Getenv("RESTORE_S3ENDPOINT"),
RestoreS3AccessKey: os.Getenv("RESTORE_ACCESSKEYID"),
Expand All @@ -105,7 +106,7 @@ func initTest(t *testing.T) *testEnvironment {
}

mainLogger := zapr.NewLogger(zaptest.NewLogger(t))
statHandler := stats.NewHandler(cfg.Config.PromURL, cfg.Config.Hostname, cfg.Config.WebhookURL, mainLogger)
statHandler := stats.NewHandler(cfg.Config.PromURL, cfg.Config.ClusterName, cfg.Config.Hostname, cfg.Config.WebhookURL, mainLogger)
resticCli := cli.New(ctx, mainLogger, statHandler)

cleanupDirs(t)
Expand Down
3 changes: 2 additions & 1 deletion cmd/restic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var (
&cli.BoolFlag{Destination: &cfg.Config.SkipPreBackup, Name: "skipPreBackup", EnvVars: []string{"SKIP_PREBACKUP"}, Usage: "If the job should skip the backup command and only backup volumes."},

&cli.StringFlag{Destination: &cfg.Config.PromURL, Name: "promURL", EnvVars: []string{"PROM_URL"}, Usage: "Sets the URL of a prometheus push gateway to report metrics."},
&cli.StringFlag{Destination: &cfg.Config.ClusterName, Name: "clusterName", EnvVars: []string{"CLUSTER_NAME"}, Usage: "Sets the Kubernetes cluster name for grouping metrics in push gateway"},
&cli.StringFlag{Destination: &cfg.Config.WebhookURL, Name: "webhookURL", Aliases: []string{"statsURL"}, EnvVars: []string{"STATS_URL"}, Usage: "Sets the URL of a server which will retrieve a webhook after the action completes."},

&cli.StringFlag{Destination: &cfg.Config.Hostname, Name: "hostname", EnvVars: []string{"HOSTNAME"}, Usage: "Sets the hostname to use in reports.", Hidden: true, Required: true},
Expand Down Expand Up @@ -120,7 +121,7 @@ func resticMain(c *cli.Context) error {
ctx, cancel := context.WithCancel(c.Context)
cancelOnTermination(cancel, resticLog)

statHandler := stats.NewHandler(cfg.Config.PromURL, cfg.Config.Hostname, cfg.Config.WebhookURL, resticLog)
statHandler := stats.NewHandler(cfg.Config.PromURL, cfg.Config.ClusterName, cfg.Config.Hostname, cfg.Config.WebhookURL, resticLog)

resticCLI := resticCli.New(ctx, resticLog.WithName("restic"), statHandler)

Expand Down
4 changes: 4 additions & 0 deletions config/crd/apiextensions.k8s.io/v1/k8up.io_backups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,10 @@ spec:
type: object
type: array
type: object
clusterName:
description: ClusterName sets athe kubernetes cluster name to send
to pushgateway for grouping metrics
type: string
failedJobsHistoryLimit:
description: |-
FailedJobsHistoryLimit amount of failed jobs to keep for later analysis.
Expand Down
4 changes: 4 additions & 0 deletions config/crd/apiextensions.k8s.io/v1/k8up.io_checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ spec:
type: object
type: array
type: object
clusterName:
description: ClusterName sets athe kubernetes cluster name to send
to pushgateway for grouping metrics
type: string
failedJobsHistoryLimit:
description: |-
FailedJobsHistoryLimit amount of failed jobs to keep for later analysis.
Expand Down
8 changes: 8 additions & 0 deletions config/crd/apiextensions.k8s.io/v1/k8up.io_schedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,10 @@ spec:
type: object
type: array
type: object
clusterName:
description: ClusterName sets athe kubernetes cluster name to
send to pushgateway for grouping metrics
type: string
concurrentRunsAllowed:
type: boolean
failedJobsHistoryLimit:
Expand Down Expand Up @@ -2574,6 +2578,10 @@ spec:
type: object
type: array
type: object
clusterName:
description: ClusterName sets athe kubernetes cluster name to
send to pushgateway for grouping metrics
type: string
concurrentRunsAllowed:
type: boolean
failedJobsHistoryLimit:
Expand Down
2 changes: 2 additions & 0 deletions config/samples/k8up_v1_schedule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ spec:
failedJobsHistoryLimit: 2
successfulJobsHistoryLimit: 2
promURL: http://minio.minio:9000
clusterName: default
resources:
requests:
memory: "64Mi"
Expand All @@ -55,6 +56,7 @@ spec:
cpu: "250m"
schedule: '@hourly-random'
promURL: http://minio.minio:9000
clusterName: default
prune:
schedule: '*/4 * * * *'
retention:
Expand Down
1 change: 1 addition & 0 deletions operator/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Configuration struct {
PodExecRoleName string
PodFilter string
PromURL string
ClusterName string
RestartPolicy string
SkipWithoutAnnotation bool

Expand Down
5 changes: 3 additions & 2 deletions restic/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type Configuration struct {

SkipPreBackup bool

PromURL string
WebhookURL string
PromURL string
ClusterName string
WebhookURL string

Hostname string
KubeConfig string
Expand Down
6 changes: 4 additions & 2 deletions restic/stats/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ type Handler struct {
promURL string
promHostname string
webhookURL string
clusterName string
log logr.Logger
}

func NewHandler(promURL, promHostname, webhookURL string, log logr.Logger) *Handler {
func NewHandler(promURL, clusterName, promHostname, webhookURL string, log logr.Logger) *Handler {
return &Handler{
promHostname: promHostname,
promURL: promURL,
webhookURL: webhookURL,
clusterName: clusterName,
log: log.WithName("statsHandler"),
}
}
Expand All @@ -55,7 +57,7 @@ func (h *Handler) SendPrometheus(promStats cli.PrometheusProvider) error {

func (h *Handler) updatePrometheus(collector prometheus.Collector) error {
return push.New(h.promURL, subsystem).Collector(collector).
Grouping("instance", h.promHostname).
Grouping("instance", h.promHostname).Grouping("cluster", h.clusterName).
Add()
}

Expand Down

0 comments on commit f7d2ddf

Please sign in to comment.