Skip to content

Commit f0787e5

Browse files
add: Machine Pool existence check (#102)
* update namespace for ocm secret * add: machinePoolExists function to check if a machine pool is available to use Co-authored-by: Fiona-Waters [email protected] * add: update instascale-clusterrole.yaml, machinePoolExists and onAdd func, check for instascale-ocm-secret before defaulting machinetype, update error messages. --------- Co-authored-by: Fiona Waters <[email protected]>
1 parent 866f2dc commit f0787e5

File tree

5 files changed

+77
-36
lines changed

5 files changed

+77
-36
lines changed

controllers/appWrapper_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestDiscoverInstanceTypes(t *testing.T) {
8686
expected: map[string]int{},
8787
},
8888
{
89-
name: "Test with empty orderedinstance",
89+
name: "Test with no orderedinstance",
9090
input: &arbv1.AppWrapper{
9191
ObjectMeta: metav1.ObjectMeta{
9292
Labels: map[string]string{

controllers/appwrapper_controller.go

+44-31
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ import (
4545
// AppWrapperReconciler reconciles a AppWrapper object
4646
type AppWrapperReconciler struct {
4747
client.Client
48-
Scheme *runtime.Scheme
49-
ConfigsNamespace string
48+
Scheme *runtime.Scheme
49+
ConfigsNamespace string
50+
OcmSecretNamespace string
5051
}
5152

5253
var (
@@ -149,36 +150,49 @@ func (r *AppWrapperReconciler) SetupWithManager(mgr ctrl.Manager) error {
149150
instascaleConfigmap, err := kubeClient.CoreV1().ConfigMaps(r.ConfigsNamespace).Get(context.Background(), "instascale-config", metav1.GetOptions{})
150151
if err != nil {
151152
klog.Infof("Config map named instascale-config is not available in namespace %v", r.ConfigsNamespace)
153+
return err
152154
}
153155

154-
if maxScaleNodesAllowed, err = strconv.Atoi(instascaleConfigmap.Data["maxScaleoutAllowed"]); err != nil {
155-
klog.Infof("Error converting %v to int. Setting maxScaleNodesAllowed to 3", maxScaleNodesAllowed)
156+
maxScaleNodesAllowed, err = strconv.Atoi(instascaleConfigmap.Data["maxScaleoutAllowed"])
157+
if err != nil {
158+
klog.Warningf("Error converting %v to int. Setting maxScaleNodesAllowed to 3", maxScaleNodesAllowed)
156159
maxScaleNodesAllowed = 3
157160
}
158-
klog.Infof("Got config map named %v from namespace %v that configures max nodes in cluster to value %v", instascaleConfigmap.Name, instascaleConfigmap.Namespace, maxScaleNodesAllowed)
161+
if instascaleConfigmap != nil {
162+
klog.Errorf("Got config map named %v from namespace %v that configures max nodes in cluster to value %v", instascaleConfigmap.Name, instascaleConfigmap.Namespace, maxScaleNodesAllowed)
163+
}
159164

160165
useMachineSets = true
161-
useMachinePools, err := strconv.ParseBool(instascaleConfigmap.Data["useMachinePools"])
162-
if err != nil {
163-
klog.Infof("Error converting %v to bool. Defaulting to using Machine Sets", useMachineSets)
164-
} else {
165-
useMachineSets = !useMachinePools
166-
klog.Infof("Setting useMachineSets to %v", useMachineSets)
167-
}
166+
ocmSecretExists := ocmSecretExists(r.OcmSecretNamespace)
167+
if ocmSecretExists {
168+
machinePoolsExists := machinePoolExists()
168169

169-
if !useMachineSets {
170-
instascaleOCMSecret, err := kubeClient.CoreV1().Secrets(r.ConfigsNamespace).Get(context.Background(), "instascale-ocm-secret", metav1.GetOptions{})
171-
if err != nil {
172-
klog.Errorf("Error getting instascale-ocm-secret from namespace %v - Error : %v", r.ConfigsNamespace, err)
170+
if machinePoolsExists {
171+
useMachineSets = false
172+
klog.Infof("Using machine pools %v", machinePoolsExists)
173+
} else {
174+
klog.Infof("Setting useMachineSets to %v", useMachineSets)
173175
}
174-
ocmToken = string(instascaleOCMSecret.Data["token"])
175176
}
176177

177178
return ctrl.NewControllerManagedBy(mgr).
178179
For(&arbv1.AppWrapper{}).
179180
Complete(r)
180181
}
181182

183+
func ocmSecretExists(namespace string) bool {
184+
instascaleOCMSecret, err := kubeClient.CoreV1().Secrets(namespace).Get(context.Background(), "instascale-ocm-secret", metav1.GetOptions{})
185+
if err != nil {
186+
klog.Errorf("Error getting instascale-ocm-secret from namespace %v: %v", namespace, err)
187+
klog.Infof("If you are looking to use OCM, ensure that the 'instascale-ocm-secret' secret is available on the cluster within namespace %v", namespace)
188+
klog.Infof("Setting useMachineSets to %v.", useMachineSets)
189+
return false
190+
}
191+
192+
ocmToken = string(instascaleOCMSecret.Data["token"])
193+
return true
194+
}
195+
182196
func addAppwrappersThatNeedScaling() {
183197
kubeconfig := os.Getenv("KUBECONFIG")
184198
restConfig, err := getRestConfig(kubeconfig)
@@ -223,28 +237,27 @@ func onAdd(obj interface{}) {
223237
aw, ok := obj.(*arbv1.AppWrapper)
224238
if ok {
225239
klog.Infof("Found Appwrapper named %s that has status %v", aw.Name, aw.Status.State)
226-
if aw.Status.State == arbv1.AppWrapperStateEnqueued || aw.Status.State == "" {
240+
if aw.Status.State == arbv1.AppWrapperStateEnqueued || aw.Status.State == "" && aw.Labels["orderedinstance"] != "" {
227241
//scaledAppwrapper = append(scaledAppwrapper, aw.Name)
228242
demandPerInstanceType := discoverInstanceTypes(aw)
229243
//TODO: simplify the looping
230-
if useMachineSets {
231-
if canScaleMachineset(demandPerInstanceType) {
232-
scaleUp(aw, demandPerInstanceType)
244+
if demandPerInstanceType != nil {
245+
if useMachineSets {
246+
if canScaleMachineset(demandPerInstanceType) {
247+
scaleUp(aw, demandPerInstanceType)
248+
} else {
249+
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
250+
}
233251
} else {
234-
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
235-
}
236-
} else {
237-
if canScaleMachinepool(demandPerInstanceType) {
238-
scaleUp(aw, demandPerInstanceType)
239-
} else {
240-
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
252+
if canScaleMachinepool(demandPerInstanceType) {
253+
scaleUp(aw, demandPerInstanceType)
254+
} else {
255+
klog.Infof("Cannot scale up replicas max replicas allowed is %v", maxScaleNodesAllowed)
256+
}
241257
}
242258
}
243-
244259
}
245-
246260
}
247-
248261
}
249262

250263
func onUpdate(old, new interface{}) {

controllers/machinepools.go

+24
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ func deleteMachinePool(aw *arbv1.AppWrapper) {
8686
})
8787
}
8888

89+
// Check if machine pools exist
90+
func machinePoolExists() bool {
91+
logger, err := ocmsdk.NewGoLoggerBuilder().
92+
Debug(false).
93+
Build()
94+
if err != nil {
95+
fmt.Fprintf(os.Stderr, "Can't build logger: %v\n", err)
96+
os.Exit(1)
97+
}
98+
connection, err := ocmsdk.NewConnectionBuilder().
99+
Logger(logger).
100+
Tokens(ocmToken).
101+
Build()
102+
if err != nil {
103+
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err)
104+
os.Exit(1)
105+
}
106+
defer connection.Close()
107+
108+
machinePools := connection.ClustersMgmt().V1().Clusters().Cluster(ocmClusterID).MachinePools()
109+
klog.Infof("Machine pools: %v", machinePools)
110+
return machinePools != nil
111+
}
112+
89113
// getOCMClusterID determines the internal clusterID to be used for OCM API calls
90114
func getOCMClusterID(r *AppWrapperReconciler) error {
91115

deployment/instascale-clusterrole.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ rules:
6767
verbs:
6868
- get
6969
- list
70+
- watch

main.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ func main() {
5555
var enableLeaderElection bool
5656
var probeAddr string
5757
var configsNamespace string
58+
var ocmSecretNamespace string
5859
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
5960
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
60-
flag.StringVar(&configsNamespace, "configs-namespace", "kube-system", "The namespace containing the InstaScale configmap and OCM secret")
61+
flag.StringVar(&configsNamespace, "configs-namespace", "kube-system", "The namespace containing the Instacale configmap")
62+
flag.StringVar(&ocmSecretNamespace, "ocm-secret-namespace", "default", "The namespace containing the OCM secret")
6163
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6264
"Enable leader election for controller manager. "+
6365
"Enabling this will ensure there is only one active controller manager.")
@@ -83,9 +85,10 @@ func main() {
8385
}
8486

8587
if err = (&controllers.AppWrapperReconciler{
86-
Client: mgr.GetClient(),
87-
Scheme: mgr.GetScheme(),
88-
ConfigsNamespace: configsNamespace,
88+
Client: mgr.GetClient(),
89+
Scheme: mgr.GetScheme(),
90+
ConfigsNamespace: configsNamespace,
91+
OcmSecretNamespace: ocmSecretNamespace,
8992
}).SetupWithManager(mgr); err != nil {
9093
setupLog.Error(err, "unable to create controller", "controller", "AppWrapper")
9194
os.Exit(1)

0 commit comments

Comments
 (0)