Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"os"
"path/filepath"
"time"

_ "github.com/denisenkom/go-mssqldb"
_ "github.com/go-sql-driver/mysql"
Expand All @@ -28,11 +29,13 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"

xpcontroller "github.com/crossplane/crossplane-runtime/v2/pkg/controller"
"github.com/crossplane/crossplane-runtime/v2/pkg/feature"
"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
"github.com/crossplane/crossplane-runtime/v2/pkg/ratelimiter"
"github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics"

"github.com/crossplane-contrib/provider-sql/apis"
"github.com/crossplane-contrib/provider-sql/pkg/controller"
Expand Down Expand Up @@ -74,6 +77,14 @@ func main() {
kingpin.FatalIfError(err, "Cannot create controller manager")
kingpin.FatalIfError(apis.AddToScheme(mgr.GetScheme()), "Cannot add SQL APIs to scheme")

// Initialize state metrics for Prometheus
mrStateMetrics := statemetrics.NewMRStateMetrics()
metrics.Registry.MustRegister(mrStateMetrics)
stateMetricsPollInterval := 30 * time.Second

// Setup state recorders for all managed resource types
kingpin.FatalIfError(controller.SetupStateMetrics(mgr, log, mrStateMetrics, stateMetricsPollInterval), "Cannot setup state metrics recorders")

o := xpcontroller.Options{
Logger: log,
MaxConcurrentReconciles: *maxReconcileRate,
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/google/go-cmp v0.7.0
github.com/lib/pq v1.10.9
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.22.0
k8s.io/api v0.34.0
k8s.io/apimachinery v0.34.0
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
Expand Down Expand Up @@ -55,6 +56,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -63,7 +65,6 @@ require (
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.22.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.62.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
95 changes: 95 additions & 0 deletions pkg/controller/statemetrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
Copyright 2020 The Crossplane Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"time"

ctrl "sigs.k8s.io/controller-runtime"

"github.com/crossplane/crossplane-runtime/v2/pkg/logging"
"github.com/crossplane/crossplane-runtime/v2/pkg/resource"
"github.com/crossplane/crossplane-runtime/v2/pkg/statemetrics"

clustermssql "github.com/crossplane-contrib/provider-sql/apis/cluster/mssql/v1alpha1"
clustermysql "github.com/crossplane-contrib/provider-sql/apis/cluster/mysql/v1alpha1"
clusterpostgresql "github.com/crossplane-contrib/provider-sql/apis/cluster/postgresql/v1alpha1"
namespacedmssql "github.com/crossplane-contrib/provider-sql/apis/namespaced/mssql/v1alpha1"
namespacedmysql "github.com/crossplane-contrib/provider-sql/apis/namespaced/mysql/v1alpha1"
namespacedpostgresql "github.com/crossplane-contrib/provider-sql/apis/namespaced/postgresql/v1alpha1"
)

// stateRecorderConfig holds the configuration for a state recorder.
type stateRecorderConfig struct {
name string
list resource.ManagedList
}

// SetupStateMetrics adds MRStateRecorders for all managed resource types
// to the supplied manager. This enables Prometheus metrics for managed
// resource state (exists, ready, synced).
func SetupStateMetrics(mgr ctrl.Manager, log logging.Logger, metrics *statemetrics.MRStateMetrics, pollInterval time.Duration) error {
configs := []stateRecorderConfig{
// Cluster-scoped PostgreSQL resources
{name: "postgresql-cluster-database", list: &clusterpostgresql.DatabaseList{}},
{name: "postgresql-cluster-extension", list: &clusterpostgresql.ExtensionList{}},
{name: "postgresql-cluster-grant", list: &clusterpostgresql.GrantList{}},
{name: "postgresql-cluster-role", list: &clusterpostgresql.RoleList{}},
{name: "postgresql-cluster-schema", list: &clusterpostgresql.SchemaList{}},

// Namespaced PostgreSQL resources
{name: "postgresql-namespaced-database", list: &namespacedpostgresql.DatabaseList{}},
{name: "postgresql-namespaced-extension", list: &namespacedpostgresql.ExtensionList{}},
{name: "postgresql-namespaced-grant", list: &namespacedpostgresql.GrantList{}},
{name: "postgresql-namespaced-role", list: &namespacedpostgresql.RoleList{}},
{name: "postgresql-namespaced-schema", list: &namespacedpostgresql.SchemaList{}},

// Cluster-scoped MySQL resources
{name: "mysql-cluster-database", list: &clustermysql.DatabaseList{}},
{name: "mysql-cluster-grant", list: &clustermysql.GrantList{}},
{name: "mysql-cluster-user", list: &clustermysql.UserList{}},

// Namespaced MySQL resources
{name: "mysql-namespaced-database", list: &namespacedmysql.DatabaseList{}},
{name: "mysql-namespaced-grant", list: &namespacedmysql.GrantList{}},
{name: "mysql-namespaced-user", list: &namespacedmysql.UserList{}},

// Cluster-scoped MSSQL resources
{name: "mssql-cluster-database", list: &clustermssql.DatabaseList{}},
{name: "mssql-cluster-grant", list: &clustermssql.GrantList{}},
{name: "mssql-cluster-user", list: &clustermssql.UserList{}},

// Namespaced MSSQL resources
{name: "mssql-namespaced-database", list: &namespacedmssql.DatabaseList{}},
{name: "mssql-namespaced-grant", list: &namespacedmssql.GrantList{}},
{name: "mssql-namespaced-user", list: &namespacedmssql.UserList{}},
}

for _, cfg := range configs {
if err := mgr.Add(statemetrics.NewMRStateRecorder(
mgr.GetClient(),
log.WithValues("recorder", cfg.name),
metrics,
cfg.list,
pollInterval,
)); err != nil {
return err
}
}

return nil
}
Loading