-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/tool: support export cluster targets for monitor
Signed-off-by: wsehjk <[email protected]>
- Loading branch information
1 parent
3310650
commit 88a6091
Showing
8 changed files
with
323 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (c) 2022 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Project: CurveCli | ||
* Created Date: 2023-12-26 | ||
* Author: wsehjk | ||
*/ | ||
package cobrautil | ||
const ( | ||
LabelKey = "label" | ||
ClientTarget = "client" | ||
EtcdTarget = "etcd" | ||
MdsTarget = "mds" | ||
ChunkServerTarget = "chunkserver" | ||
SnapshotTarget = "snapshot" | ||
SnapshotServerTarget = "snapshotcloneserver" | ||
TargetKey = "targets" | ||
JobKey = "job" | ||
OutputFile = "target.json" | ||
TargetPathFlag = "path" | ||
) |
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,48 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* | ||
* Project: CurveCli | ||
* Created Date: 2023-12-11 | ||
* Author: wsehjk | ||
*/ | ||
|
||
package export | ||
|
||
import ( | ||
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" | ||
target "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/export/target" | ||
"github.com/spf13/cobra" | ||
) | ||
type ExportCommand struct { | ||
basecmd.MidCurveCmd | ||
} | ||
|
||
var _ basecmd.MidCurveCmdFunc = (*ExportCommand)(nil) // check interface | ||
|
||
func (eCmd *ExportCommand) AddSubCommands() { | ||
eCmd.Cmd.AddCommand( | ||
target.NewTargetCommand(), | ||
) | ||
} | ||
func NewExportCommand() *cobra.Command{ | ||
eCmd := &ExportCommand { | ||
basecmd.MidCurveCmd{ | ||
Use: "export", | ||
Short: "export the target in curvebs", | ||
}, | ||
} | ||
return basecmd.NewMidCurveCli(&eCmd.MidCurveCmd, eCmd) | ||
} |
137 changes: 137 additions & 0 deletions
137
tools-v2/pkg/cli/command/curvebs/export/target/target.go
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,137 @@ | ||
/* | ||
* Copyright (c) 2023 NetEase Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* | ||
* Project: curve | ||
* Created Date: 2023-12-11 | ||
* Author: wsehjk | ||
*/ | ||
|
||
package target | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
cmderror "github.com/opencurve/curve/tools-v2/internal/error" | ||
cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" | ||
basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" | ||
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/client" | ||
"github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/status/cluster" | ||
"github.com/opencurve/curve/tools-v2/pkg/output" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
targetExample = "$curve bs export target" | ||
) | ||
type ResultStruct map[string]interface{} | ||
type targetcommand struct { | ||
basecmd.FinalCurveCmd | ||
} | ||
// check interface targetcommand implement FinalCurveCmdFunc | ||
var _ basecmd.FinalCurveCmdFunc = (*targetcommand)(nil) | ||
|
||
func ExtractClusterResult(clusterResult map[string]interface{}, service string) ResultStruct{ | ||
result := ResultStruct{} | ||
arrs := clusterResult[service].([]map[string]string) | ||
result[cobrautil.LabelKey] = map[string]string{ | ||
cobrautil.JobKey : service, | ||
} | ||
if service == cobrautil.SnapshotTarget { | ||
result[cobrautil.LabelKey] = map[string]string{ | ||
cobrautil.JobKey : cobrautil.SnapshotServerTarget, | ||
} | ||
} | ||
result[cobrautil.TargetKey] = make([]string, 0) | ||
for _, arr := range arrs { | ||
if addr, ok := arr["addr"]; ok { // see the output of curve bs status cluster | ||
result[cobrautil.TargetKey] = append(result[cobrautil.TargetKey].([]string), addr) | ||
} | ||
if addr, ok := arr["internalAddr"]; ok { | ||
result[cobrautil.TargetKey] = append(result[cobrautil.TargetKey].([]string), addr) | ||
} | ||
} | ||
return result | ||
} | ||
func NewTargetCommand() (cmd *cobra.Command) { | ||
tCmd := &targetcommand{ | ||
FinalCurveCmd: basecmd.FinalCurveCmd { | ||
Use: "target", | ||
Short: "export all the target in cluster and used by monitor", | ||
Example: targetExample, | ||
}, | ||
} | ||
return basecmd.NewFinalCurveCli(&tCmd.FinalCurveCmd, tCmd) | ||
} | ||
|
||
func (tCmd *targetcommand)Init(cmd *cobra.Command, args []string) error { | ||
return nil | ||
} | ||
func (tCmd *targetcommand)RunCommand(cmd *cobra.Command, args []string) error { | ||
// get cluster target | ||
clusterResult, err := cluster.GetClusterStatus() | ||
if err != nil { | ||
retErr := cmderror.ErrTargetCluster() | ||
retErr.Format(err.Error()) | ||
return retErr.ToError() | ||
} | ||
tCmd.Result = []ResultStruct{} | ||
tCmd.Result = append(tCmd.Result.([]ResultStruct), ExtractClusterResult(clusterResult.(map[string]interface{}), cobrautil.EtcdTarget)) | ||
tCmd.Result = append(tCmd.Result.([]ResultStruct), ExtractClusterResult(clusterResult.(map[string]interface{}), cobrautil.MdsTarget)) | ||
tCmd.Result = append(tCmd.Result.([]ResultStruct), ExtractClusterResult(clusterResult.(map[string]interface{}), cobrautil.ChunkServerTarget)) | ||
tCmd.Result = append(tCmd.Result.([]ResultStruct), ExtractClusterResult(clusterResult.(map[string]interface{}), cobrautil.SnapshotTarget)) | ||
|
||
// get client target | ||
clientResult, err := client.GetClientStatus() | ||
if err != nil { | ||
retErr := cmderror.ErrTargetClient() | ||
retErr.Format(err.Error()) | ||
return retErr.ToError() | ||
} | ||
clientStruct := ResultStruct{} | ||
clientStruct[cobrautil.LabelKey] = map[string]string{ | ||
cobrautil.JobKey : cobrautil.ClientTarget, | ||
} | ||
clientStruct[cobrautil.TargetKey] = make([]string, 0) | ||
for _, result := range clientResult.([]map[string]string) { | ||
clientStruct[cobrautil.TargetKey] = append(clientStruct[cobrautil.TargetKey].([]string), result["addr"]) | ||
} | ||
tCmd.Result = append(tCmd.Result.([]ResultStruct), clientStruct) | ||
return nil | ||
} | ||
|
||
func (tCmd *targetcommand)Print(cmd *cobra.Command, args []string) error { | ||
output, err := json.MarshalIndent(tCmd.Result, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
path := tCmd.Cmd.Flag(cobrautil.TargetPathFlag).Value.String() | ||
if err = os.WriteFile(path + "/" + cobrautil.OutputFile, output, 0666); err != nil { | ||
return err | ||
} | ||
fmt.Println(string(output)) | ||
return nil | ||
} | ||
// result in plain format string | ||
func (tCmd *targetcommand)ResultPlainOutput() error { | ||
return output.FinalCmdOutputPlain(&tCmd.FinalCurveCmd) | ||
} | ||
|
||
// target final command has no flag | ||
func (tCmd *targetcommand)AddFlags() { | ||
tCmd.Cmd.Flags().String(cobrautil.TargetPathFlag, ".", "specify the path of target.json") | ||
} |
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