-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.go
81 lines (68 loc) · 1.89 KB
/
set.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
package cmd
import (
"os"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(setCmd)
}
var setCmd = &cobra.Command{
Use: "set",
Short: "Set a profile from the kube-lock config as the status for a context.",
PreRun: toggleDebug,
Run: func(cmd *cobra.Command, args []string) {
nativeCmd = true
err := setProfile(cmd, args)
if err != nil {
log.Fatal(err)
}
},
}
func setProfile(cmd *cobra.Command, args []string) error {
var kubeContext string
var err error
if context != "" {
kubeContext = context
} else {
kubeContext, err = findContext(args)
}
if err != nil {
return err
}
config, err := getViperConfig()
if err != nil {
return err
}
_, _, index, err := findContextInConfig(kubeContext, config)
if err != nil {
return err
}
ok, blockedVerbs, deleteExceptions := validateProfileInConfig(args[0], config)
if !ok {
log.Error("Profile '", args[0], "' not found. Please add it, or change Profile for context '", kubeContext, "'.")
os.Exit(1)
}
log.Info("Setting Status '", args[0], "' for context '", kubeContext, "'.")
setContextStatus(kubeContext, index, args[0], config)
blockedVerbsOut := "'" + strings.Join(blockedVerbs, `','`) + `'`
log.Info("\nProfile Rules:")
log.Info("Blocked Verbs: ", blockedVerbsOut)
log.Info("Delete Exceptions: ", deleteExceptions)
return nil
}
func validateProfileInConfig(profile string, config KubeLockConfig) (bool, []string, []KubeLockDeleteExceptions) {
log.Debug("Validating that Profile '", profile, "' exists in kube-lock config.")
var blockedVerbs []string
var deleteExceptions []KubeLockDeleteExceptions
var ok bool
for i, profiles := range config.Profiles {
if profiles.Name == profile {
blockedVerbs = config.Profiles[i].BlockedVerbs
deleteExceptions = config.Profiles[i].DeleteExceptions
ok = true
}
}
return ok, blockedVerbs, deleteExceptions
}