kube-state-metrics icon indicating copy to clipboard operation
kube-state-metrics copied to clipboard

kube_cronjob_next_schedule_time in the past when cronjob timezone specified

Open paulclark222 opened this issue 2 years ago • 13 comments

What happened: Defined a timezone for a cronjob: timeZone: "Europe/London" Now kube_cronjob_next_schedule_time is always returning the last schedule time rather than the next one.

What you expected to happen: kube_cronjob_next_schedule_time should be the next scheduled time.

How to reproduce it (as minimally and precisely as possible): Define a cronjob with non-UTC timezone (may only happen on zones east of UTC). Wait for the job to execute and then check kube_cronjob_next_schedule_time - you will see it's in the past.

  • kube-state-metrics version: 2.10
  • Kubernetes version (use kubectl version): 1.27
  • Cloud provider or hardware configuration: Azure AKS

paulclark222 avatar Sep 26 '23 12:09 paulclark222

/assign @dgrisonnet /triage accepted

dashpole avatar Oct 05 '23 16:10 dashpole

The source code is here: https://github.com/kubernetes/kube-state-metrics/blob/b91dd311397047f15aae4f6dacba0708f24cf446/internal/store/cronjob.go#L238-L263

Could you dive into it and help debug if you are available?

CatherineF-dev avatar Oct 11 '23 01:10 CatherineF-dev

I'm not a go developer, but I suspect that what's happening is that j.Spec.Schedule doesn't include the timezone, so the cron library is interpreting it in local time (UTC). So if this code were to run just after the cronjob had run (in UTC+1), it would say the next scheduled time was still 1 hour away.

The cronjob library expects the timezone to be specified like this: schedule, err := cron.ParseStandard("TZ=America/Los_Angeles 0 2 * * 0") or loc, _ := time.LoadLocation("Europe/Paris") cronProcess := cron.New(cron.WithLocation(loc))

I'm not sure if this is being done.

paulclark222 avatar Oct 12 '23 08:10 paulclark222

Hi @paulclark222, j.Spec does have a field named timezone, which hasn't been used

https://github.com/kubernetes/api/blob/master/batch/v1/types.go#L637

CatherineF-dev avatar Oct 14 '23 18:10 CatherineF-dev

Could you ramp up golang and try to fix this issue, if you are available and have enough time?

CatherineF-dev avatar Oct 14 '23 18:10 CatherineF-dev

The code for the metric predates the support for timezone, so it is not taken into account. We would need to had support for it in ksm.

/help

dgrisonnet avatar Nov 06 '23 22:11 dgrisonnet

@dgrisonnet: This request has been marked as needing help from a contributor.

Guidelines

Please ensure that the issue body includes answers to the following questions:

  • Why are we solving this issue?
  • To address this issue, are there any code changes? If there are code changes, what needs to be done in the code and what places can the assignee treat as reference points?
  • Does this issue have zero to low barrier of entry?
  • How can the assignee reach out to you for help?

For more details on the requirements of such an issue, please see here and ensure that they are met.

If this request no longer meets these requirements, the label can be removed by commenting with the /remove-help command.

In response to this:

The code for the metric predates the support for timezone, so it is not taken into account. We would need to had support for it in ksm.

/help

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

k8s-ci-robot avatar Nov 06 '23 22:11 k8s-ci-robot

I encountered the same issue after configuring the timezone. Any recent updates or ETA on a resolution? Thanks!

mhmtsvr avatar Feb 01 '24 03:02 mhmtsvr

+1

sekot avatar Feb 23 '24 13:02 sekot

+1 Really an issue when you are trying to create alerts if cronjobs are running longer than expected.

dehoej avatar Apr 22 '24 06:04 dehoej

/assign

mrueg avatar Apr 22 '24 08:04 mrueg

I have a fix in https://github.com/kubernetes/kube-state-metrics/pull/2376

Can you test if this resolves your issue?

mrueg avatar Apr 22 '24 08:04 mrueg