Skip to content

Commit

Permalink
Merge pull request #326 from awgreene/marketplace-alert
Browse files Browse the repository at this point in the history
Bug 1872880: Correctly count  non-default Marketplace CRs
  • Loading branch information
openshift-merge-robot committed Oct 15, 2020
2 parents 2f0c700 + 27d3ec1 commit 288fca4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 35 deletions.
4 changes: 1 addition & 3 deletions pkg/catalogsourceconfig/deleted.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"context"

"github.com/operator-framework/operator-marketplace/pkg/apis/operators/shared"
"github.com/operator-framework/operator-marketplace/pkg/apis/operators/v2"
v2 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v2"
wrapper "github.com/operator-framework/operator-marketplace/pkg/client"
"github.com/operator-framework/operator-marketplace/pkg/grpccatalog"
"github.com/operator-framework/operator-marketplace/pkg/metrics"
"github.com/operator-framework/operator-marketplace/pkg/phase"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -58,7 +57,6 @@ type deletedReconciler struct {
func (r *deletedReconciler) Reconcile(ctx context.Context, in *v2.CatalogSourceConfig) (out *v2.CatalogSourceConfig, nextPhase *shared.Phase, err error) {
out = in

metrics.DeregisterCustomResource(metrics.ResourceTypeCSC)
// Evict the catalogsourceconfig data from the cache.
r.cache.Evict(out)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
v2 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v2"
catalogsourceconfighandler "github.com/operator-framework/operator-marketplace/pkg/catalogsourceconfig"
"github.com/operator-framework/operator-marketplace/pkg/controller/options"
"github.com/operator-framework/operator-marketplace/pkg/metrics"
"github.com/operator-framework/operator-marketplace/pkg/status"
"github.com/operator-framework/operator-marketplace/pkg/watches"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -93,7 +92,6 @@ type ReconcileCatalogSourceConfig struct {
func (r *ReconcileCatalogSourceConfig) Reconcile(request reconcile.Request) (reconcile.Result, error) {
log.Printf("Reconciling CatalogSourceConfig %s/%s\n", request.Namespace, request.Name)
log.Warning("DEPRECATION NOTICE: The CatalogSourceConfig API is deprecated in future versions. Please visit this link for futher details: https://docs.openshift.com/container-platform/4.4/release_notes/ocp-4-4-release-notes.html#ocp-4-4-marketplace-apis-deprecated")
metrics.RegisterCustomResource(metrics.ResourceTypeCSC)
// Reconcile kicked off, message Sync Channel
r.syncSender.SendSyncMessage(nil)

Expand Down
11 changes: 3 additions & 8 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,7 @@ func GetRoundTripper() http.RoundTripper {
return roundTripper
}

// RegisterCustomResource increases the count of the custom_resource_total metric
func RegisterCustomResource(resourceType string) {
customResourceGaugeVec.With(prometheus.Labels{ResourceTypeLabel: resourceType}).Inc()
}

// DeregisterCustomResource decreases the count of the custom_resource_total metric
func DeregisterCustomResource(resourceType string) {
customResourceGaugeVec.With(prometheus.Labels{ResourceTypeLabel: resourceType}).Dec()
// RegisterCustomResource sets the count of the custom_resource_total metric to the provided value.
func RegisterCustomResource(resourceType string, count float64) {
customResourceGaugeVec.With(prometheus.Labels{ResourceTypeLabel: resourceType}).Set(count)
}
4 changes: 0 additions & 4 deletions pkg/operatorsource/deleted.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/operator-framework/operator-marketplace/pkg/grpccatalog"
"github.com/operator-framework/operator-marketplace/pkg/metrics"

"github.com/operator-framework/operator-marketplace/pkg/apis/operators/shared"
"github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
Expand Down Expand Up @@ -65,9 +64,6 @@ func (r *deletedReconciler) Reconcile(ctx context.Context, in *v1.OperatorSource
// Otherwise this phase has been requeued because the garbage collector hasn't
// finished its work yet.
if in.HasFinalizer() {
if !defaults.IsDefaultSource(in.Name) {
metrics.DeregisterCustomResource(metrics.ResourceTypeOpsrc)
}
// Delete the operator source manifests.
r.datastore.RemoveOperatorSource(out.UID)
grpcCatalog := grpccatalog.New(r.logger, nil, r.client)
Expand Down
5 changes: 0 additions & 5 deletions pkg/operatorsource/initial.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/operator-framework/operator-marketplace/pkg/apis/operators/shared"
"github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
"github.com/operator-framework/operator-marketplace/pkg/datastore"
"github.com/operator-framework/operator-marketplace/pkg/defaults"
"github.com/operator-framework/operator-marketplace/pkg/metrics"
"github.com/operator-framework/operator-marketplace/pkg/phase"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -47,9 +45,6 @@ func (r *initialReconciler) Reconcile(ctx context.Context, in *v1.OperatorSource
err = phase.ErrWrongReconcilerInvoked
return
}
if !defaults.IsDefaultSource(in.Name) {
metrics.RegisterCustomResource(metrics.ResourceTypeOpsrc)
}
out = in.DeepCopy()

// When an opsrc is created, make sure the opsrc finalizer is included
Expand Down
30 changes: 17 additions & 13 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
v1 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
v2 "github.com/operator-framework/operator-marketplace/pkg/apis/operators/v2"
"github.com/operator-framework/operator-marketplace/pkg/defaults"
"github.com/operator-framework/operator-marketplace/pkg/metrics"
"github.com/operator-framework/operator-marketplace/pkg/operatorhub"
log "github.com/sirupsen/logrus"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -462,40 +463,43 @@ func (NoOpReporter) ReportMigration() error {
// returns false if it detects the presence of any CSC or non default Opsrc in
// the cluster to indicate that the cluster is not upgradable to future versions.
func CheckOperatorUpgradeablity(kubeClient client.Client) (bool, error) {
clusterHasCSC, err := isCSCPresent(kubeClient)
cscCount, err := cscCount(kubeClient)
if err != nil {
return false, err
}
clusterHasOpsrc, err := isNonDefaultOpsrcPresent(kubeClient)

nonDefaultOpsrcCount, err := nonDefaultMarketplaceCRCount(kubeClient)
if err != nil {
return false, err
}
if clusterHasCSC || clusterHasOpsrc {

totalCount := cscCount + nonDefaultOpsrcCount

metrics.RegisterCustomResource(metrics.ResourceTypeOpsrc, float64(totalCount))
if totalCount > 0 {
return false, nil
}
return true, nil
}

func isCSCPresent(kubeClient client.Client) (bool, error) {
func cscCount(kubeClient client.Client) (int, error) {
cscs := &v2.CatalogSourceConfigList{}
if err := kubeClient.List(context.TODO(), &client.ListOptions{}, cscs); err != nil {
return false, err
}
if len(cscs.Items) > 0 {
return true, nil
return 0, err
}
return false, nil
return len(cscs.Items), nil
}

func isNonDefaultOpsrcPresent(kubeClient client.Client) (bool, error) {
func nonDefaultMarketplaceCRCount(kubeClient client.Client) (int, error) {
opsrcs := &v1.OperatorSourceList{}
nonDefaultOpsrcCount := 0
if err := kubeClient.List(context.TODO(), &client.ListOptions{}, opsrcs); err != nil {
return false, err
return nonDefaultOpsrcCount, err
}
for _, opsrc := range opsrcs.Items {
if !defaults.IsDefaultSource(opsrc.Name) {
return true, nil
nonDefaultOpsrcCount++
}
}
return false, nil
return nonDefaultOpsrcCount, nil
}

0 comments on commit 288fca4

Please sign in to comment.