Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove scaffold code from operator-sdk init #604

Merged
Merged
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
78 changes: 3 additions & 75 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,22 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"os"
"runtime"

"github.com/operator-framework/operator-sdk/pkg/k8sutil"
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
"github.com/operator-framework/operator-sdk/pkg/metrics"

"github.com/operator-framework/operator-sdk/pkg/ready"
sdkVersion "github.com/operator-framework/operator-sdk/version"
"github.com/spf13/pflag"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"

"github.com/shipwright-io/build/pkg/apis"
buildconfig "github.com/shipwright-io/build/pkg/config"
"github.com/shipwright-io/build/pkg/controller"
"github.com/shipwright-io/build/pkg/ctxlog"
Expand All @@ -39,9 +31,8 @@ import (

// Change below variables to serve metrics on different host or port.
var (
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
operatorMetricsPort int32 = 8686
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
)

func printVersion(ctx context.Context) {
Expand Down Expand Up @@ -77,12 +68,6 @@ func main() {

printVersion(ctx)

namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
ctxlog.Error(ctx, err, "Failed to get watch namespace")
os.Exit(1)
}

// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
Expand Down Expand Up @@ -119,8 +104,6 @@ func main() {
os.Exit(1)
}

// Add the Metrics Service
addMetrics(ctx, cfg, namespace)
buildMetrics.InitPrometheus(buildCfg)

// Add optionally configured extra handlers to metrics endpoint
Expand All @@ -139,58 +122,3 @@ func main() {
os.Exit(1)
}
}

// 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) {
if err := serveCRMetrics(cfg); err != nil {
if errors.Is(err, k8sutil.ErrRunLocal) {
ctxlog.Info(ctx, "Skipping CR metrics server creation; not running in a cluster.")
return
}
ctxlog.Info(ctx, "Could not generate and serve custom resource metrics", "error", err.Error())
}

// Add to the below struct any other metrics ports you want to expose.
servicePorts := []v1.ServicePort{
{Port: metricsPort, Name: metrics.OperatorPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: metricsPort}},
{Port: operatorMetricsPort, Name: metrics.CRPortName, Protocol: v1.ProtocolTCP, TargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: operatorMetricsPort}},
}

// Create Service object to expose the metrics port(s).
service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts)
if err != nil {
ctxlog.Info(ctx, "Could not create metrics Service", "error", err.Error())
}

// 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)
if err != nil {
ctxlog.Info(ctx, "Could not create ServiceMonitor object", "error", err.Error())
// If this operator is deployed to a cluster without the prometheus-operator running, it will return
// ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation.
if err == metrics.ErrServiceMonitorNotPresent {
ctxlog.Info(ctx, "Install prometheus-operator in your cluster to create ServiceMonitor objects", "error", err.Error())
}
}
}

// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
func serveCRMetrics(cfg *rest.Config) error {
// Below function returns filtered operator/CustomResource specific GVKs.
// For more control override the below GVK list with your own custom logic.
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(apis.AddToScheme)
if err != nil {
return err
}
ns := []string{""}
// Generate and serve custom resource specific metrics.
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort)
if err != nil {
return err
}
return nil
}
202 changes: 0 additions & 202 deletions vendor/github.com/coreos/prometheus-operator/LICENSE

This file was deleted.

5 changes: 0 additions & 5 deletions vendor/github.com/coreos/prometheus-operator/NOTICE

This file was deleted.

This file was deleted.

This file was deleted.

Loading