|
15 | 15 | package config |
16 | 16 |
|
17 | 17 | import ( |
18 | | - "fmt" |
19 | | - |
20 | 18 | "github.com/coreos/butane/config/common" |
21 | 19 | fcos1_0 "github.com/coreos/butane/config/fcos/v1_0" |
22 | 20 | fcos1_1 "github.com/coreos/butane/config/fcos/v1_1" |
@@ -48,108 +46,40 @@ import ( |
48 | 46 | r4e1_1 "github.com/coreos/butane/config/r4e/v1_1" |
49 | 47 | r4e1_2_exp "github.com/coreos/butane/config/r4e/v1_2_exp" |
50 | 48 |
|
51 | | - "github.com/coreos/go-semver/semver" |
52 | 49 | "github.com/coreos/vcontext/report" |
53 | | - "gopkg.in/yaml.v3" |
54 | | -) |
55 | | - |
56 | | -var ( |
57 | | - registry = map[string]translator{} |
58 | 50 | ) |
59 | 51 |
|
60 | | -// Fields that must be included in the root struct of every spec version. |
61 | | -type commonFields struct { |
62 | | - Version string `yaml:"version"` |
63 | | - Variant string `yaml:"variant"` |
64 | | -} |
65 | | - |
66 | 52 | func init() { |
67 | | - RegisterTranslator("fcos", "1.0.0", fcos1_0.ToIgn3_0Bytes) |
68 | | - RegisterTranslator("fcos", "1.1.0", fcos1_1.ToIgn3_1Bytes) |
69 | | - RegisterTranslator("fcos", "1.2.0", fcos1_2.ToIgn3_2Bytes) |
70 | | - RegisterTranslator("fcos", "1.3.0", fcos1_3.ToIgn3_2Bytes) |
71 | | - RegisterTranslator("fcos", "1.4.0", fcos1_4.ToIgn3_3Bytes) |
72 | | - RegisterTranslator("fcos", "1.5.0", fcos1_5.ToIgn3_4Bytes) |
73 | | - RegisterTranslator("fcos", "1.6.0", fcos1_6.ToIgn3_5Bytes) |
74 | | - RegisterTranslator("fcos", "1.7.0-experimental", fcos1_7_exp.ToIgn3_6Bytes) |
75 | | - RegisterTranslator("flatcar", "1.0.0", flatcar1_0.ToIgn3_3Bytes) |
76 | | - RegisterTranslator("flatcar", "1.1.0", flatcar1_1.ToIgn3_4Bytes) |
77 | | - RegisterTranslator("flatcar", "1.2.0-experimental", flatcar1_2_exp.ToIgn3_6Bytes) |
78 | | - RegisterTranslator("openshift", "4.8.0", openshift4_8.ToConfigBytes) |
79 | | - RegisterTranslator("openshift", "4.9.0", openshift4_9.ToConfigBytes) |
80 | | - RegisterTranslator("openshift", "4.10.0", openshift4_10.ToConfigBytes) |
81 | | - RegisterTranslator("openshift", "4.11.0", openshift4_11.ToConfigBytes) |
82 | | - RegisterTranslator("openshift", "4.12.0", openshift4_12.ToConfigBytes) |
83 | | - RegisterTranslator("openshift", "4.13.0", openshift4_13.ToConfigBytes) |
84 | | - RegisterTranslator("openshift", "4.14.0", openshift4_14.ToConfigBytes) |
85 | | - RegisterTranslator("openshift", "4.15.0", openshift4_15.ToConfigBytes) |
86 | | - RegisterTranslator("openshift", "4.16.0", openshift4_16.ToConfigBytes) |
87 | | - RegisterTranslator("openshift", "4.17.0", openshift4_17.ToConfigBytes) |
88 | | - RegisterTranslator("openshift", "4.18.0", openshift4_18.ToConfigBytes) |
89 | | - RegisterTranslator("openshift", "4.19.0", openshift4_19.ToConfigBytes) |
90 | | - RegisterTranslator("openshift", "4.20.0-experimental", openshift4_20_exp.ToConfigBytes) |
91 | | - RegisterTranslator("r4e", "1.0.0", r4e1_0.ToIgn3_3Bytes) |
92 | | - RegisterTranslator("r4e", "1.1.0", r4e1_1.ToIgn3_4Bytes) |
93 | | - RegisterTranslator("r4e", "1.2.0-experimental", r4e1_2_exp.ToIgn3_6Bytes) |
94 | | - RegisterTranslator("fiot", "1.0.0", fiot1_0.ToIgn3_4Bytes) |
95 | | - RegisterTranslator("fiot", "1.1.0-experimental", fiot1_1_exp.ToIgn3_6Bytes) |
96 | | - RegisterTranslator("rhcos", "0.1.0", unsupportedRhcosVariant) |
97 | | -} |
98 | | - |
99 | | -// RegisterTranslator registers a translator for the specified variant and |
100 | | -// version to be available for use by TranslateBytes. This is only needed |
101 | | -// by users implementing their own translators outside the Butane package. |
102 | | -func RegisterTranslator(variant, version string, trans translator) { |
103 | | - key := fmt.Sprintf("%s+%s", variant, version) |
104 | | - if _, ok := registry[key]; ok { |
105 | | - panic("tried to reregister existing translator") |
106 | | - } |
107 | | - registry[key] = trans |
108 | | -} |
109 | | - |
110 | | -func getTranslator(variant string, version semver.Version) (translator, error) { |
111 | | - t, ok := registry[fmt.Sprintf("%s+%s", variant, version.String())] |
112 | | - if !ok { |
113 | | - return nil, common.ErrUnknownVersion{ |
114 | | - Variant: variant, |
115 | | - Version: version, |
116 | | - } |
117 | | - } |
118 | | - return t, nil |
119 | | -} |
120 | | - |
121 | | -// translators take a raw config and translate it to a raw Ignition config. The report returned should include any |
122 | | -// errors, warnings, etc. and may or may not be fatal. If report is fatal, or other errors are encountered while translating |
123 | | -// translators should return an error. |
124 | | -type translator func([]byte, common.TranslateBytesOptions) ([]byte, report.Report, error) |
125 | | - |
126 | | -// TranslateBytes wraps all of the individual TranslateBytes functions in a switch that determines the correct one to call. |
127 | | -// TranslateBytes returns an error if the report had fatal errors or if other errors occured during translation. |
128 | | -func TranslateBytes(input []byte, options common.TranslateBytesOptions) ([]byte, report.Report, error) { |
129 | | - // first determine version; this will ignore most fields |
130 | | - ver := commonFields{} |
131 | | - if err := yaml.Unmarshal(input, &ver); err != nil { |
132 | | - return nil, report.Report{}, common.ErrUnmarshal{ |
133 | | - Detail: err.Error(), |
134 | | - } |
135 | | - } |
136 | | - |
137 | | - if ver.Variant == "" { |
138 | | - return nil, report.Report{}, common.ErrNoVariant |
139 | | - } |
140 | | - |
141 | | - tmp, err := semver.NewVersion(ver.Version) |
142 | | - if err != nil { |
143 | | - return nil, report.Report{}, common.ErrInvalidVersion |
144 | | - } |
145 | | - version := *tmp |
146 | | - |
147 | | - translator, err := getTranslator(ver.Variant, version) |
148 | | - if err != nil { |
149 | | - return nil, report.Report{}, err |
150 | | - } |
151 | | - |
152 | | - return translator(input, options) |
| 53 | + common.RegisterTranslator("fcos", "1.0.0", fcos1_0.ToIgn3_0Bytes) |
| 54 | + common.RegisterTranslator("fcos", "1.1.0", fcos1_1.ToIgn3_1Bytes) |
| 55 | + common.RegisterTranslator("fcos", "1.2.0", fcos1_2.ToIgn3_2Bytes) |
| 56 | + common.RegisterTranslator("fcos", "1.3.0", fcos1_3.ToIgn3_2Bytes) |
| 57 | + common.RegisterTranslator("fcos", "1.4.0", fcos1_4.ToIgn3_3Bytes) |
| 58 | + common.RegisterTranslator("fcos", "1.5.0", fcos1_5.ToIgn3_4Bytes) |
| 59 | + common.RegisterTranslator("fcos", "1.6.0", fcos1_6.ToIgn3_5Bytes) |
| 60 | + common.RegisterTranslator("fcos", "1.7.0-experimental", fcos1_7_exp.ToIgn3_6Bytes) |
| 61 | + common.RegisterTranslator("flatcar", "1.0.0", flatcar1_0.ToIgn3_3Bytes) |
| 62 | + common.RegisterTranslator("flatcar", "1.1.0", flatcar1_1.ToIgn3_4Bytes) |
| 63 | + common.RegisterTranslator("flatcar", "1.2.0-experimental", flatcar1_2_exp.ToIgn3_6Bytes) |
| 64 | + common.RegisterTranslator("openshift", "4.8.0", openshift4_8.ToConfigBytes) |
| 65 | + common.RegisterTranslator("openshift", "4.9.0", openshift4_9.ToConfigBytes) |
| 66 | + common.RegisterTranslator("openshift", "4.10.0", openshift4_10.ToConfigBytes) |
| 67 | + common.RegisterTranslator("openshift", "4.11.0", openshift4_11.ToConfigBytes) |
| 68 | + common.RegisterTranslator("openshift", "4.12.0", openshift4_12.ToConfigBytes) |
| 69 | + common.RegisterTranslator("openshift", "4.13.0", openshift4_13.ToConfigBytes) |
| 70 | + common.RegisterTranslator("openshift", "4.14.0", openshift4_14.ToConfigBytes) |
| 71 | + common.RegisterTranslator("openshift", "4.15.0", openshift4_15.ToConfigBytes) |
| 72 | + common.RegisterTranslator("openshift", "4.16.0", openshift4_16.ToConfigBytes) |
| 73 | + common.RegisterTranslator("openshift", "4.17.0", openshift4_17.ToConfigBytes) |
| 74 | + common.RegisterTranslator("openshift", "4.18.0", openshift4_18.ToConfigBytes) |
| 75 | + common.RegisterTranslator("openshift", "4.19.0", openshift4_19.ToConfigBytes) |
| 76 | + common.RegisterTranslator("openshift", "4.20.0-experimental", openshift4_20_exp.ToConfigBytes) |
| 77 | + common.RegisterTranslator("r4e", "1.0.0", r4e1_0.ToIgn3_3Bytes) |
| 78 | + common.RegisterTranslator("r4e", "1.1.0", r4e1_1.ToIgn3_4Bytes) |
| 79 | + common.RegisterTranslator("r4e", "1.2.0-experimental", r4e1_2_exp.ToIgn3_6Bytes) |
| 80 | + common.RegisterTranslator("fiot", "1.0.0", fiot1_0.ToIgn3_4Bytes) |
| 81 | + common.RegisterTranslator("fiot", "1.1.0-experimental", fiot1_1_exp.ToIgn3_6Bytes) |
| 82 | + common.RegisterTranslator("rhcos", "0.1.0", unsupportedRhcosVariant) |
153 | 83 | } |
154 | 84 |
|
155 | 85 | func unsupportedRhcosVariant(input []byte, options common.TranslateBytesOptions) ([]byte, report.Report, error) { |
|
0 commit comments