|
| 1 | +/* |
| 2 | +Copyright The Kubernetes Authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package oke |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "fmt" |
| 23 | + "io" |
| 24 | + "net/http" |
| 25 | + "time" |
| 26 | + |
| 27 | + "k8s.io/apimachinery/pkg/util/wait" |
| 28 | + "k8s.io/klog/v2" |
| 29 | + |
| 30 | + resourceapi "k8s.io/api/resource/v1" |
| 31 | + "sigs.k8s.io/dranet/pkg/apis" |
| 32 | + "sigs.k8s.io/dranet/pkg/cloudprovider" |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + OKEAttrPrefix = "oke.dra.net" |
| 37 | + |
| 38 | + AttrOKEShape = OKEAttrPrefix + "/" + "shape" |
| 39 | + AttrOKEFaultDomain = OKEAttrPrefix + "/" + "faultDomain" |
| 40 | + AttrOKEAvailabilityDomain = OKEAttrPrefix + "/" + "availabilityDomain" |
| 41 | + |
| 42 | + // imdsEndpoint is the Oracle Cloud Instance Metadata Service endpoint. |
| 43 | + imdsEndpoint = "http://169.254.169.254/opc/v2" |
| 44 | +) |
| 45 | + |
| 46 | +// imdsInstanceMetadata contains the fields we care about from the OCI IMDS |
| 47 | +// instance metadata response. |
| 48 | +type imdsInstanceMetadata struct { |
| 49 | + Shape string `json:"shape"` |
| 50 | + FaultDomain string `json:"faultDomain"` |
| 51 | + AvailabilityDomain string `json:"availabilityDomain"` |
| 52 | +} |
| 53 | + |
| 54 | +var _ cloudprovider.CloudInstance = (*OKEInstance)(nil) |
| 55 | + |
| 56 | +// OKEInstance holds OCI/OKE specific instance data. |
| 57 | +type OKEInstance struct { |
| 58 | + Shape string |
| 59 | + FaultDomain string |
| 60 | + AvailabilityDomain string |
| 61 | +} |
| 62 | + |
| 63 | +// GetDeviceAttributes returns OKE-specific attributes for a device. |
| 64 | +// These are node-level attributes applied to all devices since OCI IMDS |
| 65 | +// does not expose per-RDMA-NIC metadata. |
| 66 | +func (o *OKEInstance) GetDeviceAttributes(id cloudprovider.DeviceIdentifiers) map[resourceapi.QualifiedName]resourceapi.DeviceAttribute { |
| 67 | + attributes := make(map[resourceapi.QualifiedName]resourceapi.DeviceAttribute) |
| 68 | + |
| 69 | + if o.Shape != "" { |
| 70 | + attributes[AttrOKEShape] = resourceapi.DeviceAttribute{StringValue: &o.Shape} |
| 71 | + } |
| 72 | + if o.FaultDomain != "" { |
| 73 | + attributes[AttrOKEFaultDomain] = resourceapi.DeviceAttribute{StringValue: &o.FaultDomain} |
| 74 | + } |
| 75 | + if o.AvailabilityDomain != "" { |
| 76 | + attributes[AttrOKEAvailabilityDomain] = resourceapi.DeviceAttribute{StringValue: &o.AvailabilityDomain} |
| 77 | + } |
| 78 | + |
| 79 | + return attributes |
| 80 | +} |
| 81 | + |
| 82 | +// GetDeviceConfig returns nil as OCI does not provide device-specific |
| 83 | +// network configuration through IMDS. |
| 84 | +func (o *OKEInstance) GetDeviceConfig(id cloudprovider.DeviceIdentifiers) *apis.NetworkConfig { |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// OnOKE returns true if running on an Oracle Cloud Infrastructure instance. |
| 89 | +// Detection is done by probing the OCI IMDS v2 endpoint. |
| 90 | +func OnOKE(ctx context.Context) bool { |
| 91 | + pollCtx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 92 | + defer cancel() |
| 93 | + return wait.PollUntilContextCancel(pollCtx, 1*time.Second, true, func(ctx context.Context) (bool, error) { |
| 94 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, imdsEndpoint+"/instance/", nil) |
| 95 | + if err != nil { |
| 96 | + return false, nil |
| 97 | + } |
| 98 | + req.Header.Set("Authorization", "Bearer Oracle") |
| 99 | + |
| 100 | + resp, err := http.DefaultClient.Do(req) |
| 101 | + if err != nil { |
| 102 | + return false, nil |
| 103 | + } |
| 104 | + defer resp.Body.Close() |
| 105 | + return resp.StatusCode == http.StatusOK, nil |
| 106 | + }) == nil |
| 107 | +} |
| 108 | + |
| 109 | +// GetInstance retrieves OCI instance properties by querying the IMDS. |
| 110 | +func GetInstance(ctx context.Context) (cloudprovider.CloudInstance, error) { |
| 111 | + var instance *OKEInstance |
| 112 | + err := wait.PollUntilContextTimeout(ctx, 1*time.Second, 15*time.Second, true, func(ctx context.Context) (bool, error) { |
| 113 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, imdsEndpoint+"/instance/", nil) |
| 114 | + if err != nil { |
| 115 | + klog.Infof("could not create OCI IMDS request ... retrying: %v", err) |
| 116 | + return false, nil |
| 117 | + } |
| 118 | + req.Header.Set("Authorization", "Bearer Oracle") |
| 119 | + |
| 120 | + resp, err := http.DefaultClient.Do(req) |
| 121 | + if err != nil { |
| 122 | + klog.Infof("could not reach OCI IMDS ... retrying: %v", err) |
| 123 | + return false, nil |
| 124 | + } |
| 125 | + defer resp.Body.Close() |
| 126 | + |
| 127 | + if resp.StatusCode != http.StatusOK { |
| 128 | + klog.Infof("OCI IMDS returned status %d ... retrying", resp.StatusCode) |
| 129 | + return false, nil |
| 130 | + } |
| 131 | + |
| 132 | + body, err := io.ReadAll(resp.Body) |
| 133 | + if err != nil { |
| 134 | + klog.Infof("could not read OCI IMDS response ... retrying: %v", err) |
| 135 | + return false, nil |
| 136 | + } |
| 137 | + |
| 138 | + var metadata imdsInstanceMetadata |
| 139 | + if err := json.Unmarshal(body, &metadata); err != nil { |
| 140 | + return false, fmt.Errorf("could not parse OCI IMDS response: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + instance = &OKEInstance{ |
| 144 | + Shape: metadata.Shape, |
| 145 | + FaultDomain: metadata.FaultDomain, |
| 146 | + AvailabilityDomain: metadata.AvailabilityDomain, |
| 147 | + } |
| 148 | + return true, nil |
| 149 | + }) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + return instance, nil |
| 154 | +} |
0 commit comments