|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + |
| 9 | + "github.com/jessevdk/go-flags" |
| 10 | +) |
| 11 | + |
| 12 | +type targetK8sConfigmap struct { |
| 13 | + k8sConfigmapOptions |
| 14 | + |
| 15 | + Helm *helmOptions `group:"Flags for configuring the Helm annotations (use when --target=k8s)" namespace:"helm"` |
| 16 | +} |
| 17 | + |
| 18 | +type storeConfigmapCommand struct { |
| 19 | + Batch bool `long:"batch" description:"Instead of reading one configmap from stdin, read all files of the argument list and store them as entries in the configmap"` |
| 20 | + Overwrite bool `long:"overwrite" description:"Overwrite existing configmap entries instead of aborting"` |
| 21 | + Target string `long:"target" short:"t" description:"Configmap storage target" choice:"k8s"` |
| 22 | + K8s *targetK8sConfigmap `group:"Flags for storing the key/value pair inside a Kubernetes Configmap (use when --target=k8s)" namespace:"k8s"` |
| 23 | +} |
| 24 | + |
| 25 | +func newStoreConfigmapCommand() *storeConfigmapCommand { |
| 26 | + return &storeConfigmapCommand{ |
| 27 | + Target: storageK8s, |
| 28 | + K8s: &targetK8sConfigmap{ |
| 29 | + k8sConfigmapOptions: k8sConfigmapOptions{ |
| 30 | + Namespace: defaultK8sNamespace, |
| 31 | + }, |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (x *storeConfigmapCommand) Register(parser *flags.Parser) error { |
| 37 | + _, err := parser.AddCommand( |
| 38 | + "store-configmap", |
| 39 | + "Write key/value pairs to a Kubernetes configmap", |
| 40 | + "Read a configmap from stdin and store it to the "+ |
| 41 | + "external configmaps storage indicated by the --target "+ |
| 42 | + "flag; if the --batch flag is used, instead of "+ |
| 43 | + "reading a single configmap entry from stdin, each command "+ |
| 44 | + "line argument is treated as a file and each file's "+ |
| 45 | + "content is added to the configmap with the file's name "+ |
| 46 | + "as the key name for the configmap entry", |
| 47 | + x, |
| 48 | + ) |
| 49 | + return err |
| 50 | +} |
| 51 | + |
| 52 | +func (x *storeConfigmapCommand) Execute(args []string) error { |
| 53 | + var entries []*entry |
| 54 | + |
| 55 | + switch { |
| 56 | + case x.Batch && len(args) == 0: |
| 57 | + return fmt.Errorf("at least one command line argument is " + |
| 58 | + "required when using --batch flag") |
| 59 | + |
| 60 | + case x.Batch: |
| 61 | + for _, file := range args { |
| 62 | + log("Reading value/entry from file %s", file) |
| 63 | + content, err := readFile(file) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("cannot read file %s: %v", |
| 66 | + file, err) |
| 67 | + } |
| 68 | + |
| 69 | + entries = append(entries, &entry{ |
| 70 | + key: filepath.Base(file), |
| 71 | + value: content, |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + default: |
| 76 | + log("Reading value/entry from stdin") |
| 77 | + value, err := io.ReadAll(os.Stdin) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("error reading entry from stdin: %v", err) |
| 80 | + } |
| 81 | + entries = append(entries, &entry{value: string(value)}) |
| 82 | + } |
| 83 | + |
| 84 | + switch x.Target { |
| 85 | + case storageK8s: |
| 86 | + // Take the actual entry key from the options if we aren't in |
| 87 | + // batch mode. |
| 88 | + if len(entries) == 1 && entries[0].key == "" { |
| 89 | + entries[0].key = x.K8s.KeyName |
| 90 | + } |
| 91 | + |
| 92 | + return storeConfigmapsK8s(entries, x.K8s, x.Overwrite) |
| 93 | + |
| 94 | + default: |
| 95 | + return fmt.Errorf("invalid configmap storage target %s", x.Target) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func storeConfigmapsK8s(entries []*entry, opts *targetK8sConfigmap, |
| 100 | + overwrite bool) error { |
| 101 | + |
| 102 | + if opts.Name == "" { |
| 103 | + return fmt.Errorf("configmap name is required") |
| 104 | + } |
| 105 | + |
| 106 | + for _, entry := range entries { |
| 107 | + if entry.key == "" { |
| 108 | + return fmt.Errorf("configmap entry key name is required") |
| 109 | + } |
| 110 | + |
| 111 | + entryOpts := &k8sObjectOptions{ |
| 112 | + Namespace: opts.Namespace, |
| 113 | + Name: opts.Name, |
| 114 | + KeyName: entry.key, |
| 115 | + ObjectType: ObjectTypeConfigMap, |
| 116 | + } |
| 117 | + |
| 118 | + log("Storing key with name %s to configmap %s in namespace %s", |
| 119 | + entryOpts.KeyName, entryOpts.Name, |
| 120 | + entryOpts.Namespace) |
| 121 | + |
| 122 | + err := saveK8s(entry.value, entryOpts, overwrite, opts.Helm) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("error storing key %s in configmap %s: "+ |
| 125 | + "%v", entry.key, opts.Name, err) |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return nil |
| 130 | +} |
0 commit comments