From 0b6844be4583e292587723cdcbd2989f170e6f3f Mon Sep 17 00:00:00 2001 From: Tom Wieczorek Date: Wed, 10 Jul 2024 16:27:49 +0200 Subject: [PATCH] Remove some context fields from some structs Do it the idiomatic way instead, by passing the context around as the first parameter. Signed-off-by: Tom Wieczorek --- cmd/controller/controller.go | 2 +- cmd/worker/worker.go | 2 +- pkg/component/controller/etcd.go | 4 +--- pkg/component/controller/kine.go | 9 +++++---- pkg/component/status/status.go | 4 ++-- pkg/component/worker/autopilot.go | 2 +- pkg/component/worker/certificate_manager.go | 8 +++----- 7 files changed, 14 insertions(+), 17 deletions(-) diff --git a/cmd/controller/controller.go b/cmd/controller/controller.go index 394f461b4e73..60896434f15b 100644 --- a/cmd/controller/controller.go +++ b/cmd/controller/controller.go @@ -324,7 +324,7 @@ func (c *command) start(ctx context.Context) error { ClusterConfig: nodeConfig, }, Socket: c.K0sVars.StatusSocketPath, - CertManager: worker.NewCertificateManager(ctx, c.K0sVars.KubeletAuthConfigPath), + CertManager: worker.NewCertificateManager(c.K0sVars.KubeletAuthConfigPath), }) if nodeConfig.Spec.Storage.Type == v1beta1.EtcdStorageType && !nodeConfig.Spec.Storage.Etcd.IsExternalClusterUsed() { diff --git a/cmd/worker/worker.go b/cmd/worker/worker.go index 0f9a0de357e1..c5eaba4541c4 100644 --- a/cmd/worker/worker.go +++ b/cmd/worker/worker.go @@ -162,7 +162,7 @@ func (c *Command) Start(ctx context.Context) error { DualStackEnabled: workerConfig.DualStackEnabled, }) - certManager := worker.NewCertificateManager(ctx, kubeletKubeconfigPath) + certManager := worker.NewCertificateManager(kubeletKubeconfigPath) // if running inside a controller, status component is already running if !c.SingleNode && !c.EnableWorker { diff --git a/pkg/component/controller/etcd.go b/pkg/component/controller/etcd.go index a15109d0a863..a1562cc1afc4 100644 --- a/pkg/component/controller/etcd.go +++ b/pkg/component/controller/etcd.go @@ -56,7 +56,6 @@ type Etcd struct { supervisor supervisor.Supervisor uid int gid int - ctx context.Context } var _ manager.Component = (*Etcd)(nil) @@ -144,7 +143,6 @@ func (e *Etcd) syncEtcdConfig(ctx context.Context, etcdRequest v1beta1.EtcdReque // Run runs etcd if external cluster is not configured func (e *Etcd) Start(ctx context.Context) error { - e.ctx = ctx if e.Config.IsExternalClusterUsed() { return nil } @@ -325,7 +323,7 @@ func (e *Etcd) setupCerts(ctx context.Context) error { // Health-check interface func (e *Etcd) Ready() error { logrus.WithField("component", "etcd").Debug("checking etcd endpoint for health") - ctx, cancel := context.WithTimeout(e.ctx, 1*time.Second) + ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second) defer cancel() err := etcd.CheckEtcdReady(ctx, e.K0sVars.CertRootDir, e.K0sVars.EtcdCertDir, e.Config) return err diff --git a/pkg/component/controller/kine.go b/pkg/component/controller/kine.go index f8357e040c02..ed36f14f73f2 100644 --- a/pkg/component/controller/kine.go +++ b/pkg/component/controller/kine.go @@ -47,7 +47,6 @@ type Kine struct { supervisor supervisor.Supervisor uid int bypassClient *etcd.Client - ctx context.Context } var _ manager.Component = (*Kine)(nil) @@ -105,7 +104,6 @@ func (k *Kine) Init(_ context.Context) error { // Run runs kine func (k *Kine) Start(ctx context.Context) error { logrus.Info("Starting kine") - k.ctx = ctx k.supervisor = supervisor.Supervisor{ Name: "kine", @@ -137,7 +135,10 @@ const hcKey = "/k0s-health-check" const hcValue = "value" func (k *Kine) Ready() error { - ok, err := k.bypassClient.Write(k.ctx, hcKey, hcValue, 64*time.Second) + ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Second) + defer cancel() + + ok, err := k.bypassClient.Write(ctx, hcKey, hcValue, 64*time.Second) if err != nil { return fmt.Errorf("kine-etcd-health: %w", err) } @@ -145,7 +146,7 @@ func (k *Kine) Ready() error { logrus.Warningf("kine-etcd-health: health-check value was not written") } - v, err := k.bypassClient.Read(k.ctx, hcKey) + v, err := k.bypassClient.Read(ctx, hcKey) if err != nil { return fmt.Errorf("kine-etcd-health read: %w", err) } diff --git a/pkg/component/status/status.go b/pkg/component/status/status.go index 08c669428882..161d82bdfa4f 100644 --- a/pkg/component/status/status.go +++ b/pkg/component/status/status.go @@ -52,7 +52,7 @@ type Status struct { } type certManager interface { - GetRestConfig() (*rest.Config, error) + GetRestConfig(ctx context.Context) (*rest.Config, error) } var _ manager.Component = (*Status)(nil) @@ -173,7 +173,7 @@ func (sh *statusHandler) buildWorkerSideKubeAPIClient(ctx context.Context) (kube timeout, cancel := context.WithTimeout(ctx, defaultPollTimeout) defer cancel() if err := wait.PollUntilWithContext(timeout, defaultPollDuration, func(ctx context.Context) (done bool, err error) { - if restConfig, err = sh.Status.CertManager.GetRestConfig(); err != nil { + if restConfig, err = sh.Status.CertManager.GetRestConfig(ctx); err != nil { return false, nil } return true, nil diff --git a/pkg/component/worker/autopilot.go b/pkg/component/worker/autopilot.go index 798c5f70e9a7..d6bcc772c86e 100644 --- a/pkg/component/worker/autopilot.go +++ b/pkg/component/worker/autopilot.go @@ -62,7 +62,7 @@ func (a *Autopilot) Start(ctx context.Context) error { // needed by autopilot. if err := wait.PollUntilWithContext(timeout, defaultPollDuration, func(ctx context.Context) (done bool, err error) { log.Debugf("Attempting to load autopilot client config") - if restConfig, err = a.CertManager.GetRestConfig(); err != nil { + if restConfig, err = a.CertManager.GetRestConfig(ctx); err != nil { log.WithError(err).Warnf("Failed to load autopilot client config, retrying in %v", defaultPollDuration) return false, nil } diff --git a/pkg/component/worker/certificate_manager.go b/pkg/component/worker/certificate_manager.go index ef5ec01c61c3..9c1c0581fd35 100644 --- a/pkg/component/worker/certificate_manager.go +++ b/pkg/component/worker/certificate_manager.go @@ -35,7 +35,6 @@ import ( var _ certificate.Manager = (*CertificateManager)(nil) type CertificateManager struct { - ctx context.Context config *rest.Config kubeletClientConfigPath string @@ -91,7 +90,7 @@ func (c *CertificateManager) Current() *tls.Certificate { return c.currentCertificate } -func (c *CertificateManager) GetRestConfig() (*rest.Config, error) { +func (c *CertificateManager) GetRestConfig(ctx context.Context) (*rest.Config, error) { restConfig, err := clientcmd.BuildConfigFromFlags("", c.kubeletClientConfigPath) if err != nil { return nil, err @@ -101,7 +100,7 @@ func (c *CertificateManager) GetRestConfig() (*rest.Config, error) { return nil, err } transportConfig := rest.AnonymousClientConfig(restConfig) - if _, err := k8skubeletcert.UpdateTransport(c.ctx.Done(), transportConfig, c, 0); err != nil { + if _, err := k8skubeletcert.UpdateTransport(ctx.Done(), transportConfig, c, 0); err != nil { return nil, err } @@ -114,9 +113,8 @@ func (c *CertificateManager) Start() {} func (c *CertificateManager) Stop() {} func (c *CertificateManager) ServerHealthy() bool { return true } -func NewCertificateManager(ctx context.Context, kubeletClientConfigPath string) *CertificateManager { +func NewCertificateManager(kubeletClientConfigPath string) *CertificateManager { return &CertificateManager{ - ctx: ctx, kubeletClientConfigPath: kubeletClientConfigPath, } }