-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pkg/asset: Add asset for Worker machinesets #468
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Package aws generates Machine objects for aws. | ||
package aws | ||
|
||
import ( | ||
"text/template" | ||
|
||
"github.com/openshift/installer/pkg/types" | ||
) | ||
|
||
// Config is used to generate the machine. | ||
type Config struct { | ||
ClusterName string | ||
Replicas int64 | ||
AMIID string | ||
Tags map[string]string | ||
Region string | ||
Machine types.AWSMachinePoolPlatform | ||
} | ||
|
||
// WorkerMachineSetTmpl is template for worker machineset. | ||
var WorkerMachineSetTmpl = template.Must(template.New("aws-worker-machineset").Parse(` | ||
apiVersion: cluster.k8s.io/v1alpha1 | ||
kind: MachineSet | ||
metadata: | ||
name: {{.ClusterName}}-worker-0 | ||
namespace: openshift-cluster-api | ||
labels: | ||
sigs.k8s.io/cluster-api-cluster: {{.ClusterName}} | ||
sigs.k8s.io/cluster-api-machine-role: worker | ||
sigs.k8s.io/cluster-api-machine-type: worker | ||
spec: | ||
replicas: {{.Replicas}} | ||
selector: | ||
matchLabels: | ||
sigs.k8s.io/cluster-api-machineset: worker | ||
sigs.k8s.io/cluster-api-cluster: {{.ClusterName}} | ||
template: | ||
metadata: | ||
labels: | ||
sigs.k8s.io/cluster-api-machineset: worker | ||
sigs.k8s.io/cluster-api-cluster: {{.ClusterName}} | ||
sigs.k8s.io/cluster-api-machine-role: worker | ||
sigs.k8s.io/cluster-api-machine-type: worker | ||
spec: | ||
providerConfig: | ||
value: | ||
apiVersion: aws.cluster.k8s.io/v1alpha1 | ||
kind: AWSMachineProviderConfig | ||
ami: | ||
id: {{.AMIID}} | ||
instanceType: {{.Machine.InstanceType}} | ||
placement: | ||
region: {{.Region}} | ||
subnet: | ||
filters: | ||
- name: "tag:Name" | ||
values: | ||
- "{{.ClusterName}}-worker-*" | ||
iamInstanceProfile: | ||
id: "{{.ClusterName}}-worker-profile" | ||
tags: | ||
{{- range $key,$value := .Tags}} | ||
- name: "{{$key}}" | ||
value: "{{$value}}" | ||
{{- end}} | ||
securityGroups: | ||
- filters: | ||
- name: "tag:Name" | ||
values: | ||
- "{{.ClusterName}}_worker_sg" | ||
userDataSecret: | ||
name: worker-user-data | ||
versions: | ||
kubelet: "" | ||
controlPlane: "" | ||
`)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package machines | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
|
||
"github.com/openshift/installer/pkg/asset" | ||
"github.com/openshift/installer/pkg/asset/installconfig" | ||
"github.com/openshift/installer/pkg/types" | ||
) | ||
|
||
// ClusterK8sIO generates the `Cluster.cluster.k8s.io/v1alpha1` object. | ||
type ClusterK8sIO struct { | ||
Raw []byte | ||
} | ||
|
||
var _ asset.Asset = (*ClusterK8sIO)(nil) | ||
|
||
// Name returns a human friendly name for the ClusterK8sIO Asset. | ||
func (c *ClusterK8sIO) Name() string { | ||
return "Cluster.cluster.k8s.io/v1alpha1" | ||
} | ||
|
||
// Dependencies returns all of the dependencies directly needed by the | ||
// ClusterK8sIO asset | ||
func (c *ClusterK8sIO) Dependencies() []asset.Asset { | ||
return []asset.Asset{ | ||
&installconfig.InstallConfig{}, | ||
} | ||
} | ||
|
||
// Generate generates the Worker asset. | ||
func (c *ClusterK8sIO) Generate(dependencies asset.Parents) error { | ||
installconfig := &installconfig.InstallConfig{} | ||
dependencies.Get(installconfig) | ||
|
||
c.Raw = clusterK8sIO(installconfig.Config) | ||
return nil | ||
} | ||
|
||
var clusterK8sIOTmpl = template.Must(template.New("cluster").Parse(` | ||
apiVersion: "cluster.k8s.io/v1alpha1" | ||
kind: Cluster | ||
metadata: | ||
name: {{.Name}} | ||
namespace: openshift-cluster-api | ||
spec: | ||
clusterNetwork: | ||
services: | ||
cidrBlocks: | ||
- {{.ServiceCIDR}} | ||
pods: | ||
cidrBlocks: | ||
- {{.PodCIDR}} | ||
serviceDomain: unused | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've had this since openshift/machine-api-operator@04253cb9, but I'm not clear on why we aren't using our base domain or some such. @enxebre, do you remember why you chose this value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of the cluster object fields itself is used. However it's currently coupled in the actuator interface upstream hence we need it to exist for now https://github.com/kubernetes-sigs/cluster-api/blob/master/pkg/controller/machine/actuator.go#L25 |
||
`)) | ||
|
||
func clusterK8sIO(ic *types.InstallConfig) []byte { | ||
templateData := struct { | ||
Name string | ||
ServiceCIDR string | ||
PodCIDR string | ||
}{ | ||
Name: ic.ObjectMeta.Name, | ||
ServiceCIDR: ic.Networking.ServiceCIDR.String(), | ||
PodCIDR: ic.Networking.PodCIDR.String(), | ||
} | ||
buf := &bytes.Buffer{} | ||
if err := clusterK8sIOTmpl.Execute(buf, templateData); err != nil { | ||
panic(err) | ||
} | ||
return buf.Bytes() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package machines is responsible for creating Machine objects for machinepools. | ||
package machines |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Package libvirt generates Machine objects for libvirt. | ||
package libvirt | ||
|
||
import ( | ||
"text/template" | ||
|
||
"github.com/openshift/installer/pkg/types" | ||
) | ||
|
||
// Config is used to generate the machine. | ||
type Config struct { | ||
ClusterName string | ||
Replicas int64 | ||
Platform types.LibvirtPlatform | ||
} | ||
|
||
// WorkerMachineSetTmpl is template for worker machineset. | ||
var WorkerMachineSetTmpl = template.Must(template.New("worker-machineset").Parse(` | ||
apiVersion: cluster.k8s.io/v1alpha1 | ||
kind: MachineSet | ||
metadata: | ||
name: {{.ClusterName}}-worker-0 | ||
namespace: openshift-cluster-api | ||
labels: | ||
sigs.k8s.io/cluster-api-cluster: {{.ClusterName}} | ||
sigs.k8s.io/cluster-api-machine-role: worker | ||
sigs.k8s.io/cluster-api-machine-type: worker | ||
spec: | ||
replicas: {{.Replicas}} | ||
selector: | ||
matchLabels: | ||
sigs.k8s.io/cluster-api-machineset: worker | ||
sigs.k8s.io/cluster-api-cluster: {{.ClusterName}} | ||
sigs.k8s.io/cluster-api-machine-role: worker | ||
sigs.k8s.io/cluster-api-machine-type: worker | ||
template: | ||
metadata: | ||
labels: | ||
sigs.k8s.io/cluster-api-machineset: worker | ||
sigs.k8s.io/cluster-api-cluster: {{.ClusterName}} | ||
sigs.k8s.io/cluster-api-machine-role: worker | ||
sigs.k8s.io/cluster-api-machine-type: worker | ||
spec: | ||
providerConfig: | ||
value: | ||
apiVersion: libvirtproviderconfig/v1alpha1 | ||
kind: LibvirtMachineProviderConfig | ||
domainMemory: 2048 | ||
domainVcpu: 2 | ||
ignKey: /var/lib/libvirt/images/worker.ign | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make this just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with changing this to Long term i want consistency between libvirt and aws, as AWS uses a secret to specify the useradata. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wking this doesn't work The actuator doesn't support this:
|
||
volume: | ||
poolName: default | ||
baseVolumeID: /var/lib/libvirt/images/coreos_base | ||
networkInterfaceName: {{.Platform.Network.Name}} | ||
networkInterfaceAddress: {{.Platform.Network.IPRange}} | ||
autostart: false | ||
uri: {{.Platform.URI}} | ||
versions: | ||
kubelet: "" | ||
controlPlane: "" | ||
`)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package machines | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"text/template" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var userDataTmpl = template.Must(template.New("user-data").Parse(` | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: {{.Name}} | ||
namespace: openshift-cluster-api | ||
type: Opaque | ||
data: | ||
userData: {{.UserDataContent}} | ||
`)) | ||
|
||
func userData(secretName string, content []byte) ([]byte, error) { | ||
templateData := struct { | ||
Name string | ||
UserDataContent string | ||
}{ | ||
Name: secretName, | ||
UserDataContent: base64.StdEncoding.EncodeToString(content), | ||
} | ||
buf := &bytes.Buffer{} | ||
if err := userDataTmpl.Execute(buf, templateData); err != nil { | ||
return nil, errors.Wrap(err, "failed to execute content.UserDataTmpl") | ||
} | ||
return buf.Bytes(), nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package machines | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"text/template" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
|
||
"github.com/openshift/installer/pkg/asset" | ||
"github.com/openshift/installer/pkg/asset/ignition/machine" | ||
"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/rhcos" | ||
"github.com/openshift/installer/pkg/types" | ||
) | ||
|
||
func defaultAWSMachinePoolPlatform() types.AWSMachinePoolPlatform { | ||
return types.AWSMachinePoolPlatform{ | ||
InstanceType: "t2.medium", | ||
} | ||
} | ||
|
||
// Worker generates the machinesets for `worker` machine pool. | ||
type Worker struct { | ||
MachineSetRaw []byte | ||
UserDataSecretRaw []byte | ||
} | ||
|
||
var _ asset.Asset = (*Worker)(nil) | ||
|
||
// Name returns a human friendly name for the Worker Asset. | ||
func (w *Worker) Name() string { | ||
return "Worker Machines" | ||
} | ||
|
||
// Dependencies returns all of the dependencies directly needed by the | ||
// Worker asset | ||
func (w *Worker) Dependencies() []asset.Asset { | ||
return []asset.Asset{ | ||
&installconfig.InstallConfig{}, | ||
&machine.Worker{}, | ||
} | ||
} | ||
|
||
// Generate generates the Worker asset. | ||
func (w *Worker) Generate(dependencies asset.Parents) error { | ||
installconfig := &installconfig.InstallConfig{} | ||
wign := &machine.Worker{} | ||
dependencies.Get(installconfig, wign) | ||
|
||
var err error | ||
w.UserDataSecretRaw, err = userData("worker-user-data", wign.File.Data) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to create user-data secret for worker machines") | ||
} | ||
|
||
ic := installconfig.Config | ||
pool := workerPool(ic.Machines) | ||
numOfWorkers := int64(0) | ||
if pool.Replicas != nil { | ||
numOfWorkers = *pool.Replicas | ||
} | ||
|
||
switch ic.Platform.Name() { | ||
case "aws": | ||
config := aws.Config{ | ||
ClusterName: ic.ObjectMeta.Name, | ||
Replicas: numOfWorkers, | ||
Region: ic.Platform.AWS.Region, | ||
Machine: defaultAWSMachinePoolPlatform(), | ||
} | ||
|
||
tags := map[string]string{ | ||
"tectonicClusterID": ic.ClusterID, | ||
// Info: https://github.com/openshift/cluster-api-provider-aws/issues/73 | ||
// fmt.Sprintf("kubernetes.io/cluster/%s", ic.ObjectMeta.Name): "owned", | ||
} | ||
for k, v := range ic.Platform.AWS.UserTags { | ||
tags[k] = v | ||
} | ||
config.Tags = tags | ||
|
||
config.Machine.Set(ic.Platform.AWS.DefaultMachinePlatform) | ||
config.Machine.Set(pool.Platform.AWS) | ||
|
||
ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second) | ||
defer cancel() | ||
ami, err := rhcos.AMI(ctx, rhcos.DefaultChannel, config.Region) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This duplicates the later lookup for master/bootstrap AMIs. Are we ok with the redundant request (and possible race coming up with two different AMIs), or do we want to pull this lookup back to a separate location where it can be shared by both consumers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not different than what we have today, in MAO here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also when we move masters to machinesets, it will end up in same location. |
||
if err != nil { | ||
return errors.Wrap(err, "failed to determine default AMI") | ||
} | ||
config.AMIID = ami | ||
|
||
w.MachineSetRaw = applyTemplateData(aws.WorkerMachineSetTmpl, config) | ||
case "libvirt": | ||
config := libvirt.Config{ | ||
ClusterName: ic.ObjectMeta.Name, | ||
Replicas: numOfWorkers, | ||
Platform: *ic.Platform.Libvirt, | ||
} | ||
w.MachineSetRaw = applyTemplateData(libvirt.WorkerMachineSetTmpl, config) | ||
default: | ||
return fmt.Errorf("invalid Platform") | ||
} | ||
return nil | ||
} | ||
|
||
func workerPool(pools []types.MachinePool) types.MachinePool { | ||
for idx, pool := range pools { | ||
if pool.Name == "worker" { | ||
return pools[idx] | ||
} | ||
} | ||
return types.MachinePool{} | ||
} | ||
|
||
func applyTemplateData(template *template.Template, templateData interface{}) []byte { | ||
buf := &bytes.Buffer{} | ||
if err := template.Execute(buf, templateData); err != nil { | ||
panic(err) | ||
} | ||
return buf.Bytes() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's up the
-0
suffix on these? This will be copied intogenerateName
by the controller, so the nodes will end up with names likeopenshift-worker-0-abdef
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We there are going to have multiple machinesets (in aws per az).
I think the node names is not dictated by the machine object name. it will be whatever kubelet decides. on aws it is usually internal name, on libvirt host name.
From the ci:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, forgot about the multiple AZ thing.
So, yeah, this is kind of confusing. The node object's name is determined by what the kubelet registers. The machine object's name is this string copied into
generateName
, so basically this with some extra characters.