diff --git a/app/recurring_job.go b/app/recurring_job.go index 37c73fba1c..862f29a1df 100644 --- a/app/recurring_job.go +++ b/app/recurring_job.go @@ -68,7 +68,7 @@ func recurringJob(c *cli.Context) (err error) { return errors.Wrap(err, "failed to update job execution count") } - job, err := recurringjob.NewJob(jobName, logger, managerURL, recurringJob) + job, err := recurringjob.NewJob(jobName, logger, managerURL, recurringJob, lhClient) if err != nil { return errors.Wrap(err, "failed to initialize job") } diff --git a/app/recurringjob/job.go b/app/recurringjob/job.go index 9ce5c0574c..fc37099f33 100644 --- a/app/recurringjob/job.go +++ b/app/recurringjob/job.go @@ -23,21 +23,12 @@ import ( lhclientset "github.com/longhorn/longhorn-manager/k8s/pkg/client/clientset/versioned" ) -func NewJob(name string, logger logrus.FieldLogger, managerURL string, recurringJob *longhorn.RecurringJob) (*Job, error) { +func NewJob(name string, logger logrus.FieldLogger, managerURL string, recurringJob *longhorn.RecurringJob, lhClient *lhclientset.Clientset) (*Job, error) { namespace := os.Getenv(types.EnvPodNamespace) if namespace == "" { return nil, fmt.Errorf("failed detect pod namespace, environment variable %v is missing", types.EnvPodNamespace) } - config, err := rest.InClusterConfig() - if err != nil { - return nil, errors.Wrap(err, "failed to get client config") - } - lhClient, err := lhclientset.NewForConfig(config) - if err != nil { - return nil, errors.Wrap(err, "failed to get clientset") - } - clientOpts := &longhornclient.ClientOpts{ Url: managerURL, Timeout: HTTPClientTimout, @@ -52,6 +43,10 @@ func NewJob(name string, logger logrus.FieldLogger, managerURL string, recurring return nil, errors.Wrap(err, "failed to create scheme") } + config, err := rest.InClusterConfig() + if err != nil { + return nil, errors.Wrap(err, "failed to get client config") + } eventBroadcaster, err := apputil.CreateEventBroadcaster(config) if err != nil { return nil, err