mirror of
https://github.com/minio/mc.git
synced 2025-11-10 13:42:32 +03:00
Implement mc ready to check if the cluster is ready or not (#4101)
Also adds option to check if the cluster has enough read quorum and if the cluster is taken down for any maintenance. Uses madmin.AnonymousClient for invoking the requests
This commit is contained in:
@@ -425,6 +425,7 @@ var completeCmds = map[string]complete.Predictor{
|
|||||||
"/support/status": aliasCompleter,
|
"/support/status": aliasCompleter,
|
||||||
|
|
||||||
"/update": nil,
|
"/update": nil,
|
||||||
|
"/ready": nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
// flagsToCompleteFlags transforms a cli.Flag to complete.Flags
|
// flagsToCompleteFlags transforms a cli.Flag to complete.Flags
|
||||||
|
|||||||
@@ -155,6 +155,77 @@ func newAdminClient(aliasedURL string) (*madmin.AdminClient, *probe.Error) {
|
|||||||
return s3Client, nil
|
return s3Client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newAnonymousClient(aliasedURL string) (*madmin.AnonymousClient, *probe.Error) {
|
||||||
|
_, urlStrFull, aliasCfg, err := expandAlias(aliasedURL)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err.Trace(aliasedURL)
|
||||||
|
}
|
||||||
|
// Verify if the aliasedURL is a real URL, fail in those cases
|
||||||
|
// indicating the user to add alias.
|
||||||
|
if aliasCfg == nil && urlRgx.MatchString(aliasedURL) {
|
||||||
|
return nil, errInvalidAliasedURL(aliasedURL).Trace(aliasedURL)
|
||||||
|
}
|
||||||
|
if aliasCfg == nil {
|
||||||
|
return nil, probe.NewError(fmt.Errorf("No valid configuration found for '%s' host alias", urlStrFull))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creates a parsed URL.
|
||||||
|
targetURL, e := url.Parse(urlStrFull)
|
||||||
|
if e != nil {
|
||||||
|
return nil, probe.NewError(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// By default enable HTTPs.
|
||||||
|
useTLS := true
|
||||||
|
if targetURL.Scheme == "http" {
|
||||||
|
useTLS = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct an anonymous client
|
||||||
|
anonClient, e := madmin.NewAnonymousClient(targetURL.Host, useTLS)
|
||||||
|
if e != nil {
|
||||||
|
return nil, probe.NewError(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep TLS config.
|
||||||
|
tlsConfig := &tls.Config{
|
||||||
|
RootCAs: globalRootCAs,
|
||||||
|
// Can't use SSLv3 because of POODLE and BEAST
|
||||||
|
// Can't use TLSv1.0 because of POODLE and BEAST using CBC cipher
|
||||||
|
// Can't use TLSv1.1 because of RC4 cipher usage
|
||||||
|
MinVersion: tls.VersionTLS12,
|
||||||
|
}
|
||||||
|
if globalInsecure {
|
||||||
|
tlsConfig.InsecureSkipVerify = true
|
||||||
|
}
|
||||||
|
// Set custom transport
|
||||||
|
var transport http.RoundTripper = &http.Transport{
|
||||||
|
Proxy: ieproxy.GetProxyFunc(),
|
||||||
|
DialContext: (&net.Dialer{
|
||||||
|
Timeout: 10 * time.Second,
|
||||||
|
KeepAlive: 15 * time.Second,
|
||||||
|
}).DialContext,
|
||||||
|
MaxIdleConnsPerHost: 256,
|
||||||
|
IdleConnTimeout: 90 * time.Second,
|
||||||
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
|
ExpectContinueTimeout: 10 * time.Second,
|
||||||
|
TLSClientConfig: tlsConfig,
|
||||||
|
// Set this value so that the underlying transport round-tripper
|
||||||
|
// doesn't try to auto decode the body of objects with
|
||||||
|
// content-encoding set to `gzip`.
|
||||||
|
//
|
||||||
|
// Refer:
|
||||||
|
// https://golang.org/src/net/http/transport.go?h=roundTrip#L1843
|
||||||
|
DisableCompression: true,
|
||||||
|
}
|
||||||
|
if globalDebug {
|
||||||
|
transport = httptracer.GetNewTraceTransport(newTraceV4(), transport)
|
||||||
|
}
|
||||||
|
anonClient.SetCustomTransport(transport)
|
||||||
|
|
||||||
|
return anonClient, nil
|
||||||
|
}
|
||||||
|
|
||||||
// s3AdminNew returns an initialized minioAdmin structure. If debug is enabled,
|
// s3AdminNew returns an initialized minioAdmin structure. If debug is enabled,
|
||||||
// it also enables an internal trace transport.
|
// it also enables an internal trace transport.
|
||||||
var s3AdminNew = NewAdminFactory()
|
var s3AdminNew = NewAdminFactory()
|
||||||
|
|||||||
@@ -450,6 +450,7 @@ var appCmds = []cli.Command{
|
|||||||
adminCmd,
|
adminCmd,
|
||||||
configCmd,
|
configCmd,
|
||||||
updateCmd,
|
updateCmd,
|
||||||
|
readyCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
func printMCVersion(c *cli.Context) {
|
func printMCVersion(c *cli.Context) {
|
||||||
|
|||||||
153
cmd/ready.go
Normal file
153
cmd/ready.go
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
// Copyright (c) 2015-2022 MinIO, Inc.
|
||||||
|
//
|
||||||
|
// This file is part of MinIO Object Storage stack
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"github.com/minio/cli"
|
||||||
|
json "github.com/minio/colorjson"
|
||||||
|
"github.com/minio/madmin-go"
|
||||||
|
"github.com/minio/mc/pkg/probe"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
healthCheckInterval = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
|
var readyFlags = []cli.Flag{
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "cluster-read",
|
||||||
|
Usage: "check if the cluster has enough read quorum",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "maintenance",
|
||||||
|
Usage: "check if the cluster is taken down for maintenance",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if the cluster is ready or not
|
||||||
|
var readyCmd = cli.Command{
|
||||||
|
Name: "ready",
|
||||||
|
Usage: "checks if the cluster is ready or not",
|
||||||
|
Action: mainReady,
|
||||||
|
OnUsageError: onUsageError,
|
||||||
|
Before: setGlobalsFromContext,
|
||||||
|
Flags: append(readyFlags, globalFlags...),
|
||||||
|
CustomHelpTemplate: `NAME:
|
||||||
|
{{.HelpName}} - {{.Usage}}
|
||||||
|
|
||||||
|
USAGE:
|
||||||
|
{{.HelpName}} [FLAGS] TARGET
|
||||||
|
{{if .VisibleFlags}}
|
||||||
|
FLAGS:
|
||||||
|
{{range .VisibleFlags}}{{.}}
|
||||||
|
{{end}}{{end}}
|
||||||
|
EXAMPLES:
|
||||||
|
1. Check if the cluster is ready or not
|
||||||
|
{{.Prompt}} {{.HelpName}} myminio
|
||||||
|
|
||||||
|
2. Check if the cluster has enough read quorum
|
||||||
|
{{.Prompt}} {{.HelpName}} myminio --cluster-read
|
||||||
|
|
||||||
|
3. Check if the cluster is taken down for maintenance
|
||||||
|
{{.Prompt}} {{.HelpName}} myminio --maintenance
|
||||||
|
`,
|
||||||
|
}
|
||||||
|
|
||||||
|
type readyMessage struct {
|
||||||
|
Healthy bool `json:"healthy"`
|
||||||
|
MaintenanceMode bool `json:"maintenanceMode"`
|
||||||
|
WriteQuorum int `json:"writeQuorum"`
|
||||||
|
HealingDrives int `json:"healingDrives"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r readyMessage) String() string {
|
||||||
|
if r.Healthy {
|
||||||
|
return color.GreenString("The cluster is ready")
|
||||||
|
}
|
||||||
|
return color.RedString("The cluster is not ready")
|
||||||
|
}
|
||||||
|
|
||||||
|
// JSON jsonified ready result
|
||||||
|
func (r readyMessage) JSON() string {
|
||||||
|
jsonMessageBytes, e := json.MarshalIndent(r, "", " ")
|
||||||
|
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||||
|
return string(jsonMessageBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
// mainReady - main handler for mc ready command.
|
||||||
|
func mainReady(cliCtx *cli.Context) error {
|
||||||
|
if !cliCtx.Args().Present() {
|
||||||
|
exitCode := 1
|
||||||
|
cli.ShowCommandHelpAndExit(cliCtx, "ready", exitCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set command flags from context.
|
||||||
|
clusterRead := cliCtx.Bool("cluster-read")
|
||||||
|
maintenance := cliCtx.Bool("maintenance")
|
||||||
|
|
||||||
|
ctx, cancelClusterReady := context.WithCancel(globalContext)
|
||||||
|
defer cancelClusterReady()
|
||||||
|
aliasedURL := cliCtx.Args().Get(0)
|
||||||
|
|
||||||
|
anonClient, err := newAnonymousClient(aliasedURL)
|
||||||
|
fatalIf(err.Trace(aliasedURL), "Couldn't construct anonymous client for `"+aliasedURL+"`.")
|
||||||
|
|
||||||
|
healthOpts := madmin.HealthOpts{
|
||||||
|
ClusterRead: clusterRead,
|
||||||
|
Maintenance: maintenance,
|
||||||
|
}
|
||||||
|
|
||||||
|
healthResult, hErr := anonClient.Healthy(ctx, healthOpts)
|
||||||
|
fatalIf(probe.NewError(hErr).Trace(aliasedURL), "Couldn't get the health status for `"+aliasedURL+"`.")
|
||||||
|
|
||||||
|
if healthResult.Healthy {
|
||||||
|
printMsg(readyMessage{
|
||||||
|
Healthy: healthResult.Healthy,
|
||||||
|
MaintenanceMode: healthResult.MaintenanceMode,
|
||||||
|
WriteQuorum: healthResult.WriteQuorum,
|
||||||
|
HealingDrives: healthResult.HealingDrives,
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
timer := time.NewTimer(healthCheckInterval)
|
||||||
|
defer timer.Stop()
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil
|
||||||
|
case <-timer.C:
|
||||||
|
timer.Reset(healthCheckInterval)
|
||||||
|
healthResult, hErr := anonClient.Healthy(ctx, healthOpts)
|
||||||
|
fatalIf(probe.NewError(hErr).Trace(aliasedURL), "Couldn't get the health status for `"+aliasedURL+"`.")
|
||||||
|
printMsg(readyMessage{
|
||||||
|
Healthy: healthResult.Healthy,
|
||||||
|
MaintenanceMode: healthResult.MaintenanceMode,
|
||||||
|
WriteQuorum: healthResult.WriteQuorum,
|
||||||
|
HealingDrives: healthResult.HealingDrives,
|
||||||
|
})
|
||||||
|
if healthResult.Healthy {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -151,8 +151,8 @@ func NewS3Config(urlStr string, aliasCfg *aliasConfigV10) *Config {
|
|||||||
s3Config.SecretKey = aliasCfg.SecretKey
|
s3Config.SecretKey = aliasCfg.SecretKey
|
||||||
s3Config.SessionToken = aliasCfg.SessionToken
|
s3Config.SessionToken = aliasCfg.SessionToken
|
||||||
s3Config.Signature = aliasCfg.API
|
s3Config.Signature = aliasCfg.API
|
||||||
|
s3Config.Lookup = getLookupType(aliasCfg.Path)
|
||||||
}
|
}
|
||||||
s3Config.Lookup = getLookupType(aliasCfg.Path)
|
|
||||||
return s3Config
|
return s3Config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -19,7 +19,7 @@ require (
|
|||||||
github.com/minio/cli v1.22.0
|
github.com/minio/cli v1.22.0
|
||||||
github.com/minio/colorjson v1.0.2
|
github.com/minio/colorjson v1.0.2
|
||||||
github.com/minio/filepath v1.0.0
|
github.com/minio/filepath v1.0.0
|
||||||
github.com/minio/madmin-go v1.3.19
|
github.com/minio/madmin-go v1.3.20
|
||||||
github.com/minio/md5-simd v1.1.2 // indirect
|
github.com/minio/md5-simd v1.1.2 // indirect
|
||||||
github.com/minio/minio-go/v7 v7.0.30
|
github.com/minio/minio-go/v7 v7.0.30
|
||||||
github.com/minio/pkg v1.1.22
|
github.com/minio/pkg v1.1.22
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -342,6 +342,8 @@ github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEX
|
|||||||
github.com/minio/madmin-go v1.3.5/go.mod h1:vGKGboQgGIWx4DuDUaXixjlIEZOCIp6ivJkQoiVaACc=
|
github.com/minio/madmin-go v1.3.5/go.mod h1:vGKGboQgGIWx4DuDUaXixjlIEZOCIp6ivJkQoiVaACc=
|
||||||
github.com/minio/madmin-go v1.3.19 h1:X/L4MTnDoR1VG1wwkeaQOBIQQohMTp5k8mjKNBbxGkE=
|
github.com/minio/madmin-go v1.3.19 h1:X/L4MTnDoR1VG1wwkeaQOBIQQohMTp5k8mjKNBbxGkE=
|
||||||
github.com/minio/madmin-go v1.3.19/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98=
|
github.com/minio/madmin-go v1.3.19/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98=
|
||||||
|
github.com/minio/madmin-go v1.3.20 h1:XHlIdewxAEEd/poI43CPiSmHGgJplkbFKFXZ1vZ6zMY=
|
||||||
|
github.com/minio/madmin-go v1.3.20/go.mod h1:ez87VmMtsxP7DRxjKJKD4RDNW+nhO2QF9KSzwxBDQ98=
|
||||||
github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw=
|
github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw=
|
||||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||||
|
|||||||
Reference in New Issue
Block a user