|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/oxidecomputer/oxide.go/oxide" |
| 9 | + v1 "k8s.io/api/core/v1" |
| 10 | + "k8s.io/client-go/kubernetes" |
| 11 | + cloudprovider "k8s.io/cloud-provider" |
| 12 | +) |
| 13 | + |
| 14 | +var _ cloudprovider.InstancesV2 = (*InstancesV2)(nil) |
| 15 | + |
| 16 | +// InstancesV2 implements [cloudprovider.InstancesV2] to provide Oxide specific |
| 17 | +// instance functionality. |
| 18 | +type InstancesV2 struct { |
| 19 | + client *oxide.Client |
| 20 | + project string |
| 21 | + |
| 22 | + k8sClient kubernetes.Interface |
| 23 | +} |
| 24 | + |
| 25 | +// InstanceExists checks whether the provided Kubernetes node exists as instance |
| 26 | +// in Oxide. |
| 27 | +func (c *InstancesV2) InstanceExists(ctx context.Context, node *v1.Node) (bool, error) { |
| 28 | + instanceID := strings.TrimPrefix(node.Spec.ProviderID, "oxide://") |
| 29 | + |
| 30 | + if _, err := c.client.InstanceView(ctx, oxide.InstanceViewParams{ |
| 31 | + Instance: oxide.NameOrId(instanceID), |
| 32 | + }); err != nil { |
| 33 | + if strings.Contains(err.Error(), "NotFound") { |
| 34 | + return false, nil |
| 35 | + } |
| 36 | + |
| 37 | + return false, fmt.Errorf("failed viewing oxide instance %s: %v", instanceID, err) |
| 38 | + } |
| 39 | + |
| 40 | + return true, nil |
| 41 | +} |
| 42 | + |
| 43 | +// InstanceMetadata populates the metadata for the provided node, notably |
| 44 | +// setting its provider ID. |
| 45 | +func (c *InstancesV2) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloudprovider.InstanceMetadata, error) { |
| 46 | + var ( |
| 47 | + err error |
| 48 | + instance *oxide.Instance |
| 49 | + instanceID string |
| 50 | + ) |
| 51 | + |
| 52 | + if node.Spec.ProviderID != "" { |
| 53 | + instanceID = strings.TrimPrefix(node.Spec.ProviderID, "oxide://") |
| 54 | + |
| 55 | + instance, err = c.client.InstanceView(ctx, oxide.InstanceViewParams{ |
| 56 | + Instance: oxide.NameOrId(instanceID), |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("failed viewing oxide instance by id: %v", err) |
| 60 | + } |
| 61 | + } else { |
| 62 | + instance, err = c.client.InstanceView(ctx, oxide.InstanceViewParams{ |
| 63 | + Project: oxide.NameOrId(c.project), |
| 64 | + Instance: oxide.NameOrId(node.GetName()), |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("failed viewing oxide instance by name: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + instanceID = instance.Id |
| 71 | + } |
| 72 | + |
| 73 | + nics, err := c.client.InstanceNetworkInterfaceList(ctx, oxide.InstanceNetworkInterfaceListParams{ |
| 74 | + Instance: oxide.NameOrId(instanceID), |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("failed listing instance network interfaces: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + externalIPs, err := c.client.InstanceExternalIpList(ctx, oxide.InstanceExternalIpListParams{ |
| 81 | + Instance: oxide.NameOrId(instanceID), |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("failed listing instance external ips: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + nodeAddresses := make([]v1.NodeAddress, 0) |
| 88 | + nodeAddresses = append(nodeAddresses, v1.NodeAddress{ |
| 89 | + Type: v1.NodeHostName, |
| 90 | + Address: instance.Hostname, |
| 91 | + }) |
| 92 | + |
| 93 | + for _, nic := range nics.Items { |
| 94 | + nodeAddresses = append(nodeAddresses, v1.NodeAddress{ |
| 95 | + Type: v1.NodeInternalIP, |
| 96 | + Address: nic.Ip, |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + for _, externalIP := range externalIPs.Items { |
| 101 | + if externalIP.Kind == "snat" { |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + nodeAddresses = append(nodeAddresses, v1.NodeAddress{ |
| 106 | + Type: v1.NodeExternalIP, |
| 107 | + Address: externalIP.Ip, |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + return &cloudprovider.InstanceMetadata{ |
| 112 | + ProviderID: fmt.Sprintf("oxide://%s", instanceID), |
| 113 | + InstanceType: fmt.Sprintf("%v-%v", instance.Ncpus, (instance.Memory / (1024 * 1024 * 1024))), |
| 114 | + NodeAddresses: nodeAddresses, |
| 115 | + }, nil |
| 116 | +} |
| 117 | + |
| 118 | +// InstanceShutdown checks whether the provided node is shut down in Oxide. |
| 119 | +func (c *InstancesV2) InstanceShutdown(ctx context.Context, node *v1.Node) (bool, error) { |
| 120 | + instanceID := strings.TrimPrefix(node.Spec.ProviderID, "oxide://") |
| 121 | + |
| 122 | + instance, err := c.client.InstanceView(ctx, oxide.InstanceViewParams{ |
| 123 | + Instance: oxide.NameOrId(instanceID), |
| 124 | + }) |
| 125 | + if err != nil { |
| 126 | + return false, fmt.Errorf("failed viewing oxide instance %s: %v", instanceID, err) |
| 127 | + } |
| 128 | + |
| 129 | + return instance.RunState == oxide.InstanceStateStopped, nil |
| 130 | +} |
0 commit comments