Skip to content

Commit

Permalink
remove mapperFn to make codes simpler
Browse files Browse the repository at this point in the history
Signed-off-by: Icarus Wu <icaruswu66@qq.com>
  • Loading branch information
Icarus9913 committed Oct 24, 2024
1 parent 269e8c7 commit 09e07ae
Showing 1 changed file with 16 additions and 31 deletions.
47 changes: 16 additions & 31 deletions pkg/api-server/service_insight_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ func (s *serviceInsightEndpoints) findResource(request *restful.Request, respons
res := out.(*rest_unversioned.Resource)
res.Meta.Name = service
res.Spec = stat

mapperFn := removeDisplayNameLabel()
mapperFn(res)

removeDisplayNameLabel(res)
if err := response.WriteAsJson(res); err != nil {
core.Log.Error(err, "Could not write the response")
}
Expand Down Expand Up @@ -104,16 +101,13 @@ func (s *serviceInsightEndpoints) listResources(request *restful.Request, respon
}
}

items := s.expandInsights(serviceInsightList, nameContains,
func(service *v1alpha1.ServiceInsight_Service) bool {
if len(filterMap) == 0 {
return true
}
_, exists := filterMap[service.ServiceType]
return exists
},
removeDisplayNameLabel(),
)
items := s.expandInsights(serviceInsightList, nameContains, func(service *v1alpha1.ServiceInsight_Service) bool {
if len(filterMap) == 0 {
return true
}
_, exists := filterMap[service.ServiceType]
return exists
})

restList := rest.ResourceList{
Total: uint32(len(items)),
Expand Down Expand Up @@ -144,12 +138,7 @@ func (s *serviceInsightEndpoints) fillStaticInfo(name string, stat *v1alpha1.Ser
// 2) Mesh+Name is a key on Universal, but not on Kubernetes, so if there are two services of the same name in different Meshes we would have problems with naming.
// From the API perspective it's better to provide ServiceInsight per Service, not per Mesh.
// For this reason, this method expand the one ServiceInsight resource for the mesh to resource per service
func (s *serviceInsightEndpoints) expandInsights(
serviceInsightList *mesh.ServiceInsightResourceList,
nameContains string,
filterFn func(service *v1alpha1.ServiceInsight_Service) bool,
mapperFn unversionedResourceMapper,
) []rest.Resource {
func (s *serviceInsightEndpoints) expandInsights(serviceInsightList *mesh.ServiceInsightResourceList, nameContains string, filterFn func(service *v1alpha1.ServiceInsight_Service) bool) []rest.Resource {
restItems := []rest.Resource{} // Needs to be set to avoid returning nil and have the api return []
for _, insight := range serviceInsightList.Items {
for serviceName, service := range insight.Spec.Services {
Expand All @@ -159,7 +148,7 @@ func (s *serviceInsightEndpoints) expandInsights(
res := out.(*rest_unversioned.Resource)
res.Meta.Name = serviceName
res.Spec = service
mapperFn(res)
removeDisplayNameLabel(res)
restItems = append(restItems, out)
}
}
Expand Down Expand Up @@ -212,16 +201,12 @@ func (s *serviceInsightEndpoints) paginateResources(request *restful.Request, re
return nil
}

type unversionedResourceMapper func(resource *rest_unversioned.Resource)

// Since the value of label "kuma.io/display-name" is same with the ServiceInsight resource name,
// in which it looks weird for the API to each service. Ref: https://github.com/kumahq/kuma/issues/9729
func removeDisplayNameLabel() unversionedResourceMapper {
return func(resource *rest_unversioned.Resource) {
tmpMeta := resource.GetMeta()
maps.DeleteFunc(tmpMeta.Labels, func(key string, val string) bool {
return key == v1alpha1.DisplayName
})
resource.Meta = tmpMeta
}
func removeDisplayNameLabel(resource *rest_unversioned.Resource) {
tmpMeta := resource.GetMeta()
maps.DeleteFunc(tmpMeta.Labels, func(key string, val string) bool {
return key == v1alpha1.DisplayName
})
resource.Meta = tmpMeta
}

0 comments on commit 09e07ae

Please sign in to comment.