Skip to content

Commit c59806f

Browse files
author
hexi.ghx
committed
add cronhpa name&namespace in log and change to job queue to *sync.map
1 parent f53e20f commit c59806f

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

pkg/controller/cronhorizontalpodautoscaler_controller.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (r *ReconcileCronHorizontalPodAutoscaler) Reconcile(request reconcile.Reque
9696
for _, cJob := range conditions {
9797
err := r.CronManager.delete(cJob.JobId)
9898
if err != nil {
99-
log.Errorf("Failed to delete job %s,because of %v", cJob.Name, err)
99+
log.Errorf("Failed to delete job %s in cronHPA %s namespace %s, because of %v", cJob.Name, instance.Name, instance.Namespace, err)
100100
}
101101
}
102102
// update scaleTargetRef and excludeDates
@@ -114,7 +114,7 @@ func (r *ReconcileCronHorizontalPodAutoscaler) Reconcile(request reconcile.Reque
114114
if cJob.JobId != "" {
115115
err := r.CronManager.delete(cJob.JobId)
116116
if err != nil {
117-
log.Errorf("Failed to delete expired job %s,because of %v", cJob.Name, err)
117+
log.Errorf("Failed to delete expired job %s in cronHPA %s namespace %s,because of %v", cJob.Name, instance.Name, instance.Namespace, err)
118118
}
119119
}
120120
continue
@@ -129,7 +129,7 @@ func (r *ReconcileCronHorizontalPodAutoscaler) Reconcile(request reconcile.Reque
129129
if cJob.JobId != "" {
130130
err := r.CronManager.delete(cJob.JobId)
131131
if err != nil {
132-
log.Errorf("Failed to delete expired job %s,because of %v", cJob.Name, err)
132+
log.Errorf("Failed to delete expired job %s in cronHPA %s namespace %s, because of %v", cJob.Name, instance.Name, instance.Namespace, err)
133133
}
134134
}
135135
}
@@ -159,7 +159,8 @@ func (r *ReconcileCronHorizontalPodAutoscaler) Reconcile(request reconcile.Reque
159159

160160
if err != nil {
161161
jobCondition.State = v1beta1.Failed
162-
jobCondition.Message = fmt.Sprintf("Failed to create cron hpa job %s,because of %v", job.Name, err)
162+
jobCondition.Message = fmt.Sprintf("Failed to create cron hpa job %s in %s namespace %s,because of %v",
163+
job.Name, instance.Name, instance.Namespace, err)
163164
log.Errorf("Failed to create cron hpa job %s,because of %v", job.Name, err)
164165
} else {
165166
name := job.Name
@@ -171,7 +172,8 @@ func (r *ReconcileCronHorizontalPodAutoscaler) Reconcile(request reconcile.Reque
171172
if runOnce(job) && (c.State == v1beta1.Succeed || c.State == v1beta1.Failed) {
172173
err := r.CronManager.delete(jobId)
173174
if err != nil {
174-
log.Errorf("cron hpa %s(%s) has ran once but fail to exit,because of %v", name, jobId, err)
175+
log.Errorf("cron hpa runonce job %s(%s) in %s namespace %s has ran once but fail to exit,because of %v",
176+
name, jobId, instance.Name, instance.Namespace, err)
175177
}
176178
continue
177179
}

pkg/controller/cronjob.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (ch *CronJobHPA) ScaleHPA() (msg string, err error) {
147147

148148
targetGV, err := schema.ParseGroupVersion(targetRef.APIVersion)
149149
if err != nil {
150-
return "", fmt.Errorf("Failed to get TargetGroup of HPA %s,because of %v", hpa.Name, err)
150+
return "", fmt.Errorf("Failed to get TargetGroup of HPA %s in namespace %s ,because of %v", hpa.Name, hpa.Namespace, err)
151151
}
152152

153153
targetGK := schema.GroupKind{
@@ -207,7 +207,8 @@ func (ch *CronJobHPA) ScaleHPA() (msg string, err error) {
207207

208208
if hpa.Status.CurrentReplicas >= ch.DesiredSize {
209209
// skip change replicas and exit
210-
return fmt.Sprintf("Skip scale replicas because HPA %s current replicas:%d >= desired replicas:%d.", hpa.Name, scale.Spec.Replicas, ch.DesiredSize), nil
210+
return fmt.Sprintf("Skip scale replicas because HPA %s in namespace %s current replicas:%d >= desired replicas:%d.",
211+
hpa.Name, hpa.Namespace, scale.Spec.Replicas, ch.DesiredSize), nil
211212
}
212213

213214
msg = fmt.Sprintf("current replicas:%d, desired replicas:%d.", scale.Spec.Replicas, ch.DesiredSize)

pkg/controller/cronmanager.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type CronManager struct {
3838
sync.Mutex
3939
cfg *rest.Config
4040
client client.Client
41-
jobQueue sync.Map
41+
jobQueue *sync.Map
4242
//cronProcessor CronProcessor
4343
cronExecutor CronExecutor
4444
mapper meta.RESTMapper
@@ -99,7 +99,7 @@ func (cm *CronManager) JobResultHandler(js *cron.JobResult) {
9999
}, instance)
100100

101101
if e != nil {
102-
log.Errorf("Failed to fetch cronHPA job %s of cronHPA %s in %s namespace,because of %v", job.Name(), cronHpa.Name, cronHpa.Namespace, e)
102+
log.Errorf("Failed to fetch cronHPA job %s of cronHPA %s in namespace %s, because of %v", job.Name(), cronHpa.Name, cronHpa.Namespace, e)
103103
return
104104
}
105105

@@ -230,11 +230,11 @@ func (cm *CronManager) GC() {
230230
}, instance); err != nil {
231231
exitsts = false
232232
if errors.IsNotFound(err) {
233-
log.Infof("remove job %s(%s) of cronHPA %s in %s namespace", job.Name(), job.SchedulePlan(), hpa.Name, hpa.Namespace)
233+
log.Infof("remove job %s(%s) of cronHPA %s in namespace %s", job.Name(), job.SchedulePlan(), hpa.Name, hpa.Namespace)
234234
if found {
235235
err := cm.cronExecutor.RemoveJob(job)
236236
if err != nil {
237-
log.Errorf("Failed to gc job %s(%s) of cronHPA %s in %s namespace", job.Name(), job.SchedulePlan(), hpa.Name, hpa.Namespace)
237+
log.Errorf("Failed to gc job %s(%s) of cronHPA %s in namespace %s", job.Name(), job.SchedulePlan(), hpa.Name, hpa.Namespace)
238238
return true
239239
}
240240
}
@@ -252,11 +252,11 @@ func (cm *CronManager) GC() {
252252
if exitsts {
253253
if reason == JobTimeOut {
254254
cm.eventRecorder.Event(instance, v1.EventTypeWarning, "OutOfDate", fmt.Sprintf("rerun out of date job: %s", job.Name()))
255-
log.Warningf("Failed to find job %s (job id: %s, plan %s) of cronHPA %s in %s in cron engine and rerun the job.", job.Name(), job.ID(), job.SchedulePlan(), hpa.Name, hpa.Namespace)
255+
log.Warningf("Failed to find job %s (job id: %s, plan %s) in cronHPA %s in %s in cron engine and rerun the job.", job.Name(), job.ID(), job.SchedulePlan(), hpa.Name, hpa.Namespace)
256256
if msg, reRunErr := job.Run(); reRunErr != nil {
257-
log.Errorf("failed to rerun out of date job %s, msg:%s, err %v", job.Name(), msg, reRunErr)
257+
log.Errorf("failed to rerun out of date job %s (job id: %s, plan %s) in cronHPA %s in %s, msg:%s, err %v",
258+
job.Name(), job.ID(), job.SchedulePlan(), hpa.Name, hpa.Namespace, msg, reRunErr)
258259
}
259-
return true
260260
}
261261

262262
log.Warningf("Failed to find job %s of cronHPA %s in %s in cron engine and resubmit the job.", job.Name(), hpa.Name, hpa.Namespace)
@@ -305,7 +305,7 @@ func NewCronManager(cfg *rest.Config, client client.Client, recorder record.Even
305305
cm := &CronManager{
306306
cfg: cfg,
307307
client: client,
308-
jobQueue: sync.Map{},
308+
jobQueue: &sync.Map{},
309309
eventRecorder: recorder,
310310
}
311311

@@ -331,7 +331,7 @@ func NewCronManager(cfg *rest.Config, client client.Client, recorder record.Even
331331
return cm
332332
}
333333

334-
func queueLength(que sync.Map) int64 {
334+
func queueLength(que *sync.Map) int64 {
335335
len := int64(0)
336336
que.Range(func(k, v interface{}) bool {
337337
len++

0 commit comments

Comments
 (0)