-
Notifications
You must be signed in to change notification settings - Fork 499
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
stability: add fault-trigger client #326
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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,194 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/pingcap/tidb-operator/tests/pkg/fault-trigger/api" | ||
"github.com/pingcap/tidb-operator/tests/pkg/fault-trigger/manager" | ||
"github.com/pingcap/tidb-operator/tests/pkg/util" | ||
) | ||
|
||
// Client is a fault-trigger client | ||
type Client interface { | ||
// ListVMs lists all virtual machines | ||
ListVMs() ([]*manager.VM, error) | ||
// StartVM start a specified virtual machine | ||
StartVM(vm *manager.VM) error | ||
// StopVM stops a specified virtual machine | ||
StopVM(vm *manager.VM) error | ||
// StartETCD starts the etcd service | ||
StartETCD() error | ||
// StopETCD stops the etcd service | ||
StopETCD() error | ||
// StartKubelet starts the kubelet service | ||
StartKubelet() error | ||
// StopKubelet stops the kubelet service | ||
StopKubelet() error | ||
} | ||
|
||
// client is used to communicate with the fault-trigger | ||
type client struct { | ||
cfg Config | ||
httpCli *http.Client | ||
} | ||
|
||
// Config defines for fault-trigger client | ||
type Config struct { | ||
Addr string | ||
} | ||
|
||
// NewClient creates a new fault-trigger client from a given address | ||
func NewClient(cfg Config) Client { | ||
return &client{ | ||
cfg: cfg, | ||
httpCli: http.DefaultClient, | ||
} | ||
} | ||
|
||
type clientError struct { | ||
code int | ||
msg string | ||
} | ||
|
||
func (e *clientError) Error() string { | ||
return fmt.Sprintf("%s (code: %d)", e.msg, e.code) | ||
} | ||
|
||
func (c client) do(req *http.Request) (*http.Response, []byte, error) { | ||
resp, err := c.httpCli.Do(req) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
code := resp.StatusCode | ||
|
||
if code != http.StatusOK { | ||
return resp, nil, &clientError{ | ||
code: code, | ||
msg: "fail to request to http service", | ||
} | ||
} | ||
|
||
bodyByte, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return resp, nil, &clientError{ | ||
code: code, | ||
msg: fmt.Sprintf("failed to read data from resp body, error: %v", err), | ||
} | ||
} | ||
|
||
data, err := api.ExtractResponse(bodyByte) | ||
if err != nil { | ||
return resp, nil, &clientError{ | ||
code: code, | ||
msg: err.Error(), | ||
} | ||
} | ||
|
||
return resp, data, err | ||
} | ||
|
||
func (c client) get(url string) ([]byte, error) { | ||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating request: %v", err) | ||
} | ||
|
||
_, body, err := c.do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
func (c *client) ListVMs() ([]*manager.VM, error) { | ||
url := util.GenURL(fmt.Sprintf("%s%s/vms", c.cfg.Addr, api.APIPrefix)) | ||
data, err := c.get(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var vms []*manager.VM | ||
if err = json.Unmarshal(data, &vms); err != nil { | ||
return nil, err | ||
} | ||
|
||
return vms, nil | ||
} | ||
|
||
func (c *client) StartVM(vm *manager.VM) error { | ||
if err := vm.Verify(); err != nil { | ||
return err | ||
} | ||
|
||
vmName := vm.Name | ||
if len(vmName) == 0 { | ||
vmName = vm.IP | ||
} | ||
|
||
url := util.GenURL(fmt.Sprintf("%s/%s/vm/%s/start", c.cfg.Addr, api.APIPrefix, vmName)) | ||
if _, err := c.get(url); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) StopVM(vm *manager.VM) error { | ||
if err := vm.Verify(); err != nil { | ||
return err | ||
} | ||
|
||
vmName := vm.Name | ||
if len(vmName) == 0 { | ||
vmName = vm.IP | ||
} | ||
|
||
url := util.GenURL(fmt.Sprintf("%s/%s/vm/%s/stop", c.cfg.Addr, api.APIPrefix, vmName)) | ||
if _, err := c.get(url); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) StartETCD() error { | ||
url := util.GenURL(fmt.Sprintf("%s/%s/etcd/start", c.cfg.Addr, api.APIPrefix)) | ||
if _, err := c.get(url); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) StopETCD() error { | ||
url := util.GenURL(fmt.Sprintf("%s/%s/etcd/stop", c.cfg.Addr, api.APIPrefix)) | ||
if _, err := c.get(url); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) StartKubelet() error { | ||
url := util.GenURL(fmt.Sprintf("%s/%s/kubelet/start", c.cfg.Addr, api.APIPrefix)) | ||
if _, err := c.get(url); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *client) StopKubelet() error { | ||
url := util.GenURL(fmt.Sprintf("%s/%s/kubelet/stop", c.cfg.Addr, api.APIPrefix)) | ||
if _, err := c.get(url); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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.
Do all operations use the HTTP
Get
method?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 the operation changes the state, I will replace the
Get
withPost
in a sigle pr.