Skip to content

Commit 123bc95

Browse files
authored
Add Common Controller Caching Infrastructure (hashicorp#19767)
* Add Common Controller Caching Infrastructure
1 parent c870c00 commit 123bc95

File tree

161 files changed

+14128
-985
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+14128
-985
lines changed

.grpcmocks.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: BUSL-1.1
3+
4+
with-expecter: true
5+
all: true
6+
recursive: true
7+
# We don't want the mocks within proto-public to prevent forcing a dependency
8+
# of the testify library on the modules usage. The mocks are only for
9+
# internal testing purposes. Other consumers can generated the mocks into
10+
# their own code base.
11+
dir: "grpcmocks/{{.InterfaceDirRelative}}"
12+
outpkg: "mock{{.PackageName}}"
13+
mockname: "{{.InterfaceName}}"
14+
packages:
15+
github.com/hashicorp/consul/proto-public/pbacl:
16+
github.com/hashicorp/consul/proto-public/pbconnectca:
17+
github.com/hashicorp/consul/proto-public/pbdataplane:
18+
github.com/hashicorp/consul/proto-public/pbserverdiscovery:
19+
github.com/hashicorp/consul/proto-public/pbresource:
20+
github.com/hashicorp/consul/proto-public/pbdns:

Makefile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ GO_MODULES := $(shell find . -name go.mod -exec dirname {} \; | grep -v "proto-g
1111
# or the string @DEV to imply use what is currently installed locally.
1212
###
1313
GOLANGCI_LINT_VERSION='v1.51.1'
14-
MOCKERY_VERSION='v2.20.0'
14+
MOCKERY_VERSION='v2.37.1'
1515
BUF_VERSION='v1.26.0'
1616

1717
PROTOC_GEN_GO_GRPC_VERSION='v1.2.0'
@@ -562,11 +562,8 @@ proto-gen: proto-tools ## Regenerates all Go files from protobuf definitions
562562

563563
.PHONY: proto-mocks
564564
proto-mocks: ## Proto mocks
565-
for dir in $(MOCKED_PB_DIRS) ; do \
566-
cd proto-public && \
567-
rm -f $$dir/mock*.go && \
568-
mockery --dir $$dir --inpackage --all --recursive --log-level trace ; \
569-
done
565+
@rm -rf grpcmocks/*
566+
@mockery --config .grpcmocks.yaml
570567

571568
.PHONY: proto-format
572569
proto-format: proto-tools ## Proto format

agent/grpc-external/services/resource/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func (s *Server) ensureDeleteRequestValid(req *pbresource.DeleteRequest) (*resou
189189
return nil, err
190190
}
191191

192-
if err := validateScopedTenancy(reg.Scope, reg.Type, req.Id.Tenancy); err != nil {
192+
if err := validateScopedTenancy(reg.Scope, reg.Type, req.Id.Tenancy, false); err != nil {
193193
return nil, err
194194
}
195195

agent/grpc-external/services/resource/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func (s *Server) ensureListRequestValid(req *pbresource.ListRequest) (*resource.
109109
}
110110

111111
// Error when partition scoped and namespace not empty.
112-
if reg.Scope == resource.ScopePartition && req.Tenancy.Namespace != "" {
112+
if reg.Scope == resource.ScopePartition && req.Tenancy.Namespace != "" && req.Tenancy.Namespace != storage.Wildcard {
113113
return nil, status.Errorf(
114114
codes.InvalidArgument,
115115
"partition scoped type %s cannot have a namespace. got: %s",

agent/grpc-external/services/resource/list_by_owner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (s *Server) ensureListByOwnerRequestValid(req *pbresource.ListByOwnerReques
104104
return nil, err
105105
}
106106

107-
if err = validateScopedTenancy(reg.Scope, reg.Type, req.Owner.Tenancy); err != nil {
107+
if err = validateScopedTenancy(reg.Scope, reg.Type, req.Owner.Tenancy, true); err != nil {
108108
return nil, err
109109
}
110110

agent/grpc-external/services/resource/list_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,26 @@ func TestList_Tenancy_Defaults_And_Normalization(t *testing.T) {
220220
artistRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: artist})
221221
require.NoError(t, err)
222222

223+
// Write a cluster scoped Executive
224+
executive, err := demo.GenerateV1Executive("king-arthur", "CEO")
225+
require.NoError(t, err)
226+
executiveRsp, err := client.Write(ctx, &pbresource.WriteRequest{Resource: executive})
227+
require.NoError(t, err)
228+
223229
// List and verify correct resource returned for empty tenancy units.
224230
listRsp, err := client.List(ctx, &pbresource.ListRequest{
225231
Type: tc.typ,
226232
Tenancy: tc.tenancy,
227233
})
228234
require.NoError(t, err)
229235
require.Len(t, listRsp.Resources, 1)
230-
if tc.typ == demo.TypeV1RecordLabel {
236+
switch tc.typ {
237+
case demo.TypeV1RecordLabel:
231238
prototest.AssertDeepEqual(t, recordLabelRsp.Resource, listRsp.Resources[0])
232-
} else {
239+
case demo.TypeV1Artist:
233240
prototest.AssertDeepEqual(t, artistRsp.Resource, listRsp.Resources[0])
241+
case demo.TypeV1Executive:
242+
prototest.AssertDeepEqual(t, executiveRsp.Resource, listRsp.Resources[0])
234243
}
235244
})
236245
}

agent/grpc-external/services/resource/read.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (s *Server) ensureReadRequestValid(req *pbresource.ReadRequest) (*resource.
107107
}
108108

109109
// Check scope
110-
if err = validateScopedTenancy(reg.Scope, req.Id.Type, req.Id.Tenancy); err != nil {
110+
if err = validateScopedTenancy(reg.Scope, req.Id.Type, req.Id.Tenancy, false); err != nil {
111111
return nil, err
112112
}
113113

agent/grpc-external/services/resource/server.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,25 +243,26 @@ func tenancyExists(reg *resource.Registration, tenancyBridge TenancyBridge, tena
243243
return nil
244244
}
245245

246-
func validateScopedTenancy(scope resource.Scope, resourceType *pbresource.Type, tenancy *pbresource.Tenancy) error {
247-
if scope == resource.ScopePartition && tenancy.Namespace != "" {
246+
func validateScopedTenancy(scope resource.Scope, resourceType *pbresource.Type, tenancy *pbresource.Tenancy, allowWildcards bool) error {
247+
if scope == resource.ScopePartition && tenancy.Namespace != "" && (!allowWildcards || tenancy.Namespace != storage.Wildcard) {
248248
return status.Errorf(
249249
codes.InvalidArgument,
250250
"partition scoped resource %s cannot have a namespace. got: %s",
251251
resource.ToGVK(resourceType),
252252
tenancy.Namespace,
253253
)
254254
}
255+
255256
if scope == resource.ScopeCluster {
256-
if tenancy.Partition != "" {
257+
if tenancy.Partition != "" && (!allowWildcards || tenancy.Partition != storage.Wildcard) {
257258
return status.Errorf(
258259
codes.InvalidArgument,
259260
"cluster scoped resource %s cannot have a partition: %s",
260261
resource.ToGVK(resourceType),
261262
tenancy.Partition,
262263
)
263264
}
264-
if tenancy.Namespace != "" {
265+
if tenancy.Namespace != "" && (!allowWildcards || tenancy.Namespace != storage.Wildcard) {
265266
return status.Errorf(
266267
codes.InvalidArgument,
267268
"cluster scoped resource %s cannot have a namespace: %s",

agent/grpc-external/services/resource/server_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,31 @@ func wildcardTenancyCases() map[string]struct {
209209
PeerName: "local",
210210
},
211211
},
212+
"partitioned type with wildcard partition and namespace": {
213+
typ: demo.TypeV1RecordLabel,
214+
tenancy: &pbresource.Tenancy{
215+
Partition: "*",
216+
Namespace: "*",
217+
PeerName: "local",
218+
},
219+
},
220+
"cluster type with empty partition and namespace": {
221+
typ: demo.TypeV1Executive,
222+
tenancy: &pbresource.Tenancy{
223+
Partition: "",
224+
Namespace: "",
225+
PeerName: "local",
226+
},
227+
},
228+
229+
"cluster type with wildcard partition and namespace": {
230+
typ: demo.TypeV1Executive,
231+
tenancy: &pbresource.Tenancy{
232+
Partition: "*",
233+
Namespace: "*",
234+
PeerName: "local",
235+
},
236+
},
212237
}
213238
}
214239

agent/grpc-external/services/resource/watch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (s *Server) ensureWatchListRequestValid(req *pbresource.WatchListRequest) (
116116
}
117117

118118
// Check scope
119-
if err = validateScopedTenancy(reg.Scope, req.Type, req.Tenancy); err != nil {
119+
if err = validateScopedTenancy(reg.Scope, req.Type, req.Tenancy, true); err != nil {
120120
return nil, err
121121
}
122122

0 commit comments

Comments
 (0)