Skip to content

Commit 4d2de61

Browse files
authored
Merge pull request #152 from mauriciopoppe/rename-internal
Rename /internal to pkg, nested /internal/server/<group>/internal to impl
2 parents e7c1df0 + 915b50d commit 4d2de61

File tree

141 files changed

+5155
-5162
lines changed

Some content is hidden

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

141 files changed

+5155
-5162
lines changed

.github/workflows/windows.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- name: Build
2222
run: |
2323
go build -v -a -o ./bin/csi-proxy.exe ./cmd/csi-proxy
24+
go build -v -a -o ./bin/csi-proxy-api-gen.exe ./cmd/csi-proxy-api-gen
2425
- name: Run Windows Integration Tests
2526
run: |
2627
# start the CSI Proxy before running tests on windows
@@ -47,4 +48,4 @@ jobs:
4748
uses: actions/checkout@v2
4849
- name: Run Windows Unit Tests
4950
run: |
50-
go test -v -race ./internal/...
51+
go test -v -race ./pkg/...

cmd/csi-proxy-api-gen/generators/api.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const (
3232
csiProxyAPIPath = csiProxyRootPath + "/client/api/"
3333

3434
// defaultServerBasePkg is the default output location for generated server files.
35-
defaultServerBasePkg = csiProxyRootPath + "/internal/server"
35+
defaultServerBasePkg = csiProxyRootPath + "/pkg/server"
3636

3737
// defaultClientBasePkg is the default output location for generated client files.
3838
defaultClientBasePkg = csiProxyRootPath + "/client/groups"
@@ -45,7 +45,7 @@ const (
4545
// * +csi-proxy-api-gen=groupName:<snake_case_group_name> to set the group's name
4646
// - defaults to the package's name
4747
// * +csi-proxy-api-gen=serverBasePkg:<pkg_path> to set the base output directory
48-
// for generated server files - defaults to github.com/kubernetes-csi/csi-proxy/internal/server
48+
// for generated server files - defaults to github.com/kubernetes-csi/csi-proxy/pkg/server
4949
// * +csi-proxy-api-gen=clientBasePkg:<pkg_path> to set the base output directory
5050
// for generated client files - defaults to github.com/kubernetes-csi/csi-proxy/client/groups
5151
tagMarker = "+"
@@ -264,7 +264,7 @@ func packagesForGroup(group *groupDefinition, outputBase string) generator.Packa
264264
},
265265

266266
&generator.DefaultPackage{
267-
PackageName: "internal",
267+
PackageName: "impl",
268268
PackagePath: group.internalServerPkg(),
269269
HeaderText: []byte(headerComment),
270270

@@ -299,7 +299,7 @@ func packagesForGroup(group *groupDefinition, outputBase string) generator.Packa
299299
// generate types.go if it doesn't exist and the group only has one version
300300
if len(group.versions) == 1 && !fileExists(path.Join(outputBase, group.internalServerPkg(), "types.go")) {
301301
pkgs = append(pkgs, &generator.DefaultPackage{
302-
PackageName: "internal",
302+
PackageName: "impl",
303303
PackagePath: group.internalServerPkg(),
304304

305305
GeneratorList: []generator.Generator{

cmd/csi-proxy-api-gen/generators/api_group_generated_gen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (g *apiGroupGeneratedGenerator) Filter(*generator.Context, *types.Type) boo
2323
func (g *apiGroupGeneratedGenerator) Imports(*generator.Context) []string {
2424
imports := []string{
2525
"github.com/kubernetes-csi/csi-proxy/client/apiversion",
26-
"srvtypes \"github.com/kubernetes-csi/csi-proxy/internal/server/types\"",
26+
"srvtypes \"github.com/kubernetes-csi/csi-proxy/pkg/server/types\"",
2727
g.groupDefinition.internalServerPkg(),
2828
}
2929

@@ -42,7 +42,7 @@ func (g *apiGroupGeneratedGenerator) Init(context *generator.Context, writer io.
4242
snippetWriter.Do(`
4343
4444
// ensure the server defines all the required methods
45-
var _ internal.ServerInterface = &Server{}
45+
var _ impl.ServerInterface = &Server{}
4646
4747
func (s *Server) VersionedAPIs() []*srvtypes.VersionedAPI {
4848
`, nil)

cmd/csi-proxy-api-gen/generators/groups_and_versions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,21 +164,21 @@ func (d *groupDefinition) serverInterfaceName() string {
164164
}
165165

166166
// serverPkg returns the path of the server package, e.g.
167-
// github.com/kubernetes-csi/csi-proxy/internal/server/<api_group_name>
167+
// github.com/kubernetes-csi/csi-proxy/pkg/server/<api_group_name>
168168
func (d *groupDefinition) serverPkg() string {
169169
return fmt.Sprintf("%s/%s", d.serverBasePkg, d.name)
170170
}
171171

172172
// internalServerPkg returns the path of the internal server package, e.g.
173-
// github.com/kubernetes-csi/csi-proxy/internal/server/<api_group_name>/internal
173+
// github.com/kubernetes-csi/csi-proxy/pkg/server/<api_group_name>/impl
174174
func (d *groupDefinition) internalServerPkg() string {
175-
return fmt.Sprintf("%s/%s/internal", d.serverBasePkg, d.name)
175+
return fmt.Sprintf("%s/%s/impl", d.serverBasePkg, d.name)
176176
}
177177

178178
// versionedServerPkg returns the path of the versioned server package, e.g.
179-
// github.com/kubernetes-csi/csi-proxy/internal/server/<api_group_name>/internal/<version>
179+
// github.com/kubernetes-csi/csi-proxy/pkg/server/<api_group_name>/impl/<version>
180180
func (d *groupDefinition) versionedServerPkg(version string) string {
181-
return fmt.Sprintf("%s/%s/internal/%s", d.serverBasePkg, d.name, version)
181+
return fmt.Sprintf("%s/%s/impl/%s", d.serverBasePkg, d.name, version)
182182
}
183183

184184
// versionedClientPkg returns the path of the versioned client package, e.g.

cmd/csi-proxy-api-gen/generators/server_generated_gen.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ func (g *serverGeneratedGenerator) Init(context *generator.Context, writer io.Wr
4848
snippetWriter.Do(`var version = apiversion.NewVersionOrPanic("$.version$")
4949
5050
type versionedAPI struct {
51-
apiGroupServer internal.ServerInterface
51+
apiGroupServer impl.ServerInterface
5252
}
5353
54-
func NewVersionedServer(apiGroupServer internal.ServerInterface) internal.VersionedAPI {
54+
func NewVersionedServer(apiGroupServer impl.ServerInterface) impl.VersionedAPI {
5555
return &versionedAPI{
5656
apiGroupServer: apiGroupServer,
5757
}
@@ -94,8 +94,8 @@ func (g *serverGeneratedGenerator) writeWrapperFunction(callbackName string, cal
9494
if !isVersionedVariable(param, g.version) {
9595
continue
9696
}
97-
snippetWriter.Do("$.|short$ := &internal.$.|removePackage${}\n", param)
98-
snippetWriter.Do("if err := Convert_"+g.version.Name+"_$.|removePackage$_To_internal_$.|removePackage$($.|versionedVariable$, $.|short$); err != nil {\n", param)
97+
snippetWriter.Do("$.|short$ := &impl.$.|removePackage${}\n", param)
98+
snippetWriter.Do("if err := Convert_"+g.version.Name+"_$.|removePackage$_To_impl_$.|removePackage$($.|versionedVariable$, $.|short$); err != nil {\n", param)
9999
snippetWriter.Do(returnErrLine+"\n}\n", nil)
100100
}
101101
snippetWriter.Do("\n", nil)
@@ -119,7 +119,7 @@ func (g *serverGeneratedGenerator) writeWrapperFunction(callbackName string, cal
119119
continue
120120
}
121121
snippetWriter.Do("$.|versionedVariable$ := &"+g.version.Name+".$.|removePackage${}\n", returnValue)
122-
snippetWriter.Do("if err := Convert_internal_$.|removePackage$_To_"+g.version.Name+"_$.|removePackage$($.|short$, $.|versionedVariable$); err != nil {\n", returnValue)
122+
snippetWriter.Do("if err := Convert_impl_$.|removePackage$_To_"+g.version.Name+"_$.|removePackage$($.|short$, $.|versionedVariable$); err != nil {\n", returnValue)
123123
snippetWriter.Do(returnErrLine+"\n}\n", nil)
124124
}
125125
snippetWriter.Do("\n", nil)

cmd/csi-proxy/main.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package main
33
import (
44
"flag"
55

6-
diskapi "github.com/kubernetes-csi/csi-proxy/internal/os/disk"
7-
filesystemapi "github.com/kubernetes-csi/csi-proxy/internal/os/filesystem"
8-
iscsiapi "github.com/kubernetes-csi/csi-proxy/internal/os/iscsi"
9-
smbapi "github.com/kubernetes-csi/csi-proxy/internal/os/smb"
10-
sysapi "github.com/kubernetes-csi/csi-proxy/internal/os/system"
11-
volumeapi "github.com/kubernetes-csi/csi-proxy/internal/os/volume"
12-
"github.com/kubernetes-csi/csi-proxy/internal/server"
13-
disksrv "github.com/kubernetes-csi/csi-proxy/internal/server/disk"
14-
filesystemsrv "github.com/kubernetes-csi/csi-proxy/internal/server/filesystem"
15-
iscsisrv "github.com/kubernetes-csi/csi-proxy/internal/server/iscsi"
16-
smbsrv "github.com/kubernetes-csi/csi-proxy/internal/server/smb"
17-
syssrv "github.com/kubernetes-csi/csi-proxy/internal/server/system"
18-
srvtypes "github.com/kubernetes-csi/csi-proxy/internal/server/types"
19-
volumesrv "github.com/kubernetes-csi/csi-proxy/internal/server/volume"
6+
diskapi "github.com/kubernetes-csi/csi-proxy/pkg/os/disk"
7+
filesystemapi "github.com/kubernetes-csi/csi-proxy/pkg/os/filesystem"
8+
iscsiapi "github.com/kubernetes-csi/csi-proxy/pkg/os/iscsi"
9+
smbapi "github.com/kubernetes-csi/csi-proxy/pkg/os/smb"
10+
sysapi "github.com/kubernetes-csi/csi-proxy/pkg/os/system"
11+
volumeapi "github.com/kubernetes-csi/csi-proxy/pkg/os/volume"
12+
"github.com/kubernetes-csi/csi-proxy/pkg/server"
13+
disksrv "github.com/kubernetes-csi/csi-proxy/pkg/server/disk"
14+
filesystemsrv "github.com/kubernetes-csi/csi-proxy/pkg/server/filesystem"
15+
iscsisrv "github.com/kubernetes-csi/csi-proxy/pkg/server/iscsi"
16+
smbsrv "github.com/kubernetes-csi/csi-proxy/pkg/server/smb"
17+
syssrv "github.com/kubernetes-csi/csi-proxy/pkg/server/system"
18+
srvtypes "github.com/kubernetes-csi/csi-proxy/pkg/server/types"
19+
volumesrv "github.com/kubernetes-csi/csi-proxy/pkg/server/volume"
2020
"golang.org/x/sys/windows"
2121
"golang.org/x/sys/windows/svc"
2222
"k8s.io/klog/v2"

docs/API.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ The server exposes a number of API groups, all independent of each other. Additi
88

99
APIs are defined by protobuf files; each API group should live in its own directory under `client/api/<api_group_name>` in this repo's root (e.g. `client/api/iscsi`), and then define each of its version in `client/api/<api_group_name>/<version>/api.proto` files (e.g. `client/api/iscsi/v1/api.proto`). Each `proto` file should define exactly one RPC service.
1010

11-
Internally, there is only one server `struct` per API group, that handles all the versions for that API group. That server is defined in this repo's `internal/server/<api_group_name>` (e.g. `internal/server/iscsi`) go package. This go package should follow the following pattern:
11+
Internally, there is only one server `struct` per API group, that handles all the versions for that API group. That server is defined in this repo's `pkg/server/<api_group_name>` (e.g. `pkg/server/iscsi`) go package. This go package should follow the following pattern:
1212

1313
<a name="serverPkgTree"></a>
1414
```
15-
internal/server/<api_group_name>
16-
├── internal
15+
pkg/server/<api_group_name>
16+
├── impl
1717
│   └── types.go
1818
└── server.go
1919
```
@@ -59,13 +59,13 @@ message ComputeDoubleRequest{
5959
6060
message ComputeDoubleResponse{
6161
int64 response = 2;
62-
62+
6363
// set to true if the result overflowed
6464
bool overflow = 3;
6565
}
6666
```
6767

68-
then `internal/server/dummy/internal/types.go` could look something like:
68+
then `pkg/server/dummy/impl/types.go` could look something like:
6969
```go
7070
type ComputeDoubleRequest struct {
7171
Input int64
@@ -76,15 +76,15 @@ type ComputeDoubleResponse struct {
7676
Overflow bool
7777
}
7878
```
79-
and then the API group's server (`internal/server/dummy/server.go`) needs to define the callbacks to handle requests for all API versions, e.g.:
79+
and then the API group's server (`pkg/server/dummy/server.go`) needs to define the callbacks to handle requests for all API versions, e.g.:
8080
```go
8181
type Server struct{}
8282

83-
func (s *Server) ComputeDouble(ctx context.Context, request *internal.ComputeDoubleRequest, version apiversion.Version) (*internal.ComputeDoubleResponse, error) {
83+
func (s *Server) ComputeDouble(ctx context.Context, request *impl.ComputeDoubleRequest, version apiversion.Version) (*impl.ComputeDoubleResponse, error) {
8484
in := request.Input64
8585
out := 2 * in
86-
87-
response := &internal.ComputeDoubleResponse{}
86+
87+
response := &impl.ComputeDoubleResponse{}
8888

8989
if sign(in) != sign(out) {
9090
// overflow
@@ -114,14 +114,14 @@ All the boilerplate code to:
114114
* create clients to talk to the API group and its versions
115115
is generated automatically using [gengo](https://github.com/kubernetes/gengo).
116116

117-
The only caveat is that when conversions cannot be made trivially (e.g. when fields from internal and versioned `struct`s have different types), API devs need to define conversion functions. They can do that by creating an (otherwise optional) `internal/server/<api_group_name>/internal/<version>/conversion.go` file, containing functions of the form `func convert_pb_<Type>_To_internal_<Type>(in *pb.<Type>, out *internal.<Type>) error` or `func convert_internal_<Type>_To_pb_<Type>(in *internal.<Type>, out *pb.<Type>) error`; for example, in our `dummy` example above, we need to define a conversion function to account for the different fields in requests and responses from `v1alpha1` to `v1`; so `internal/server/dummy/internal/v1alpha1/conversion.go` could look like:
117+
The only caveat is that when conversions cannot be made trivially (e.g. when fields from internal and versioned `struct`s have different types), API devs need to define conversion functions. They can do that by creating an (otherwise optional) `pkg/server/<api_group_name>/impl/<version>/conversion.go` file, containing functions of the form `func convert_pb_<Type>_To_impl_<Type>(in *pb.<Type>, out *impl.<Type>) error` or `func convert_impl_<Type>_To_pb_<Type>(in *impl.<Type>, out *pb.<Type>) error`; for example, in our `dummy` example above, we need to define a conversion function to account for the different fields in requests and responses from `v1alpha1` to `v1`; so `pkg/server/dummy/impl/v1alpha1/conversion.go` could look like:
118118
```go
119-
func convert_pb_ComputeDoubleRequest_To_internal_ComputeDoubleRequest(in *pb.ComputeDoubleRequest, out *internal.ComputeDoubleRequest) error {
119+
func convert_pb_ComputeDoubleRequest_To_impl_ComputeDoubleRequest(in *pb.ComputeDoubleRequest, out *impl.ComputeDoubleRequest) error {
120120
out.Input64 = int64(in.Input32)
121121
return nil
122122
}
123123

124-
func convert_internal_ComputeDoubleResponse_To_pb_ComputeDoubleResponse(in *internal.ComputeDoubleResponse, out *pb.ComputeDoubleResponse) error {
124+
func convert_impl_ComputeDoubleResponse_To_pb_ComputeDoubleResponse(in *impl.ComputeDoubleResponse, out *pb.ComputeDoubleResponse) error {
125125
i := in.Response
126126
if i > math.MaxInt32 || i < math.MinInt32 {
127127
return fmt.Errorf("int32 overflow for %d", i)
@@ -220,23 +220,23 @@ First, it looks for all API group definitions, which are either subdirectories o
220220

221221
Then for each API group it finds:
222222
1. it iterates through each version subpackage, and in each looks for the `<ApiGroupName>Server` interface, and compiles the list of callbacks that the group's `Server` needs to implement as well as the list of top-level `struct`s (`*Request`s and `*Response`s)
223-
2. it looks for an existing `internal/server/<api_group_name>/internal/types.go` file:
223+
2. it looks for an existing `pkg/server/<api_group_name>/impl/types.go` file:
224224
* if it exists, it checks that it contains all the expected top-level `struct`s from the previous step
225225
* if it doesn't exist, _and_ the API group only defines one version, it auto-generates one that simply copies the protobuf `struct`s (from the previous step) - this is meant to make it easy to bootstrap a new API group
226-
3. it generates the `internal/server/<api_group_name>/internal/types_generated.go` file, using the list of callbacks from the first step above
227-
4. if `internal/server/<api_group_name>/server.go` doesn't exist, it generates a skeleton for it - this, too, is meant to make it easy to bootstrap new API groups
226+
3. it generates the `pkg/server/<api_group_name>/impl/types_generated.go` file, using the list of callbacks from the first step above
227+
4. if `pkg/server/<api_group_name>/server.go` doesn't exist, it generates a skeleton for it - this, too, is meant to make it easy to bootstrap new API groups
228228
5. then for each version of the API:
229-
1. it looks for an existing `internal/server/<api_group_name>/internal/<version>/conversion.go`, generates an empty one if it doesn't exist; then looks for existing conversion functions
230-
2. it generates missing conversion functions to `internal/server/<api_group_name>/internal/<version>/conversion_generated.go`
231-
3. it generates `internal/server/<api_group_name>/internal/<version>/server_generated.go`
232-
6. it generates `internal/server/<api_group_name>/internal/api_group_generated.go` to list all the versioned servers it's just created
229+
1. it looks for an existing `pkg/server/<api_group_name>/impl/<version>/conversion.go`, generates an empty one if it doesn't exist; then looks for existing conversion functions
230+
2. it generates missing conversion functions to `pkg/server/<api_group_name>/impl/<version>/conversion_generated.go`
231+
3. it generates `pkg/server/<api_group_name>/impl/<version>/server_generated.go`
232+
6. it generates `pkg/server/<api_group_name>/impl/api_group_generated.go` to list all the versioned servers it's just created
233233
7. and finally, it generates `client/groups/<api_group_name>/<version>/client_generated.go` for each version
234234

235235
When `csi-proxy-api-gen` has successfully run to completion, [our example API group's go package from earlier](#serverPkgTree) will look something like:
236236
```
237-
internal/server/<api_group_name>
237+
pkg/server/<api_group_name>
238238
├── api_group_generated.go
239-
├── internal
239+
├── impl
240240
│   ├── types.go
241241
│   ├── types_generated.go
242242
│   ├── v1

0 commit comments

Comments
 (0)