Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Signed-off-by: PhilippPlotnikov <70917163+PhilippDeveloper@users.noreply.github.com>
  • Loading branch information
Philipp-Plotnikov committed Mar 13, 2022
1 parent 0accb50 commit 58779d2
Showing 1 changed file with 50 additions and 53 deletions.
103 changes: 50 additions & 53 deletions rollout/trafficrouting/traefik/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,47 @@ func (r *Reconciler) SetWeight(desiredWeight int32, additionalDestinations ...v1
if err != nil || !isFound {
return err
}
canaryService, err := getService(canaryServiceName, services)
if err != nil {
return err
}
err = unstructured.SetNestedField(canaryService, int64(desiredWeight), "weight")
if err != nil {
return err
}
stableService, err := getService(stableServiceName, services)
if err != nil {
return err
}
err = unstructured.SetNestedField(stableService, int64(100-desiredWeight), "weight")
if err != nil {
return err
}
err = unstructured.SetNestedSlice(traefikService.Object, services, "spec", "weighted", "services")
if err != nil {
return err
}
_, err = r.Client.Update(ctx, traefikService, metav1.UpdateOptions{})
return err
}

func getService(serviceName string, services []interface{}) (map[string]interface{}, error) {
var selectedService map[string]interface{}
for _, service := range services {
typedService, ok := service.(map[string]interface{})
if !ok {
return errors.New("Failed type assertion setting weight for traefik service")
return nil, errors.New("Failed type assertion setting weight for traefik service")
}
serviceName, isFound, err := unstructured.NestedString(typedService, "name")
nameOfCurrentService, isFound, err := unstructured.NestedString(typedService, "name")
if err != nil || !isFound {
return err
return nil, err
}
if serviceName == canaryServiceName {
err := unstructured.SetNestedField(typedService, int64(desiredWeight), "weight")
if err != nil {
return err
}
continue
if nameOfCurrentService == serviceName {
selectedService = typedService
break
}
if serviceName == stableServiceName {
err := unstructured.SetNestedField(typedService, int64(100-desiredWeight), "weight")
if err != nil {
return err
}
continue
}
}
err = unstructured.SetNestedSlice(traefikService.Object, services, "spec", "weighted", "services")
if err != nil {
return err
}
_, err = r.Client.Update(ctx, traefikService, metav1.UpdateOptions{})
return err
return selectedService, nil
}

func (r *Reconciler) VerifyWeight(desiredWeight int32, additionalDestinations ...v1alpha1.WeightDestination) (*bool, error) {
Expand All @@ -129,37 +140,23 @@ func (r *Reconciler) VerifyWeight(desiredWeight int32, additionalDestinations ..
if err != nil || !isFound {
return &verifyingStatus, err
}
for _, service := range services {
typedService, ok := service.(map[string]interface{})
if !ok {
return &verifyingStatus, errors.New("Failed type assertion setting weight for traefik service")
}
serviceName, isFound, err := unstructured.NestedString(typedService, "name")
if err != nil || !isFound {
return &verifyingStatus, err
}
if serviceName == canaryServiceName {
verifyingStatus = false
weight, isFound, err := unstructured.NestedInt64(typedService, "weight")
if err != nil || !isFound {
return &verifyingStatus, err
}
verifyingStatus = weight == int64(desiredWeight)
if !verifyingStatus {
return &verifyingStatus, errors.New("Traefik service weight for canary service is not right")
}
}
if serviceName == stableServiceName {
verifyingStatus = false
weight, isFound, err := unstructured.NestedInt64(typedService, "weight")
if err != nil || !isFound {
return &verifyingStatus, err
}
verifyingStatus = weight == int64(100-desiredWeight)
if !verifyingStatus {
return &verifyingStatus, errors.New("Traefik service weight for canary service is not right")
}
}
canaryService, err := getService(canaryServiceName, services)
if err != nil {
return &verifyingStatus, err
}
weight, isFound, err := unstructured.NestedInt64(canaryService, "weight")
verifyingStatus = weight == int64(desiredWeight)
if err != nil || !isFound || !verifyingStatus {
return &verifyingStatus, err
}
stableService, err := getService(stableServiceName, services)
if err != nil {
return &verifyingStatus, err
}
weight, isFound, err = unstructured.NestedInt64(stableService, "weight")
verifyingStatus = weight == int64(100-desiredWeight)
if err != nil || !isFound || !verifyingStatus {
return &verifyingStatus, err
}
return &verifyingStatus, nil
}
Expand Down

0 comments on commit 58779d2

Please sign in to comment.