Skip to content

Commit

Permalink
Refactor provider spec and status serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Demichev committed Apr 6, 2020
1 parent 1bec1e1 commit c72c26f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 273 deletions.
10 changes: 4 additions & 6 deletions pkg/actuators/machine/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ func stubProviderConfig() *awsproviderv1.AWSMachineProviderConfig {
func stubMachine() (*machinev1.Machine, error) {
machinePc := stubProviderConfig()

codec, err := awsproviderv1.NewCodec()
if err != nil {
return nil, fmt.Errorf("failed creating codec: %v", err)
}
providerSpec, err := codec.EncodeProviderSpec(machinePc)
providerSpec, err := awsproviderv1.RawExtensionFromProviderSpec(machinePc)
if err != nil {
return nil, fmt.Errorf("codec.EncodeProviderSpec failed: %v", err)
}
Expand All @@ -127,7 +123,9 @@ func stubMachine() (*machinev1.Machine, error) {
"node-role.kubernetes.io/infra": "",
},
},
ProviderSpec: *providerSpec,
ProviderSpec: machinev1.ProviderSpec{
Value: providerSpec,
},
},
}

Expand Down
16 changes: 1 addition & 15 deletions pkg/actuators/machineset/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func isInvalidConfigurationError(err error) bool {
}

func reconcile(machineSet *machinev1.MachineSet) (ctrl.Result, error) {
providerConfig, err := getProviderConfig(*machineSet)
providerConfig, err := awsproviderv1.ProviderSpecFromRawExtension(machineSet.Spec.Template.Spec.ProviderSpec.Value)
if err != nil {
return ctrl.Result{}, mapierrors.InvalidMachineConfiguration("failed to get providerConfig: %v", err)
}
Expand All @@ -127,17 +127,3 @@ func reconcile(machineSet *machinev1.MachineSet) (ctrl.Result, error) {

return ctrl.Result{}, nil
}

func getProviderConfig(machineSet machinev1.MachineSet) (*awsproviderv1.AWSMachineProviderConfig, error) {
codec, err := awsproviderv1.NewCodec()
if err != nil {
return nil, fmt.Errorf("failed to create codec: %v", err)
}

var providerConfig awsproviderv1.AWSMachineProviderConfig
if err := codec.DecodeProviderSpec(&machineSet.Spec.Template.Spec.ProviderSpec, &providerConfig); err != nil {
return nil, fmt.Errorf("failed to decode machineSet provider config: %v", err)
}

return &providerConfig, nil
}
7 changes: 0 additions & 7 deletions pkg/apis/awsprovider/v1beta1/awsproviderconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// Annotation constants
const (
// ClusterIDLabel is the label that a machineset must have to identify the
// cluster to which it belongs.
ClusterIDLabel = "machine.openshift.io/cluster-api-cluster"
)

// 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
113 changes: 49 additions & 64 deletions pkg/apis/awsprovider/v1beta1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ limitations under the License.
package v1beta1

import (
"bytes"
"encoding/json"
"fmt"

machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/scheme"
"sigs.k8s.io/yaml"
)

var (
Expand All @@ -43,81 +43,66 @@ var (
SchemeBuilder = &scheme.Builder{GroupVersion: SchemeGroupVersion}
)

// AWSProviderConfigCodec is a runtime codec for the provider configuration
// +k8s:deepcopy-gen=false
type AWSProviderConfigCodec struct {
encoder runtime.Encoder
decoder runtime.Decoder
}

// NewScheme creates a new Scheme
func NewScheme() (*runtime.Scheme, error) {
return SchemeBuilder.Build()
}

// NewCodec creates a serializer/deserializer for the provider configuration
func NewCodec() (*AWSProviderConfigCodec, error) {
scheme, err := NewScheme()
if err != nil {
return nil, err
// RawExtensionFromProviderSpec marshals the machine provider spec.
func RawExtensionFromProviderSpec(spec *AWSMachineProviderConfig) (*runtime.RawExtension, error) {
if spec == nil {
return &runtime.RawExtension{}, nil
}
codecFactory := serializer.NewCodecFactory(scheme)
encoder, err := newEncoder(&codecFactory)
if err != nil {
return nil, err
}
codec := AWSProviderConfigCodec{
encoder: encoder,
decoder: codecFactory.UniversalDecoder(SchemeGroupVersion),

var rawBytes []byte
var err error
if rawBytes, err = json.Marshal(spec); err != nil {
return nil, fmt.Errorf("error marshalling providerSpec: %v", err)
}
return &codec, nil

return &runtime.RawExtension{
Raw: rawBytes,
}, nil
}

// DecodeProviderSpec deserialises an object from the provider config
func (codec *AWSProviderConfigCodec) DecodeProviderSpec(providerSpec *machinev1.ProviderSpec, out runtime.Object) error {
if providerSpec.Value != nil {
if _, _, err := codec.decoder.Decode(providerSpec.Value.Raw, nil, out); err != nil {
return fmt.Errorf("decoding failure: %v", err)
}
// RawExtensionFromProviderStatus marshals the machine provider status
func RawExtensionFromProviderStatus(status *AWSMachineProviderStatus) (*runtime.RawExtension, error) {
if status == nil {
return &runtime.RawExtension{}, nil
}
return nil
}

// EncodeProviderSpec serialises an object to the provider config
func (codec *AWSProviderConfigCodec) EncodeProviderSpec(in runtime.Object) (*machinev1.ProviderSpec, error) {
var buf bytes.Buffer
if err := codec.encoder.Encode(in, &buf); err != nil {
return nil, fmt.Errorf("encoding failed: %v", err)
var rawBytes []byte
var err error
if rawBytes, err = json.Marshal(status); err != nil {
return nil, fmt.Errorf("error marshalling providerStatus: %v", err)
}
return &machinev1.ProviderSpec{
Value: &runtime.RawExtension{Raw: buf.Bytes()},

return &runtime.RawExtension{
Raw: rawBytes,
}, nil
}

// EncodeProviderStatus serialises the provider status
func (codec *AWSProviderConfigCodec) EncodeProviderStatus(in runtime.Object) (*runtime.RawExtension, error) {
var buf bytes.Buffer
if err := codec.encoder.Encode(in, &buf); err != nil {
return nil, fmt.Errorf("encoding failed: %v", err)
// ProviderSpecFromRawExtension unmarshals a raw extension into an AWSMachineProviderSpec type
func ProviderSpecFromRawExtension(rawExtension *runtime.RawExtension) (*AWSMachineProviderConfig, error) {
if rawExtension == nil {
return &AWSMachineProviderConfig{}, nil
}
return &runtime.RawExtension{Raw: buf.Bytes()}, nil
}

// DecodeProviderStatus deserialises the provider status
func (codec *AWSProviderConfigCodec) DecodeProviderStatus(providerStatus *runtime.RawExtension, out runtime.Object) error {
if providerStatus != nil {
if _, _, err := codec.decoder.Decode(providerStatus.Raw, nil, out); err != nil {
return fmt.Errorf("decoding failure: %v", err)
}
spec := new(AWSMachineProviderConfig)
if err := yaml.Unmarshal(rawExtension.Raw, &spec); err != nil {
return nil, fmt.Errorf("error unmarshalling providerSpec: %v", err)
}
return nil

klog.V(5).Infof("Got provider Spec from raw extension: %+v", spec)
return spec, nil
}

func newEncoder(codecFactory *serializer.CodecFactory) (runtime.Encoder, error) {
serializerInfos := codecFactory.SupportedMediaTypes()
if len(serializerInfos) == 0 {
return nil, fmt.Errorf("unable to find any serlializers")
// ProviderStatusFromRawExtension unmarshals a raw extension into an AWSMachineProviderStatus type
func ProviderStatusFromRawExtension(rawExtension *runtime.RawExtension) (*AWSMachineProviderStatus, error) {
if rawExtension == nil {
return &AWSMachineProviderStatus{}, nil
}
encoder := codecFactory.EncoderForVersion(serializerInfos[0].Serializer, SchemeGroupVersion)
return encoder, nil

providerStatus := new(AWSMachineProviderStatus)
if err := yaml.Unmarshal(rawExtension.Raw, providerStatus); err != nil {
return nil, fmt.Errorf("error unmarshalling providerStatus: %v", err)
}

klog.V(5).Infof("Got provider Status from raw extension: %+v", providerStatus)
return providerStatus, nil
}
181 changes: 0 additions & 181 deletions pkg/apis/awsprovider/v1beta1/register_test.go

This file was deleted.

0 comments on commit c72c26f

Please sign in to comment.