Skip to content

Commit

Permalink
fix protocol missing in ambassador canary mapping creation
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Mathew <[email protected]>
  • Loading branch information
joshuamcarestack committed May 28, 2024
1 parent 2e7cf69 commit bae86dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
22 changes: 6 additions & 16 deletions rollout/trafficrouting/ambassador/ambassador.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -218,7 +217,8 @@ func (r *Reconciler) createCanaryMapping(ctx context.Context,
}

canarySvc := r.Rollout.Spec.Strategy.Canary.CanaryService
canaryMapping := buildCanaryMapping(baseMapping, canarySvc, desiredWeight)
stableService := r.Rollout.Spec.Strategy.Canary.StableService
canaryMapping := buildCanaryMapping(baseMapping, canarySvc, stableService, desiredWeight)
_, err = client.Create(ctx, canaryMapping, metav1.CreateOptions{})
if err != nil {
msg := fmt.Sprintf("Error creating canary mapping: %s", err)
Expand All @@ -227,9 +227,9 @@ func (r *Reconciler) createCanaryMapping(ctx context.Context,
return err
}

func buildCanaryMapping(baseMapping *unstructured.Unstructured, canarySvc string, desiredWeight int32) *unstructured.Unstructured {
func buildCanaryMapping(baseMapping *unstructured.Unstructured, canarySvc string, stableService string, desiredWeight int32) *unstructured.Unstructured {
canaryMapping := baseMapping.DeepCopy()
svc := buildCanaryService(baseMapping, canarySvc)
svc := buildCanaryService(baseMapping, canarySvc, stableService)
unstructured.RemoveNestedField(canaryMapping.Object, "metadata")
cMappingName := buildCanaryMappingName(baseMapping.GetName())
canaryMapping.SetName(cMappingName)
Expand All @@ -239,19 +239,9 @@ func buildCanaryMapping(baseMapping *unstructured.Unstructured, canarySvc string
return canaryMapping
}

func buildCanaryService(baseMapping *unstructured.Unstructured, canarySvc string) string {
func buildCanaryService(baseMapping *unstructured.Unstructured, canarySvc string, stableService string) string {
curSvc := GetMappingService(baseMapping)
parts := strings.Split(curSvc, ":")
if len(parts) < 2 {
return canarySvc
}
// Check if the last part is a valid int that can be used as the port
port := parts[len(parts)-1]
if _, err := strconv.Atoi(port); err != nil {
return canarySvc

}
return fmt.Sprintf("%s:%s", canarySvc, port)
return strings.Replace(curSvc, stableService, canarySvc, 1)
}

func (r *Reconciler) VerifyWeight(desiredWeight int32, additionalDestinations ...v1alpha1.WeightDestination) (*bool, error) {
Expand Down
12 changes: 6 additions & 6 deletions rollout/trafficrouting/ambassador/ambassador_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ metadata:
spec:
prefix: /myapp/
rewrite: /myapp/
service: myapp:8080`
service: main-service:8080`

baseMappingNoPort = `
apiVersion: getambassador.io/v2
Expand All @@ -42,7 +42,7 @@ metadata:
spec:
prefix: /myapp/
rewrite: /myapp/
service: myapp`
service: main-service`

baseMappingWithWeight = `
apiVersion: getambassador.io/v2
Expand All @@ -53,7 +53,7 @@ metadata:
spec:
prefix: /myapp/
rewrite: /myapp/
service: myapp:8080
service: main-service:8080
weight: 20`

baseV3Mapping = `
Expand All @@ -66,7 +66,7 @@ spec:
hostname: 'example.com'
prefix: /myapp/
rewrite: /myapp/
service: myapp:8080`
service: main-service:8080`

canaryMapping = `
apiVersion: getambassador.io/v2
Expand All @@ -77,7 +77,7 @@ metadata:
spec:
prefix: /myapp/
rewrite: /myapp/
service: myapp:8080
service: main-service:8080
weight: 20`

canaryMappingWithZeroWeight = `
Expand All @@ -89,7 +89,7 @@ metadata:
spec:
prefix: /myapp/
rewrite: /myapp/
service: myapp:8080
service: main-service:8080
weight: 0`
)

Expand Down

0 comments on commit bae86dd

Please sign in to comment.