This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
client.go
67 lines (53 loc) · 2.39 KB
/
client.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
66
67
package config
import (
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"github.com/openservicemesh/osm/pkg/announcements"
configV1alpha1Client "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"
configV1alpha1Informers "github.com/openservicemesh/osm/pkg/gen/client/config/informers/externalversions"
"github.com/openservicemesh/osm/pkg/k8s"
"github.com/openservicemesh/osm/pkg/messaging"
)
const (
// apiGroup is the k8s API group that this package interacts with
apiGroup = "config.openservicemesh.io"
multiclusterInformerName = `MulticlusterService`
)
// NewConfigController returns a config.Controller struct related to functionality provided by the resources in the config.openservicemesh.io API group
func NewConfigController(kubeConfig *rest.Config, kubeController k8s.Controller, stop chan struct{}, msgBroker *messaging.Broker) (Controller, error) {
configClient := configV1alpha1Client.NewForConfigOrDie(kubeConfig)
informerFactory := configV1alpha1Informers.NewSharedInformerFactory(configClient, k8s.DefaultKubeEventResyncInterval)
client := client{
informer: informerFactory.Config().V1alpha2().MultiClusterServices(),
kubeController: kubeController,
}
shouldObserve := func(obj interface{}) bool {
object, ok := obj.(metav1.Object)
return ok && kubeController.IsMonitoredNamespace(object.GetNamespace())
}
multiclusterServiceEventTypes := k8s.EventTypes{
Add: announcements.MultiClusterServiceAdded,
Update: announcements.MultiClusterServiceUpdated,
Delete: announcements.MultiClusterServiceDeleted,
}
client.informer.Informer().AddEventHandler(k8s.GetEventHandlerFuncs(shouldObserve, multiclusterServiceEventTypes, msgBroker))
if err := client.run(stop); err != nil {
return client, errors.Errorf("Could not start %s client: %s", apiGroup, err)
}
return client, nil
}
func (c client) run(stop <-chan struct{}) error {
log.Info().Msgf("Starting informers for %s", apiGroup)
if c.informer == nil {
return errInitInformers
}
go c.informer.Informer().Run(stop)
log.Info().Msgf("Waiting for %s %s informers' cache to sync", apiGroup, multiclusterInformerName)
if !cache.WaitForCacheSync(stop, c.informer.Informer().HasSynced) {
return errSyncingCaches
}
log.Info().Msgf("Cache sync finished for %s %s informers", apiGroup, multiclusterInformerName)
return nil
}