Skip to content

fix: mirror validation #239

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

Merged
merged 1 commit into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions pkg/webhook/validation/scheme/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (

"github.com/hashicorp/go-multierror"
networking "istio.io/api/networking/v1alpha3"
"istio.io/istio/pkg/config/validation"
"istio.io/istio/pkg/config"
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/protocol"
"istio.io/istio/pkg/config/validation"
"istio.io/istio/pkg/config/visibility"
)

Expand Down Expand Up @@ -262,6 +262,10 @@ func validateMetaRoute(route *metaprotocol.MetaRoute) (errs Validation) {
}
}

if (route.MirrorPercentage != nil && route.Mirror == nil) || (route.MirrorPercentage == nil && route.Mirror != nil) {
errs = appendValidation(errs, fmt.Errorf("mirror_percentage and mirror must be set together"))
}

if route.MirrorPercentage != nil {
value := route.MirrorPercentage.GetValue()
if value > 100 {
Expand Down Expand Up @@ -487,4 +491,3 @@ func appendErrors(err error, errs ...error) error {
func (aae *AnalysisAwareError) Error() string {
return aae.Msg
}

49 changes: 5 additions & 44 deletions pkg/xds/cache_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package xds
import (
"context"
"fmt"
"istio.io/istio/pkg/config/labels"
"istio.io/istio/pkg/config/validation"
"strings"
"time"

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

if c.validateMirror(route) {
if c.hasMirrorPolicy(route) {
routeAction.RequestMirrorPolicies = []*metaroute.RouteAction_RequestMirrorPolicy{
{
Cluster: model.BuildClusterName(model.TrafficDirectionOutbound, route.Mirror.Subset,
Expand All @@ -293,48 +291,11 @@ 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
func (c *CacheMgr) hasMirrorPolicy(route *metaprotocolapi.MetaRoute) bool {
if route.MirrorPercentage != nil && route.Mirror != nil {
return true
}

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
return false
}

func (c *CacheMgr) defaultRoute(service *networking.ServiceEntry, port *networking.Port,
Expand Down