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

Migrate to CRDs - WIP #67

Merged
merged 20 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/client"
"os"
"github.com/spf13/pflag"
"sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1"
)

var (
Expand Down Expand Up @@ -105,11 +106,17 @@ func initActuator(m manager.Manager) {

logger := log.WithField("controller", "awsMachine")

codec, err := v1alpha1.NewCodec()
if err != nil {
glog.Fatal(err)
}

params := machineactuator.ActuatorParams{
ClusterClient: client,
KubeClient: kubeClient,
AwsClientBuilder: awsclient.NewClient,
Logger: logger,
Codec: codec,
}

machineactuator.MachineActuator, err = machineactuator.NewActuator(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Annotation constants
const (
// ClusterNameLabel is the label that a machineset must have to identify the
// cluster to which it belongs.
ClusterNameLabel = "sigs.k8s.io/cluster-api-cluster"
Copy link
Member

Choose a reason for hiding this comment

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

Are those labels community defined?

Copy link
Member Author

Choose a reason for hiding this comment

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

this is a carry over, there's bunch of things to consolidate after this. Let's address any non specific to CRDs move in different follow ups

MachineRoleLabel = "sigs.k8s.io/cluster-api-machine-role"
MachineTypeLabel = "sigs.k8s.io/cluster-api-machine-type"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

Expand Down Expand Up @@ -180,5 +189,5 @@ type AWSMachineProviderConfigList struct {
}

func init() {
SchemeBuilder.Register(&AWSMachineProviderConfig{}, &AWSMachineProviderConfigList{}, AWSMachineProviderStatus{})
SchemeBuilder.Register(&AWSMachineProviderConfig{}, &AWSMachineProviderConfigList{}, &AWSMachineProviderStatus{})
}
1 change: 0 additions & 1 deletion pkg/apis/awsproviderconfig/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func (codec *AWSProviderConfigCodec) EncodeProviderStatus(in runtime.Object) (*r
if err := codec.encoder.Encode(in, &buf); err != nil {
return nil, fmt.Errorf("encoding failed: %v", err)
}

return &runtime.RawExtension{Raw: buf.Bytes()}, nil
}

Expand Down
29 changes: 19 additions & 10 deletions pkg/cloud/aws/actuators/machine/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/providerconfig/v1alpha1"
providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
clusterclient "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset"
clustererror "sigs.k8s.io/cluster-api/pkg/controller/error"
Expand All @@ -39,6 +39,7 @@ import (

awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/client"
clustoplog "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/logging"
"k8s.io/apimachinery/pkg/runtime"
)

const (
Expand All @@ -61,6 +62,7 @@ type Actuator struct {
clusterClient clusterclient.Interface
logger *log.Entry
awsClientBuilder awsclient.AwsClientBuilderFuncType
codec codec
}

// ActuatorParams holds parameter information for Actuator
Expand All @@ -69,6 +71,13 @@ type ActuatorParams struct {
ClusterClient clusterclient.Interface
Logger *log.Entry
AwsClientBuilder awsclient.AwsClientBuilderFuncType
Codec codec
}

type codec interface {
DecodeFromProviderConfig(clusterv1.ProviderConfig, runtime.Object) error
DecodeProviderStatus(*runtime.RawExtension, runtime.Object) error
EncodeProviderStatus(runtime.Object) (*runtime.RawExtension, error)
}

// NewActuator returns a new AWS Actuator
Expand All @@ -78,6 +87,7 @@ func NewActuator(params ActuatorParams) (*Actuator, error) {
clusterClient: params.ClusterClient,
logger: params.Logger,
awsClientBuilder: params.AwsClientBuilder,
codec: params.Codec,
}
return actuator, nil
}
Expand Down Expand Up @@ -105,7 +115,7 @@ func (a *Actuator) Create(cluster *clusterv1.Cluster, machine *clusterv1.Machine
}

func (a *Actuator) updateMachineStatus(machine *clusterv1.Machine, awsStatus *providerconfigv1.AWSMachineProviderStatus, mLog log.FieldLogger, networkAddresses []corev1.NodeAddress) error {
awsStatusRaw, err := EncodeAWSMachineProviderStatus(awsStatus)
awsStatusRaw, err := EncodeProviderStatus(a.codec, awsStatus)
if err != nil {
mLog.Errorf("error encoding AWS provider status: %v", err)
return err
Expand All @@ -122,7 +132,7 @@ func (a *Actuator) updateMachineStatus(machine *clusterv1.Machine, awsStatus *pr
time := metav1.Now()
machineCopy.Status.LastUpdated = &time

_, err := a.clusterClient.ClusterV1alpha1().Machines(machineCopy.Namespace).UpdateStatus(machineCopy)
_, err := a.clusterClient.ClusterV1alpha1().Machines(machineCopy.Namespace).Update(machineCopy)
if err != nil {
mLog.Errorf("error updating machine status: %v", err)
return err
Expand All @@ -139,7 +149,7 @@ func (a *Actuator) updateMachineProviderConditions(machine *clusterv1.Machine, m

mLog.Debug("updating machine conditions")

awsStatus, err := AWSMachineProviderStatusFromClusterAPIMachine(machine)
awsStatus, err := ProviderStatusFromMachine(a.codec, machine)
if err != nil {
mLog.Errorf("error decoding machine provider status: %v", err)
return err
Expand Down Expand Up @@ -247,7 +257,7 @@ func getSubnetIDs(subnet providerconfigv1.AWSResourceReference, client awsclient
func (a *Actuator) CreateMachine(cluster *clusterv1.Cluster, machine *clusterv1.Machine) (*ec2.Instance, error) {
mLog := clustoplog.WithMachine(a.logger, machine)

machineProviderConfig, err := ProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
machineProviderConfig, err := ProviderConfigMachine(machine)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return nil, err
Expand Down Expand Up @@ -433,7 +443,7 @@ func (a *Actuator) Delete(cluster *clusterv1.Cluster, machine *clusterv1.Machine
func (a *Actuator) DeleteMachine(cluster *clusterv1.Cluster, machine *clusterv1.Machine) error {
mLog := clustoplog.WithMachine(a.logger, machine)

machineProviderConfig, err := ProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
machineProviderConfig, err := ProviderConfigMachine(machine)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return err
Expand Down Expand Up @@ -470,7 +480,7 @@ func (a *Actuator) Update(cluster *clusterv1.Cluster, machine *clusterv1.Machine
mLog := clustoplog.WithMachine(a.logger, machine)
mLog.Debugf("updating machine")

machineProviderConfig, err := ProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
machineProviderConfig, err := ProviderConfigMachine(machine)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return err
Expand Down Expand Up @@ -568,7 +578,7 @@ func (a *Actuator) Describe(cluster *clusterv1.Cluster, machine *clusterv1.Machi
func (a *Actuator) getMachineInstances(cluster *clusterv1.Cluster, machine *clusterv1.Machine) ([]*ec2.Instance, error) {
mLog := clustoplog.WithMachine(a.logger, machine)

machineProviderConfig, err := ProviderConfigFromClusterAPIMachineSpec(&machine.Spec)
machineProviderConfig, err := ProviderConfigMachine(machine)
if err != nil {
mLog.Errorf("error decoding MachineProviderConfig: %v", err)
return nil, err
Expand All @@ -594,7 +604,7 @@ func (a *Actuator) updateStatus(machine *clusterv1.Machine, instance *ec2.Instan
mLog.Debug("updating status")

// Starting with a fresh status as we assume full control of it here.
awsStatus, err := AWSMachineProviderStatusFromClusterAPIMachine(machine)
awsStatus, err := ProviderStatusFromMachine(a.codec, machine)
if err != nil {
mLog.Errorf("error decoding machine provider status: %v", err)
return err
Expand Down Expand Up @@ -637,7 +647,6 @@ func (a *Actuator) updateStatus(machine *clusterv1.Machine, instance *ec2.Instan
mLog.Debug("finished calculating AWS status")

awsStatus.Conditions = SetAWSMachineProviderCondition(awsStatus.Conditions, providerconfigv1.MachineCreation, corev1.ConditionTrue, MachineCreationSucceeded, "machine successfully created", UpdateConditionIfReasonOrMessageChange)

// TODO(jchaloup): do we really need to update tis?
// origInstanceID := awsStatus.InstanceID
// if !StringPtrsEqual(origInstanceID, awsStatus.InstanceID) {
Expand Down
65 changes: 17 additions & 48 deletions pkg/cloud/aws/actuators/machine/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package machine

import (
"bytes"
"fmt"

"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -26,14 +25,14 @@ import (

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
jsonserializer "k8s.io/apimachinery/pkg/runtime/serializer/json"

awsclient "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/client"
providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/aws/providerconfig/v1alpha1"
providerconfigv1 "sigs.k8s.io/cluster-api-provider-aws/pkg/apis/awsproviderconfig/v1alpha1"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/ghodss/yaml"
)

// SortInstances will examine the given slice of instances and return the current active instance for
Expand Down Expand Up @@ -185,59 +184,29 @@ func TerminateInstances(client awsclient.Client, instances []*ec2.Instance, mLog
return nil
}

// ProviderConfigFromClusterAPIMachineSpec gets the machine provider config MachineSetSpec from the
// ProviderConfigMachine gets the machine provider config MachineSetSpec from the
// specified cluster-api MachineSpec.
func ProviderConfigFromClusterAPIMachineSpec(ms *clusterv1.MachineSpec) (*providerconfigv1.AWSMachineProviderConfig, error) {
if ms.ProviderConfig.Value == nil {
return nil, fmt.Errorf("no Value in ProviderConfig")
}
obj, gvk, err := providerconfigv1.Codecs.UniversalDecoder(providerconfigv1.SchemeGroupVersion).Decode([]byte(ms.ProviderConfig.Value.Raw), nil, nil)
if err != nil {
func ProviderConfigMachine(machine *clusterv1.Machine) (*providerconfigv1.AWSMachineProviderConfig, error) {
var config providerconfigv1.AWSMachineProviderConfig
if err := yaml.Unmarshal(machine.Spec.ProviderConfig.Value.Raw, &config); err != nil {
return nil, err
}
spec, ok := obj.(*providerconfigv1.AWSMachineProviderConfig)
if !ok {
return nil, fmt.Errorf("unexpected object when parsing machine provider config: %#v", gvk)
}
return spec, nil
return &config, nil
}

// AWSMachineProviderStatusFromClusterAPIMachine gets the machine provider status from the specified machine.
func AWSMachineProviderStatusFromClusterAPIMachine(m *clusterv1.Machine) (*providerconfigv1.AWSMachineProviderStatus, error) {
return AWSMachineProviderStatusFromMachineStatus(&m.Status)
}

// AWSMachineProviderStatusFromMachineStatus gets the machine provider status from the specified machine status.
func AWSMachineProviderStatusFromMachineStatus(s *clusterv1.MachineStatus) (*providerconfigv1.AWSMachineProviderStatus, error) {
if s.ProviderStatus == nil {
return &providerconfigv1.AWSMachineProviderStatus{}, nil
// ProviderStatusFromMachine gets the machine provider status from the specified machine.
func ProviderStatusFromMachine(codec codec, m *clusterv1.Machine) (*providerconfigv1.AWSMachineProviderStatus, error) {
status := &providerconfigv1.AWSMachineProviderStatus{}
var err error
if m.Status.ProviderStatus != nil {
err = codec.DecodeProviderStatus(m.Status.ProviderStatus, status)
}
obj, gvk, err := providerconfigv1.Codecs.UniversalDecoder(providerconfigv1.SchemeGroupVersion).Decode([]byte(s.ProviderStatus.Raw), nil, nil)
if err != nil {
return nil, err
}
status, ok := obj.(*providerconfigv1.AWSMachineProviderStatus)
if !ok {
return nil, fmt.Errorf("unexpected object: %#v", gvk)
}
return status, nil
return status, err
}

// EncodeAWSMachineProviderStatus encodes the machine status into RawExtension
func EncodeAWSMachineProviderStatus(awsStatus *providerconfigv1.AWSMachineProviderStatus) (*runtime.RawExtension, error) {
awsStatus.TypeMeta = metav1.TypeMeta{
APIVersion: providerconfigv1.SchemeGroupVersion.String(),
Kind: "AWSMachineProviderStatus",
}
serializer := jsonserializer.NewSerializer(jsonserializer.DefaultMetaFactory, providerconfigv1.Scheme, providerconfigv1.Scheme, false)
var buffer bytes.Buffer
err := serializer.Encode(awsStatus, &buffer)
if err != nil {
return nil, err
}
return &runtime.RawExtension{
Raw: bytes.TrimSpace(buffer.Bytes()),
}, nil
// EncodeProviderStatus encodes the machine status into RawExtension
func EncodeProviderStatus(codec codec, awsStatus *providerconfigv1.AWSMachineProviderStatus) (*runtime.RawExtension, error) {
return codec.EncodeProviderStatus(awsStatus)
}

// IsMaster returns true if the machine is part of a cluster's control plane
Expand Down