Skip to content

Commit

Permalink
refactor(recurring-job): reduce duplicated logic for getting lhClient
Browse files Browse the repository at this point in the history
longhorn/longhorn-6534

Signed-off-by: Chin-Ya Huang <chin-ya.huang@suse.com>
  • Loading branch information
c3y1huang authored and derekbit committed Jan 22, 2025
1 parent b17bb9b commit b73c24c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/recurring_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
15 changes: 5 additions & 10 deletions app/recurringjob/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit b73c24c

Please sign in to comment.