-
Notifications
You must be signed in to change notification settings - Fork 15
/
resource_set.go
85 lines (72 loc) · 1.49 KB
/
resource_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
82
83
84
85
package cmd
import (
"bufio"
"context"
"io"
"os"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
k8sYaml "k8s.io/apimachinery/pkg/util/yaml"
)
func updateResource(ctx context.Context, data []byte) error {
key, parsed, err := cke.ParseResource(data)
if err != nil {
return err
}
return storage.SetResource(ctx, key, string(parsed))
}
var resourceSetCmd = &cobra.Command{
Use: "set FILE",
Short: "register user-defined resources.",
Long: `Register user-defined resources.
FILE should contain multiple Kubernetes resources in YAML or JSON format.
If FILE is "-", then data is read from stdin.
Only the following resource types can be used:
* Namespace
* ServiceAccount
* PodSecurityPolicy
* NetworkPolicy
* Role
* RoleBinding
* ClusterRole
* ClusterRoleBinding
* ConfigMap
* Deployment
* DaemonSet
* CronJob
* Service`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
r := os.Stdin
if args[0] != "-" {
f, err := os.Open(args[0])
if err != nil {
return err
}
defer f.Close()
r = f
}
well.Go(func(ctx context.Context) error {
y := k8sYaml.NewYAMLReader(bufio.NewReader(r))
for {
data, err := y.Read()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
err = updateResource(ctx, data)
if err != nil {
return err
}
}
})
well.Stop()
return well.Wait()
},
}
func init() {
resourceCmd.AddCommand(resourceSetCmd)
}