Repository navigation
Expand file tree
/
Copy pathdiff.go
More file actions
81 lines (64 loc) · 2.22 KB
/
Copy pathdiff.go
File metadata and controls
81 lines (64 loc) · 2.22 KB
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
package cmd
import (
"fmt"
"strings"
"github.com/kubernetes-incubator/kube-aws/core/root"
"github.com/kubernetes-incubator/kube-aws/logger"
"github.com/spf13/cobra"
)
var (
cmdDiff = &cobra.Command{
Use: "diff",
Short: "Compare the current and the desired states of the cluster",
Long: ``,
RunE: runCmdDiff,
SilenceUsage: true,
}
diffOpts = struct {
awsDebug, prettyPrint, skipWait, export bool
context int
profile string
targets []string
}{}
)
type ExitError struct {
msg string
Code int
}
func (e *ExitError) Error() string {
return e.msg
}
func init() {
RootCmd.AddCommand(cmdDiff)
cmdDiff.Flags().BoolVar(&diffOpts.awsDebug, "aws-debug", false, "Log debug information from aws-sdk-go library")
cmdDiff.Flags().StringVar(&diffOpts.profile, "profile", "", "The AWS profile to use from credentials file")
cmdDiff.Flags().StringSliceVar(&diffOpts.targets, "targets", root.AllOperationTargetsAsStringSlice(), "Diff nothing but specified sub-stacks. Specify `all` or any combination of `etcd`, `control-plane`, and node pool names. Defaults to `all`")
cmdDiff.Flags().IntVarP(&diffOpts.context, "context", "C", -1, "output NUM lines of context around changes")
}
func runCmdDiff(c *cobra.Command, _ []string) error {
opts := root.NewOptions(diffOpts.prettyPrint, diffOpts.skipWait, diffOpts.profile)
cluster, err := root.LoadClusterFromFile(configPath, opts, diffOpts.awsDebug)
if err != nil {
return fmt.Errorf("failed to read cluster config: %v", err)
}
targets := root.OperationTargetsFromStringSlice(diffOpts.targets)
if _, err := cluster.ValidateStack(targets); err != nil {
return err
}
diffs, err := cluster.Diff(targets, diffOpts.context)
if err != nil {
return fmt.Errorf("error comparing cluster states: %v", err)
}
for _, diff := range diffs {
logger.Infof("Detected changes in: %s\n%s", diff.Target, diff.String())
}
names := make([]string, len(diffs))
for i := range diffs {
names[i] = diffs[i].Target
}
if len(diffs) > 0 {
c.SilenceErrors = true
return &ExitError{fmt.Sprintf("Detected changes in: %s", strings.Join(names, ", ")), 2}
}
return nil
}