-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmachinepools.go
165 lines (140 loc) · 5.36 KB
/
machinepools.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
package controllers
import (
"context"
"fmt"
"os"
"strings"
ocmsdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
configv1 "github.com/openshift/api/config/v1"
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
)
func (r *AppWrapperReconciler) createOCMConnection() (*ocmsdk.Connection, error) {
logger, err := ocmsdk.NewGoLoggerBuilder().
Debug(false).
Build()
if err != nil {
return nil, fmt.Errorf("can't build logger: %v", err)
}
connection, err := ocmsdk.NewConnectionBuilder().
Logger(logger).
Tokens(r.ocmToken).
Build()
if err != nil {
return nil, fmt.Errorf("can't build connection: %v", err)
}
return connection, nil
}
func hasAwLabel(machinePool *cmv1.MachinePool, aw *arbv1.AppWrapper) bool {
value, ok := machinePool.Labels()[aw.Name]
if ok && value == aw.Name {
return true
}
return false
}
func (r *AppWrapperReconciler) scaleMachinePool(ctx context.Context, aw *arbv1.AppWrapper, demandPerInstanceType map[string]int) (ctrl.Result, error) {
connection, err := r.createOCMConnection()
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating OCM connection: %v", err)
return ctrl.Result{}, err
}
defer connection.Close()
for userRequestedInstanceType := range demandPerInstanceType {
replicas := demandPerInstanceType[userRequestedInstanceType]
clusterMachinePools := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).MachinePools()
response, err := clusterMachinePools.List().SendContext(ctx)
if err != nil {
return ctrl.Result{}, err
}
numberOfMachines := 0
response.Items().Each(func(machinePool *cmv1.MachinePool) bool {
if machinePool.InstanceType() == userRequestedInstanceType && hasAwLabel(machinePool, aw) {
numberOfMachines = machinePool.Replicas()
return false
}
return true
})
if numberOfMachines != replicas {
m := make(map[string]string)
m[aw.Name] = aw.Name
klog.Infof("The instanceRequired array: %v", userRequestedInstanceType)
machinePoolTaint := cmv1.NewTaint().
Key(aw.Name).
Value("value1").
Effect("PreferNoSchedule")
machinePoolID := strings.ReplaceAll(aw.Name+"-"+userRequestedInstanceType, ".", "-")
createMachinePool, err := cmv1.NewMachinePool().ID(machinePoolID).InstanceType(userRequestedInstanceType).Replicas(replicas).Labels(m).Taints(machinePoolTaint).Build()
if err != nil {
klog.Errorf(`Error building MachinePool: %v`, err)
}
klog.Infof("Built MachinePool with instance type %v and name %v", userRequestedInstanceType, createMachinePool.ID())
response, err := clusterMachinePools.Add().Body(createMachinePool).SendContext(ctx)
if err != nil {
klog.Errorf(`Error creating MachinePool: %v`, err)
}
klog.Infof("Created MachinePool: %v", response)
}
}
return ctrl.Result{Requeue: false}, nil
}
func (r *AppWrapperReconciler) deleteMachinePool(ctx context.Context, aw *arbv1.AppWrapper) (ctrl.Result, error) {
connection, err := r.createOCMConnection()
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating OCM connection: %v", err)
return ctrl.Result{}, err
}
defer connection.Close()
machinePoolsConnection := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).MachinePools().List()
machinePoolsListResponse, _ := machinePoolsConnection.Send()
machinePoolsList := machinePoolsListResponse.Items()
machinePoolsList.Range(func(index int, item *cmv1.MachinePool) bool {
id, _ := item.GetID()
if strings.Contains(id, aw.Name) {
targetMachinePool, err := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).MachinePools().MachinePool(id).Delete().SendContext(ctx)
if err != nil {
klog.Infof("Error deleting target machinepool %v", targetMachinePool)
}
klog.Infof("Successfully Scaled down target machinepool %v", id)
}
return true
})
return ctrl.Result{Requeue: false}, nil
}
func (r *AppWrapperReconciler) machinePoolExists() (bool, error) {
connection, err := r.createOCMConnection()
if err != nil {
return false, fmt.Errorf("error creating OCM connection: %w", err)
}
defer connection.Close()
machinePools := connection.ClustersMgmt().V1().Clusters().Cluster(r.ocmClusterID).MachinePools()
return machinePools != nil, nil
}
// getOCMClusterID determines the internal clusterID to be used for OCM API calls
func (r *AppWrapperReconciler) getOCMClusterID(ctx context.Context) error {
cv := &configv1.ClusterVersion{}
err := r.Get(ctx, types.NamespacedName{Name: "version"}, cv)
if err != nil {
return fmt.Errorf("can't get clusterversion: %v", err)
}
internalClusterID := string(cv.Spec.ClusterID)
connection, err := r.createOCMConnection()
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating OCM connection: %v", err)
}
defer connection.Close()
// Get the client for the resource that manages the collection of clusters:
collection := connection.ClustersMgmt().V1().Clusters()
response, err := collection.List().Search(fmt.Sprintf("external_id = '%s'", internalClusterID)).Size(1).Page(1).SendContext(ctx)
if err != nil {
klog.Errorf(`Error getting cluster id: %v`, err)
}
response.Items().Each(func(cluster *cmv1.Cluster) bool {
r.ocmClusterID = cluster.ID()
fmt.Printf("%s - %s - %s\n", cluster.ID(), cluster.Name(), cluster.State())
return true
})
return nil
}