forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_bootstrappolicy_file.go
133 lines (108 loc) · 3.55 KB
/
create_bootstrappolicy_file.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package admin
import (
"bytes"
"errors"
"io"
"io/ioutil"
"os"
"path"
"github.com/spf13/cobra"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/kubernetes/pkg/api/legacyscheme"
kapi "k8s.io/kubernetes/pkg/apis/core"
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
kprinters "k8s.io/kubernetes/pkg/printers"
"github.com/openshift/origin/pkg/api/latest"
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
)
const (
DefaultPolicyFile = "openshift.local.config/master/policy.json"
CreateBootstrapPolicyFileCommand = "create-bootstrap-policy-file"
CreateBootstrapPolicyFileFullCommand = "oc adm " + CreateBootstrapPolicyFileCommand
)
type CreateBootstrapPolicyFileOptions struct {
File string
}
func NewCommandCreateBootstrapPolicyFile(commandName string, fullName string, out io.Writer) *cobra.Command {
options := &CreateBootstrapPolicyFileOptions{}
cmd := &cobra.Command{
Use: commandName,
Short: "Create the default bootstrap policy",
Run: func(cmd *cobra.Command, args []string) {
if err := options.Validate(args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageErrorf(cmd, err.Error()))
}
if err := options.CreateBootstrapPolicyFile(); err != nil {
kcmdutil.CheckErr(err)
}
},
}
flags := cmd.Flags()
flags.StringVar(&options.File, "filename", DefaultPolicyFile, "The policy template file that will be written with roles and bindings.")
// autocompletion hints
cmd.MarkFlagFilename("filename")
return cmd
}
func (o CreateBootstrapPolicyFileOptions) Validate(args []string) error {
if len(args) != 0 {
return errors.New("no arguments are supported")
}
if len(o.File) == 0 {
return errors.New("filename must be provided")
}
return nil
}
func (o CreateBootstrapPolicyFileOptions) CreateBootstrapPolicyFile() error {
if err := os.MkdirAll(path.Dir(o.File), os.FileMode(0755)); err != nil {
return err
}
policyList := &kapi.List{}
policy := bootstrappolicy.Policy()
for i := range policy.ClusterRoles {
versionedObject, err := legacyscheme.Scheme.ConvertToVersion(&policy.ClusterRoles[i], rbacv1.SchemeGroupVersion)
if err != nil {
return err
}
policyList.Items = append(policyList.Items, versionedObject)
}
for i := range policy.ClusterRoleBindings {
versionedObject, err := legacyscheme.Scheme.ConvertToVersion(&policy.ClusterRoleBindings[i], rbacv1.SchemeGroupVersion)
if err != nil {
return err
}
policyList.Items = append(policyList.Items, versionedObject)
}
// iterate in a defined order
for _, namespace := range sets.StringKeySet(policy.Roles).List() {
roles := policy.Roles[namespace]
for i := range roles {
versionedObject, err := legacyscheme.Scheme.ConvertToVersion(&roles[i], rbacv1.SchemeGroupVersion)
if err != nil {
return err
}
policyList.Items = append(policyList.Items, versionedObject)
}
}
// iterate in a defined order
for _, namespace := range sets.StringKeySet(policy.RoleBindings).List() {
roleBindings := policy.RoleBindings[namespace]
for i := range roleBindings {
versionedObject, err := legacyscheme.Scheme.ConvertToVersion(&roleBindings[i], rbacv1.SchemeGroupVersion)
if err != nil {
return err
}
policyList.Items = append(policyList.Items, versionedObject)
}
}
versionedPolicyList, err := legacyscheme.Scheme.ConvertToVersion(policyList, latest.Version)
if err != nil {
return err
}
buffer := &bytes.Buffer{}
(&kprinters.JSONPrinter{}).PrintObj(versionedPolicyList, buffer)
if err := ioutil.WriteFile(o.File, buffer.Bytes(), 0644); err != nil {
return err
}
return nil
}