From a3871956c30ce6c22561a45c8ce24d0bb9689419 Mon Sep 17 00:00:00 2001 From: googs1025 Date: Thu, 28 Mar 2024 17:29:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=AE=8C=E5=96=84klog=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E5=88=86=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 1 - deploy/deploy.yaml | 2 ++ helm/templates/deployment.yaml | 2 ++ main.go | 10 +++++++++- .../v1alpha1/fake/fake_jobtemplate_client.go | 2 +- .../typed/jobtemplate/v1alpha1/jobtemplate_client.go | 2 +- pkg/controller/daemonjob_controller.go | 11 ++++++----- pkg/controller/helper.go | 6 ++++-- pkg/controller/jobflow_controller.go | 6 +++--- pkg/controller/jobflow_template_controller.go | 8 ++++---- pkg/k8sconfig/init_k8s_config.go | 4 ++-- 11 files changed, 34 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9d815bb..57e2b60 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 \ No newline at end of file diff --git a/deploy/deploy.yaml b/deploy/deploy.yaml index 56563ae..a7b4d03 100644 --- a/deploy/deploy.yaml +++ b/deploy/deploy.yaml @@ -19,6 +19,8 @@ spec: - name: jobflowoperator # 控制器镜像 image: jobflowoperator:v1 imagePullPolicy: IfNotPresent + args: + - --v=2 # 日志等级 env: - name: "Release" value: "1" diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml index d59178d..56d5e4f 100644 --- a/helm/templates/deployment.yaml +++ b/helm/templates/deployment.yaml @@ -20,6 +20,8 @@ spec: - name: {{ .Values.base.name }} image: {{ .Values.base.image }} imagePullPolicy: IfNotPresent + args: + - --v=2 # 日志等级 env: - name: "Release" value: "1" diff --git a/main.go b/main.go index 87dfaf5..26d572a 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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{ diff --git a/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/fake/fake_jobtemplate_client.go b/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/fake/fake_jobtemplate_client.go index 5950778..d541221 100644 --- a/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/fake/fake_jobtemplate_client.go +++ b/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/fake/fake_jobtemplate_client.go @@ -28,7 +28,7 @@ type FakeApiV1alpha1 struct { *testing.Fake } -func (c *FakeApiV1alpha1) JobFlows(namespace string) v1alpha1.JobTemplateInterface { +func (c *FakeApiV1alpha1) JobTemplates(namespace string) v1alpha1.JobTemplateInterface { return &FakeJobTemplates{c, namespace} } diff --git a/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/jobtemplate_client.go b/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/jobtemplate_client.go index aa24c3c..2b955a7 100644 --- a/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/jobtemplate_client.go +++ b/pkg/client/clientset/versioned/typed/jobtemplate/v1alpha1/jobtemplate_client.go @@ -34,7 +34,7 @@ type ApiV1alpha1Client struct { restClient rest.Interface } -func (c *ApiV1alpha1Client) JobFlows(namespace string) JobTemplateInterface { +func (c *ApiV1alpha1Client) JobTemplates(namespace string) JobTemplateInterface { return newJobTemplates(c, namespace) } diff --git a/pkg/controller/daemonjob_controller.go b/pkg/controller/daemonjob_controller.go index eafe5a7..89a1ce0 100644 --- a/pkg/controller/daemonjob_controller.go +++ b/pkg/controller/daemonjob_controller.go @@ -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{} @@ -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()) @@ -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 } @@ -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) @@ -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(), @@ -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()}}) diff --git a/pkg/controller/helper.go b/pkg/controller/helper.go index 03a76bd..1d059f0 100644 --- a/pkg/controller/helper.go +++ b/pkg/controller/helper.go @@ -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 } @@ -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) @@ -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(), @@ -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()}}) diff --git a/pkg/controller/jobflow_controller.go b/pkg/controller/jobflow_controller.go index c833907..41700db 100644 --- a/pkg/controller/jobflow_controller.go +++ b/pkg/controller/jobflow_controller.go @@ -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{} @@ -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()) @@ -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 } diff --git a/pkg/controller/jobflow_template_controller.go b/pkg/controller/jobflow_template_controller.go index d182ade..da5f8da 100644 --- a/pkg/controller/jobflow_template_controller.go +++ b/pkg/controller/jobflow_template_controller.go @@ -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{} @@ -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()) @@ -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) diff --git a/pkg/k8sconfig/init_k8s_config.go b/pkg/k8sconfig/init_k8s_config.go index ca514df..c491b2f 100644 --- a/pkg/k8sconfig/init_k8s_config.go +++ b/pkg/k8sconfig/init_k8s_config.go @@ -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() } @@ -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 }