Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions pkg/asset/machines/aws/aws.go
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
Copy link
Contributor

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 into generateName by the controller, so the nodes will end up with names like openshift-worker-0-abdef.

Copy link
Contributor Author

@abhinavdahiya abhinavdahiya Oct 16, 2018

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

We there are going to have multiple machinesets (in aws per az).

so the nodes will end up with names like openshift-worker-0-abdef

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:

NAME                           STATUS    ROLES       AGE       VERSION
ip-10-0-13-26.ec2.internal     Ready     master      10m       v1.11.0+d4cacc0
ip-10-0-14-47.ec2.internal     Ready     bootstrap   10m       v1.11.0+d4cacc0
ip-10-0-151-203.ec2.internal   Ready     worker      5m        v1.11.0+d4cacc0
ip-10-0-152-41.ec2.internal    Ready     worker      5m        v1.11.0+d4cacc0
ip-10-0-155-96.ec2.internal    Ready     worker      4m        v1.11.0+d4cacc0
ip-10-0-16-202.ec2.internal    Ready     master      10m       v1.11.0+d4cacc0
ip-10-0-39-160.ec2.internal    Ready     master      10m       v1.11.0+d4cacc0

Copy link
Contributor

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.

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: ""
`))
73 changes: 73 additions & 0 deletions pkg/asset/machines/cluster_k8s_io.go
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
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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()
}
2 changes: 2 additions & 0 deletions pkg/asset/machines/doc.go
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
61 changes: 61 additions & 0 deletions pkg/asset/machines/libvirt/libvirt.go
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this just worker.ign? More on why in openshift/machine-api-operator#70, in case you want any fodder for the explanatory commit message (although we probably don't have time to explain everything in these big "drop in a bunch of stuff which was worked up somewhere else" commits).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with changing this to worker.ign.

Long term i want consistency between libvirt and aws, as AWS uses a secret to specify the useradata.

Copy link
Contributor Author

@abhinavdahiya abhinavdahiya Oct 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wking this doesn't work

The actuator doesn't support this:
locally

Coud not create libvirt machine: error creating domain: error creating libvirt domain: virError(Code=1, Domain=10, Message='internal error: process exited while connecting to monitor: 2018-10-16T17:59:15.773168Z qemu-system-x86_64: -fw_cfg name=opt/com.coreos/config,file=worker.ign: can't load worker.ign')

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: ""
`))
35 changes: 35 additions & 0 deletions pkg/asset/machines/userdata.go
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
}
127 changes: 127 additions & 0 deletions pkg/asset/machines/worker.go
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)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not different than what we have today, in MAO here

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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()
}
Loading