-
Notifications
You must be signed in to change notification settings - Fork 0
/
limitrange.go
168 lines (144 loc) · 5.36 KB
/
limitrange.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
package cmd
import (
"context"
"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 limitRangesCmd = &cobra.Command{
Use: "limitranges",
Short: "Configure Limit Ranges",
Long: `Configure Limit Ranges 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)))
LimitRangeInformer := kubeInformerFactory.Core().V1().LimitRanges()
LimitRangeLister := LimitRangeInformer.Lister()
// Setup controller
controller := profiles.NewController(
kubeflowInformerFactory.Kubeflow().V1().Profiles(),
func(profile *kubeflowv1.Profile) error {
// Generate limit ranges
LimitRanges := generateLimitRanges(profile)
// Delete s no longer needed
for _, LimitRangeName := range []string{} {
_, err := LimitRangeLister.LimitRanges(profile.Name).Get(LimitRangeName)
if err == nil {
klog.Infof("removing limit range %s/%s", profile.Name, LimitRangeName)
err = kubeClient.CoreV1().LimitRanges(profile.Name).Delete(context.Background(), LimitRangeName, metav1.DeleteOptions{})
if err != nil {
return err
}
}
}
// Create
for _, LimitRange := range LimitRanges {
currentLimitRange, err := LimitRangeLister.LimitRanges(LimitRange.Namespace).Get(LimitRange.Name)
if errors.IsNotFound(err) {
klog.Infof("creating limit range %s/%s", LimitRange.Namespace, LimitRange.Name)
currentLimitRange, err = kubeClient.CoreV1().LimitRanges(LimitRange.Namespace).Create(context.Background(), LimitRange, metav1.CreateOptions{})
if err != nil {
return err
}
}
if !equality.Semantic.DeepDerivative(LimitRange.Spec, currentLimitRange.Spec) {
klog.Infof("updating limit range %s/%s", LimitRange.Namespace, LimitRange.Name)
currentLimitRange.Spec = LimitRange.Spec
_, err = kubeClient.CoreV1().LimitRanges(LimitRange.Namespace).Update(context.Background(), currentLimitRange, metav1.UpdateOptions{})
if err != nil {
return err
}
}
}
return nil
},
)
LimitRangeInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: func(old, new interface{}) {
newNP := new.(*corev1.LimitRange)
oldNP := old.(*corev1.LimitRange)
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, LimitRangeInformer.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)
}
},
}
// generateLimitRanges generates limitranges for the given profile.
// TODO: Allow overrides in a namespace
func generateLimitRanges(profile *kubeflowv1.Profile) []*corev1.LimitRange {
limitranges := []*corev1.LimitRange{}
limitranges = append(limitranges, &corev1.LimitRange{
ObjectMeta: metav1.ObjectMeta{
Name: "default-container-limits",
Namespace: profile.Name,
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(profile, kubeflowv1.SchemeGroupVersion.WithKind("Profile")),
},
},
Spec: corev1.LimitRangeSpec{
Limits: []corev1.LimitRangeItem{
{
Default: corev1.ResourceList{
"cpu": *resource.NewScaledQuantity(250, resource.Milli),
"memory": *resource.NewScaledQuantity(256, resource.Mega),
},
DefaultRequest: corev1.ResourceList{
"cpu": *resource.NewScaledQuantity(250, resource.Milli),
"memory": *resource.NewScaledQuantity(256, resource.Mega),
},
Type: "Container",
},
},
},
})
return limitranges
}
func init() {
rootCmd.AddCommand(limitRangesCmd)
}