This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Upgrade to operator sdk v0.10.0 #72
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
be87a1d
⬆ Upgrade to operator-sdk v0.10.0
grdryn 952e425
📈 Expose CR metrics on port 8686
grdryn 3f79c0b
👷 Install correct version of operator-sdk in CI
grdryn 8f1ec72
🐛 Create proper ServiceMonitor for operator
grdryn 88307a4
🔒 Add additional perms to role for ServiceMonitor
grdryn File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,24 +7,33 @@ import ( | |
"os" | ||
"runtime" | ||
|
||
enmassev1beta "github.com/enmasseproject/enmasse/pkg/apis/enmasse/v1beta1" | ||
messaginguserv1beta "github.com/enmasseproject/enmasse/pkg/apis/user/v1beta1" | ||
routev1 "github.com/openshift/api/route/v1" | ||
|
||
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) | ||
_ "k8s.io/client-go/plugin/pkg/client/auth" | ||
"k8s.io/client-go/rest" | ||
|
||
"github.com/aerogear/unifiedpush-operator/pkg/apis" | ||
"github.com/aerogear/unifiedpush-operator/pkg/controller" | ||
|
||
openshiftappsv1 "github.com/openshift/api/apps/v1" | ||
imagev1 "github.com/openshift/api/image/v1" | ||
routev1 "github.com/openshift/api/route/v1" | ||
|
||
enmassev1beta "github.com/enmasseproject/enmasse/pkg/apis/enmasse/v1beta1" | ||
messaginguserv1beta "github.com/enmasseproject/enmasse/pkg/apis/user/v1beta1" | ||
|
||
monitoringv1 "github.com/coreos/prometheus-operator/pkg/apis/monitoring/v1" | ||
monclientv1 "github.com/coreos/prometheus-operator/pkg/client/versioned/typed/monitoring/v1" | ||
"github.com/operator-framework/operator-sdk/pkg/k8sutil" | ||
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics" | ||
"github.com/operator-framework/operator-sdk/pkg/leader" | ||
"github.com/operator-framework/operator-sdk/pkg/log/zap" | ||
"github.com/operator-framework/operator-sdk/pkg/metrics" | ||
"github.com/operator-framework/operator-sdk/pkg/restmapper" | ||
sdkVersion "github.com/operator-framework/operator-sdk/version" | ||
"github.com/spf13/pflag" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/config" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
|
@@ -33,8 +42,9 @@ import ( | |
|
||
// Change below variables to serve metrics on different host or port. | ||
var ( | ||
metricsHost = "0.0.0.0" | ||
metricsPort int32 = 8383 | ||
metricsHost = "0.0.0.0" | ||
metricsPort int32 = 8383 | ||
operatorMetricsPort int32 = 8686 | ||
) | ||
var log = logf.Log.WithName("cmd") | ||
|
||
|
@@ -92,6 +102,7 @@ func main() { | |
// Create a new Cmd to provide shared dependencies and start components | ||
mgr, err := manager.New(cfg, manager.Options{ | ||
Namespace: namespace, | ||
MapperProvider: restmapper.NewDynamicRESTMapper, | ||
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort), | ||
}) | ||
if err != nil { | ||
|
@@ -141,10 +152,40 @@ func main() { | |
os.Exit(1) | ||
} | ||
|
||
// Create Service object to expose the metrics port. | ||
_, err = metrics.ExposeMetricsPort(ctx, metricsPort) | ||
operatorNamespace, err := k8sutil.GetOperatorNamespace() | ||
if err != nil { | ||
log.Info(err.Error()) | ||
log.Error(err, "") | ||
} | ||
|
||
if err = serveCRMetrics(cfg); err != nil { | ||
log.Info("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 { | ||
log.Info("Could not create metrics Service", "error", err.Error()) | ||
} | ||
|
||
err = addMonitoringKeyLabelToService(cfg, operatorNamespace, service) | ||
if err != nil { | ||
log.Error(err, "Could not add monitoring-key label to operator metrics Service") | ||
} | ||
|
||
err = createServiceMonitor(cfg, operatorNamespace, service) | ||
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 | ||
// ErrServiceMonitorNotPresent, which can be used to safely skip ServiceMonitor creation. | ||
if err == metrics.ErrServiceMonitorNotPresent { | ||
log.Info("Install prometheus-operator in you cluster to create ServiceMonitor objects", "error", err.Error()) | ||
} | ||
} | ||
|
||
log.Info("Starting the Cmd.") | ||
|
@@ -155,3 +196,67 @@ func main() { | |
os.Exit(1) | ||
} | ||
} | ||
|
||
func addMonitoringKeyLabelToService(cfg *rest.Config, ns string, service *v1.Service) error { | ||
kclient, err := client.New(cfg, client.Options{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
updatedLabels := map[string]string{"monitoring-key": "middleware"} | ||
for k, v := range service.ObjectMeta.Labels { | ||
updatedLabels[k] = v | ||
} | ||
service.ObjectMeta.Labels = updatedLabels | ||
|
||
err = kclient.Update(context.TODO(), service) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// createServiceMonitor is a temporary fix until the version in the | ||
// operator-sdk is fixed to have the correct Path set on the Endpoints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll create a separate PR for the operator-sdk to get the path set as expected. |
||
func createServiceMonitor(config *rest.Config, ns string, service *v1.Service) error { | ||
mclient := monclientv1.NewForConfigOrDie(config) | ||
|
||
sm := metrics.GenerateServiceMonitor(service) | ||
eps := []monitoringv1.Endpoint{} | ||
for _, ep := range sm.Spec.Endpoints { | ||
eps = append(eps, monitoringv1.Endpoint{Port: ep.Port, Path: "/metrics"}) | ||
} | ||
sm.Spec.Endpoints = eps | ||
|
||
_, err := mclient.ServiceMonitors(ns).Create(sm) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// 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 | ||
} | ||
// Get the namespace the operator is currently deployed in. | ||
operatorNs, err := k8sutil.GetOperatorNamespace() | ||
if err != nil { | ||
return err | ||
} | ||
// To generate metrics in other namespaces, add the values below. | ||
ns := []string{operatorNs} | ||
// Generate and serve custom resource specific metrics. | ||
err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, metricsHost, operatorMetricsPort) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reduces the number of files that we have in
vendor/
, but it means that we have to rundep ensure
again if we want to use any package from a current dependency that we're not currently using.