Skip to content

Commit

Permalink
fix(meshtrafficpermission): nil pointer for autoreachableservice when…
Browse files Browse the repository at this point in the history
… no top targetRef (#12152)

Signed-off-by: Lukasz Dziedziak <[email protected]>
  • Loading branch information
lukidzi authored and kumahq[bot] committed Dec 3, 2024
1 parent 9bb63e6 commit 4db1cb9
Show file tree
Hide file tree
Showing 4 changed files with 513 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package backends

import (
"maps"

mesh_proto "github.com/kumahq/kuma/api/mesh/v1alpha1"
"github.com/kumahq/kuma/pkg/core"
ms_api "github.com/kumahq/kuma/pkg/core/resources/apis/meshservice/api/v1alpha1"

Check failure on line 8 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / test

no required module provides package github.com/kumahq/kuma/pkg/core/resources/apis/meshservice/api/v1alpha1; to add it:

Check failure on line 8 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / test

no required module provides package github.com/kumahq/kuma/pkg/core/resources/apis/meshservice/api/v1alpha1; to add it:
core_model "github.com/kumahq/kuma/pkg/core/resources/model"
core_rules "github.com/kumahq/kuma/pkg/plugins/policies/core/rules"
mtp_api "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/api/v1alpha1"
graph_util "github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/graph/util"

Check failure on line 12 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / test

no required module provides package github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/graph/util; to add it:

Check failure on line 12 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / test

no required module provides package github.com/kumahq/kuma/pkg/plugins/policies/meshtrafficpermission/graph/util; to add it:
)

var log = core.Log.WithName("rms-graph")

func BuildRules(meshServices []*ms_api.MeshServiceResource, mtps []*mtp_api.MeshTrafficPermissionResource) map[core_model.TypedResourceIdentifier]core_rules.Rules {

Check failure on line 17 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / check

undefined: core_model.TypedResourceIdentifier (typecheck)
rules := map[core_model.TypedResourceIdentifier]core_rules.Rules{}

Check failure on line 18 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / check

undefined: core_model.TypedResourceIdentifier (typecheck)
for _, ms := range meshServices {
dpTags := maps.Clone(ms.Spec.Selector.DataplaneTags)

Check failure on line 20 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / check

cannot infer M (/opt/hostedtoolcache/go/1.22.9/x64/src/maps/maps.go:41:12) (typecheck)
if origin, ok := core_model.ResourceOrigin(ms.GetMeta()); ok {
dpTags[mesh_proto.ResourceOriginLabel] = string(origin)
}
if ms.GetMeta().GetLabels() != nil && ms.GetMeta().GetLabels()[mesh_proto.ZoneTag] != "" {
dpTags[mesh_proto.ZoneTag] = ms.GetMeta().GetLabels()[mesh_proto.ZoneTag]
}
rl, ok, err := graph_util.ComputeMtpRulesForTags(dpTags, trimNotSupportedTags(mtps, dpTags))
if err != nil {
log.Error(err, "service could not be matched. It won't be reached by any other service", "service", ms.Meta.GetName())
continue
}
if !ok {
continue
}
rules[core_model.NewTypedResourceIdentifier(ms)] = rl

Check failure on line 35 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / check

undefined: core_model.NewTypedResourceIdentifier (typecheck)
}
return rules
}

// trimNotSupportedTags removes tags that are not available in MeshService.dpTags + kuma.io/origin and kuma.io/zone
func trimNotSupportedTags(mtps []*mtp_api.MeshTrafficPermissionResource, supportedTags map[string]string) []*mtp_api.MeshTrafficPermissionResource {
newMtps := make([]*mtp_api.MeshTrafficPermissionResource, len(mtps))
for i, mtp := range mtps {
if mtp.Spec != nil && mtp.Spec.TargetRef != nil && len(mtp.Spec.TargetRef.Tags) > 0 {

Check failure on line 44 in pkg/plugins/policies/meshtrafficpermission/graph/backends/reachable_backend_refs_graph.go

View workflow job for this annotation

GitHub Actions / check

invalid operation: mtp.Spec.TargetRef != nil (mismatched types "github.com/kumahq/kuma/api/common/v1alpha1".TargetRef and untyped nil) (typecheck)
filteredTags := map[string]string{}
for tag, val := range mtp.Spec.TargetRef.Tags {
if _, ok := supportedTags[tag]; ok {
filteredTags[tag] = val
}
}
if len(filteredTags) != len(mtp.Spec.TargetRef.Tags) {
mtp = &mtp_api.MeshTrafficPermissionResource{
Meta: mtp.Meta,
Spec: mtp.Spec.DeepCopy(),
}
mtp.Spec.TargetRef.Tags = filteredTags
}
}
newMtps[i] = mtp
}
return newMtps
}
Loading

0 comments on commit 4db1cb9

Please sign in to comment.