Skip to content

Commit

Permalink
Bug 1831818: Remove Obsolete default OperatorSource
Browse files Browse the repository at this point in the history
In #300, the OperatorHub API was enhanced to accept both OperatorSource
and CatalogSource as defaults. However, when an existing OperatorSource
in a cluster was switched to a CatalogSource, the old OperatorSource
persisted. This PR fixes the issue, and removes the obsolete OperatorSource,
so that there's only one source for a catalog of operators.
  • Loading branch information
anik120 committed May 8, 2020
1 parent c53c1e3 commit c6457f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func main() {
exit(err)
}

err = defaults.RemoveObsoleteOpsrc(clientGo)
if err != nil {
log.Error(err, "[defaults] Could not remove obsolete default OperatorSource/s")
}
// statusReportingDoneCh will be closed after the operator has successfully stopped reporting ClusterOperator status.
statusReportingDoneCh := statusReporter.StartReporting()

Expand Down
23 changes: 23 additions & 0 deletions pkg/defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package defaults

import (
"context"
"io/ioutil"
"os"

olm "github.com/operator-framework/operator-lifecycle-manager/pkg/api/apis/operators/v1alpha1"
"github.com/operator-framework/operator-marketplace/pkg/apis/operators/v1"
wrapper "github.com/operator-framework/operator-marketplace/pkg/client"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var (
Expand Down Expand Up @@ -179,3 +182,23 @@ func populateDefsConfig(dir string) (map[string]v1.OperatorSource, map[string]ol
}
return opsrcDefinitions, catsrcDefinitions, config, nil
}

// RemoveObsoleteOpsrc removes any existing default OperatorSource
// in the cluster that has been switched into a default CatalogSource
func RemoveObsoleteOpsrc(kubeClient client.Client) error {
opsrcs := &v1.OperatorSourceList{}
if err := kubeClient.List(context.TODO(), &client.ListOptions{}, opsrcs); err != nil {
return err
}
allErrors := []error{}
for _, opsrc := range opsrcs.Items {
_, presentInOpsrcDefs := globalOpsrcDefinitions[opsrc.Name]
_, presentInCatsrcDefs := globalCatsrcDefinitions[opsrc.Name]
if !presentInOpsrcDefs && presentInCatsrcDefs {
if err := kubeClient.Delete(context.TODO(), &opsrc); err != nil {
allErrors = append(allErrors, err)
}
}
}
return utilerrors.NewAggregate(allErrors)
}

0 comments on commit c6457f8

Please sign in to comment.