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

catalog: improve the bound workload identity encoding on services #20458

Merged
merged 4 commits into from
Feb 2, 2024
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
10 changes: 10 additions & 0 deletions internal/catalog/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func SimplifyFailoverPolicy(svc *pbcatalog.Service, failover *pbcatalog.Failover
return types.SimplifyFailoverPolicy(svc, failover)
}

// GetBoundIdentities returns the unique list of workload identity references
// encoded into a data-bearing status condition on a Service resource by the
// endpoints controller.
//
// This allows a controller to skip watching ServiceEndpoints (which is
// expensive) to discover this data.
func GetBoundIdentities(res *pbresource.Resource) []string {
return endpoints.GetBoundIdentities(res)
}

// ValidateLocalServiceRefNoSection ensures the following:
//
// - ref is non-nil
Expand Down
46 changes: 46 additions & 0 deletions internal/catalog/internal/controllers/endpoints/bound.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package endpoints

import (
"sort"
"strings"

"github.com/hashicorp/consul/proto-public/pbresource"
)

// GetBoundIdentities returns the unique list of workload identity references
// encoded into a data-bearing status condition on a Service resource by the
// endpoints controller.
//
// This allows a controller to skip watching ServiceEndpoints (which is
// expensive) to discover this data.
func GetBoundIdentities(res *pbresource.Resource) []string {
if res.GetStatus() == nil {
return nil
}

status, ok := res.GetStatus()[ControllerID]
if !ok {
return nil
}

var encoded string
for _, cond := range status.GetConditions() {
if cond.GetType() == StatusConditionBoundIdentities && cond.GetState() == pbresource.Condition_STATE_TRUE {
encoded = cond.GetMessage()
break
}
}
if encoded == "" {
return nil
}

identities := strings.Split(encoded, ",")

// Ensure determinstic sort so we don't get into infinite-reconcile
sort.Strings(identities)

return identities
}
63 changes: 63 additions & 0 deletions internal/catalog/internal/controllers/endpoints/bound_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package endpoints_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/hashicorp/consul/internal/catalog/internal/controllers/endpoints"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/demo"
rtest "github.com/hashicorp/consul/internal/resource/resourcetest"
"github.com/hashicorp/consul/proto-public/pbresource"
pbdemo "github.com/hashicorp/consul/proto/private/pbdemo/v2"
)

func TestGetBoundIdentities(t *testing.T) {
tenancy := resource.DefaultNamespacedTenancy()

build := func(conds ...*pbresource.Condition) *pbresource.Resource {
b := rtest.Resource(demo.TypeV2Artist, "artist").
WithTenancy(tenancy).
WithData(t, &pbdemo.Artist{Name: "very arty"})
if len(conds) > 0 {
b.WithStatus(endpoints.ControllerID, &pbresource.Status{
Conditions: conds,
})
}
return b.Build()
}

run := endpoints.GetBoundIdentities

require.Empty(t, run(build(nil)))
require.Empty(t, run(build(&pbresource.Condition{
Type: endpoints.StatusConditionBoundIdentities,
State: pbresource.Condition_STATE_TRUE,
Message: "",
})))
require.Equal(t, []string{"foo"}, run(build(&pbresource.Condition{
Type: endpoints.StatusConditionBoundIdentities,
State: pbresource.Condition_STATE_TRUE,
Message: "foo",
})))
require.Empty(t, run(build(&pbresource.Condition{
Type: endpoints.StatusConditionBoundIdentities,
State: pbresource.Condition_STATE_FALSE,
Message: "foo",
})))
require.Equal(t, []string{"bar", "foo"}, run(build(&pbresource.Condition{
Type: endpoints.StatusConditionBoundIdentities,
State: pbresource.Condition_STATE_TRUE,
Message: "bar,foo", // proper order
})))
require.Equal(t, []string{"bar", "foo"}, run(build(&pbresource.Condition{
Type: endpoints.StatusConditionBoundIdentities,
State: pbresource.Condition_STATE_TRUE,
Message: "foo,bar", // incorrect order gets fixed
})))

}
11 changes: 5 additions & 6 deletions internal/catalog/internal/controllers/endpoints/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package endpoints

import (
"fmt"
"sort"
"strings"

"github.com/hashicorp/consul/proto-public/pbresource"
Expand All @@ -24,9 +24,6 @@ const (

StatusReasonWorkloadIdentitiesFound = "WorkloadIdentitiesFound"
StatusReasonNoWorkloadIdentitiesFound = "NoWorkloadIdentitiesFound"

identitiesFoundMessageFormat = "Found workload identities associated with this service: %q."
identitiesNotFoundChangedMessage = "No associated workload identities found."
)

var (
Expand All @@ -48,15 +45,17 @@ var (
Type: StatusConditionBoundIdentities,
State: pbresource.Condition_STATE_FALSE,
Reason: StatusReasonNoWorkloadIdentitiesFound,
Message: identitiesNotFoundChangedMessage,
Message: "",
}
)

func ConditionIdentitiesFound(identities []string) *pbresource.Condition {
sort.Strings(identities)

return &pbresource.Condition{
Type: StatusConditionBoundIdentities,
State: pbresource.Condition_STATE_TRUE,
Reason: StatusReasonWorkloadIdentitiesFound,
Message: fmt.Sprintf(identitiesFoundMessageFormat, strings.Join(identities, ",")),
Message: strings.Join(identities, ","),
}
}
Loading