Skip to content

Commit

Permalink
pkg/asset: add ClusterK8sIO, machines.Worker assets
Browse files Browse the repository at this point in the history
Add assets that can create the machinesets for workers.
This depends on the cluster-apiserver to be deployed by the machine-api-operator,
so the tectonic.sh will block on creating these machinesets objects until then.
  • Loading branch information
abhinavdahiya committed Oct 15, 2018
1 parent 55c9cb0 commit c6ca08a
Show file tree
Hide file tree
Showing 8 changed files with 407 additions and 20 deletions.
93 changes: 93 additions & 0 deletions pkg/asset/machines/aws/aws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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
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:
filters:
- name: "name"
values:
- rhcos*
- name: "architecture"
values:
- "x86_64"
- name: "virtualization-type"
values:
- "hvm"
- name: "owner-id"
values:
- "531415883065"
- name: "image-type"
values:
- "machine"
- name: "state"
values:
- "available"
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
`))

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
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
}
115 changes: 115 additions & 0 deletions pkg/asset/machines/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package machines

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

"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/types"
)

func defaultAWSMachinePoolPlatform() types.AWSMachinePoolPlatform {
return types.AWSMachinePoolPlatform{
InstanceType: "m2.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,
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)

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

0 comments on commit c6ca08a

Please sign in to comment.