-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Simplify gateway backend registration #5111
Conversation
4dd4004
to
a187c4c
Compare
Codecov Report
@@ Coverage Diff @@
## master #5111 +/- ##
==========================================
+ Coverage 61.25% 61.82% +0.56%
==========================================
Files 193 153 -40
Lines 28411 26216 -2195
==========================================
- Hits 17404 16208 -1196
+ Misses 9742 8808 -934
+ Partials 1265 1200 -65
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #5111 +/- ##
==========================================
+ Coverage 61.36% 61.82% +0.46%
==========================================
Files 194 153 -41
Lines 28567 26237 -2330
==========================================
- Hits 17529 16220 -1309
+ Misses 9768 8821 -947
+ Partials 1270 1196 -74
Continue to review full report at Codecov.
|
9272afa
to
47b8a77
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall a nice change, but some comments about style and structure.
cmd/gateway-main.go
Outdated
} | ||
|
||
return nil, fmt.Errorf("Unrecognized backend type %s", backendType) | ||
return nil, fmt.Errorf("unrecognized backend type %s", backendType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Capitalize the strings for errors, this is to be consistent with our code base.
cmd/gateway-s3.go
Outdated
` | ||
|
||
mustRegisterGatewayBackend(gatewayBackendEntry{ | ||
Name: "s3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use s3Backend
const here..
cmd/gateway-s3.go
Outdated
mustRegisterGatewayBackend(gatewayBackendEntry{ | ||
Name: "s3", | ||
Cmd: cli.Command{ | ||
Name: "s3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above and can be replicated for all gateway backends..
cmd/gateway-s3.go
Outdated
@@ -102,12 +161,13 @@ type s3Objects struct { | |||
} | |||
|
|||
// newS3Gateway returns s3 gatewaylayer | |||
func newS3Gateway(host string) (GatewayLayer, error) { | |||
func newS3Gateway(args cli.Args) (GatewayLayer, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if a gateway takes only one argument then we don't need to expand the scope here by adding more args. This refactor may have led to that. But taking in input param as host
is more meaningful here.
cmd/gateway-main.go
Outdated
Name gatewayBackend | ||
Cmd cli.Command | ||
New func(args cli.Args) (GatewayLayer, error) | ||
IsExperimental bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to call this IsExperimental
just say it as Experimental
is fine.
cmd/gateway-main.go
Outdated
} | ||
|
||
gatewayBackendReg = map[gatewayBackend]gatewayBackendEntry{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid map and make this more safer by using a slice instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can avoid map and make this more safer by using a slice instead?
Not sure, would you mind telling why a slice is more safer in this scenario?
cmd/gateway-main.go
Outdated
) | ||
// Represents the type of each item in gateway backend registry. | ||
type gatewayBackendEntry struct { | ||
Name gatewayBackend |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no reason for this type if there are no constants anymore.
cmd/gateway-main.go
Outdated
return errors.New("field 'New' cannot be nil") | ||
} | ||
|
||
if e.Cmd.Action != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is sort of incorrect with the overall usage and pattern, There is no reason we should add this check as it might be frivolous indeed. We should run a Validate() func right after the Action is registered, or just remove verifying Action.
cmd/gateway-main.go
Outdated
return fmt.Errorf("duplicate backend type found in registry: %s", string(backendType)) | ||
} | ||
|
||
entry.Cmd.Action = gatewayMainAction(backendType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This additional code here is not needed, modifying input structs causing side-affects is not a good idea usually. We should simply move this code to mustRegisterGatewayBackend() - gatewayMainAction("s3")
for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So all you would have to do would to do is just simply do validation here and avoid mutation.
cmd/gateway-azure.go
Outdated
@@ -292,12 +354,13 @@ func azureParseBlockID(blockID string) (partID, subPartNumber int, uploadID, md5 | |||
} | |||
|
|||
// Inits azure blob storage client and returns AzureObjects. | |||
func newAzureLayer(host string) (GatewayLayer, error) { | |||
func newAzureLayer(args cli.Args) (GatewayLayer, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue about changing input params similar to my comment on s3 gateway
913aabe
to
5c75965
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have an interface
approach to solve the problem. That way each gateway backend is completely independent. Below is the approach
gateway-main.go:
- Add an interface
type Gateway interface{
Name() string
NewGatewayLayer() (GatewayLayer, error)
}
- Rename
gatewayMain()
tostartGateway()
gateway-s3.go:
- In
init()
add subcommand bygatewayCmd.Subcommands = append(gatewayCmd.Subcommands, cli.Command{...})
- Have
S3Gateway
compatible toGateway
interface
type S3Gateway struct {
host string
}
func (g *S3Gateway) Name() string {
return "s3"
}
func (g *S3Gateway) NewObjectLayer() (GatewayLayer, error) {
// log.Println(colorYellow("\n *** Warning: Not Ready for Production ***"))
// have newS3Gateway() here
}
This logic needs to be done in all gateway code
I think we should not access What about this? type GatewayCommand interface {
// Name is the global unique name for each gateway.
Name() string
// A short description of the usage of this command.
Description() string
// HelpTemplate returns the text template for the command help topic.
HelpTemplate() string
// Parse parse the command line context.
Parse(ctx *cli.Context) error
// NewGatewayLayer returns a new gateway layer.
NewGatewayLayer() (GatewayLayer, error)
}
func RegisterGatewayCommand(command GatewayCommand) error {
// We should not have multiple subcommands with same name.
for _, c := range gatewayCmd.Subcommands {
if c.Name == command.Name() {
return fmt.Errorf("duplicate gateway command: %s", command.Name())
}
}
gatewayCmd.Subcommands = append(gatewayCmd.Subcommands, cli.Command{
Name: command.Name(),
Usage: command.Description(),
Action: gatewayCommandMain(command),
CustomHelpTemplate: command.HelpTemplate(),
Flags: append(serverFlags, globalFlags...),
HideHelpCommand: true,
})
return nil
} |
@timonwong This approach brings a cyclic usage i.e. |
f9e0147
to
5110625
Compare
42632c9
to
6becd79
Compare
Can you have a look latest commit, though I'm afraid it's not very feasible, because:
|
cmd/gateway-main.go
Outdated
// Add more backends here. | ||
) | ||
// GatewayCommandDesc is descriptor for gateway command. | ||
type GatewayCommandDesc struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than cli
package, this information is nowhere required. You could directly pass cli.Command{}
from each gateway to MustRegisterGatewayCommand()
and RegisterGatewayCommand()
.
Note that all gateway-main and gateway-*.go are same package. Its not required to visualize as separate layer. |
6becd79
to
a147717
Compare
4970069
to
90f0c8a
Compare
@balamurugana Done, just check the latest commit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
In minio#5111, 'help' subcommand was moved to `startGateway` during review, but not get restored then. So the "help" subcommand won't function if credentials are not provided.
Description
Motivation and Context
When I'm trying to add a new gateway backend in #5103, several files need to be touched, which IMHO is not very convenient.
In this pull request, gateway backend implementations only need to focus their own scope, and
just invoke
mustRegisterGatewayBackend()
function in theirinit()
.How Has This Been Tested?
Types of changes
Checklist: