Skip to content

Commit 7fac51d

Browse files
authored
new hetzner field "image" (#817)
Signed-off-by: Artiom Diomin <[email protected]>
1 parent 697bee0 commit 7fac51d

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

examples/hetzner-machinedeployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ spec:
4747
# Optional
4848
datacenter: ""
4949
location: "fsn1"
50+
image: "ubuntu-20.04"
5051
# Optional: network IDs or names
5152
networks:
5253
- "<< YOUR_NETWORK >>"

pkg/cloudprovider/provider/hetzner/provider.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

pkg/cloudprovider/provider/hetzner/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type RawConfig struct {
2424
Token providerconfigtypes.ConfigVarString `json:"token,omitempty"`
2525
ServerType providerconfigtypes.ConfigVarString `json:"serverType"`
2626
Datacenter providerconfigtypes.ConfigVarString `json:"datacenter"`
27+
Image providerconfigtypes.ConfigVarString `json:"image"`
2728
Location providerconfigtypes.ConfigVarString `json:"location"`
2829
Networks []providerconfigtypes.ConfigVarString `json:"networks"`
2930
Labels map[string]string `json:"labels,omitempty"`

0 commit comments

Comments
 (0)