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 16aacbb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
8 changes: 4 additions & 4 deletions api/v1alpha1/trafficrouting_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 2 additions & 6 deletions api/v1alpha1/zz_generated.deepcopy.go

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

4 changes: 4 additions & 0 deletions config/crd/bases/rollouts.kruise.io_rollouts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,10 @@ spec:
type: string
name:
type: string
required:
- apiVersion
- kind
- name
type: object
type: array
gateway:
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/rollouts.kruise.io_trafficroutings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ spec:
type: string
name:
type: string
required:
- apiVersion
- kind
- name
type: object
type: array
gateway:
Expand Down
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,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
}
}
Expand All @@ -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
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
}

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 16aacbb

Please sign in to comment.