-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquotas.go
211 lines (175 loc) · 6.99 KB
/
quotas.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package cmd
import (
"context"
"strconv"
"time"
kubeflowv1 "github.com/StatCan/profiles-controller/pkg/apis/kubeflow/v1"
"github.com/StatCan/profiles-controller/pkg/controllers/profiles"
kubeflowclientset "github.com/StatCan/profiles-controller/pkg/generated/clientset/versioned"
kubeflowinformers "github.com/StatCan/profiles-controller/pkg/generated/informers/externalversions"
"github.com/StatCan/profiles-controller/pkg/signals"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
)
var quotaPrefixLabel = "quotas.statcan.gc.ca/"
var defaultResources = corev1.ResourceList{
"requests.cpu": *resource.NewQuantity(70, resource.DecimalSI),
"limits.cpu": *resource.NewQuantity(70, resource.DecimalSI),
// Memory
"requests.memory": *resource.NewScaledQuantity(368, resource.Giga),
"limits.memory": *resource.NewScaledQuantity(368, resource.Giga),
// Storage
"requests.storage": *resource.NewScaledQuantity(100, resource.Tera),
// GPU
"requests.nvidia.com/gpu": *resource.NewQuantity(2, resource.DecimalSI),
// Pods
"pods": *resource.NewQuantity(100, resource.DecimalSI),
// Services
"services.nodeports": *resource.NewQuantity(0, resource.DecimalSI),
"services.loadbalancers": *resource.NewQuantity(0, resource.DecimalSI),
}
// Override the default resources from profile labels
func overrideResourceQuotas(profile *kubeflowv1.Profile) corev1.ResourceList {
overrides := map[corev1.ResourceName]resource.Quantity{}
// copy in the defaults
for k, v := range defaultResources {
overrides[k] = v
}
// Special case, quotas.statcan.gc.ca/gpu -> requests.nvidia.com/gpu
if val, ok := profile.Labels[quotaPrefixLabel+"gpu"]; ok {
numgpu, _ := strconv.Atoi(val)
overrides["requests.nvidia.com/gpu"] = *resource.NewQuantity(int64(numgpu), resource.DecimalSI)
klog.Infof("Overriding resource quota from label profile: %s, requests.nvidia.com/gpu: %d", profile.Name, int64(numgpu))
}
// Loop over again, searching profile labels for
// quotas.statcan.gc.ca/{key}
// We will not clobber requests.nvidia.com/gpu because
// quotas.statcan.gc.ca/requests.nvidia.com/gpu is not a valid label.
for key := range defaultResources {
if overrideValue, ok := profile.Labels[quotaPrefixLabel+key.String()]; ok {
overrides[key] = resource.MustParse(overrideValue)
klog.Infof("Overriding resource quota from label profile %s, %s: %d", profile.Name, key.String(), overrides[key])
}
}
return overrides
}
var quotasCmd = &cobra.Command{
Use: "quotas",
Short: "Configure quotas resources",
Long: `Configure quota resources for Kubeflow profiles.
`,
Run: func(cmd *cobra.Command, args []string) {
// Setup signals so we can shutdown cleanly
stopCh := signals.SetupSignalHandler()
// Create Kubernetes config
cfg, err := clientcmd.BuildConfigFromFlags(apiserver, kubeconfig)
if err != nil {
klog.Fatalf("error building kubeconfig: %v", err)
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building kubernetes clientset: %s", err.Error())
}
kubeflowClient, err := kubeflowclientset.NewForConfig(cfg)
if err != nil {
klog.Fatalf("error building Kubeflow client: %v", err)
}
// Setup informers
kubeInformerFactory := kubeinformers.NewSharedInformerFactory(kubeClient, time.Minute*(time.Duration(requeue_time)))
kubeflowInformerFactory := kubeflowinformers.NewSharedInformerFactory(kubeflowClient, time.Minute*(time.Duration(requeue_time)))
resourceQuotaInformer := kubeInformerFactory.Core().V1().ResourceQuotas()
resourceQuotaLister := resourceQuotaInformer.Lister()
// Setup controller
controller := profiles.NewController(
kubeflowInformerFactory.Kubeflow().V1().Profiles(),
func(profile *kubeflowv1.Profile) error {
// Generate quotas
resourceQuotas := generateResourceQuotas(profile)
// Delete resources no longer needed
for _, resourceQuotaName := range []string{} {
_, err := resourceQuotaLister.ResourceQuotas(profile.Name).Get(resourceQuotaName)
if err == nil {
klog.Infof("removing resource quota %s/%s", profile.Name, resourceQuotaName)
err = kubeClient.CoreV1().ResourceQuotas(profile.Name).Delete(context.Background(), resourceQuotaName, metav1.DeleteOptions{})
if err != nil {
return err
}
}
}
// Create
for _, resourceQuota := range resourceQuotas {
currentResourceQuota, err := resourceQuotaLister.ResourceQuotas(resourceQuota.Namespace).Get(resourceQuota.Name)
if errors.IsNotFound(err) {
klog.Infof("creating resource quota %s/%s", resourceQuota.Namespace, resourceQuota.Name)
currentResourceQuota, err = kubeClient.CoreV1().ResourceQuotas(resourceQuota.Namespace).Create(context.Background(), resourceQuota, metav1.CreateOptions{})
if err != nil {
return err
}
}
if !equality.Semantic.DeepDerivative(resourceQuota.Spec, currentResourceQuota.Spec) {
klog.Infof("updating resource quota %s/%s", resourceQuota.Namespace, resourceQuota.Name)
currentResourceQuota.Spec = resourceQuota.Spec
_, err = kubeClient.CoreV1().ResourceQuotas(resourceQuota.Namespace).Update(context.Background(), currentResourceQuota, metav1.UpdateOptions{})
if err != nil {
return err
}
}
}
return nil
},
)
resourceQuotaInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(old, new interface{}) {
newNP := new.(*corev1.ResourceQuota)
oldNP := old.(*corev1.ResourceQuota)
if newNP.ResourceVersion == oldNP.ResourceVersion {
return
}
controller.HandleObject(new)
},
DeleteFunc: controller.HandleObject,
})
// Start informers
kubeInformerFactory.Start(stopCh)
kubeflowInformerFactory.Start(stopCh)
// Wait for caches
klog.Info("Waiting for informer caches to sync")
if ok := cache.WaitForCacheSync(stopCh, resourceQuotaInformer.Informer().GetController().HasSynced); !ok {
klog.Fatalf("failed to wait for caches to sync")
}
// Run the controller
if err = controller.Run(2, stopCh); err != nil {
klog.Fatalf("error running controller: %v", err)
}
},
}
// generateResourceQuotas generates resource quotas for the given profile.
func generateResourceQuotas(profile *kubeflowv1.Profile) []*corev1.ResourceQuota {
overrideResourceQuotas(profile)
quotas := []*corev1.ResourceQuota{}
quotas = append(quotas, &corev1.ResourceQuota{
ObjectMeta: metav1.ObjectMeta{
Name: "quotas",
Namespace: profile.Name,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(profile, kubeflowv1.SchemeGroupVersion.WithKind("Profile")),
},
},
Spec: corev1.ResourceQuotaSpec{
Hard: overrideResourceQuotas(profile),
},
})
return quotas
}
func init() {
rootCmd.AddCommand(quotasCmd)
}