Skip to content

Commit

Permalink
Implement traefil support
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp-Plotnikov committed Mar 11, 2022
1 parent 6e1e93e commit f63cf7c
Showing 1 changed file with 72 additions and 11 deletions.
83 changes: 72 additions & 11 deletions rollout/trafficrouting/traefik/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"strings"

"github.com/pkg/errors"

"github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/argoproj/argo-rollouts/utils/defaults"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -71,36 +73,95 @@ func (r *Reconciler) SetWeight(desiredWeight int32, additionalDestinations ...v1
rollout := r.Rollout
traefikServiceName := rollout.Spec.Strategy.Canary.TrafficRouting.Traefik.Service
traefikService, err := r.Client.Get(ctx, traefikServiceName, metav1.GetOptions{})
desiredTraefikService := traefikService.DeepCopy()
canaryServiceName := rollout.Spec.Strategy.Canary.CanaryService
if err != nil {
return err
}
canaryServiceName := rollout.Spec.Strategy.Canary.CanaryService
stableServiceName := rollout.Spec.Strategy.Canary.StableService
services, isFound, err := unstructured.NestedSlice(traefikService.Object, "spec", "weighted", "services")
if err != nil || !isFound {
return err
}
for _, service := range services {
serviceTest, ok := service.(map[string]interface{})
typedService, ok := service.(map[string]interface{})
if !ok {
continue
return errors.New("Failed type assertion setting weight for traefik service")
}
serviceName, isFound, err := unstructured.NestedString(typedService, "name")
if err != nil || !isFound {
return err
}
serviceName, _, _ := unstructured.NestedString(serviceTest, "name")
if serviceName == canaryServiceName {
unstructured.SetNestedField(serviceTest, int64(desiredWeight), "weight")
break
err := unstructured.SetNestedField(typedService, int64(desiredWeight), "weight")
if err != nil {
return err
}
continue
}
if serviceName == stableServiceName {
err := unstructured.SetNestedField(typedService, int64(100-desiredWeight), "weight")
if err != nil {
return err
}
continue
}
}
err = unstructured.SetNestedSlice(desiredTraefikService.Object, services, "spec", "weighted", "services")
err = unstructured.SetNestedSlice(traefikService.Object, services, "spec", "weighted", "services")
if err != nil {
return err
}
r.Client.Create(ctx, desiredTraefikService, metav1.CreateOptions{})
return nil
_, err = r.Client.Update(ctx, traefikService, metav1.UpdateOptions{})
return err
}

func (r *Reconciler) VerifyWeight(desiredWeight int32, additionalDestinations ...v1alpha1.WeightDestination) (*bool, error) {
return nil, nil
ctx := context.TODO()
verifyingStatus := false
rollout := r.Rollout
traefikServiceName := rollout.Spec.Strategy.Canary.TrafficRouting.Traefik.Service
traefikService, err := r.Client.Get(ctx, traefikServiceName, metav1.GetOptions{})
if err != nil {
return &verifyingStatus, err
}
canaryServiceName := rollout.Spec.Strategy.Canary.CanaryService
stableServiceName := rollout.Spec.Strategy.Canary.StableService
services, isFound, err := unstructured.NestedSlice(traefikService.Object, "spec", "weighted", "services")
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")
}
}
}
return &verifyingStatus, nil
}

func (r *Reconciler) Type() string {
Expand Down

0 comments on commit f63cf7c

Please sign in to comment.