Skip to content

Commit

Permalink
log format updates
Browse files Browse the repository at this point in the history
Signed-off-by: Kuromesi <[email protected]>
  • Loading branch information
Kuromesi committed Sep 11, 2023
1 parent 4bf65e3 commit b407236
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions pkg/trafficrouting/network/custom/custom.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -91,17 +91,17 @@ 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)
klog.Errorf("failed to store custom network provider %s(%s/%s): %s", ref.Kind, r.conf.RolloutNs, ref.Name, err.Error())
return err
}
}
Expand All @@ -120,27 +120,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
Expand All @@ -153,13 +155,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
}
}
Expand Down Expand Up @@ -187,10 +188,9 @@ 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())
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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b407236

Please sign in to comment.