Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zhao <zhaoyu@koderover.com>
  • Loading branch information
PetrusZ committed Dec 5, 2023
1 parent e88d128 commit e11b914
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions pkg/microservice/aslan/core/common/service/kube/istio_grayscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ func generateGrayscaleWeightVirtualService(ctx context.Context, envMap map[strin
vsObj.Name = vsName
vsObj.Namespace = ns

baseEnv := ""

if vsObj.Labels == nil {
vsObj.Labels = map[string]string{}
}
Expand All @@ -245,6 +247,14 @@ func generateGrayscaleWeightVirtualService(ctx context.Context, envMap map[strin
return nil, fmt.Errorf("env %s is not found", weightConfig.Env)
}

if baseEnv == "" {
if envMap[weightConfig.Env].IstioGrayscale.IsBase {
baseEnv = envMap[weightConfig.Env].EnvName
} else {
baseEnv = envMap[weightConfig.Env].IstioGrayscale.BaseEnv
}
}

if !skipWorkloadCheck {
// If there is no workloads in this environment, then service is not updated.
hasWorkload, err := doesSvcHasWorkload(ctx, envMap[weightConfig.Env].Namespace, labels.SelectorFromSet(labels.Set(svc.Spec.Selector)), kclient)
Expand Down Expand Up @@ -280,6 +290,7 @@ func generateGrayscaleWeightVirtualService(ctx context.Context, envMap map[strin
httpRoutes = append(httpRoutes, httpRoute)
}

weightSum := 0
routes := []*networkingv1alpha3.HTTPRouteDestination{}
for _, weightConfig := range weightConfigs {
if envMap[weightConfig.Env] == nil {
Expand All @@ -298,6 +309,7 @@ func generateGrayscaleWeightVirtualService(ctx context.Context, envMap map[strin
}
}

weightSum += int(weightConfig.Weight)
route := &networkingv1alpha3.HTTPRouteDestination{
Destination: &networkingv1alpha3.Destination{
Host: fmt.Sprintf("%s.%s.svc.cluster.local", svcName, envMap[weightConfig.Env].Namespace),
Expand All @@ -313,6 +325,19 @@ func generateGrayscaleWeightVirtualService(ctx context.Context, envMap map[strin
}
routes = append(routes, route)
}
if weightSum != 100 {
log.Warnf("The sum of weight is not 100 for the service %s, the full-path grayscale may not work correctly", svcName)
if baseEnv == "" {
return nil, fmt.Errorf("base env is not found")
}

route := &networkingv1alpha3.HTTPRouteDestination{
Destination: &networkingv1alpha3.Destination{
Host: fmt.Sprintf("%s.%s.svc.cluster.local", svcName, envMap[baseEnv].Namespace),
},
}
routes = append([]*networkingv1alpha3.HTTPRouteDestination{}, route)
}
httpRoutes = append(httpRoutes, &networkingv1alpha3.HTTPRoute{
Route: routes,
})
Expand Down Expand Up @@ -359,8 +384,8 @@ func generateGrayscaleHeaderMatchVirtualService(ctx context.Context, envMap map[

configedEnvSet.Insert(headerMatchConfig.Env)

header := map[string]*networkingv1alpha3.StringMatch{}
for _, headerMatch := range headerMatchConfig.HeaderMatchs {
header := map[string]*networkingv1alpha3.StringMatch{}
if headerMatch.Match == commonmodels.StringMatchPrefix {
header[headerMatch.Key] = &networkingv1alpha3.StringMatch{
MatchType: &networkingv1alpha3.StringMatch_Prefix{
Expand All @@ -382,21 +407,22 @@ func generateGrayscaleHeaderMatchVirtualService(ctx context.Context, envMap map[
} else {
return nil, fmt.Errorf("unsupported header match type: %s", headerMatch.Match)
}
}
httpRoutes = append(httpRoutes, &networkingv1alpha3.HTTPRoute{
Match: []*networkingv1alpha3.HTTPMatchRequest{
{
Headers: header,

httpRoutes = append(httpRoutes, &networkingv1alpha3.HTTPRoute{
Match: []*networkingv1alpha3.HTTPMatchRequest{
{
Headers: header,
},
},
},
Route: []*networkingv1alpha3.HTTPRouteDestination{
{
Destination: &networkingv1alpha3.Destination{
Host: fmt.Sprintf("%s.%s.svc.cluster.local", svcName, envMap[headerMatchConfig.Env].Namespace),
Route: []*networkingv1alpha3.HTTPRouteDestination{
{
Destination: &networkingv1alpha3.Destination{
Host: fmt.Sprintf("%s.%s.svc.cluster.local", svcName, envMap[headerMatchConfig.Env].Namespace),
},
},
},
},
})
})
}
}

for _, env := range envMap {
Expand Down

0 comments on commit e11b914

Please sign in to comment.