Skip to content

Commit 0671951

Browse files
Merge pull request openshift#7531 from bfournie/agent-install-config-hosts
AGENT-713: Use BM hosts in install-config if not defined in agent-config
2 parents 8b1b663 + b44d792 commit 0671951

File tree

12 files changed

+1225
-911
lines changed

12 files changed

+1225
-911
lines changed

pkg/agent/rest.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func NewNodeZeroRestClient(ctx context.Context, assetDir string) (*NodeZeroRestC
3737
agentConfigAsset := &agentconfig.AgentConfig{}
3838
agentManifestsAsset := &manifests.AgentManifests{}
3939
installConfigAsset := &installconfig.InstallConfig{}
40+
agentHostsAsset := &agentconfig.AgentHosts{}
4041

4142
assetStore, err := assetstore.NewStore(assetDir)
4243
if err != nil {
@@ -46,6 +47,7 @@ func NewNodeZeroRestClient(ctx context.Context, assetDir string) (*NodeZeroRestC
4647
agentConfig, agentConfigError := assetStore.Load(agentConfigAsset)
4748
agentManifests, manifestError := assetStore.Load(agentManifestsAsset)
4849
installConfig, installConfigError := assetStore.Load(installConfigAsset)
50+
agentHosts, agentHostsError := assetStore.Load(agentHostsAsset)
4951

5052
if agentConfigError != nil {
5153
logrus.Debug(errors.Wrapf(agentConfigError, "failed to load %s", agentConfigAsset.Name()))
@@ -56,20 +58,23 @@ func NewNodeZeroRestClient(ctx context.Context, assetDir string) (*NodeZeroRestC
5658
if installConfigError != nil {
5759
logrus.Debug(errors.Wrapf(installConfigError, "failed to load %s", installConfigAsset.Name()))
5860
}
59-
if agentConfigError != nil || manifestError != nil || installConfigError != nil {
60-
return nil, errors.New("failed to load AgentConfig, NMStateConfig, or InstallConfig")
61+
if agentHostsError != nil {
62+
logrus.Debug(errors.Wrapf(agentConfigError, "failed to load %s", agentHostsAsset.Name()))
63+
}
64+
if agentConfigError != nil || manifestError != nil || installConfigError != nil || agentHostsError != nil {
65+
return nil, errors.New("failed to load AgentConfig, NMStateConfig, InstallConfig, or AgentHosts")
6166
}
6267

6368
var RendezvousIP string
6469
var rendezvousIPError error
6570
var emptyNMStateConfigs []*v1beta1.NMStateConfig
6671

6772
if agentConfig != nil && agentManifests != nil {
68-
RendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(agentConfig.(*agentconfig.AgentConfig).Config, agentManifests.(*manifests.AgentManifests).NMStateConfigs)
73+
RendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(agentConfig.(*agentconfig.AgentConfig).Config, agentHosts.(*agentconfig.AgentHosts).Hosts, agentManifests.(*manifests.AgentManifests).NMStateConfigs)
6974
} else if agentConfig == nil && agentManifests != nil {
70-
RendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(&agent.Config{}, agentManifests.(*manifests.AgentManifests).NMStateConfigs)
75+
RendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(&agent.Config{}, agentHosts.(*agentconfig.AgentHosts).Hosts, agentManifests.(*manifests.AgentManifests).NMStateConfigs)
7176
} else if agentConfig != nil && agentManifests == nil {
72-
RendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(agentConfig.(*agentconfig.AgentConfig).Config, emptyNMStateConfigs)
77+
RendezvousIP, rendezvousIPError = image.RetrieveRendezvousIP(agentConfig.(*agentconfig.AgentConfig).Config, agentHosts.(*agentconfig.AgentHosts).Hosts, emptyNMStateConfigs)
7378
} else {
7479
return nil, errors.New("both AgentConfig and NMStateConfig are empty")
7580
}

pkg/asset/agent/agentconfig/agent_config.go

Lines changed: 3 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
"strings"
87

98
"github.com/pkg/errors"
109
"k8s.io/apimachinery/pkg/util/validation/field"
@@ -13,7 +12,6 @@ import (
1312
"github.com/openshift/installer/pkg/asset"
1413
"github.com/openshift/installer/pkg/types/agent"
1514
"github.com/openshift/installer/pkg/types/agent/conversion"
16-
"github.com/openshift/installer/pkg/types/baremetal/validation"
1715
"github.com/openshift/installer/pkg/validate"
1816
)
1917

@@ -43,7 +41,6 @@ func (*AgentConfig) Dependencies() []asset.Asset {
4341

4442
// Generate generates the Agent Config manifest.
4543
func (a *AgentConfig) Generate(dependencies asset.Parents) error {
46-
4744
// TODO: We are temporarily generating a template of the agent-config.yaml
4845
// Change this when its interactive survey is implemented.
4946
agentConfigTemplate := `#
@@ -103,7 +100,7 @@ hosts:
103100
return nil
104101
}
105102

106-
// PersistToFile writes the agent-config.yaml file to the assets folder
103+
// PersistToFile writes the agent-config.yaml file to the assets folder.
107104
func (a *AgentConfig) PersistToFile(directory string) error {
108105
templatePath := filepath.Join(directory, agentConfigFilename)
109106
templateByte := []byte(a.Template)
@@ -126,7 +123,6 @@ func (a *AgentConfig) Files() []*asset.File {
126123

127124
// Load returns agent config asset from the disk.
128125
func (a *AgentConfig) Load(f asset.FileFetcher) (bool, error) {
129-
130126
file, err := f.FetchByName(agentConfigFilename)
131127
if err != nil {
132128
if os.IsNotExist(err) {
@@ -146,6 +142,7 @@ func (a *AgentConfig) Load(f asset.FileFetcher) (bool, error) {
146142
}
147143

148144
a.File, a.Config = file, config
145+
149146
if err = a.finish(); err != nil {
150147
return false, err
151148
}
@@ -168,18 +165,10 @@ func (a *AgentConfig) validateAgent() field.ErrorList {
168165
allErrs = append(allErrs, err...)
169166
}
170167

171-
if err := a.validateHosts(); err != nil {
172-
allErrs = append(allErrs, err...)
173-
}
174-
175168
if err := a.validateAdditionalNTPSources(field.NewPath("AdditionalNTPSources"), a.Config.AdditionalNTPSources); err != nil {
176169
allErrs = append(allErrs, err...)
177170
}
178171

179-
if err := a.validateRendezvousIPNotWorker(a.Config.RendezvousIP, a.Config.Hosts); err != nil {
180-
allErrs = append(allErrs, err...)
181-
}
182-
183172
if err := a.validateBootArtifactsBaseURL(); err != nil {
184173
allErrs = append(allErrs, err...)
185174
}
@@ -192,7 +181,7 @@ func (a *AgentConfig) validateRendezvousIP() field.ErrorList {
192181

193182
rendezvousIPPath := field.NewPath("rendezvousIP")
194183

195-
//empty rendezvous ip is fine
184+
// empty rendezvous ip is fine
196185
if a.Config.RendezvousIP == "" {
197186
return nil
198187
}
@@ -204,86 +193,6 @@ func (a *AgentConfig) validateRendezvousIP() field.ErrorList {
204193
return allErrs
205194
}
206195

207-
func (a *AgentConfig) validateHosts() field.ErrorList {
208-
var allErrs field.ErrorList
209-
210-
macs := make(map[string]bool)
211-
for i, host := range a.Config.Hosts {
212-
213-
hostPath := field.NewPath("Hosts").Index(i)
214-
215-
if err := a.validateHostInterfaces(hostPath, host, macs); err != nil {
216-
allErrs = append(allErrs, err...)
217-
}
218-
219-
if err := a.validateHostRootDeviceHints(hostPath, host); err != nil {
220-
allErrs = append(allErrs, err...)
221-
}
222-
223-
if err := a.validateRoles(hostPath, host); err != nil {
224-
allErrs = append(allErrs, err...)
225-
}
226-
}
227-
228-
return allErrs
229-
}
230-
231-
func (a *AgentConfig) validateHostInterfaces(hostPath *field.Path, host agent.Host, macs map[string]bool) field.ErrorList {
232-
var allErrs field.ErrorList
233-
234-
interfacePath := hostPath.Child("Interfaces")
235-
if len(host.Interfaces) == 0 {
236-
allErrs = append(allErrs, field.Required(interfacePath, "at least one interface must be defined for each node"))
237-
}
238-
239-
for j := range host.Interfaces {
240-
mac := host.Interfaces[j].MacAddress
241-
macAddressPath := interfacePath.Index(j).Child("macAddress")
242-
243-
if mac == "" {
244-
allErrs = append(allErrs, field.Required(macAddressPath, "each interface must have a MAC address defined"))
245-
continue
246-
}
247-
248-
if err := validate.MAC(mac); err != nil {
249-
allErrs = append(allErrs, field.Invalid(macAddressPath, mac, err.Error()))
250-
}
251-
252-
if _, ok := macs[mac]; ok {
253-
allErrs = append(allErrs, field.Invalid(macAddressPath, mac, "duplicate MAC address found"))
254-
}
255-
macs[mac] = true
256-
}
257-
258-
return allErrs
259-
}
260-
261-
func (a *AgentConfig) validateHostRootDeviceHints(hostPath *field.Path, host agent.Host) field.ErrorList {
262-
rdhPath := hostPath.Child("rootDeviceHints")
263-
allErrs := validation.ValidateHostRootDeviceHints(&host.RootDeviceHints, rdhPath)
264-
265-
if host.RootDeviceHints.WWNWithExtension != "" {
266-
allErrs = append(allErrs, field.Forbidden(
267-
rdhPath.Child("wwnWithExtension"), "WWN extensions are not supported in root device hints"))
268-
}
269-
270-
if host.RootDeviceHints.WWNVendorExtension != "" {
271-
allErrs = append(allErrs, field.Forbidden(rdhPath.Child("wwnVendorExtension"), "WWN vendor extensions are not supported in root device hints"))
272-
}
273-
274-
return allErrs
275-
}
276-
277-
func (a *AgentConfig) validateRoles(hostPath *field.Path, host agent.Host) field.ErrorList {
278-
var allErrs field.ErrorList
279-
280-
if len(host.Role) > 0 && host.Role != "master" && host.Role != "worker" {
281-
allErrs = append(allErrs, field.Forbidden(hostPath.Child("Host"), "host role has incorrect value. Role must either be 'master' or 'worker'"))
282-
}
283-
284-
return allErrs
285-
}
286-
287196
func (a *AgentConfig) validateAdditionalNTPSources(additionalNTPSourcesPath *field.Path, sources []string) field.ErrorList {
288197
var allErrs field.ErrorList
289198

@@ -300,21 +209,6 @@ func (a *AgentConfig) validateAdditionalNTPSources(additionalNTPSourcesPath *fie
300209
return allErrs
301210
}
302211

303-
func (a *AgentConfig) validateRendezvousIPNotWorker(rendezvousIP string, hosts []agent.Host) field.ErrorList {
304-
var allErrs field.ErrorList
305-
306-
if rendezvousIP != "" {
307-
for i, host := range hosts {
308-
hostPath := field.NewPath("Hosts").Index(i)
309-
if strings.Contains(string(host.NetworkConfig.Raw), rendezvousIP) && host.Role == "worker" {
310-
errMsg := "Host " + host.Hostname + " has role 'worker' and has the rendezvousIP assigned to it. The rendezvousIP must be assigned to a control plane host."
311-
allErrs = append(allErrs, field.Forbidden(hostPath.Child("Host"), errMsg))
312-
}
313-
}
314-
}
315-
return allErrs
316-
}
317-
318212
func (a *AgentConfig) validateBootArtifactsBaseURL() field.ErrorList {
319213
var allErrs field.ErrorList
320214

@@ -332,48 +226,6 @@ func (a *AgentConfig) validateBootArtifactsBaseURL() field.ErrorList {
332226
return allErrs
333227
}
334228

335-
// HostConfigFileMap is a map from a filepath ("<host>/<file>") to file content
336-
// for hostconfig files.
337-
type HostConfigFileMap map[string][]byte
338-
339-
// HostConfigFiles returns a map from filename to contents of the files used for
340-
// host-specific configuration by the agent installer client
341-
func (a *AgentConfig) HostConfigFiles() (HostConfigFileMap, error) {
342-
if a == nil || a.Config == nil {
343-
return nil, nil
344-
}
345-
346-
files := HostConfigFileMap{}
347-
for i, host := range a.Config.Hosts {
348-
name := fmt.Sprintf("host-%d", i)
349-
if host.Hostname != "" {
350-
name = host.Hostname
351-
}
352-
353-
macs := []string{}
354-
for _, iface := range host.Interfaces {
355-
macs = append(macs, strings.ToLower(iface.MacAddress)+"\n")
356-
}
357-
358-
if len(macs) > 0 {
359-
files[filepath.Join(name, "mac_addresses")] = []byte(strings.Join(macs, ""))
360-
}
361-
362-
rdh, err := yaml.Marshal(host.RootDeviceHints)
363-
if err != nil {
364-
return nil, err
365-
}
366-
if len(rdh) > 0 && string(rdh) != "{}\n" {
367-
files[filepath.Join(name, "root-device-hints.yaml")] = rdh
368-
}
369-
370-
if len(host.Role) > 0 {
371-
files[filepath.Join(name, "role")] = []byte(host.Role)
372-
}
373-
}
374-
return files, nil
375-
}
376-
377229
func unmarshalJSON(b []byte) []byte {
378230
output, _ := yaml.JSONToYAML(b)
379231
return output

0 commit comments

Comments
 (0)