Skip to content

Commit

Permalink
asset/machines/worker: allow adding MachineConfigs for compute machin…
Browse files Browse the repository at this point in the history
…epools

This sets up the worker machine asset to allow adding MachineConfigs. A list of machine configs can be added to compute machinepools
that will be created alongside the user-data and machineset objects.
  • Loading branch information
abhinavdahiya committed Mar 25, 2019
1 parent 6723c88 commit 3ad4777
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
58 changes: 45 additions & 13 deletions pkg/asset/machines/worker.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package machines

import (
"bytes"
"fmt"
"text/template"
"path/filepath"

"github.com/ghodss/yaml"
"github.com/pkg/errors"
Expand All @@ -15,13 +14,23 @@ import (
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/machines/aws"
"github.com/openshift/installer/pkg/asset/machines/libvirt"
"github.com/openshift/installer/pkg/asset/machines/machineconfig"
"github.com/openshift/installer/pkg/asset/machines/openstack"
"github.com/openshift/installer/pkg/asset/rhcos"
awstypes "github.com/openshift/installer/pkg/types/aws"
awsdefaults "github.com/openshift/installer/pkg/types/aws/defaults"
libvirttypes "github.com/openshift/installer/pkg/types/libvirt"
nonetypes "github.com/openshift/installer/pkg/types/none"
openstacktypes "github.com/openshift/installer/pkg/types/openstack"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"
)

const (
// workerMachineSetFileName is the filename used for the worker MachineSet manifest.
workerMachineSetFileName = "99_openshift-cluster-api_worker-machineset.yaml"

// workerUserDataFileName is the filename used for the worker user-data secret.
workerUserDataFileName = "99_openshift-cluster-api_worker-user-data-secret.yaml"
)

func defaultAWSMachinePoolPlatform() awstypes.MachinePool {
Expand All @@ -45,8 +54,9 @@ func defaultOpenStackMachinePoolPlatform(flavor string) openstacktypes.MachinePo

// Worker generates the machinesets for `worker` machine pool.
type Worker struct {
MachineSetRaw []byte
UserDataSecretRaw []byte
UserDataFile *asset.File
MachineConfigFiles []*asset.File
MachineSetFile *asset.File
}

var _ asset.Asset = (*Worker)(nil)
Expand Down Expand Up @@ -85,13 +95,17 @@ func (w *Worker) Generate(dependencies asset.Parents) error {
wign := &machine.Worker{}
dependencies.Get(clusterID, installconfig, rhcosImage, wign)

var err error
userDataMap := map[string][]byte{"worker-user-data": wign.File.Data}
w.UserDataSecretRaw, err = userDataList(userDataMap)
data, err := userDataList(userDataMap)
if err != nil {
return errors.Wrap(err, "failed to create user-data secret for worker machines")
}
w.UserDataFile = &asset.File{
Filename: filepath.Join(directory, workerUserDataFileName),
Data: data,
}

machineConfigs := []*mcfgv1.MachineConfig{}
machineSets := []runtime.Object{}
ic := installconfig.Config
for _, pool := range ic.Compute {
Expand Down Expand Up @@ -147,6 +161,11 @@ func (w *Worker) Generate(dependencies asset.Parents) error {
}
}

w.MachineConfigFiles, err = machineconfig.Manifests(machineConfigs, "worker", directory)
if err != nil {
return errors.Wrap(err, "failed to create MachineConfig manifests for worker machines")
}

if len(machineSets) == 0 {
return nil
}
Expand All @@ -160,18 +179,31 @@ func (w *Worker) Generate(dependencies asset.Parents) error {
for i, set := range machineSets {
list.Items[i] = runtime.RawExtension{Object: set}
}
raw, err := yaml.Marshal(list)
data, err = yaml.Marshal(list)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
w.MachineSetRaw = raw
w.MachineSetFile = &asset.File{
Filename: filepath.Join(directory, workerMachineSetFileName),
Data: data,
}
return nil
}

func applyTemplateData(template *template.Template, templateData interface{}) []byte {
buf := &bytes.Buffer{}
if err := template.Execute(buf, templateData); err != nil {
panic(err)
// Files returns the files generated by the asset.
func (w *Worker) Files() []*asset.File {
files := make([]*asset.File, 0, 1+len(w.MachineConfigFiles)+1)
if w.UserDataFile != nil {
files = append(files, w.UserDataFile)
}
files = append(files, w.MachineConfigFiles...)
if w.MachineSetFile != nil {
files = append(files, w.MachineSetFile)
}
return buf.Bytes()
return files
}

// Load returns false since this asset is not written to disk by the installer.
func (w *Worker) Load(f asset.FileFetcher) (found bool, err error) {
return false, nil
}
7 changes: 3 additions & 4 deletions pkg/asset/manifests/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ func (o *Openshift) Generate(dependencies asset.Parents) error {
roleCloudCredsSecretReader)

assetData := map[string][]byte{
"99_binding-discovery.yaml": []byte(bindingDiscovery.Files()[0].Data),
"99_kubeadmin-password-secret.yaml": applyTemplateData(kubeadminPasswordSecret.Files()[0].Data, templateData),
"99_openshift-cluster-api_worker-machineset.yaml": worker.MachineSetRaw,
"99_openshift-cluster-api_worker-user-data-secret.yaml": worker.UserDataSecretRaw,
"99_binding-discovery.yaml": []byte(bindingDiscovery.Files()[0].Data),
"99_kubeadmin-password-secret.yaml": applyTemplateData(kubeadminPasswordSecret.Files()[0].Data, templateData),
}

switch platform {
Expand All @@ -135,6 +133,7 @@ func (o *Openshift) Generate(dependencies asset.Parents) error {
Data: data,
})
}
o.FileList = append(o.FileList, worker.Files()...)

asset.SortFiles(o.FileList)

Expand Down

0 comments on commit 3ad4777

Please sign in to comment.