Skip to content

Commit

Permalink
pkg/asset: use vendored cluster-api instead of go templates
Browse files Browse the repository at this point in the history
Currently the Machine and MachineSet objects are go templates. Moving them
to vendored code allows other consumers like openshift/hive to use these public
helpers effectively.

openstack platform still uses go templates because of vendoring problems.
  • Loading branch information
abhinavdahiya committed Nov 2, 2018
1 parent d92442d commit 476be07
Show file tree
Hide file tree
Showing 12 changed files with 522 additions and 374 deletions.
124 changes: 124 additions & 0 deletions pkg/asset/machines/aws/machines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Package aws generates Machine objects for aws.
package aws

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"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/pointer"
awsprovider "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1"
clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"

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

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

total := int64(1)
if pool.Replicas != nil {
total = *pool.Replicas
}
numOfAZs := int64(len(azs))
var machines []clusterapi.Machine
for idx := range azs {
replicas := int32(total / numOfAZs)
if int64(idx) < total%numOfAZs {
replicas++
}

provider, err := provider(config.ClusterID, clustername, platform, mpool, idx, 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{
ProviderConfig: clusterapi.ProviderConfig{
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 *types.AWSPlatform, mpool *types.AWSMachinePoolPlatform, azIdx int, role, userDataSecret string) (*awsprovider.AWSMachineProviderConfig, error) {
az := mpool.Zones[azIdx]
tags, err := tagsFromUserTags(clusterID, clusterName, platform.UserTags)
if err != nil {
return nil, errors.Wrap(err, "failed to create awsprovider.TagSpecifications from UserTags")
}
return &awsprovider.AWSMachineProviderConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: "aws.cluster.k8s.io/v1alpha1",
Kind: "AWSMachineProviderConfig",
},
InstanceType: mpool.InstanceType,
AMI: awsprovider.AWSResourceReference{ID: &mpool.AMIID},
Tags: tags,
IAMInstanceProfile: &awsprovider.AWSResourceReference{ID: pointer.StringPtr(fmt.Sprintf("%s-%s-profile", clusterName, role))},
UserDataSecret: &corev1.LocalObjectReference{Name: userDataSecret},
Subnet: awsprovider.AWSResourceReference{
Filters: []awsprovider.Filter{{
Name: "tag:Name",
Values: []string{fmt.Sprintf("%s-%s-%s", clusterName, role, az)},
}},
},
Placement: awsprovider.Placement{Region: platform.Region, AvailabilityZone: az},
SecurityGroups: []awsprovider.AWSResourceReference{{
Filters: []awsprovider.Filter{{
Name: "tag:Name",
Values: []string{fmt.Sprintf("%s_%s_sg", clusterName, role)},
}},
}},
}, nil
}

func tagsFromUserTags(clusterID, clusterName string, usertags map[string]string) ([]awsprovider.TagSpecification, error) {
tags := []awsprovider.TagSpecification{
{Name: "tectonicClusterID", Value: clusterID},
{Name: fmt.Sprintf("kubernetes.io/cluster/%s", clusterName), Value: "owned"},
}
forbiddenTags := sets.NewString()
for idx := range tags {
forbiddenTags.Insert(tags[idx].Name)
}
for k, v := range usertags {
if forbiddenTags.Has(k) {
return nil, fmt.Errorf("user tags may not clobber %s", k)
}
tags = append(tags, awsprovider.TagSpecification{Name: k, Value: v})
}
return tags, nil
}
89 changes: 89 additions & 0 deletions pkg/asset/machines/aws/machinesets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Package aws generates Machine objects for aws.
package aws

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/pkg/errors"
)

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

total := int64(0)
if pool.Replicas != nil {
total = *pool.Replicas
}
numOfAZs := int64(len(azs))
var machinesets []clusterapi.MachineSet
for idx, az := range azs {
replicas := int32(total / numOfAZs)
if int64(idx) < total%numOfAZs {
replicas++
}

provider, err := provider(config.ClusterID, clustername, platform, mpool, idx, role, userDataSecret)
if err != nil {
return nil, errors.Wrap(err, "failed to create provider")
}
name := fmt.Sprintf("%s-%s-%s", clustername, pool.Name, az)
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: &replicas,
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{
ProviderConfig: clusterapi.ProviderConfig{
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
}
73 changes: 0 additions & 73 deletions pkg/asset/machines/aws/master.go

This file was deleted.

99 changes: 0 additions & 99 deletions pkg/asset/machines/aws/worker.go

This file was deleted.

Loading

0 comments on commit 476be07

Please sign in to comment.