kube-controller-manager: also support context for record.NewBroadcaster

27a68ae introduced context support. In order to use that
also with NewBroadcaster, a variant of the call is needed to allow
the caller to specify the context.
This commit is contained in:
Mengjiao Liu
2024-10-26 19:15:20 +08:00
parent 762dda5d2b
commit 6fd8954fe8
3 changed files with 7 additions and 5 deletions

View File

@@ -132,7 +132,8 @@ controller, and serviceaccounts controller.`,
}
cliflag.PrintFlags(cmd.Flags())
c, err := s.Config(KnownControllers(), ControllersDisabledByDefault(), ControllerAliases())
ctx := context.Background()
c, err := s.Config(ctx, KnownControllers(), ControllersDisabledByDefault(), ControllerAliases())
if err != nil {
return err
}
@@ -140,7 +141,7 @@ controller, and serviceaccounts controller.`,
// add feature enablement metrics
fg := s.ComponentGlobalsRegistry.FeatureGateFor(featuregate.DefaultKubeComponent)
fg.(featuregate.MutableFeatureGate).AddMetrics()
return Run(context.Background(), c.Complete())
return Run(ctx, c.Complete())
},
Args: func(cmd *cobra.Command, args []string) error {
for _, arg := range args {

View File

@@ -18,6 +18,7 @@ limitations under the License.
package options
import (
"context"
"fmt"
"net"
@@ -470,7 +471,7 @@ func (s *KubeControllerManagerOptions) Validate(allControllers []string, disable
}
// Config return a controller manager config objective
func (s KubeControllerManagerOptions) Config(allControllers []string, disabledByDefaultControllers []string, controllerAliases map[string]string) (*kubecontrollerconfig.Config, error) {
func (s KubeControllerManagerOptions) Config(ctx context.Context, allControllers []string, disabledByDefaultControllers []string, controllerAliases map[string]string) (*kubecontrollerconfig.Config, error) {
if err := s.Validate(allControllers, disabledByDefaultControllers, controllerAliases); err != nil {
return nil, err
}
@@ -494,7 +495,7 @@ func (s KubeControllerManagerOptions) Config(allControllers []string, disabledBy
return nil, err
}
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster := record.NewBroadcaster(record.WithContext(ctx))
eventRecorder := eventBroadcaster.NewRecorder(clientgokubescheme.Scheme, v1.EventSource{Component: KubeControllerManagerUserAgent})
c := &kubecontrollerconfig.Config{

View File

@@ -113,7 +113,7 @@ func StartTestServer(ctx context.Context, customFlags []string) (result TestServ
logger.Info("kube-controller-manager will listen securely", "port", s.SecureServing.BindPort)
}
config, err := s.Config(all, disabled, aliases)
config, err := s.Config(ctx, all, disabled, aliases)
if err != nil {
return result, fmt.Errorf("failed to create config from options: %v", err)
}