|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package oracle |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "encoding/base64" |
| 10 | + "encoding/json" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "net" |
| 14 | + |
| 15 | + "github.com/AlekSi/pointer" |
| 16 | + "github.com/talos-systems/go-procfs/procfs" |
| 17 | + |
| 18 | + "github.com/talos-systems/talos/internal/app/machined/pkg/runtime" |
| 19 | + "github.com/talos-systems/talos/internal/app/machined/pkg/runtime/v1alpha1/platform/errors" |
| 20 | + "github.com/talos-systems/talos/pkg/download" |
| 21 | + "github.com/talos-systems/talos/pkg/machinery/config" |
| 22 | + "github.com/talos-systems/talos/pkg/machinery/config/configloader" |
| 23 | + "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1" |
| 24 | +) |
| 25 | + |
| 26 | +// Ref: https://docs.oracle.com/en-us/iaas/Content/Compute/Tasks/gettingmetadata.htm |
| 27 | +const ( |
| 28 | + // OracleHostnameEndpoint is the local metadata endpoint for the hostname. |
| 29 | + OracleHostnameEndpoint = "http://169.254.169.254/opc/v2/instance/hostname" |
| 30 | + // OracleUserDataEndpoint is the local metadata endpoint inside of Oracle Cloud. |
| 31 | + OracleUserDataEndpoint = "http://169.254.169.254/opc/v2/instance/metadata/user_data" |
| 32 | + // OracleNetworkEndpoint is the local network metadata endpoint inside of Oracle Cloud. |
| 33 | + OracleNetworkEndpoint = "http://169.254.169.254/opc/v2/vnics/" |
| 34 | +) |
| 35 | + |
| 36 | +// NetworkConfig holds network interface meta config. |
| 37 | +type NetworkConfig struct { |
| 38 | + HWAddr string `json:"macAddr"` |
| 39 | + PrivateIP string `json:"privateIp"` |
| 40 | + VirtualRouterIP string `json:"virtualRouterIp"` |
| 41 | + SubnetCidrBlock string `json:"subnetCidrBlock"` |
| 42 | + Ipv6SubnetCidrBlock string `json:"ipv6SubnetCidrBlock,omitempty"` |
| 43 | + Ipv6VirtualRouterIP string `json:"ipv6VirtualRouterIp,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +// Oracle is the concrete type that implements the platform.Platform interface. |
| 47 | +type Oracle struct{} |
| 48 | + |
| 49 | +// Name implements the platform.Platform interface. |
| 50 | +func (o *Oracle) Name() string { |
| 51 | + return "oracle" |
| 52 | +} |
| 53 | + |
| 54 | +// ConfigurationNetwork implements the network configuration interface. |
| 55 | +func (o *Oracle) ConfigurationNetwork(metadataNetworkConfig []byte, confProvider config.Provider) (config.Provider, error) { |
| 56 | + var machineConfig *v1alpha1.Config |
| 57 | + |
| 58 | + machineConfig, ok := confProvider.(*v1alpha1.Config) |
| 59 | + if !ok { |
| 60 | + return nil, fmt.Errorf("unable to determine machine config type") |
| 61 | + } |
| 62 | + |
| 63 | + if machineConfig.MachineConfig == nil { |
| 64 | + machineConfig.MachineConfig = &v1alpha1.MachineConfig{} |
| 65 | + } |
| 66 | + |
| 67 | + if machineConfig.MachineConfig.MachineNetwork == nil { |
| 68 | + machineConfig.MachineConfig.MachineNetwork = &v1alpha1.NetworkConfig{} |
| 69 | + } |
| 70 | + |
| 71 | + var interfaceAddresses []NetworkConfig |
| 72 | + |
| 73 | + if err := json.Unmarshal(metadataNetworkConfig, &interfaceAddresses); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + |
| 77 | + if machineConfig.MachineConfig.MachineNetwork.NetworkInterfaces == nil { |
| 78 | + for idx, iface := range interfaceAddresses { |
| 79 | + ipv6 := iface.Ipv6SubnetCidrBlock != "" && iface.Ipv6VirtualRouterIP != "" |
| 80 | + |
| 81 | + if ipv6 { |
| 82 | + device := &v1alpha1.Device{ |
| 83 | + DeviceInterface: fmt.Sprintf("eth%d", idx), |
| 84 | + DeviceDHCP: true, |
| 85 | + DeviceDHCPOptions: &v1alpha1.DHCPOptions{DHCPIPv6: pointer.ToBool(true)}, |
| 86 | + } |
| 87 | + |
| 88 | + machineConfig.MachineConfig.MachineNetwork.NetworkInterfaces = append(machineConfig.MachineConfig.MachineNetwork.NetworkInterfaces, device) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return confProvider, nil |
| 94 | +} |
| 95 | + |
| 96 | +// Configuration implements the platform.Platform interface. |
| 97 | +func (o *Oracle) Configuration(ctx context.Context) ([]byte, error) { |
| 98 | + log.Printf("fetching network config from %q", OracleNetworkEndpoint) |
| 99 | + |
| 100 | + metadataNetworkConfig, err := download.Download(ctx, OracleNetworkEndpoint, |
| 101 | + download.WithHeaders(map[string]string{"Authorization": "Bearer Oracle"})) |
| 102 | + if err != nil { |
| 103 | + return nil, fmt.Errorf("failed to fetch network config from metadata service") |
| 104 | + } |
| 105 | + |
| 106 | + log.Printf("fetching machine config from: %q", OracleUserDataEndpoint) |
| 107 | + |
| 108 | + machineConfigDl, err := download.Download(ctx, OracleUserDataEndpoint, |
| 109 | + download.WithHeaders(map[string]string{"Authorization": "Bearer Oracle"}), |
| 110 | + download.WithErrorOnNotFound(errors.ErrNoConfigSource), |
| 111 | + download.WithErrorOnEmptyResponse(errors.ErrNoConfigSource)) |
| 112 | + if err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + machineConfig, err := base64.StdEncoding.DecodeString(string(machineConfigDl)) |
| 117 | + if err != nil { |
| 118 | + return nil, errors.ErrNoConfigSource |
| 119 | + } |
| 120 | + |
| 121 | + confProvider, err := configloader.NewFromBytes(machineConfig) |
| 122 | + if err != nil { |
| 123 | + return nil, fmt.Errorf("error parsing machine config: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + confProvider, err = o.ConfigurationNetwork(metadataNetworkConfig, confProvider) |
| 127 | + if err != nil { |
| 128 | + return nil, err |
| 129 | + } |
| 130 | + |
| 131 | + return confProvider.Bytes() |
| 132 | +} |
| 133 | + |
| 134 | +// Hostname implements the platform.Platform interface. |
| 135 | +func (o *Oracle) Hostname(ctx context.Context) (hostname []byte, err error) { |
| 136 | + log.Printf("fetching hostname from: %q", OracleHostnameEndpoint) |
| 137 | + |
| 138 | + hostname, err = download.Download(ctx, OracleHostnameEndpoint, |
| 139 | + download.WithHeaders(map[string]string{"Authorization": "Bearer Oracle"}), |
| 140 | + download.WithErrorOnNotFound(errors.ErrNoHostname), |
| 141 | + download.WithErrorOnEmptyResponse(errors.ErrNoHostname)) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + |
| 146 | + return hostname, nil |
| 147 | +} |
| 148 | + |
| 149 | +// Mode implements the platform.Platform interface. |
| 150 | +func (o *Oracle) Mode() runtime.Mode { |
| 151 | + return runtime.ModeCloud |
| 152 | +} |
| 153 | + |
| 154 | +// ExternalIPs implements the runtime.Platform interface. |
| 155 | +func (o *Oracle) ExternalIPs(ctx context.Context) (addrs []net.IP, err error) { |
| 156 | + return nil, nil |
| 157 | +} |
| 158 | + |
| 159 | +// KernelArgs implements the runtime.Platform interface. |
| 160 | +func (o *Oracle) KernelArgs() procfs.Parameters { |
| 161 | + return []*procfs.Parameter{ |
| 162 | + procfs.NewParameter("console").Append("tty1").Append("ttyS0"), |
| 163 | + } |
| 164 | +} |
0 commit comments