|
| 1 | +// Copyright 2020 Red Hat |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// The oraclecloud provider fetches a remote configuration from Oracle's |
| 16 | +// Instance Metadata Service v2. |
| 17 | +// https://docs.oracle.com/en-us/iaas/Content/Compute/Tasks/gettingmetadata.htm |
| 18 | + |
| 19 | +package oraclecloud |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/base64" |
| 23 | + "fmt" |
| 24 | + "net/http" |
| 25 | + "net/url" |
| 26 | + |
| 27 | + "github.com/coreos/ignition/v2/config/v3_6_experimental/types" |
| 28 | + "github.com/coreos/ignition/v2/internal/platform" |
| 29 | + "github.com/coreos/ignition/v2/internal/providers/util" |
| 30 | + "github.com/coreos/ignition/v2/internal/resource" |
| 31 | + |
| 32 | + "github.com/coreos/vcontext/report" |
| 33 | +) |
| 34 | + |
| 35 | +var userdataUrl = url.URL{ |
| 36 | + Scheme: "http", |
| 37 | + Host: "169.254.169.254", |
| 38 | + Path: "opc/v2/instance/metadata/user_data", |
| 39 | +} |
| 40 | + |
| 41 | +func init() { |
| 42 | + platform.Register(platform.Provider{ |
| 43 | + Name: "oraclecloud", |
| 44 | + Fetch: fetchConfig, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func fetchConfig(f *resource.Fetcher) (types.Config, report.Report, error) { |
| 49 | + data, err := f.FetchToBuffer(userdataUrl, resource.FetchOptions{ |
| 50 | + Headers: http.Header{ |
| 51 | + "Authorization": []string{"Bearer Oracle"}, |
| 52 | + }, |
| 53 | + RetryCodes: []int{http.StatusTooManyRequests}, |
| 54 | + }) |
| 55 | + if err != nil && err != resource.ErrNotFound { |
| 56 | + return types.Config{}, report.Report{}, fmt.Errorf("fetching to buffer: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + userdata := make([]byte, base64.StdEncoding.DecodedLen(len(data))) |
| 60 | + size, err := base64.StdEncoding.Decode(userdata, data) |
| 61 | + if err != nil { |
| 62 | + return types.Config{}, report.Report{}, fmt.Errorf("decoding userdata: %w", err) |
| 63 | + } |
| 64 | + userdata = userdata[:size] |
| 65 | + |
| 66 | + return util.ParseConfig(f.Logger, userdata) |
| 67 | +} |
0 commit comments