@@ -58,6 +58,7 @@ type Config struct {
5858 Token string
5959 ServerType string
6060 Datacenter string
61+ Image string
6162 Location string
6263 Networks []string
6364 Labels map [string ]string
@@ -97,25 +98,35 @@ func (p *provider) getConfig(s v1alpha1.ProviderSpec) (*Config, *providerconfigt
9798 if err != nil {
9899 return nil , nil , fmt .Errorf ("failed to get the value of \" token\" field, error = %v" , err )
99100 }
101+
100102 c .ServerType , err = p .configVarResolver .GetConfigVarStringValue (rawConfig .ServerType )
101103 if err != nil {
102104 return nil , nil , err
103105 }
106+
104107 c .Datacenter , err = p .configVarResolver .GetConfigVarStringValue (rawConfig .Datacenter )
105108 if err != nil {
106109 return nil , nil , err
107110 }
111+
112+ c .Image , err = p .configVarResolver .GetConfigVarStringValue (rawConfig .Image )
113+ if err != nil {
114+ return nil , nil , err
115+ }
116+
108117 c .Location , err = p .configVarResolver .GetConfigVarStringValue (rawConfig .Location )
109118 if err != nil {
110119 return nil , nil , err
111120 }
121+
112122 for _ , network := range rawConfig .Networks {
113123 networkValue , err := p .configVarResolver .GetConfigVarStringValue (network )
114124 if err != nil {
115125 return nil , nil , err
116126 }
117127 c .Networks = append (c .Networks , networkValue )
118128 }
129+
119130 c .Labels = rawConfig .Labels
120131 return & c , & pconfig , err
121132}
@@ -154,6 +165,12 @@ func (p *provider) Validate(spec v1alpha1.MachineSpec) error {
154165 }
155166 }
156167
168+ if c .Image != "" {
169+ if _ , _ , err = client .Image .Get (ctx , c .Image ); err != nil {
170+ return fmt .Errorf ("failed to get image: %v" , err )
171+ }
172+ }
173+
157174 if len (c .Networks ) != 0 {
158175 for _ , network := range c .Networks {
159176 if _ , _ , err = client .Network .Get (ctx , network ); err != nil {
@@ -181,17 +198,21 @@ func (p *provider) Create(machine *v1alpha1.Machine, _ *cloudprovidertypes.Provi
181198 ctx := context .TODO ()
182199 client := getClient (c .Token )
183200
184- imageName , err := getNameForOS (pc .OperatingSystem )
185- if err != nil {
186- return nil , cloudprovidererrors.TerminalError {
187- Reason : common .InvalidConfigurationMachineError ,
188- Message : fmt .Sprintf ("Invalid operating system specified %q, details = %v" , pc .OperatingSystem , err ),
201+ if c .Image == "" {
202+ imageName , err := getNameForOS (pc .OperatingSystem )
203+ if err != nil {
204+ return nil , cloudprovidererrors.TerminalError {
205+ Reason : common .InvalidConfigurationMachineError ,
206+ Message : fmt .Sprintf ("Invalid operating system specified %q, details = %v" , pc .OperatingSystem , err ),
207+ }
189208 }
209+ c .Image = imageName
190210 }
191211
192212 if c .Labels == nil {
193213 c .Labels = map [string ]string {}
194214 }
215+
195216 c .Labels [machineUIDLabelKey ] = string (machine .UID )
196217 serverCreateOpts := hcloud.ServerCreateOpts {
197218 Name : machine .Spec .Name ,
@@ -224,7 +245,7 @@ func (p *provider) Create(machine *v1alpha1.Machine, _ *cloudprovidertypes.Provi
224245 }
225246 }
226247
227- serverCreateOpts .Image , _ , err = client .Image .Get (ctx , imageName )
248+ serverCreateOpts .Image , _ , err = client .Image .Get (ctx , c . Image )
228249 if err != nil {
229250 return nil , hzErrorToTerminalError (err , "failed to get image" )
230251 }
0 commit comments