diff --git a/internal/controller/metrics.go b/internal/controller/metrics.go new file mode 100644 index 00000000..53d07c9c --- /dev/null +++ b/internal/controller/metrics.go @@ -0,0 +1,42 @@ +package controller + +import ( + "github.com/prometheus/client_golang/prometheus" + "sigs.k8s.io/controller-runtime/pkg/metrics" +) + +func init() { + registerPrometheusMetrics() +} + +func registerPrometheusMetrics() { + metrics.Registry.MustRegister(spinOperatorSpinAppInfo) + metrics.Registry.MustRegister(spinOperatorSpinAppExecutorInfo) +} + +var ( + spinOperatorSpinAppInfo = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "spin_operator_spinapp_info", + Help: "info about spinapp labeled by name, namespace, executor", + }, + []string{ + "name", + "namespace", + "executor", + }, + ) + + spinOperatorSpinAppExecutorInfo = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "spin_operator_spinapp_executor_info", + Help: "info about spinapp executor labeled by name, namespace, create_deployment and runtimeclass", + }, + []string{ + "name", + "namespace", + "create_deployment", + "runtimeclass", + }, + ) +) diff --git a/internal/controller/spinapp_controller.go b/internal/controller/spinapp_controller.go index 4562cc05..eb59982b 100644 --- a/internal/controller/spinapp_controller.go +++ b/internal/controller/spinapp_controller.go @@ -31,6 +31,7 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "github.com/prometheus/client_golang/prometheus" spinv1 "github.com/spinkube/spin-operator/api/v1" "github.com/spinkube/spin-operator/internal/logging" "github.com/spinkube/spin-operator/pkg/spinapp" @@ -90,6 +91,13 @@ func (r *SpinAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct return ctrl.Result{}, client.IgnoreNotFound(err) } + // record spin_operator_spinapp_info metric + spinOperatorSpinAppInfo.With(prometheus.Labels{ + "name": spinApp.Name, + "namespace": spinApp.Namespace, + "executor": spinApp.Spec.Executor, + }).Set(1) + var executor spinv1.SpinAppExecutor if err := r.Client.Get(ctx, types.NamespacedName{ // Executors must currently be defined in the same namespace as the app. diff --git a/internal/controller/spinappexecutor_controller.go b/internal/controller/spinappexecutor_controller.go index f5501e61..a58237fc 100644 --- a/internal/controller/spinappexecutor_controller.go +++ b/internal/controller/spinappexecutor_controller.go @@ -19,12 +19,14 @@ package controller import ( "context" "errors" + "fmt" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "github.com/prometheus/client_golang/prometheus" spinv1 "github.com/spinkube/spin-operator/api/v1" "github.com/spinkube/spin-operator/internal/logging" ) @@ -76,6 +78,19 @@ func (r *SpinAppExecutorReconciler) Reconcile(ctx context.Context, req ctrl.Requ return ctrl.Result{}, client.IgnoreNotFound(err) } + runtimeClass := "" + if executor.Spec.DeploymentConfig != nil { + runtimeClass = executor.Spec.DeploymentConfig.RuntimeClassName + } + + // record spin_operator_spinapp_executor_info metric + spinOperatorSpinAppExecutorInfo.With(prometheus.Labels{ + "name": executor.Name, + "namespace": executor.Namespace, + "create_deployment": fmt.Sprintf("%t", executor.Spec.CreateDeployment), + "runtimeclass": runtimeClass, + }).Set(1) + // Make sure the finalizer is present err := r.ensureFinalizer(ctx, &executor) return ctrl.Result{}, client.IgnoreNotFound(err)