Skip to content

Commit

Permalink
fix: 完善klog日志分级
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Mar 28, 2024
1 parent 6a54369 commit a387195
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 20 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ jobs:
GO111MODULE: auto
- name: Run unit test
run: go test -v ./pkg/controller
# bug fix: go build
- name: Build the project
run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o jobflowoperator main.go
2 changes: 2 additions & 0 deletions deploy/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ spec:
- name: jobflowoperator # 控制器镜像
image: jobflowoperator:v1
imagePullPolicy: IfNotPresent
args:
- --v=2 # 日志等级
env:
- name: "Release"
value: "1"
Expand Down
2 changes: 2 additions & 0 deletions helm/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ spec:
- name: {{ .Values.base.name }}
image: {{ .Values.base.image }}
imagePullPolicy: IfNotPresent
args:
- --v=2 # 日志等级
env:
- name: "Release"
value: "1"
Expand Down
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
daemonjobv1alpha1 "github.com/myoperator/jobflowoperator/pkg/apis/daemonjob/v1alpha1"
jobtemplatev1alpha1 "github.com/myoperator/jobflowoperator/pkg/apis/jobTemplate/v1alpha1"
jobflowv1alpha1 "github.com/myoperator/jobflowoperator/pkg/apis/jobflow/v1alpha1"
Expand Down Expand Up @@ -49,8 +50,15 @@ import (
operator = crd + controller + webhook
*/

func main() {
func init() {
klog.InitFlags(nil) // initializing the flags
defer klog.Flush()
flag.Parse() // parses the command-line flags
logf.SetLogger(zap.New())
}

func main() {

var d time.Duration = 0
// 1. 管理器初始化
mgr, err := manager.New(k8sconfig.K8sRestConfig(), manager.Options{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions pkg/controller/daemonjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewDaemonJobController(client client.Client, log logr.Logger,
// Reconcile 调协 loop
func (r *DaemonJobController) Reconcile(ctx context.Context,
req reconcile.Request) (reconcile.Result, error) {
klog.Info("start DaemonJob Reconcile..........")
klog.V(2).Info("start DaemonJob Reconcile..........")

// load JobFlow by namespace
daemonJob := &daemonjobv1alpha1.DaemonJob{}
Expand All @@ -53,7 +53,7 @@ func (r *DaemonJobController) Reconcile(ctx context.Context,
if err != nil {
// If no instance is found, it will be returned directly
if errors.IsNotFound(err) {
klog.Info(fmt.Sprintf("not found daemonJob : %v", req.Name))
klog.V(2).Info(fmt.Sprintf("not found daemonJob : %v", req.Name))
return reconcile.Result{}, nil
}
klog.Error("get DaemonJob error: ", err.Error())
Expand All @@ -79,7 +79,7 @@ func (r *DaemonJobController) Reconcile(ctx context.Context,
r.event.Eventf(daemonJob, v1.EventTypeWarning, "Failed", err.Error())
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * 60}, err
}
klog.Info("end DaemonJob Reconcile........")
klog.V(2).Info("end DaemonJob Reconcile........")

return reconcile.Result{}, nil
}
Expand Down Expand Up @@ -186,7 +186,7 @@ func prepareJobFromDaemonJob(daemonJob *daemonjobv1alpha1.DaemonJob, jobName, no

// update status
func (r *DaemonJobController) updateJobFlowStatus(ctx context.Context, daemonJob *daemonjobv1alpha1.DaemonJob) error {
klog.Info(fmt.Sprintf("start to update daemonJob status! daemonJobName: %v, daemonJobNamespace: %v ", daemonJob.Name, daemonJob.Namespace))
klog.V(2).Info(fmt.Sprintf("start to update daemonJob status! daemonJobName: %v, daemonJobNamespace: %v ", daemonJob.Name, daemonJob.Namespace))
// 获取 job 列表
allJobList := new(batchv1.JobList)
err := r.client.List(ctx, allJobList)
Expand Down Expand Up @@ -284,6 +284,7 @@ func (r *DaemonJobController) OnUpdateJobHandlerByDaemonJob(event event.UpdateEv
for _, ref := range event.ObjectNew.GetOwnerReferences() {
if ref.Kind == daemonjobv1alpha1.DaemonJobKind && ref.APIVersion == daemonjobv1alpha1.DaemonJobApiVersion {
// 重新放入 Reconcile 调协方法
klog.V(5).Info("update job: ", event.ObjectNew.GetName(), event.ObjectNew.GetObjectKind())
limitingInterface.Add(reconcile.Request{
NamespacedName: types.NamespacedName{
Name: ref.Name, Namespace: event.ObjectNew.GetNamespace(),
Expand All @@ -297,7 +298,7 @@ func (r *DaemonJobController) OnDeleteJobHandlerByDaemonJob(event event.DeleteEv
for _, ref := range event.Object.GetOwnerReferences() {
if ref.Kind == daemonjobv1alpha1.DaemonJobKind && ref.APIVersion == daemonjobv1alpha1.DaemonJobApiVersion {
// 重新入列
klog.Info("delete pod: ", event.Object.GetName(), event.Object.GetObjectKind())
klog.V(5).Info("delete job: ", event.Object.GetName(), event.Object.GetObjectKind())
limitingInterface.Add(reconcile.Request{
NamespacedName: types.NamespacedName{Name: ref.Name,
Namespace: event.Object.GetNamespace()}})
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (r *JobFlowController) deployJobFlow(ctx context.Context, jobFlow jobflowv1
if err = r.client.Get(ctx, namespacedName, dependenciesJob); err != nil {
if err != nil {
if errors.IsNotFound(err) {
klog.Info(fmt.Sprintf("No %v Job found!", namespacedName.Name))
klog.V(2).Info(fmt.Sprintf("No %v Job found!", namespacedName.Name))
flag = false
break
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func getJobName(jobFlowName string, jobTemplateName string) string {

// update status
func (r *JobFlowController) updateJobFlowStatus(ctx context.Context, jobFlow *jobflowv1alpha1.JobFlow) error {
klog.Info(fmt.Sprintf("start to update jobFlow status! jobFlowName: %v, jobFlowNamespace: %v ", jobFlow.Name, jobFlow.Namespace))
klog.V(2).Info(fmt.Sprintf("start to update jobFlow status! jobFlowName: %v, jobFlowNamespace: %v ", jobFlow.Name, jobFlow.Namespace))
// 获取 job 列表
allJobList := new(batchv1.JobList)
err := r.client.List(ctx, allJobList)
Expand Down Expand Up @@ -282,6 +282,7 @@ func getAllJobStatus(jobFlow *jobflowv1alpha1.JobFlow, allJobList *batchv1.JobLi
func (r *JobFlowController) OnUpdateJobHandlerByJobFlow(event event.UpdateEvent, limitingInterface workqueue.RateLimitingInterface) {
for _, ref := range event.ObjectNew.GetOwnerReferences() {
if ref.Kind == jobflowv1alpha1.JobFlowKind && ref.APIVersion == jobflowv1alpha1.JobFlowApiVersion {
klog.V(5).Info("update job: ", event.ObjectNew.GetName(), event.ObjectNew.GetObjectKind())
limitingInterface.Add(reconcile.Request{
NamespacedName: types.NamespacedName{
Name: ref.Name, Namespace: event.ObjectNew.GetNamespace(),
Expand All @@ -294,6 +295,7 @@ func (r *JobFlowController) OnUpdateJobHandlerByJobFlow(event event.UpdateEvent,
func (r *JobFlowController) OnDeleteJobHandlerByJobFlow(event event.DeleteEvent, limitingInterface workqueue.RateLimitingInterface) {
for _, ref := range event.Object.GetOwnerReferences() {
if ref.Kind == jobflowv1alpha1.JobFlowKind && ref.APIVersion == jobflowv1alpha1.JobFlowApiVersion {
klog.V(5).Info("delete job: ", event.Object.GetName(), event.Object.GetObjectKind())
limitingInterface.Add(reconcile.Request{
NamespacedName: types.NamespacedName{Name: ref.Name,
Namespace: event.Object.GetNamespace()}})
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/jobflow_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewJobFlowController(client client.Client, log logr.Logger,

// Reconcile 调协 loop
func (r *JobFlowController) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
klog.Info("start jobFlow Reconcile..........")
klog.V(2).Info("start jobFlow Reconcile..........")

// load JobFlow by namespace
jobFlow := &jobflowv1alpha1.JobFlow{}
Expand All @@ -45,7 +45,7 @@ func (r *JobFlowController) Reconcile(ctx context.Context, req reconcile.Request
if err != nil {
// If no instance is found, it will be returned directly
if errors.IsNotFound(err) {
klog.Info(fmt.Sprintf("not found jobFlow : %v", req.Name))
klog.V(2).Info(fmt.Sprintf("not found jobFlow : %v", req.Name))
return reconcile.Result{}, nil
}
klog.Error(err, err.Error())
Expand Down Expand Up @@ -79,7 +79,7 @@ continueExecution:
r.event.Eventf(jobFlow, v1.EventTypeWarning, "Failed", err.Error())
return reconcile.Result{Requeue: true, RequeueAfter: time.Second * 60}, err
}
klog.Info("end jobFlow Reconcile........")
klog.V(2).Info("end jobFlow Reconcile........")

return reconcile.Result{}, nil
}
8 changes: 4 additions & 4 deletions pkg/controller/jobflow_template_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewJobTemplateController(client client.Client, log logr.Logger,
// Reconcile 调协 loop
func (r *JobTemplateController) Reconcile(ctx context.Context,
req reconcile.Request) (reconcile.Result, error) {
klog.Info("start JobTemplate Reconcile..........")
klog.V(2).Info("start JobTemplate Reconcile..........")

// load JobTemplate by namespace
jobTemplate := &jobtemplatev1alpha1.JobTemplate{}
Expand All @@ -52,7 +52,7 @@ func (r *JobTemplateController) Reconcile(ctx context.Context,
if err != nil {
// If no instance is found, it will be returned directly
if errors.IsNotFound(err) {
klog.Info(fmt.Sprintf("not found JobTemplate : %v", req.Name))
klog.V(2).Info(fmt.Sprintf("not found JobTemplate : %v", req.Name))
return reconcile.Result{}, nil
}
klog.Error(err, err.Error())
Expand All @@ -67,14 +67,14 @@ func (r *JobTemplateController) Reconcile(ctx context.Context,
r.event.Eventf(jobTemplate, v1.EventTypeWarning, "Failed", err.Error())
return reconcile.Result{}, err
}
klog.Info("end jobTemplate Reconcile........")
klog.V(2).Info("end jobTemplate Reconcile........")

return reconcile.Result{}, nil
}

// update status
func (r *JobTemplateController) updateJobTemplateStatus(ctx context.Context, jobTemplate *jobtemplatev1alpha1.JobTemplate) error {
klog.Info(fmt.Sprintf("start to update JobTemplate status! JobTemplateName: %v, "+
klog.V(2).Info(fmt.Sprintf("start to update JobTemplate status! JobTemplateName: %v, "+
"JobTemplateNamespace: %v ", jobTemplate.Name, jobTemplate.Namespace))
// 获取 job 列表
allJobList := new(batchv1.JobList)
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8sconfig/init_k8s_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func K8sRestConfig() *rest.Config {
// 读取配置
if os.Getenv("Release") == "1" {
klog.Info("run in the cluster")
klog.V(2).Info("run in the cluster")
return k8sRestConfigInPod()
}

Expand All @@ -22,7 +22,7 @@ func K8sRestConfig() *rest.Config {
klog.Fatal(err)
}
config.Insecure = true
klog.Info("run outside the cluster")
klog.V(2).Info("run outside the cluster")
return config
}

Expand Down

0 comments on commit a387195

Please sign in to comment.