Controllers: Make available as a target (#110357)

* Controllers: Add to build process
* Allow setting through env variables
This commit is contained in:
Stephanie Hingtgen
2025-08-30 12:27:50 +02:00
committed by GitHub
parent 0d782bdedb
commit 232d68fb8c
10 changed files with 325 additions and 299 deletions
+31
View File
@@ -14,6 +14,7 @@ import (
"github.com/grafana/dskit/ring"
ringclient "github.com/grafana/dskit/ring/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/urfave/cli/v2"
"github.com/grafana/dskit/services"
@@ -21,6 +22,7 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/modules"
"github.com/grafana/grafana/pkg/services/apiserver/standalone"
"github.com/grafana/grafana/pkg/services/authz"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/frontend"
@@ -196,11 +198,40 @@ func (s *ModuleServer) Run() error {
return frontend.ProvideFrontendService(s.cfg, s.features, s.promGatherer, s.registerer, s.license)
})
m.RegisterModule(modules.OperatorServer, s.initOperatorServer)
m.RegisterModule(modules.All, nil)
return m.Run(s.context)
}
func (s *ModuleServer) initOperatorServer() (services.Service, error) {
operatorName := os.Getenv("GF_OPERATOR_NAME")
if operatorName == "" {
s.log.Debug("GF_OPERATOR_NAME environment variable empty or unset, can't start operator")
return nil, nil
}
for _, op := range GetRegisteredOperators() {
if op.Name == operatorName {
return services.NewBasicService(
nil,
func(ctx context.Context) error {
context := cli.NewContext(&cli.App{}, nil, nil)
return op.RunFunc(standalone.BuildInfo{
Version: s.version,
Commit: s.commit,
BuildBranch: s.buildBranch,
}, context, s.cfg)
},
nil,
).WithName("operator"), nil
}
}
return nil, fmt.Errorf("unknown operator: %s. available operators: %v", operatorName, GetRegisteredOperatorNames())
}
// Shutdown initiates Grafana graceful shutdown. This shuts down all
// running background services. Since Run blocks Shutdown supposed to
// be run from a separate goroutine.
+35
View File
@@ -0,0 +1,35 @@
package server
import (
"github.com/grafana/grafana/pkg/services/apiserver/standalone"
"github.com/grafana/grafana/pkg/setting"
"github.com/urfave/cli/v2"
)
// Operator represents an app operator that is available in the Grafana binary
type Operator struct {
Name string
Description string
RunFunc func(standalone.BuildInfo, *cli.Context, *setting.Cfg) error
}
var operatorsRegistry []Operator
// RegisterOperator registers an app operator that is baked into the Grafana binary
func RegisterOperator(operator Operator) {
operatorsRegistry = append(operatorsRegistry, operator)
}
// GetRegisteredOperators returns all registered operators
func GetRegisteredOperators() []Operator {
return operatorsRegistry
}
// GetRegisteredOperatorNames returns the names of all registered operators
func GetRegisteredOperatorNames() []string {
names := make([]string, len(operatorsRegistry))
for i, op := range operatorsRegistry {
names[i] = op.Name
}
return names
}