This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
/
cluster_broker_syncer.go
65 lines (55 loc) · 2.04 KB
/
cluster_broker_syncer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package broker
import (
"fmt"
"context"
"github.com/kubernetes-sigs/service-catalog/pkg/apis/servicecatalog/v1beta1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ClusterSyncer provide services to sync the ClusterServiceBroker
type ClusterSyncer struct {
clusterBrokerName string
client client.Client
log logrus.FieldLogger
}
// NewClusterBrokerSyncer allows to sync the ServiceBroker.
func NewClusterBrokerSyncer(cli client.Client, clusterBrokerName string, log logrus.FieldLogger) *ClusterSyncer {
return &ClusterSyncer{
client: cli,
clusterBrokerName: clusterBrokerName,
log: log.WithField("service", "cluster-broker-syncer"),
}
}
// Sync syncs the ServiceBrokers, does not fail if the broker does not exists
func (r *ClusterSyncer) Sync() error {
r.log.Infof("- syncing ClusterServiceBroker %s", r.clusterBrokerName)
for i := 0; i < maxSyncRetries; i++ {
broker := &v1beta1.ClusterServiceBroker{}
err := r.client.Get(context.Background(), types.NamespacedName{Name: r.clusterBrokerName}, broker)
switch {
case apiErrors.IsNotFound(err):
return nil
case err != nil:
return errors.Wrapf(err, "while getting ClusterServiceBrokers %s", r.clusterBrokerName)
}
// update RelistRequests to trigger the relist
broker.Spec.RelistRequests = broker.Spec.RelistRequests + 1
err = r.client.Update(context.Background(), broker)
switch {
case err == nil:
return nil
case apiErrors.IsConflict(err):
r.log.Infof("(%d/%d) ClusterServiceBroker %s update conflict occurred.", i, maxSyncRetries, broker.Name)
case err != nil:
return errors.Wrapf(err, "while updating ClusterServiceBroker %s", broker.Name)
}
}
return fmt.Errorf("could not sync cluster service broker (%s) after %d retries", r.clusterBrokerName, maxSyncRetries)
}
// SetNamespace sets service's working namespace
func (r *ClusterSyncer) SetNamespace(namespace string) {
return
}