Skip to content

Commit

Permalink
Use example YAML instead of struct literals for clusters and machines
Browse files Browse the repository at this point in the history
  • Loading branch information
liztio committed Feb 14, 2019
1 parent 2f98204 commit 6a16332
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 62 deletions.
2 changes: 2 additions & 0 deletions Gopkg.lock

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

5 changes: 3 additions & 2 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func NewActuator(params ActuatorParams) *Actuator {
}
}

func (a *Actuator) getControlPlaneMachines(machineList *clusterv1.MachineList) []*clusterv1.Machine {
// GetControlPlaneMachines retrieves all control plane nodes from a MachineList
func GetControlPlaneMachines(machineList *clusterv1.MachineList) []*clusterv1.Machine {
var cpm []*clusterv1.Machine
for _, m := range machineList.Items {
if m.Spec.Versions.ControlPlane != "" {
Expand Down Expand Up @@ -134,7 +135,7 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
if err != nil {
return errors.Wrapf(err, "failed to retrieve machines in cluster %q", cluster.Name)
}
controlPlaneMachines := a.getControlPlaneMachines(clusterMachines)
controlPlaneMachines := GetControlPlaneMachines(clusterMachines)
isNodeJoin, err := a.isNodeJoin(controlPlaneMachines, machine, cluster)
if err != nil {
return errors.Wrapf(err, "failed to determine whether machine %q should join cluster %q", machine.Name, cluster.Name)
Expand Down
3 changes: 1 addition & 2 deletions pkg/cloud/aws/actuators/machine/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,9 @@ func TestGetControlPlaneMachines(t *testing.T) {
expectedOut: []clusterv1.Machine{},
},
}
testActuator := NewActuator(ActuatorParams{})

for _, tc := range testCases {
actual := testActuator.getControlPlaneMachines(tc.input)
actual := GetControlPlaneMachines(tc.input)
if len(actual) != len(tc.expectedOut) {
t.Fatalf("[%s] Unexpected number of controlplane machines returned. Got: %d, Want: %d", tc.name, len(actual), len(tc.expectedOut))
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cloud/aws/actuators/machine_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
return nil, err
}

machineConfig, err := machineConfigFromProviderSpec(params.Client, params.Machine.Spec.ProviderSpec)
machineConfig, err := MachineConfigFromProviderSpec(params.Client, params.Machine.Spec.ProviderSpec)
if err != nil {
return nil, errors.Wrap(err, "failed to get machine config")
}
Expand Down Expand Up @@ -135,7 +135,8 @@ func (m *MachineScope) Close() {
}
}

func machineConfigFromProviderSpec(clusterClient client.MachineClassesGetter, providerConfig clusterv1.ProviderSpec) (*v1alpha1.AWSMachineProviderSpec, error) {
// MachineConfigFromProviderSpec tries to decode the JSON-encoded spec, falling back on getting a MachineClass if the value is absent.
func MachineConfigFromProviderSpec(clusterClient client.MachineClassesGetter, providerConfig clusterv1.ProviderSpec) (*v1alpha1.AWSMachineProviderSpec, error) {
var config v1alpha1.AWSMachineProviderSpec
if providerConfig.Value != nil {
klog.V(4).Info("Decoding ProviderConfig from Value")
Expand Down
8 changes: 4 additions & 4 deletions pkg/cloud/aws/services/ec2/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ vuO9LYxDXLVY9F7W4ccyCqe27Cj1xyAvdZxwhITrib8Wg5CMqoRpqTw5V3+TpA==
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
// defer mockCtrl.Finish()
ec2Mock := mock_ec2iface.NewMockEC2API(mockCtrl)
elbMock := mock_elbiface.NewMockELBAPI(mockCtrl)

Expand All @@ -408,13 +408,13 @@ vuO9LYxDXLVY9F7W4ccyCqe27Cj1xyAvdZxwhITrib8Wg5CMqoRpqTw5V3+TpA==
},
})

scope.Scope.ClusterConfig = tc.clusterConfig
scope.Scope.ClusterStatus = tc.clusterStatus

if err != nil {
t.Fatalf("Failed to create test context: %v", err)
}

scope.Scope.ClusterConfig = tc.clusterConfig
scope.Scope.ClusterStatus = tc.clusterStatus

tc.expect(ec2Mock.EXPECT())

s := NewService(scope.Scope)
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ go_test(
"-clusterAPIYAML=$(location //vendor/sigs.k8s.io/cluster-api/config:cluster-api-yaml)",
"-managerImageTar=$(location //cmd/manager:manager-amd64.tar)",
"-credFile=$(location //config:credential_file)",
"-clusterYAML=$(location //cmd/clusterctl/examples/aws:out/cluster.yaml)",
"-machineYAML=$(location //cmd/clusterctl/examples/aws:out/machines.yaml)",
],
data = [
"//cmd/clusterctl/examples/aws:out/cluster.yaml",
"//cmd/clusterctl/examples/aws:out/machines.yaml",
"//config:aws-provider-yaml",
"//config:credential_file",
"//vendor/sigs.k8s.io/cluster-api/config:cluster-api-yaml",
Expand All @@ -26,6 +30,8 @@ go_test(
rundir = ".",
deps = [
"//pkg/apis/awsprovider/v1alpha1:go_default_library",
"//pkg/cloud/aws/actuators:go_default_library",
"//pkg/cloud/aws/actuators/machine:go_default_library",
"//pkg/cloud/aws/services/awserrors:go_default_library",
"//pkg/cloud/aws/services/cloudformation:go_default_library",
"//pkg/cloud/aws/services/sts:go_default_library",
Expand All @@ -43,8 +49,11 @@ go_test(
"//vendor/github.com/onsi/gomega/types:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset:go_default_library",
"//vendor/sigs.k8s.io/cluster-api/pkg/controller/machine:go_default_library",
],
)
112 changes: 60 additions & 52 deletions test/e2e/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package e2e_test

import (
"flag"
"io/ioutil"
"time"

. "github.com/onsi/ginkgo"
Expand All @@ -34,15 +35,20 @@ import (
awssts "github.com/aws/aws-sdk-go/service/sts"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/kubernetes"

capa "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsprovider/v1alpha1"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/actuators/machine"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/services/awserrors"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/services/cloudformation"
"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/services/sts"
"sigs.k8s.io/cluster-api-provider-aws/test/e2e/util/kind"
capi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
clientset "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
mc "sigs.k8s.io/cluster-api/pkg/controller/machine"
)

const (
Expand All @@ -51,14 +57,16 @@ const (
clusterName = "capa-test-cluster"
controlPlaneName = "capa-test-control-plane"

kubeletVersion = "v1.13.2"
instanceType = "t2.medium"
awsRegion = "us-east-1"
stackName = "cluster-api-provider-aws-sigs-k8s-io"
keyPairName = "cluster-api-provider-aws-sigs-k8s-io"
awsRegion = "us-east-1"
stackName = "cluster-api-provider-aws-sigs-k8s-io"
keyPairName = "cluster-api-provider-aws-sigs-k8s-io"
)

var credFile = flag.String("credFile", "", "path to an AWS credentials file")
var (
credFile = flag.String("credFile", "", "path to an AWS credentials file")
clusterYAML = flag.String("clusterYAML", "", "path to the YAML for the cluster we're creating")
machineYAML = flag.String("machineYAML", "", "path to the YAML describing the control plane we're creating")
)

var _ = Describe("AWS", func() {
var (
Expand Down Expand Up @@ -121,59 +129,59 @@ func beHealthy() types.GomegaMatcher {
}

func makeCluster() *capi.Cluster {
clusterSpec, err := capa.EncodeClusterSpec(&capa.AWSClusterProviderSpec{
Region: awsRegion,
SSHKeyName: keyPairName,
})
yaml, err := ioutil.ReadFile(*clusterYAML)
Expect(err).To(BeNil())

return &capi.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
},
Spec: capi.ClusterSpec{
ClusterNetwork: capi.ClusterNetworkingConfig{
Services: capi.NetworkRanges{
CIDRBlocks: []string{"10.96.0.0/12"},
},
Pods: capi.NetworkRanges{
CIDRBlocks: []string{"192.168.0.0/16"},
},
ServiceDomain: "cluster.local",
},
ProviderSpec: capi.ProviderSpec{
Value: clusterSpec,
},
},
}
deserializer := serializer.NewCodecFactory(getScheme()).UniversalDeserializer()
cluster := &capi.Cluster{}
obj, _, err := deserializer.Decode(yaml, nil, cluster)
Expect(err).To(BeNil())
cluster, ok := obj.(*capi.Cluster)
Expect(ok).To(BeTrue(), "Wanted cluster, got %T", obj)

cluster.ObjectMeta.Name = clusterName

awsSpec, err := capa.ClusterConfigFromProviderSpec(cluster.Spec.ProviderSpec)
Expect(err).To(BeNil())
awsSpec.SSHKeyName = keyPairName
awsSpec.Region = awsRegion
cluster.Spec.ProviderSpec.Value, err = capa.EncodeClusterSpec(awsSpec)
Expect(err).To(BeNil())

return cluster
}

func makeMachine() *capi.Machine {
providerSpec, err := capa.EncodeMachineSpec(&capa.AWSMachineProviderSpec{
InstanceType: instanceType,
IAMInstanceProfile: "control-plane.cluster-api-provider-aws.sigs.k8s.io",
KeyName: keyPairName,
})
yaml, err := ioutil.ReadFile(*machineYAML)
Expect(err).To(BeNil())

return &capi.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: controlPlaneName,
Labels: map[string]string{
"set": "controlplane",
"cluster.k8s.io/cluster-name": clusterName,
},
},
Spec: capi.MachineSpec{
Versions: capi.MachineVersionInfo{
Kubelet: kubeletVersion,
ControlPlane: kubeletVersion,
},
ProviderSpec: capi.ProviderSpec{
Value: providerSpec,
},
},
}
deserializer := serializer.NewCodecFactory(getScheme()).UniversalDeserializer()
obj, _, err := deserializer.Decode(yaml, nil, &capi.MachineList{})
Expect(err).To(BeNil())
machineList, ok := obj.(*capi.MachineList)
Expect(ok).To(BeTrue(), "Wanted machine, got %T", obj)

machines := machine.GetControlPlaneMachines(machineList)
Expect(machines).NotTo(BeEmpty())

machine := machines[0]
machine.ObjectMeta.Name = controlPlaneName
machine.ObjectMeta.Labels[mc.MachineClusterLabelName] = clusterName

awsSpec, err := actuators.MachineConfigFromProviderSpec(nil, machine.Spec.ProviderSpec)
Expect(err).To(BeNil())
awsSpec.KeyName = keyPairName
machine.Spec.ProviderSpec.Value, err = capa.EncodeMachineSpec(awsSpec)
Expect(err).To(BeNil())

return machine
}

func getScheme() *runtime.Scheme {
s := runtime.NewScheme()
capi.SchemeBuilder.AddToScheme(s)
capa.SchemeBuilder.AddToScheme(s)
return s
}

func createNamespace(client kubernetes.Interface) {
Expand Down

0 comments on commit 6a16332

Please sign in to comment.