This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmdreporter.go
95 lines (79 loc) · 3.31 KB
/
cmdreporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Copyright 2019 The Rook Authors. All rights reserved.
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.
*/
package util
import (
"fmt"
"github.com/rook/cassandra/pkg/daemon/util"
"github.com/rook/cassandra/cmd/rook/rook"
"github.com/spf13/cobra"
)
// CmdReporterCmd defines a top-level utility command which runs a given command and stores the
// results in a ConfigMap. Operators are advised to use operator/k8sutil.CmdReporter, which wraps
// this functionality neatly rather than calling this with a custom setup.
var CmdReporterCmd = &cobra.Command{
Use: "cmd-reporter",
Short: "Run a given command to completion, and store the result in a ConfigMap.",
Long: `Run a given command to completion, and store the Stdout, Stderr, and return code
results of the command in a ConfigMap. If the ConfigMap already exists, the
Stdout, Stderr, and return code data which may be present in the ConfigMap
will be overwritten.
If cmd-reporter succeeds in running the command to completion, no error is
reported, even if the command's return code is nonzero (failure). Run will
terminate if the command could not be run for any reason or if there was an
error storing the command results into the ConfigMap. An application label
is applied to the ConfigMap. Run will also terminate if the label already
exists and has a different application's name name; this may indicate that
it is not safe for cmd-reporter to edit the ConfigMap.`,
Args: cobra.NoArgs,
Run: runCmdReporter,
}
var (
// run sub-command
commandString string
configMapName string
namespace string
// copy-binaries sub-command
copyToDir string
)
func init() {
// cmd-reporter
CmdReporterCmd.Flags().StringVar(&commandString, "command", "",
"The command to run in JSON list syntax. e.g., '[\"command\", \"--flag\", \"value\", \"arg\"]'")
if err := CmdReporterCmd.MarkFlagRequired("command"); err != nil {
panic(err)
}
CmdReporterCmd.Flags().StringVar(&configMapName, "config-map-name", "",
"The name of the ConfigMap into which the result of the command will be stored.")
if err := CmdReporterCmd.MarkFlagRequired("config-map-name"); err != nil {
panic(err)
}
CmdReporterCmd.Flags().StringVar(&namespace, "namespace", "", "The namespace in which to create the ConfigMap.")
if err := CmdReporterCmd.MarkFlagRequired("namespace"); err != nil {
panic(err)
}
}
func runCmdReporter(cCmd *cobra.Command, cArgs []string) {
cmd, args, err := util.CmdReporterFlagArgumentToCommand(commandString)
if err != nil {
rook.TerminateFatal(fmt.Errorf("failed to parse '--command' argument [%s]. %+v", commandString, err))
}
context := rook.NewContext()
reporter, err := util.NewCmdReporter(context.Clientset, cmd, args, configMapName, namespace)
if err != nil {
rook.TerminateFatal(fmt.Errorf("cannot start command-reporter. %+v", err))
}
err = reporter.Run()
if err != nil {
rook.TerminateFatal(err)
}
}