Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mirror validation #233

Merged
merged 1 commit into from
May 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion pkg/xds/cache_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package xds
import (
"context"
"fmt"
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/validation"
"strings"
"time"

Expand Down Expand Up @@ -267,7 +269,7 @@ func (c *CacheMgr) constructAction(port *networking.Port,
}
}

if route.Mirror != nil {
if c.validateMirror(route) {
routeAction.RequestMirrorPolicies = []*metaroute.RouteAction_RequestMirrorPolicy{
{
Cluster: model.BuildClusterName(model.TrafficDirectionOutbound, route.Mirror.Subset,
Expand All @@ -287,6 +289,51 @@ func (c *CacheMgr) constructAction(port *networking.Port,

return routeAction
}

func (c *CacheMgr) validateMirror(route *metaprotocolapi.MetaRoute) bool {
if route.MirrorPercentage != nil {
if value := route.MirrorPercentage.GetValue(); value > 100 {
xdsLog.Errorf("validate mirror failed, mirror_percentage must have a max value of 100 (it has %f)", value)
return false
}
}

if route.Mirror == nil {
return false
}

hostname := route.Mirror.Host
if hostname == "" {
xdsLog.Errorf("validate mirror failed, hostname name cannot be empty")
return false
} else if hostname == "*" {
xdsLog.Errorf("validate mirror failed, invalid destination host %s", hostname)
return false
} else {
err := validation.ValidateWildcardDomain(hostname)
if err != nil {
xdsLog.Errorf("validate mirror failed, invalid destination host %s", hostname)
return false
}
}
subsetName := route.Mirror.Subset
if subsetName == "" {
xdsLog.Errorf("validate mirror failed, subset name cannot be empty")
return false
} else if !labels.IsDNS1123Label(subsetName) {
xdsLog.Errorf("validate mirror failed, invalid destination subset name %s", subsetName)
return false
}
portSelector := route.Mirror.Port
if portSelector == nil {
return false
} else if err := validation.ValidatePort(int(portSelector.GetNumber())); err != nil {
xdsLog.Warnf("validate mirror failed, port number %d must be in the range 1..65535", portSelector.GetNumber())
return false
}
return true
}

func (c *CacheMgr) defaultRoute(service *networking.ServiceEntry, port *networking.Port,
dr *model.DestinationRuleWrapper) *metaroute.RouteConfiguration {
metaRoute := metaroute.RouteConfiguration{
Expand Down