-
Notifications
You must be signed in to change notification settings - Fork 76
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
Support registry resource #154
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package paddlecloud | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/google/subcommands" | ||
) | ||
|
||
// DeleteCommand do job killings | ||
type DeleteCommand struct { | ||
} | ||
|
||
// Name is subcommands name | ||
func (*DeleteCommand) Name() string { return "delete" } | ||
|
||
// Synopsis is subcommands synopsis | ||
func (*DeleteCommand) Synopsis() string { return "Delete the specify resource." } | ||
|
||
// Usage is subcommands usage | ||
func (*DeleteCommand) Usage() string { | ||
return `delete registry [registry-name] | ||
` | ||
} | ||
|
||
// SetFlags registers subcommands flags | ||
func (p *DeleteCommand) SetFlags(f *flag.FlagSet) { | ||
} | ||
|
||
// Execute kill command | ||
func (p *DeleteCommand) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { | ||
if f.NArg() != 2 { | ||
f.Usage() | ||
return subcommands.ExitFailure | ||
} | ||
if f.Arg(0) == RegistryCmdName { | ||
name := f.Arg(1) | ||
r := RegistryCmd{SecretName: KubeRegistryName(name)} | ||
err := r.Delete() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "error delete registry: %v\n", err) | ||
return subcommands.ExitFailure | ||
} | ||
fmt.Fprintf(os.Stdout, "registry: [%s] is deleted\n", name) | ||
} else { | ||
f.Usage() | ||
return subcommands.ExitFailure | ||
} | ||
return subcommands.ExitSuccess | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package paddlecloud | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/PaddlePaddle/cloud/go/utils" | ||
"github.com/golang/glog" | ||
"github.com/google/subcommands" | ||
) | ||
|
||
const ( | ||
// RegistryCmdName is subcommand name | ||
RegistryCmdName = "registry" | ||
// RegistryPrefix is the prefix for Kubernetes secret name | ||
RegistryPrefix = "pcloud-registry" | ||
) | ||
|
||
// RegistryCmd is Docker registry secret information | ||
type RegistryCmd struct { | ||
SecretName string `json:"name"` | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
Server string `json:"server"` | ||
} | ||
|
||
// Name is the subcommand name | ||
func (r *RegistryCmd) Name() string { return RegistryCmdName } | ||
|
||
// Synopsis is the subcommand's synopsis | ||
func (r *RegistryCmd) Synopsis() string { return "Add registry secret on paddlecloud." } | ||
|
||
// Usage is the subcommand's usage | ||
func (r *RegistryCmd) Usage() string { | ||
return `registry <options> [add|del]: | ||
` | ||
} | ||
|
||
// SetFlags registers subcommands flags. | ||
func (r *RegistryCmd) SetFlags(f *flag.FlagSet) { | ||
f.StringVar(&r.SecretName, "name", "", "registry secret name") | ||
f.StringVar(&r.Username, "username", "", "your Docker registry username") | ||
f.StringVar(&r.Password, "password", "", "your Docker registry password") | ||
f.StringVar(&r.Server, "server", "", "your Docker registry Server") | ||
} | ||
func (r *RegistryCmd) addRegistrySecret() error { | ||
jsonString, err := json.Marshal(r) | ||
if err != nil { | ||
return err | ||
} | ||
glog.V(10).Infof("Add registry secret: %s to %s\n", jsonString, utils.Config.ActiveConfig.Endpoint+"/api/v1/registry/") | ||
respBody, err := utils.PostCall(utils.Config.ActiveConfig.Endpoint+"/api/v1/registry/", jsonString) | ||
if err != nil { | ||
return err | ||
} | ||
var respObj interface{} | ||
if err = json.Unmarshal(respBody, &respObj); err != nil { | ||
return err | ||
} | ||
// FIXME: Return an error if error message is not empty. Use response code instead | ||
errMsg := respObj.(map[string]interface{})["msg"].(string) | ||
if len(errMsg) > 0 { | ||
return errors.New(errMsg) | ||
} | ||
return nil | ||
} | ||
|
||
// Delete the specify registry | ||
func (r *RegistryCmd) Delete() error { | ||
jsonString, err := json.Marshal(r) | ||
if err != nil { | ||
return err | ||
} | ||
glog.V(10).Infof("Delete registry secret: %s to %s\n", jsonString, utils.Config.ActiveConfig.Endpoint+"/api/v1/registry/") | ||
respBody, err := utils.DeleteCall(utils.Config.ActiveConfig.Endpoint+"/api/v1/registry/", jsonString) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var respObj interface{} | ||
if err = json.Unmarshal(respBody, &respObj); err != nil { | ||
return err | ||
} | ||
// FIXME: Return an error if error message is not empty. Use response code instead | ||
errMsg := respObj.(map[string]interface{})["msg"].(string) | ||
if len(errMsg) > 0 { | ||
return errors.New(errMsg) | ||
} | ||
return nil | ||
} | ||
func (r *RegistryCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { | ||
if r.SecretName == "" || r.Username == "" || r.Password == "" || r.Server == "" { | ||
f.Usage() | ||
return subcommands.ExitFailure | ||
} | ||
r.SecretName = strings.Join([]string{RegistryPrefix, r.SecretName}, "-") | ||
err := r.addRegistrySecret() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "add registry secret failed: %s\n", err) | ||
return subcommands.ExitFailure | ||
} | ||
return subcommands.ExitSuccess | ||
} | ||
|
||
// KubeRegistryName add a prefix for the name | ||
func KubeRegistryName(name string) string { | ||
return RegistryPrefix + "-" + name | ||
} | ||
|
||
// RegistryName is registry secret name for PaddleCloud | ||
func RegistryName(name string) string { | ||
if strings.HasPrefix(name, RegistryPrefix) { | ||
return name[len(RegistryPrefix)+1 : len(name)] | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from paddle_job import PaddleJob | ||
__all__ = ["PaddleJob"] | ||
import registry | ||
__all__ = ["PaddleJob", "registry"] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What does the second argument
"PFS"
mean?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 second argument of
subcommands.Register
is group, if we add a subcommand with a specify a group name, these subcommands will be explained with a group name before, such as: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.
I see.