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

Bug 1831818: Remove Obsolete default OperatorSource #308

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
kevinrizza marked this conversation as resolved.
Show resolved Hide resolved
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)
}