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(meshtrafficpermission): nil pointer for autoreachableservice when no top targetRef (backport of #12152) #12160

Closed
wants to merge 1 commit into from
Closed
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
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
Loading