Skip to content

Commit

Permalink
Merge pull request #1107 from flaper87/remove-machine-yaml
Browse files Browse the repository at this point in the history
openstack: Create machine resources programmatically
  • Loading branch information
openshift-merge-robot committed Jan 29, 2019
2 parents 322f8ed + 548d655 commit 9e263e0
Show file tree
Hide file tree
Showing 93 changed files with 7,591 additions and 5,382 deletions.
24 changes: 21 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ ignored = [
branch = "master"
name = "github.com/vincent-petithory/dataurl"

[[override]]
name = "k8s.io/api"
version = "kubernetes-1.12.2"

[[override]]
name = "k8s.io/apimachinery"
version = "kubernetes-1.12.2"
Expand Down Expand Up @@ -91,3 +95,8 @@ ignored = [
[[constraint]]
branch = "master"
name = "github.com/gophercloud/gophercloud"

[[constraint]]
branch = "master"
name = "sigs.k8s.io/cluster-api-provider-openstack"
source = "https://github.com/kubernetes-sigs/cluster-api-provider-openstack.git"
36 changes: 13 additions & 23 deletions pkg/asset/machines/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,33 +112,23 @@ func (m *Master) Generate(dependencies asset.Parents) error {
m.MachinesRaw = raw
case nonetypes.Name:
case openstacktypes.Name:
numOfMasters := int64(0)
if pool.Replicas != nil {
numOfMasters = *pool.Replicas
}
instances := []string{}
for i := 0; i < int(numOfMasters); i++ {
instances = append(instances, fmt.Sprintf("master-%d", i))
}
config := openstack.MasterConfig{
CloudName: ic.Platform.OpenStack.Cloud,
ClusterName: ic.ObjectMeta.Name,
Instances: instances,
Image: string(*rhcosImage),
Region: ic.Platform.OpenStack.Region,
Machine: defaultOpenStackMachinePoolPlatform(ic.Platform.OpenStack.FlavorName),
Trunk: trunkSupportBoolean(ic.Platform.OpenStack.TrunkSupport),
}
mpool := defaultOpenStackMachinePoolPlatform(ic.Platform.OpenStack.FlavorName)
mpool.Set(ic.Platform.OpenStack.DefaultMachinePlatform)
mpool.Set(pool.Platform.OpenStack)
pool.Platform.OpenStack = &mpool

tags := map[string]string{
"openshiftClusterID": clusterID.ClusterID,
machines, err := openstack.Machines(clusterID.ClusterID, ic, &pool, string(*rhcosImage), "master", "master-user-data")
if err != nil {
return errors.Wrap(err, "failed to create master machine objects")
}
config.Tags = tags
openstack.ConfigMasters(machines, ic.ObjectMeta.Name)

config.Machine.Set(ic.Platform.OpenStack.DefaultMachinePlatform)
config.Machine.Set(pool.Platform.OpenStack)
list := listFromMachines(machines)
m.MachinesRaw, err = yaml.Marshal(list)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}

m.MachinesRaw = applyTemplateData(openstack.MasterMachinesTmpl, config)
default:
return fmt.Errorf("invalid Platform")
}
Expand Down
104 changes: 104 additions & 0 deletions pkg/asset/machines/openstack/machines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Package openstack generates Machine objects for openstack.
package openstack

import (
"fmt"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
openstackprovider "sigs.k8s.io/cluster-api-provider-openstack/pkg/apis/openstackproviderconfig/v1alpha1"
clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"

"github.com/openshift/installer/pkg/types"
"github.com/openshift/installer/pkg/types/openstack"
)

const cloudsSecret = "openstack-credentials"

// Machines returns a list of machines for a machinepool.
func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]clusterapi.Machine, error) {
if configPlatform := config.Platform.Name(); configPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack configuration: %q", configPlatform)
}
if poolPlatform := pool.Platform.Name(); poolPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack machine-pool: %q", poolPlatform)
}
clustername := config.ObjectMeta.Name
platform := config.Platform.OpenStack
mpool := pool.Platform.OpenStack

total := int64(1)
if pool.Replicas != nil {
total = *pool.Replicas
}
var machines []clusterapi.Machine
for idx := int64(0); idx < total; idx++ {
az := ""
provider, err := provider(clusterID, clustername, platform, mpool, osImage, az, role, userDataSecret)
if err != nil {
return nil, errors.Wrap(err, "failed to create provider")
}
machine := clusterapi.Machine{
TypeMeta: metav1.TypeMeta{
APIVersion: "cluster.k8s.io/v1alpha1",
Kind: "Machine",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "openshift-cluster-api",
Name: fmt.Sprintf("%s-%s-%d", clustername, pool.Name, idx),
Labels: map[string]string{
"sigs.k8s.io/cluster-api-cluster": clustername,
"sigs.k8s.io/cluster-api-machine-role": role,
"sigs.k8s.io/cluster-api-machine-type": role,
},
},
Spec: clusterapi.MachineSpec{
ProviderSpec: clusterapi.ProviderSpec{
Value: &runtime.RawExtension{Object: provider},
},
// we don't need to set Versions, because we control those via operators.
},
}

machines = append(machines, machine)
}

return machines, nil
}

func provider(clusterID, clusterName string, platform *openstack.Platform, mpool *openstack.MachinePool, osImage string, az string, role, userDataSecret string) (*openstackprovider.OpenstackProviderSpec, error) {
return &openstackprovider.OpenstackProviderSpec{
TypeMeta: metav1.TypeMeta{
APIVersion: "openstackproviderconfig.k8s.io/v1alpha1",
Kind: "OpenstackProviderSpec",
},
Flavor: mpool.FlavorName,
/*RootVolume: openstackprovider.RootVolume{
VolumeType: pointer.StringPtr(mpool.Type),
Size: pointer.Int64Ptr(int64(mpool.Size)),
},*/
Image: osImage,
CloudName: platform.Cloud,
CloudsSecret: &corev1.SecretReference{Name: cloudsSecret},
UserDataSecret: &corev1.SecretReference{Name: userDataSecret},
Networks: []openstackprovider.NetworkParam{
{
Filter: openstackprovider.Filter{
Tags: fmt.Sprintf("%s=%s", "openshiftClusterID", clusterID),
},
},
},
AvailabilityZone: az,
SecurityGroups: []string{role},
// TODO(flaper87): Trunk support missing. Need to add it back
}, nil
}

// ConfigMasters sets the PublicIP flag and assigns a set of load balancers to the given machines
func ConfigMasters(machines []clusterapi.Machine, clusterName string) {
/*for _, machine := range machines {
providerSpec := machine.Spec.ProviderSpec.Value.Object.(*openstackprovider.OpenstackProviderSpec)
}*/
}
86 changes: 86 additions & 0 deletions pkg/asset/machines/openstack/machinesets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Package openstack generates Machine objects for openstack.
package openstack

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"

"github.com/openshift/installer/pkg/types"
"github.com/openshift/installer/pkg/types/openstack"
"github.com/pkg/errors"
)

// MachineSets returns a list of machinesets for a machinepool.
func MachineSets(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]clusterapi.MachineSet, error) {
if configPlatform := config.Platform.Name(); configPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack configuration: %q", configPlatform)
}
if poolPlatform := pool.Platform.Name(); poolPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack machine-pool: %q", poolPlatform)
}
clustername := config.ObjectMeta.Name
platform := config.Platform.OpenStack
mpool := pool.Platform.OpenStack

total := int32(0)
if pool.Replicas != nil {
total = int32(*pool.Replicas)
}

// TODO(flaper87): Add support for availability zones
var machinesets []clusterapi.MachineSet
az := ""
provider, err := provider(clusterID, clustername, platform, mpool, osImage, az, role, userDataSecret)
if err != nil {
return nil, errors.Wrap(err, "failed to create provider")
}
// TODO(flaper87): Implement AZ support sometime soon
//name := fmt.Sprintf("%s-%s-%s", clustername, pool.Name, az)
name := fmt.Sprintf("%s-%s", clustername, pool.Name)
mset := clusterapi.MachineSet{
TypeMeta: metav1.TypeMeta{
APIVersion: "cluster.k8s.io/v1alpha1",
Kind: "MachineSet",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "openshift-cluster-api",
Name: name,
Labels: map[string]string{
"sigs.k8s.io/cluster-api-cluster": clustername,
"sigs.k8s.io/cluster-api-machine-role": role,
"sigs.k8s.io/cluster-api-machine-type": role,
},
},
Spec: clusterapi.MachineSetSpec{
Replicas: &total,
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"sigs.k8s.io/cluster-api-machineset": name,
"sigs.k8s.io/cluster-api-cluster": clustername,
},
},
Template: clusterapi.MachineTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"sigs.k8s.io/cluster-api-machineset": name,
"sigs.k8s.io/cluster-api-cluster": clustername,
"sigs.k8s.io/cluster-api-machine-role": role,
"sigs.k8s.io/cluster-api-machine-type": role,
},
},
Spec: clusterapi.MachineSpec{
ProviderSpec: clusterapi.ProviderSpec{
Value: &runtime.RawExtension{Object: provider},
},
// we don't need to set Versions, because we control those via cluster operators.
},
},
},
}
machinesets = append(machinesets, mset)

return machinesets, nil
}
66 changes: 0 additions & 66 deletions pkg/asset/machines/openstack/master.go

This file was deleted.

Loading

0 comments on commit 9e263e0

Please sign in to comment.