-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lndinit: add ability to write to k8s configmap
We'll use this to write connection relateed information for consumption by payment service proxy.
- Loading branch information
1 parent
97404fa
commit 1c05001
Showing
10 changed files
with
347 additions
and
82 deletions.
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
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,127 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/jessevdk/go-flags" | ||
) | ||
|
||
type storeConfigmapCommand struct { | ||
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"` | ||
Overwrite bool `long:"overwrite" description:"Overwrite existing configmap entries instead of aborting"` | ||
Target string `long:"target" short:"t" description:"Configmap storage target" choice:"k8s"` | ||
K8s *targetK8s `group:"Flags for storing the key/value pair inside a Kubernetes Configmap (use when --target=k8s)" namespace:"k8s"` | ||
} | ||
|
||
func newStoreConfigmapCommand() *storeConfigmapCommand { | ||
return &storeConfigmapCommand{ | ||
Target: storageK8s, | ||
K8s: &targetK8s{ | ||
k8sObjectOptions: k8sObjectOptions{ | ||
Namespace: defaultK8sNamespace, | ||
ObjectType: ObjectTypeConfigMap, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (x *storeConfigmapCommand) Register(parser *flags.Parser) error { | ||
_, err := parser.AddCommand( | ||
"store-configmap", | ||
"Write key/value pairs to a Kubernetes configmap", | ||
"Read a configmap from stdin and store it to the "+ | ||
"external configmaps storage indicated by the --target "+ | ||
"flag; if the --batch flag is used, instead of "+ | ||
"reading a single configmap entry from stdin, each command "+ | ||
"line argument is treated as a file and each file's "+ | ||
"content is added to the configmap with the file's name "+ | ||
"as the key name for the configmap entry", | ||
x, | ||
) | ||
return err | ||
} | ||
|
||
func (x *storeConfigmapCommand) Execute(args []string) error { | ||
var entries []*entry | ||
|
||
switch { | ||
case x.Batch && len(args) == 0: | ||
return fmt.Errorf("at least one command line argument is " + | ||
"required when using --batch flag") | ||
|
||
case x.Batch: | ||
for _, file := range args { | ||
log("Reading value/entry from file %s", file) | ||
content, err := readFile(file) | ||
if err != nil { | ||
return fmt.Errorf("cannot read file %s: %v", | ||
file, err) | ||
} | ||
|
||
entries = append(entries, &entry{ | ||
key: filepath.Base(file), | ||
value: content, | ||
}) | ||
} | ||
|
||
default: | ||
log("Reading value/entry from stdin") | ||
value, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
return fmt.Errorf("error reading value/entry from stdin: %v", err) | ||
} | ||
entries = append(entries, &entry{value: string(value)}) | ||
} | ||
|
||
switch x.Target { | ||
case storageK8s: | ||
// Take the actual entry key from the options if we aren't in | ||
// batch mode. | ||
if len(entries) == 1 && entries[0].key == "" { | ||
entries[0].key = x.K8s.KeyName | ||
} | ||
|
||
return storeConfigmapsK8s(entries, x.K8s, x.Overwrite) | ||
|
||
default: | ||
return fmt.Errorf("invalid configmap storage target %s", x.Target) | ||
} | ||
} | ||
|
||
func storeConfigmapsK8s(entries []*entry, opts *targetK8s, | ||
overwrite bool) error { | ||
|
||
if opts.Name == "" { | ||
return fmt.Errorf("configmap name is required") | ||
} | ||
|
||
for _, entry := range entries { | ||
if entry.key == "" { | ||
return fmt.Errorf("configmap entry key name is required") | ||
} | ||
|
||
entryOpts := &k8sObjectOptions{ | ||
Namespace: opts.Namespace, | ||
Name: opts.Name, | ||
KeyName: entry.key, | ||
ObjectType: ObjectTypeConfigMap, | ||
} | ||
|
||
log("Storing key with name %s to configmap %s in namespace %s", | ||
entryOpts.KeyName, entryOpts.Name, | ||
entryOpts.Namespace) | ||
|
||
// TODO(calvin): We need to make the Kubernetes write method capable | ||
// of handling multiple different types. | ||
err := saveK8s(entry.value, entryOpts, overwrite, opts.Helm) | ||
if err != nil { | ||
return fmt.Errorf("error storing key %s in configmap %s: "+ | ||
"%v", entry.key, opts.Name, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.