Skip to content

Commit

Permalink
fix: creation of monitor service when operator is cluster-scoped (#2601
Browse files Browse the repository at this point in the history
)

**Description of the change:**
- Fix `ServiceMonitor` creation when the operator is cluster-scoped and the environment variable `WATCH_NAMESPACE` has not as value the namespace where the operator was deployed.
- Update mock data ( test-framework ) with the latest version of the main.go.  

**Motivation for the change:**
Closes: #2602
See: #2494 (comment)
  • Loading branch information
camilamacedo86 committed Mar 10, 2020
1 parent 07f1ee9 commit 770a2c5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- Fixed issue with Go dependencies caused by removed tag in `openshift/api` repository ([#2466](https://github.com/operator-framework/operator-sdk/issues/2466))
- Fixed a regression in the `operator-sdk run` command that caused `--local` flags to be ignored ([#2478](https://github.com/operator-framework/operator-sdk/issues/2478))
- Fix command `operator-sdk run --local` which was not working on Windows. ([#2481](https://github.com/operator-framework/operator-sdk/pull/2481))
- - Fix `ServiceMonitor` creation when the operator is cluster-scoped and the environment variable `WATCH_NAMESPACE` has a different value than the namespace where the operator is deployed. ([#2601](https://github.com/operator-framework/operator-sdk/pull/2601))

## v0.15.2

Expand Down
15 changes: 12 additions & 3 deletions internal/scaffold/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func main() {
}
// Add the Metrics Service
addMetrics(ctx, cfg, namespace)
addMetrics(ctx, cfg)
log.Info("Starting the Cmd.")
Expand All @@ -179,7 +179,7 @@ func main() {
// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
// the Prometheus operator
func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
func addMetrics(ctx context.Context, cfg *rest.Config) {
if err := serveCRMetrics(cfg); err != nil {
if errors.Is(err, k8sutil.ErrRunLocal) {
log.Info("Skipping CR metrics server creation; not running in a cluster.")
Expand All @@ -203,7 +203,16 @@ func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
// necessary to configure Prometheus to scrape metrics from this operator.
services := []*v1.Service{service}
_, err = metrics.CreateServiceMonitors(cfg, namespace, services)
// Get the namespace the operator is currently deployed in.
operatorNs, err := k8sutil.GetOperatorNamespace()
if err != nil {
log.Error(err, "")
os.Exit(1)
}
// The ServiceMonitor is created in the same namespace where the operator is deployed
_, err = metrics.CreateServiceMonitors(cfg, operatorNs, services)
if err != nil {
log.Info("Could not create ServiceMonitor object", "error", err.Error())
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
Expand Down
15 changes: 12 additions & 3 deletions internal/scaffold/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func main() {
}
// Add the Metrics Service
addMetrics(ctx, cfg, namespace)
addMetrics(ctx, cfg)
log.Info("Starting the Cmd.")
Expand All @@ -178,7 +178,7 @@ func main() {
// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
// the Prometheus operator
func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
func addMetrics(ctx context.Context, cfg *rest.Config) {
if err := serveCRMetrics(cfg); err != nil {
if errors.Is(err, k8sutil.ErrRunLocal) {
log.Info("Skipping CR metrics server creation; not running in a cluster.")
Expand All @@ -202,7 +202,16 @@ func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
// necessary to configure Prometheus to scrape metrics from this operator.
services := []*v1.Service{service}
_, err = metrics.CreateServiceMonitors(cfg, namespace, services)
// Get the namespace the operator is currently deployed in.
operatorNs, err := k8sutil.GetOperatorNamespace()
if err != nil {
log.Error(err, "")
os.Exit(1)
}
// The ServiceMonitor is created in the same namespace where the operator is deployed
_, err = metrics.CreateServiceMonitors(cfg, operatorNs, services)
if err != nil {
log.Info("Could not create ServiceMonitor object", "error", err.Error())
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
Expand Down
15 changes: 12 additions & 3 deletions pkg/ansible/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {
return err
}

addMetrics(context.TODO(), cfg, namespace, gvks)
addMetrics(context.TODO(), cfg, gvks)

done := make(chan error)

Expand Down Expand Up @@ -202,7 +202,7 @@ func Run(flags *aoflags.AnsibleOperatorFlags) error {

// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
// the Prometheus operator
func addMetrics(ctx context.Context, cfg *rest.Config, namespace string, gvks []schema.GroupVersionKind) {
func addMetrics(ctx context.Context, cfg *rest.Config, gvks []schema.GroupVersionKind) {
if err := serveCRMetrics(cfg, gvks); err != nil {
if errors.Is(err, k8sutil.ErrRunLocal) {
log.Info("Skipping CR metrics server creation; not running in a cluster.")
Expand All @@ -229,7 +229,16 @@ func addMetrics(ctx context.Context, cfg *rest.Config, namespace string, gvks []
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
// necessary to configure Prometheus to scrape metrics from this operator.
services := []*v1.Service{service}
_, err = metrics.CreateServiceMonitors(cfg, namespace, services)

// Get the namespace the operator is currently deployed in.
operatorNs, err := k8sutil.GetOperatorNamespace()
if err != nil {
log.Error(err, "")
return
}

// The ServiceMonitor is created in the same namespace where the operator is deployed
_, err = metrics.CreateServiceMonitors(cfg, operatorNs, services)
if err != nil {
log.Info("Could not create ServiceMonitor object", "error", err.Error())
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
Expand Down
35 changes: 29 additions & 6 deletions test/test-framework/cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"strings"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -102,11 +104,23 @@ func main() {
os.Exit(1)
}

// Create a new manager to provide shared dependencies and start components
mgr, err := manager.New(cfg, manager.Options{
// Set default manager options
options := manager.Options{
Namespace: namespace,
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
})
}

// Add support for MultiNamespace set in WATCH_NAMESPACE (e.g ns1,ns2)
// Note that this is not intended to be used for excluding namespaces, this is better done via a Predicate
// Also note that you may face performance issues when using this with a high number of namespaces.
// More Info: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
if strings.Contains(namespace, ",") {
options.Namespace = ""
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(namespace, ","))
}

// Create a new manager to provide shared dependencies and start components
mgr, err := manager.New(cfg, options)
if err != nil {
log.Error(err, "")
os.Exit(1)
Expand All @@ -127,7 +141,7 @@ func main() {
}

// Add the Metrics Service
addMetrics(ctx, cfg, namespace)
addMetrics(ctx, cfg)

log.Info("Starting the Cmd.")

Expand All @@ -140,7 +154,7 @@ func main() {

// addMetrics will create the Services and Service Monitors to allow the operator export the metrics by using
// the Prometheus operator
func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
func addMetrics(ctx context.Context, cfg *rest.Config) {
if err := serveCRMetrics(cfg); err != nil {
if errors.Is(err, k8sutil.ErrRunLocal) {
log.Info("Skipping CR metrics server creation; not running in a cluster.")
Expand All @@ -164,7 +178,16 @@ func addMetrics(ctx context.Context, cfg *rest.Config, namespace string) {
// CreateServiceMonitors will automatically create the prometheus-operator ServiceMonitor resources
// necessary to configure Prometheus to scrape metrics from this operator.
services := []*v1.Service{service}
_, err = metrics.CreateServiceMonitors(cfg, namespace, services)

// Get the namespace the operator is currently deployed in.
operatorNs, err := k8sutil.GetOperatorNamespace()
if err != nil {
log.Error(err, "")
os.Exit(1)
}

// The ServiceMonitor is created in the same namespace where the operator is deployed
_, err = metrics.CreateServiceMonitors(cfg, operatorNs, services)
if err != nil {
log.Info("Could not create ServiceMonitor object", "error", err.Error())
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
Expand Down

0 comments on commit 770a2c5

Please sign in to comment.