Skip to content

Commit 12f6b13

Browse files
committed
ce: feat(server): Add FilteredWithFeatures fn and new server options
1 parent 1a3cd9b commit 12f6b13

File tree

2 files changed

+105
-29
lines changed

2 files changed

+105
-29
lines changed

internal/server/options.go

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,46 @@ func GetOpts(opt ...Option) options {
2323
// Option - how Options are passed as arguments
2424
type Option func(*options)
2525

26+
// StorageBucketCredentialInfo defines the parameters to pass into the
27+
// WithFilterWorkersByStorageBucketCredentialState option.
28+
type StorageBucketCredentialInfo struct {
29+
CredentialId string
30+
Filters []FilterStorageBucketCredentialStateFn
31+
}
32+
2633
// options = how options are represented
2734
type options struct {
28-
withName string
29-
withPublicId string
30-
withDescription string
31-
withAddress string
32-
withLimit int
33-
withLiveness time.Duration
34-
withUpdateTags bool
35-
withWorkerTags []*Tag
36-
withWorkerKeyIdentifier string
37-
withWorkerKeys WorkerKeys
38-
withControllerEncryptionPrivateKey []byte
39-
withKeyId string
40-
withNonce []byte
41-
withNewIdFunc func(context.Context) (string, error)
42-
WithFetchNodeCredentialsRequest *types.FetchNodeCredentialsRequest
43-
withTestPkiWorkerAuthorized bool
44-
withTestPkiWorkerKeyId *string
45-
withTestUseInputTagsAsApiTags bool
46-
withWorkerType WorkerType
47-
withRoot RootInfo
48-
withStopAfter uint
49-
WithCreateControllerLedActivationToken bool
50-
withReleaseVersion string
51-
withOperationalState string
52-
withLocalStorageState string
53-
withActiveWorkers bool
54-
withFeature version.Feature
55-
withDirectlyConnected bool
56-
withWorkerPool []string
35+
withName string
36+
withPublicId string
37+
withDescription string
38+
withAddress string
39+
withLimit int
40+
withLiveness time.Duration
41+
withUpdateTags bool
42+
withWorkerTags []*Tag
43+
withWorkerKeyIdentifier string
44+
withWorkerKeys WorkerKeys
45+
withControllerEncryptionPrivateKey []byte
46+
withKeyId string
47+
withNonce []byte
48+
withNewIdFunc func(context.Context) (string, error)
49+
WithFetchNodeCredentialsRequest *types.FetchNodeCredentialsRequest
50+
withTestPkiWorkerAuthorized bool
51+
withTestPkiWorkerKeyId *string
52+
withTestUseInputTagsAsApiTags bool
53+
withWorkerType WorkerType
54+
withRoot RootInfo
55+
withStopAfter uint
56+
WithCreateControllerLedActivationToken bool
57+
withReleaseVersion string
58+
withOperationalState string
59+
withLocalStorageState string
60+
withActiveWorkers bool
61+
withFeature version.Feature
62+
withDirectlyConnected bool
63+
withWorkerPool []string
64+
withFilterWorkersByStorageBucketCredentialState *StorageBucketCredentialInfo
65+
withFilterWorkersByLocalStorageState bool
5766
}
5867

5968
func getDefaultOptions() options {
@@ -276,3 +285,21 @@ func WithLocalStorageState(state string) Option {
276285
o.withLocalStorageState = state
277286
}
278287
}
288+
289+
// WithFilterWorkersByStorageBucketCredentialState receives a storage bucket
290+
// credential id and filters to apply and calls
291+
// FilterWorkersByStorageBucketCredentialState in supported repository
292+
// functions.
293+
func WithFilterWorkersByStorageBucketCredentialState(ci *StorageBucketCredentialInfo) Option {
294+
return func(o *options) {
295+
o.withFilterWorkersByStorageBucketCredentialState = ci
296+
}
297+
}
298+
299+
// WithFilterWorkersByLocalStorageState controls whether
300+
// FilterWorkersByLocalStorageState is called in supported repository functions.
301+
func WithFilterWorkersByLocalStorageState(filter bool) Option {
302+
return func(o *options) {
303+
o.withFilterWorkersByLocalStorageState = filter
304+
}
305+
}

internal/server/worker_list.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
package server
55

66
import (
7+
"context"
78
stderrors "errors"
89
"fmt"
10+
"strings"
911

1012
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
13+
"github.com/hashicorp/boundary/internal/event"
1114
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/targets"
1215
"github.com/hashicorp/boundary/sdk/pbs/plugin"
1316
"github.com/hashicorp/boundary/version"
1417
"github.com/hashicorp/go-bexpr"
18+
gvers "github.com/hashicorp/go-version"
1519
"github.com/mitchellh/pointerstructure"
1620
"google.golang.org/grpc/codes"
1721
)
@@ -84,6 +88,51 @@ func (w WorkerList) Filtered(eval *bexpr.Evaluator) (WorkerList, error) {
8488
return ret, nil
8589
}
8690

91+
// FilteredWithFeatures returns a new workerList where all elements contained in
92+
// it are the ones which from the original workerList that pass the evaluator's
93+
// evaluation and satisfy the features required.
94+
func (w WorkerList) FilteredWithFeatures(ctx context.Context, eval *bexpr.Evaluator, features []version.Feature) (WorkerList, error) {
95+
const op = "server.WorkerList.FilteredWithFeatures"
96+
var ret []*Worker
97+
workerLoop:
98+
for _, worker := range w {
99+
filterInput := map[string]interface{}{
100+
"name": worker.GetName(),
101+
"tags": worker.CanonicalTags(),
102+
}
103+
ok, err := eval.Evaluate(filterInput)
104+
if err != nil && !stderrors.Is(err, pointerstructure.ErrNotFound) {
105+
// If we find pointerstructure.ErrNotFound, don't error out but
106+
// ignore the worker and go to the next.
107+
return nil, err
108+
}
109+
if !ok {
110+
continue
111+
}
112+
if len(features) > 0 {
113+
versionString := worker.ReleaseVersion
114+
idx := strings.Index(versionString, version.BoundaryPrefix)
115+
if idx >= 0 {
116+
versionString = versionString[idx+len(version.BoundaryPrefix):]
117+
}
118+
nodeVersion, err := gvers.NewVersion(versionString)
119+
if err != nil {
120+
// Emit error and continue, as we might still find a different worker
121+
event.WriteError(ctx, op, fmt.Errorf("cannot parse worker version %s for worker %s", versionString, worker.Name))
122+
continue
123+
}
124+
for _, f := range features {
125+
if !version.SupportsFeature(nodeVersion, f) {
126+
continue workerLoop
127+
}
128+
}
129+
}
130+
131+
ret = append(ret, worker)
132+
}
133+
return ret, nil
134+
}
135+
87136
// SeparateManagedWorkers divides the incoming workers into managed and
88137
// unmanaged workers, respectively
89138
func SeparateManagedWorkers(workers WorkerList) (managedWorkers, nonManagedWorkers WorkerList) {

0 commit comments

Comments
 (0)