Skip to content

Commit c2f39ba

Browse files
committed
remove host-instance dependency
1 parent 05543a3 commit c2f39ba

File tree

4 files changed

+25
-46
lines changed

4 files changed

+25
-46
lines changed

Diff for: .goreleaser.yml

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ docker_manifests:
4040
image_templates:
4141
- 'ghcr.io/crusoecloud/{{ .ProjectName }}:{{ .Tag }}-amd64'
4242
- 'ghcr.io/crusoecloud/{{ .ProjectName }}:{{ .Tag }}-arm64'
43+
- name_template: 'ghcr.io/crusoecloud/{{ .ProjectName }}:latest'
44+
image_templates:
45+
- 'ghcr.io/crusoecloud/{{ .ProjectName }}:{{ .Tag }}-amd64'
46+
- 'ghcr.io/crusoecloud/{{ .ProjectName }}:{{ .Tag }}-arm64'
4347
signs:
4448
- artifacts: checksum
4549
args:

Diff for: cmd/main.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,15 @@ func main() {
142142
os.Exit(1)
143143
}
144144

145-
hostInstance, crusoeClient, err := controller.GetHostInstance(context.Background())
145+
crusoeClient, err := controller.GetCrusoeClient(context.Background())
146146
if err != nil {
147-
setupLog.Error(err, "unable to get host instance", "controller", "Service")
147+
setupLog.Error(err, "unable to create crusoe client", "controller", "Service")
148148
}
149149

150150
if err = (&controller.ServiceReconciler{
151151
Client: mgr.GetClient(),
152152
Scheme: mgr.GetScheme(),
153153
CrusoeClient: crusoeClient,
154-
HostInstance: hostInstance, // TODO add hostInstance
155154
}).SetupWithManager(mgr); err != nil {
156155
setupLog.Error(err, "unable to create controller", "controller", "Service")
157156
os.Exit(1)

Diff for: internal/controller/service_controller.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ type ServiceReconciler struct {
5050
client.Client
5151
Scheme *runtime.Scheme
5252
CrusoeClient *crusoeapi.APIClient
53-
HostInstance *crusoeapi.InstanceV1Alpha5
5453
}
5554

5655
// +kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
@@ -233,26 +232,28 @@ func (r *ServiceReconciler) handleCreate(ctx context.Context, svc *corev1.Servic
233232

234233
// Prepare payload for the API call
235234
// get vpc id
236-
cluster, _, err := r.CrusoeClient.KubernetesClustersApi.GetCluster(ctx, r.HostInstance.ProjectId, viper.GetString(CrusoeClusterIDFlag))
235+
projectId := viper.GetString(CrusoeProjectIDFlag)
236+
cluster, _, err := r.CrusoeClient.KubernetesClustersApi.GetCluster(ctx, projectId, viper.GetString(CrusoeClusterIDFlag))
237237
if err != nil {
238238
logger.Error(err, "Failed to get cluster", "clusterID", viper.GetString(CrusoeClusterIDFlag))
239239
return ctrl.Result{}, err
240240
}
241-
subnet, _, err := r.CrusoeClient.VPCSubnetsApi.GetVPCSubnet(ctx, r.HostInstance.ProjectId, cluster.SubnetId)
241+
subnet, _, err := r.CrusoeClient.VPCSubnetsApi.GetVPCSubnet(ctx, projectId, cluster.SubnetId)
242242
if err != nil {
243243
logger.Error(err, "Failed to get vpc network id from cluster subnet id ", "subnetID", cluster.SubnetId)
244244
return ctrl.Result{}, err
245245
}
246+
246247
apiPayload := crusoeapi.ExternalLoadBalancerPostRequest{
247248
VpcId: subnet.VpcNetworkId,
248249
Name: svc.Name,
249-
Location: r.HostInstance.Location,
250+
Location: subnet.Location,
250251
Protocol: "LOAD_BALANCER_PROTOCOL_TCP", // only TCP supported currently
251252
ListenPortsAndBackends: listenPortsAndBackends,
252253
HealthCheckOptions: healthCheckOptions,
253254
}
254255

255-
op_resp, http_resp, err := r.CrusoeClient.ExternalLoadBalancersApi.CreateExternalLoadBalancer(ctx, apiPayload, r.HostInstance.ProjectId)
256+
op_resp, http_resp, err := r.CrusoeClient.ExternalLoadBalancersApi.CreateExternalLoadBalancer(ctx, apiPayload, projectId)
256257
if err != nil {
257258
logger.Error(err, "Failed to create load balancer via API")
258259
return ctrl.Result{}, nil
@@ -264,7 +265,7 @@ func (r *ServiceReconciler) handleCreate(ctx context.Context, svc *corev1.Servic
264265
}
265266

266267
op, err := utils.WaitForOperation(ctx, "Creating ELB ...",
267-
op_resp.Operation, r.HostInstance.ProjectId, r.CrusoeClient.ExternalLoadBalancerOperationsApi.GetExternalLoadBalancerOperation)
268+
op_resp.Operation, projectId, r.CrusoeClient.ExternalLoadBalancerOperationsApi.GetExternalLoadBalancerOperation)
268269
if err != nil || op.State != string(OpSuccess) {
269270
logger.Error(err, "Failed to create LB Service", "name", svc.Name, "namespace", svc.Namespace)
270271
return ctrl.Result{}, err
@@ -275,7 +276,7 @@ func (r *ServiceReconciler) handleCreate(ctx context.Context, svc *corev1.Servic
275276
return ctrl.Result{}, err
276277
}
277278

278-
logger.Info("GOT THIS BACK", "http_resp", op.Result)
279+
logger.Info("RESULT", "http_resp", op.Result)
279280
// update external IP of LB svc
280281
externalIP := loadBalancer.Vip
281282
patch := client.MergeFrom(svc.DeepCopy())
@@ -309,14 +310,9 @@ func (r *ServiceReconciler) handleDelete(ctx context.Context, svc *corev1.Servic
309310

310311
loadBalancerID := svc.Annotations[loadbalancerIDLabelKey]
311312

312-
if r.HostInstance == nil { // in case node hosting controller dies, need to re-grab
313-
hostInstance, crusoeClient, _ := GetHostInstance(context.Background())
314-
r.HostInstance = hostInstance
315-
r.CrusoeClient = crusoeClient
316-
}
317-
318313
// Call the API to delete the load balancer
319-
_, httpResp, err := r.CrusoeClient.ExternalLoadBalancersApi.DeleteExternalLoadBalancer(ctx, r.HostInstance.ProjectId, loadBalancerID)
314+
projectId := viper.GetString(CrusoeProjectIDFlag)
315+
_, httpResp, err := r.CrusoeClient.ExternalLoadBalancersApi.DeleteExternalLoadBalancer(ctx, projectId, loadBalancerID)
320316
if err != nil {
321317
statusCode := httpResp.StatusCode
322318
switch statusCode {

Diff for: internal/controller/util.go

+9-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
utils "lb_controller/internal/utils"
1010
"strconv"
1111

12-
"github.com/antihax/optional"
1312
crusoeapi "github.com/crusoecloud/client-go/swagger/v1alpha5"
1413
swagger "github.com/crusoecloud/client-go/swagger/v1alpha5"
1514
"github.com/go-logr/logr"
@@ -174,12 +173,12 @@ func (r *ServiceReconciler) parseListenPortsAndBackends(ctx context.Context, svc
174173
}
175174

176175
//nolint:cyclop // function is already fairly clean
177-
func GetHostInstance(ctx context.Context) (*crusoeapi.InstanceV1Alpha5, *crusoeapi.APIClient, error) {
176+
func GetCrusoeClient(ctx context.Context) (*crusoeapi.APIClient, error) {
178177
logger := log.FromContext(ctx)
179178

180179
bindErr := utils.BindEnvs()
181180
if bindErr != nil {
182-
return nil, nil, fmt.Errorf("could not bind env variables from helm: %w", bindErr)
181+
return nil, fmt.Errorf("could not bind env variables from helm: %w", bindErr)
183182
}
184183

185184
logger.Info("Creating Crusoe client with config", "endpoint", viper.GetString(CrusoeAPIEndpointFlag))
@@ -192,52 +191,32 @@ func GetHostInstance(ctx context.Context) (*crusoeapi.InstanceV1Alpha5, *crusoea
192191

193192
var projectID string
194193

195-
var instanceID string
196-
197194
projectID = viper.GetString(CrusoeProjectIDFlag)
198195
if projectID == "" {
199196
var ok bool
200197
kubeClientConfig, configErr := rest.InClusterConfig()
201198
if configErr != nil {
202-
return nil, nil, fmt.Errorf("could not get kube client config: %w", configErr)
199+
return nil, fmt.Errorf("could not get kube client config: %w", configErr)
203200
}
204201

205202
kubeClient, clientErr := kubernetes.NewForConfig(kubeClientConfig)
206203
if clientErr != nil {
207-
return nil, nil, fmt.Errorf("could not get kube client: %w", clientErr)
204+
return nil, fmt.Errorf("could not get kube client: %w", clientErr)
208205
}
209206

210207
hostNode, nodeFetchErr := kubeClient.CoreV1().Nodes().Get(ctx, viper.GetString(NodeNameFlag), metav1.GetOptions{})
211208
if nodeFetchErr != nil {
212-
return nil, nil, fmt.Errorf("could not fetch current node with kube client: %w", nodeFetchErr)
209+
return nil, fmt.Errorf("could not fetch current node with kube client: %w", nodeFetchErr)
213210
}
214211

215212
projectID, ok = hostNode.Labels[projectIDLabelKey]
216213
if !ok {
217-
return nil, nil, errProjectIDNotFound
214+
return nil, errProjectIDNotFound
218215
}
219216

220-
// Note: if missing label check what nodepool image is being used
221-
instanceID, ok = hostNode.Labels[instanceIDLabelKey]
222-
if !ok {
223-
return nil, nil, errInstanceIDNotFound
224-
}
225-
226-
}
227-
228-
instances, _, err := crusoeClient.VMsApi.ListInstances(ctx, projectID,
229-
&crusoeapi.VMsApiListInstancesOpts{
230-
Ids: optional.NewString(instanceID),
231-
})
232-
if err != nil {
233-
return nil, crusoeClient, fmt.Errorf("failed to list instances: %w", err)
234-
}
235-
236-
if len(instances.Items) == 0 {
237-
return nil, nil, fmt.Errorf("%w: %s", errInstanceNotFound, instanceID)
238217
}
239218

240-
return &instances.Items[0], crusoeClient, nil
219+
return crusoeClient, nil
241220
}
242221

243222
func OpResultToItem[T any](res interface{}) (*T, error) {
@@ -339,7 +318,8 @@ func (r *ServiceReconciler) updateLoadBalancer(
339318
}
340319

341320
logger.Info("Updating external load balancer with new specs", "lbID", lbUpdatePayload.Id)
342-
lb_updated, httpResp, err := r.CrusoeClient.ExternalLoadBalancersApi.UpdateExternalLoadBalancer(ctx, lbUpdatePayload, r.HostInstance.ProjectId, loadBalancerID)
321+
projectId := viper.GetString(CrusoeProjectIDFlag)
322+
lb_updated, httpResp, err := r.CrusoeClient.ExternalLoadBalancersApi.UpdateExternalLoadBalancer(ctx, lbUpdatePayload, projectId, loadBalancerID)
343323
if err != nil {
344324
logger.Error(err, "Failed to update load balancer via API")
345325
return err

0 commit comments

Comments
 (0)