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

Construct the typed clients we need #1637

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions pkg/internal/recorder/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import (
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can keep the name: typedcorev1

"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
)
Expand All @@ -45,7 +44,7 @@ type Provider struct {
scheme *runtime.Scheme
// logger is the logger to use when logging diagnostic event info
logger logr.Logger
evtClient typedcorev1.EventInterface
evtClient corev1client.EventInterface
makeBroadcaster EventBroadcasterProducer

broadcasterOnce sync.Once
Expand Down Expand Up @@ -98,7 +97,7 @@ func (p *Provider) getBroadcaster() record.EventBroadcaster {

p.broadcasterOnce.Do(func() {
broadcaster, stop := p.makeBroadcaster()
broadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: p.evtClient})
broadcaster.StartRecordingToSink(&corev1client.EventSinkImpl{Interface: p.evtClient})
broadcaster.StartEventWatcher(
func(e *corev1.Event) {
p.logger.V(1).Info(e.Type, "object", e.InvolvedObject, "reason", e.Reason, "message", e.Message)
Expand All @@ -112,12 +111,12 @@ func (p *Provider) getBroadcaster() record.EventBroadcaster {

// NewProvider create a new Provider instance.
func NewProvider(config *rest.Config, scheme *runtime.Scheme, logger logr.Logger, makeBroadcaster EventBroadcasterProducer) (*Provider, error) {
clientSet, err := kubernetes.NewForConfig(config)
corev1Client, err := corev1client.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("failed to init clientSet: %w", err)
return nil, fmt.Errorf("failed to init client: %w", err)
}

p := &Provider{scheme: scheme, logger: logger, makeBroadcaster: makeBroadcaster, evtClient: clientSet.CoreV1().Events("")}
p := &Provider{scheme: scheme, logger: logger, makeBroadcaster: makeBroadcaster, evtClient: corev1Client.Events("")}
return p, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/internal/recorder/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ var _ = Describe("recorder.Provider", func() {
Expect(err).NotTo(HaveOccurred())
})

It("should return an error if failed to init clientSet.", func() {
It("should return an error if failed to init client.", func() {
// Invalid the config
cfg1 := *cfg
cfg1.Host = "invalid host"
_, err := recorder.NewProvider(&cfg1, scheme.Scheme, logr.DiscardLogger{}, makeBroadcaster)
Expect(err).NotTo(BeNil())
Expect(err.Error()).To(ContainSubstring("failed to init clientSet"))
Expect(err.Error()).To(ContainSubstring("failed to init client"))
})
})
Describe("GetEventRecorder", func() {
Expand Down
17 changes: 12 additions & 5 deletions pkg/leaderelection/leader_election.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"os"

"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/kubernetes"
coordinationv1client "k8s.io/client-go/kubernetes/typed/coordination/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"sigs.k8s.io/controller-runtime/pkg/recorder"
Expand Down Expand Up @@ -84,17 +85,23 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
}
id = id + "_" + string(uuid.NewUUID())

// Construct client for leader election
client, err := kubernetes.NewForConfig(rest.AddUserAgent(config, "leader-election"))
// Construct clients for leader election
rest.AddUserAgent(config, "leader-election")
corev1Client, err := corev1client.NewForConfig(config)
if err != nil {
return nil, err
}

coordinationClient, err := coordinationv1client.NewForConfig(config)
if err != nil {
return nil, err
}

return resourcelock.New(options.LeaderElectionResourceLock,
options.LeaderElectionNamespace,
options.LeaderElectionID,
client.CoreV1(),
client.CoordinationV1(),
corev1Client,
coordinationClient,
resourcelock.ResourceLockConfig{
Identity: id,
EventRecorder: recorderProvider.GetEventRecorderFor(id),
Expand Down