From 16aacbbbc1cd78954ed5238866c1809c38c5b78b Mon Sep 17 00:00:00 2001 From: Kuromesi Date: Tue, 12 Sep 2023 01:05:36 +0800 Subject: [PATCH] log format updates Signed-off-by: Kuromesi --- api/v1alpha1/trafficrouting_types.go | 8 ++-- api/v1alpha1/zz_generated.deepcopy.go | 8 +--- .../bases/rollouts.kruise.io_rollouts.yaml | 4 ++ .../rollouts.kruise.io_trafficroutings.yaml | 4 ++ pkg/trafficrouting/network/custom/custom.go | 40 +++++++++---------- 5 files changed, 34 insertions(+), 30 deletions(-) diff --git a/api/v1alpha1/trafficrouting_types.go b/api/v1alpha1/trafficrouting_types.go index 589a5a90..01d25e65 100644 --- a/api/v1alpha1/trafficrouting_types.go +++ b/api/v1alpha1/trafficrouting_types.go @@ -37,7 +37,7 @@ type TrafficRoutingRef struct { // Gateway configuration only supports >= v0.4.0 (v1alpha2). Gateway *GatewayTrafficRouting `json:"gateway,omitempty"` // CustomNetworkRefs hold a list of custom providers to route traffic - CustomNetworkRefs *[]CustomNetworkRef `json:"customNetworkRefs,omitempty"` + CustomNetworkRefs []CustomNetworkRef `json:"customNetworkRefs,omitempty"` } // IngressTrafficRouting configuration for ingress controller to control traffic routing @@ -152,9 +152,9 @@ type TrafficRoutingList struct { } type CustomNetworkRef struct { - APIVersion string `json:"apiVersion,omitempty"` - Kind string `json:"kind,omitempty"` - Name string `json:"name,omitempty"` + APIVersion string `json:"apiVersion"` + Kind string `json:"kind"` + Name string `json:"name"` } func init() { diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 863553e5..4a78d09e 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -918,12 +918,8 @@ func (in *TrafficRoutingRef) DeepCopyInto(out *TrafficRoutingRef) { } if in.CustomNetworkRefs != nil { in, out := &in.CustomNetworkRefs, &out.CustomNetworkRefs - *out = new([]CustomNetworkRef) - if **in != nil { - in, out := *in, *out - *out = make([]CustomNetworkRef, len(*in)) - copy(*out, *in) - } + *out = make([]CustomNetworkRef, len(*in)) + copy(*out, *in) } } diff --git a/config/crd/bases/rollouts.kruise.io_rollouts.yaml b/config/crd/bases/rollouts.kruise.io_rollouts.yaml index e78296cb..84626f25 100644 --- a/config/crd/bases/rollouts.kruise.io_rollouts.yaml +++ b/config/crd/bases/rollouts.kruise.io_rollouts.yaml @@ -351,6 +351,10 @@ spec: type: string name: type: string + required: + - apiVersion + - kind + - name type: object type: array gateway: diff --git a/config/crd/bases/rollouts.kruise.io_trafficroutings.yaml b/config/crd/bases/rollouts.kruise.io_trafficroutings.yaml index f6d32c5f..a3d0ad39 100644 --- a/config/crd/bases/rollouts.kruise.io_trafficroutings.yaml +++ b/config/crd/bases/rollouts.kruise.io_trafficroutings.yaml @@ -65,6 +65,10 @@ spec: type: string name: type: string + required: + - apiVersion + - kind + - name type: object type: array gateway: diff --git a/pkg/trafficrouting/network/custom/custom.go b/pkg/trafficrouting/network/custom/custom.go index f6495d91..04182dc1 100644 --- a/pkg/trafficrouting/network/custom/custom.go +++ b/pkg/trafficrouting/network/custom/custom.go @@ -1,5 +1,5 @@ /* -Copyright 2021. +Copyright 2023 The Kruise Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -91,17 +91,16 @@ func (r *customController) Initialize(ctx context.Context) error { obj.SetAPIVersion(ref.APIVersion) obj.SetKind(ref.Kind) if err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: ref.Name}, obj); err != nil { - klog.Errorf("failed to get custom network provider %s/%s", ref.Kind, ref.Name) + klog.Errorf("failed to get custom network provider %s(%s/%s): %s", ref.Kind, r.conf.RolloutNs, ref.Name, err.Error()) return err } // check if lua script exists _, err := r.getLuaScript(ctx, ref) if err != nil { - klog.Errorf("failed to get lua script for custom network provider %s: %s", ref.Kind, err.Error()) + klog.Errorf("failed to get lua script for custom network provider %s(%s/%s): %s", ref.Kind, r.conf.RolloutNs, ref.Name, err.Error()) return err } if err := r.storeObject(obj); err != nil { - klog.Errorf("failed to store custom network provider %s/%s", ref.Kind, ref.Name) return err } } @@ -120,27 +119,29 @@ func (r *customController) EnsureRoutes(ctx context.Context, strategy *rolloutv1 } specStr := obj.GetAnnotations()[OriginalSpecAnnotation] if specStr == "" { - continue + return false, fmt.Errorf("failed to get original spec from annotation for %s(%s/%s)", ref.Kind, r.conf.RolloutNs, ref.Name) } var oSpec Data _ = json.Unmarshal([]byte(specStr), &oSpec) luaScript, err := r.getLuaScript(ctx, ref) if err != nil { - klog.Errorf("failed to get lua script for %s", ref.Kind) + klog.Errorf("failed to get lua script for %s(%s/%s): %s", ref.Kind, r.conf.RolloutNs, ref.Name, err.Error()) return false, err } nSpec, err := r.executeLuaForCanary(oSpec, strategy, luaScript) if err != nil { + klog.Errorf("failed to execute lua for %s(%s/%s): %s", ref.Kind, r.conf.RolloutNs, ref.Name, err.Error()) return false, err } - if cmpAndSetObject(nSpec, obj) { + nObj := obj.DeepCopy() + if compareAndSetObject(nSpec, nObj) { continue } - if err = r.Update(context.TODO(), obj); err != nil { - klog.Errorf("failed to update custom network provider") + if err = r.Update(context.TODO(), nObj); err != nil { + klog.Errorf("failed to update custom network provider %s(%s/%s) from (%s) to (%s)", ref.Kind, r.conf.RolloutNs, ref.Name, util.DumpJSON(obj), util.DumpJSON(nObj)) return false, err } - klog.Infof("update custom network provider %s/%s success") + klog.Infof("update custom network provider %s(%s/%s) from (%s) to (%s) success", ref.Kind, r.conf.RolloutNs, ref.Name, util.DumpJSON(obj), util.DumpJSON(nObj)) done = false } return done, nil @@ -153,13 +154,12 @@ func (r *customController) Finalise(ctx context.Context) error { obj.SetKind(ref.Kind) if err := r.Get(ctx, types.NamespacedName{Namespace: r.conf.RolloutNs, Name: ref.Name}, obj); err != nil { if errors.IsNotFound(err) { - klog.Infof("custom network provider %s/%s not found when finalising", ref.Kind, ref.Name) + klog.Infof("custom network provider %s(%s/%s) not found when finalising", ref.Kind, r.conf.RolloutNs, ref.Name) continue } return err } if err := r.restoreObject(obj); err != nil { - klog.Errorf("failed to restore object: %s/%s", ref.Kind, ref.Name) return err } } @@ -187,10 +187,10 @@ func (r *customController) storeObject(obj *unstructured.Unstructured) error { annotations[OriginalSpecAnnotation] = cSpec obj.SetAnnotations(annotations) if err := r.Update(context.TODO(), obj); err != nil { - klog.Errorf("failed to store custom network provider %s/%s", obj.GetKind(), obj.GetName()) + klog.Errorf("failed to store custom network provider %s(%s/%s): %s", obj.GetKind(), r.conf.RolloutNs, obj.GetName(), err.Error()) return err } - klog.Infof("store custom network provider %s/%s success", obj.GetKind(), obj.GetName()) + klog.Infof("store old configuration of custom network provider %s(%s/%s) in annotation(%s) success", obj.GetKind(), r.conf.RolloutNs, obj.GetName(), OriginalSpecAnnotation) return nil } @@ -207,10 +207,10 @@ func (r *customController) restoreObject(obj *unstructured.Unstructured) error { obj.SetAnnotations(oSpec.Annotations) obj.SetLabels(oSpec.Labels) if err := r.Update(context.TODO(), obj); err != nil { - klog.Errorf("failed to restore custom network provider %s/%s", obj.GetKind(), obj.GetName()) + klog.Errorf("failed to restore object %s(%s/%s) from annotation(%s): %s", obj.GetKind(), r.conf.RolloutNs, obj.GetName(), OriginalSpecAnnotation, err.Error()) return err } - klog.Infof("restore custom network provider %s/%s success", obj.GetKind(), obj.GetName()) + klog.Infof("restore custom network provider %s(%s/%s) from annotation(%s) success", obj.GetKind(), obj.GetNamespace(), obj.GetName(), OriginalSpecAnnotation) return nil } @@ -272,21 +272,21 @@ func (r *customController) getLuaScript(ctx context.Context, ref rolloutv1alpha1 configMap := &corev1.ConfigMap{} err := r.Get(ctx, types.NamespacedName{Namespace: nameSpace, Name: name}, configMap) if err != nil { - return "", fmt.Errorf("failed to get configMap %s/%s", nameSpace, name) + return "", fmt.Errorf("failed to get ConfigMap(%s/%s)", nameSpace, name) } else { // in format like "lua.traffic.routing.ingress.aliyun-alb" key = fmt.Sprintf("%s.%s.%s", configuration.LuaTrafficRoutingCustomTypePrefix, ref.Kind, group) if script, ok := configMap.Data[key]; ok { return script, nil } else if !ok { - return "", fmt.Errorf("expected script of %s not found in ConfigMap", key) + return "", fmt.Errorf("expected script not found neither locally nor in ConfigMap") } } return "", nil } -// compare and update obj, return if the obj is updated -func cmpAndSetObject(data Data, obj *unstructured.Unstructured) bool { +// compare and update obj, return whether the obj is updated +func compareAndSetObject(data Data, obj *unstructured.Unstructured) bool { spec := data.Spec annotations := data.Annotations if annotations == nil {