forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterquota.go
196 lines (166 loc) · 5.66 KB
/
clusterquota.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package create
import (
"encoding/csv"
"fmt"
"io"
"strings"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
quotaapi "github.com/openshift/origin/pkg/quota/apis/quota"
quotaclient "github.com/openshift/origin/pkg/quota/generated/internalclientset/typed/quota/internalversion"
)
const ClusterQuotaRecommendedName = "clusterresourcequota"
var (
clusterQuotaLong = templates.LongDesc(`
Create a cluster resource quota that controls certain resources.
Cluster resource quota objects defined quota restrictions that span multiple projects based on label selectors.`)
clusterQuotaExample = templates.Examples(`
# Create a cluster resource quota limited to 10 pods
%[1]s limit-bob --project-annotation-selector=openshift.io/requester=user-bob --hard=pods=10`)
)
type CreateClusterQuotaOptions struct {
ClusterQuota *quotaapi.ClusterResourceQuota
Client quotaclient.ClusterResourceQuotasGetter
DryRun bool
Mapper meta.RESTMapper
OutputFormat string
Out io.Writer
Printer ObjectPrinter
}
// NewCmdCreateClusterQuota is a macro command to create a new cluster quota.
func NewCmdCreateClusterQuota(name, fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
o := &CreateClusterQuotaOptions{Out: out}
cmd := &cobra.Command{
Use: name + " NAME --project-label-selector=key=value [--hard=RESOURCE=QUANTITY]...",
Short: "Create cluster resource quota resource.",
Long: clusterQuotaLong,
Example: fmt.Sprintf(clusterQuotaExample, fullName),
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(o.Complete(cmd, f, args))
cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run())
},
Aliases: []string{"clusterquota"},
}
cmdutil.AddPrinterFlags(cmd)
cmdutil.AddDryRunFlag(cmd)
cmd.Flags().String("project-label-selector", "", "The project label selector for the cluster resource quota")
cmd.Flags().String("project-annotation-selector", "", "The project annotation selector for the cluster resource quota")
cmd.Flags().StringSlice("hard", []string{}, "The resource to constrain: RESOURCE=QUANTITY (pods=10)")
return cmd
}
func (o *CreateClusterQuotaOptions) Complete(cmd *cobra.Command, f *clientcmd.Factory, args []string) error {
if len(args) != 1 {
return fmt.Errorf("NAME is required: %v", args)
}
o.DryRun = cmdutil.GetFlagBool(cmd, "dry-run")
var labelSelector *metav1.LabelSelector
labelSelectorString := cmdutil.GetFlagString(cmd, "project-label-selector")
if len(labelSelectorString) > 0 {
var err error
labelSelector, err = metav1.ParseToLabelSelector(labelSelectorString)
if err != nil {
return err
}
}
annotationSelector, err := parseAnnotationSelector(cmdutil.GetFlagString(cmd, "project-annotation-selector"))
if err != nil {
return err
}
o.ClusterQuota = "aapi.ClusterResourceQuota{
ObjectMeta: metav1.ObjectMeta{Name: args[0]},
Spec: quotaapi.ClusterResourceQuotaSpec{
Selector: quotaapi.ClusterResourceQuotaSelector{
LabelSelector: labelSelector,
AnnotationSelector: annotationSelector,
},
Quota: kapi.ResourceQuotaSpec{
Hard: kapi.ResourceList{},
},
},
}
for _, resourceCount := range cmdutil.GetFlagStringSlice(cmd, "hard") {
tokens := strings.Split(resourceCount, "=")
if len(tokens) != 2 {
return fmt.Errorf("%v must in the form of resource=quantity", resourceCount)
}
quantity, err := resource.ParseQuantity(tokens[1])
if err != nil {
return err
}
o.ClusterQuota.Spec.Quota.Hard[kapi.ResourceName(tokens[0])] = quantity
}
quotaClient, err := f.OpenshiftInternalQuotaClient()
if err != nil {
return err
}
o.Client = quotaClient.Quota()
o.Mapper, _ = f.Object()
o.OutputFormat = cmdutil.GetFlagString(cmd, "output")
o.Printer = func(obj runtime.Object, out io.Writer) error {
return f.PrintObject(cmd, false, o.Mapper, obj, out)
}
return nil
}
func (o *CreateClusterQuotaOptions) Validate() error {
if o.ClusterQuota == nil {
return fmt.Errorf("ClusterQuota is required")
}
if o.Client == nil {
return fmt.Errorf("Client is required")
}
if o.Mapper == nil {
return fmt.Errorf("Mapper is required")
}
if o.Out == nil {
return fmt.Errorf("Out is required")
}
if o.Printer == nil {
return fmt.Errorf("Printer is required")
}
return nil
}
func (o *CreateClusterQuotaOptions) Run() error {
actualObj := o.ClusterQuota
var err error
if !o.DryRun {
actualObj, err = o.Client.ClusterResourceQuotas().Create(o.ClusterQuota)
if err != nil {
return err
}
}
if useShortOutput := o.OutputFormat == "name"; useShortOutput || len(o.OutputFormat) == 0 {
cmdutil.PrintSuccess(o.Mapper, useShortOutput, o.Out, "clusterquota", actualObj.Name, o.DryRun, "created")
return nil
}
return o.Printer(actualObj, o.Out)
}
// parseAnnotationSelector just parses key=value,key=value=...,
// further validation is left to be done server-side.
func parseAnnotationSelector(s string) (map[string]string, error) {
if len(s) == 0 {
return nil, nil
}
stringReader := strings.NewReader(s)
csvReader := csv.NewReader(stringReader)
annotations, err := csvReader.Read()
if err != nil {
return nil, err
}
parsed := map[string]string{}
for _, annotation := range annotations {
parts := strings.SplitN(annotation, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("Malformed annotation selector, expected %q: %s", "key=value", annotation)
}
parsed[parts[0]] = parts[1]
}
return parsed, nil
}