diff --git a/pkg/actuators/machine/stubs.go b/pkg/actuators/machine/stubs.go index 5661d701d6..544be3241b 100644 --- a/pkg/actuators/machine/stubs.go +++ b/pkg/actuators/machine/stubs.go @@ -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) } @@ -127,7 +123,9 @@ func stubMachine() (*machinev1.Machine, error) { "node-role.kubernetes.io/infra": "", }, }, - ProviderSpec: *providerSpec, + ProviderSpec: machinev1.ProviderSpec{ + Value: providerSpec, + }, }, } diff --git a/pkg/actuators/machineset/controller.go b/pkg/actuators/machineset/controller.go index e6b72f4dc0..90262b3eb2 100644 --- a/pkg/actuators/machineset/controller.go +++ b/pkg/actuators/machineset/controller.go @@ -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) } @@ -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 -} diff --git a/pkg/apis/awsprovider/v1beta1/awsproviderconfig_types.go b/pkg/apis/awsprovider/v1beta1/awsproviderconfig_types.go index c30be1b5db..1bc900872b 100644 --- a/pkg/apis/awsprovider/v1beta1/awsproviderconfig_types.go +++ b/pkg/apis/awsprovider/v1beta1/awsproviderconfig_types.go @@ -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. diff --git a/pkg/apis/awsprovider/v1beta1/register.go b/pkg/apis/awsprovider/v1beta1/register.go index 1f348d0f3f..fb78966576 100644 --- a/pkg/apis/awsprovider/v1beta1/register.go +++ b/pkg/apis/awsprovider/v1beta1/register.go @@ -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 ( @@ -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 } diff --git a/pkg/apis/awsprovider/v1beta1/register_test.go b/pkg/apis/awsprovider/v1beta1/register_test.go deleted file mode 100644 index be38e778b1..0000000000 --- a/pkg/apis/awsprovider/v1beta1/register_test.go +++ /dev/null @@ -1,181 +0,0 @@ -package v1beta1 - -import ( - "reflect" - "testing" - "time" - - "github.com/aws/aws-sdk-go/aws" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestEncodeAndDecodeProviderStatus(t *testing.T) { - - codec, err := NewCodec() - if err != nil { - t.Fatal(err) - } - time := metav1.Time{ - Time: time.Date(2018, 6, 3, 0, 0, 0, 0, time.Local), - } - - instanceState := "running" - instanceID := "id" - providerStatus := &AWSMachineProviderStatus{ - TypeMeta: metav1.TypeMeta{ - Kind: "AWSMachineProviderStatus", - APIVersion: "awsproviderconfig.openshift.io/v1beta1", - }, - InstanceState: &instanceState, - InstanceID: &instanceID, - Conditions: []AWSMachineProviderCondition{ - { - Type: "MachineCreation", - Status: "True", - Reason: "MachineCreationSucceeded", - Message: "machine successfully created", - LastTransitionTime: time, - LastProbeTime: time, - }, - }, - } - providerStatusEncoded, err := codec.EncodeProviderStatus(providerStatus) - if err != nil { - t.Error(err) - } - - // without deep copy - { - providerStatusDecoded := &AWSMachineProviderStatus{} - codec.DecodeProviderStatus(providerStatusEncoded, providerStatusDecoded) - if !reflect.DeepEqual(providerStatus, providerStatusDecoded) { - t.Errorf("failed EncodeProviderStatus/DecodeProviderStatus. Expected: %+v, got: %+v", providerStatus, providerStatusDecoded) - } - } - - // with deep copy - { - providerStatusDecoded := &AWSMachineProviderStatus{} - codec.DecodeProviderStatus(providerStatusEncoded.DeepCopy(), providerStatusDecoded) - if !reflect.DeepEqual(providerStatus, providerStatusDecoded) { - t.Errorf("failed EncodeProviderStatus/DecodeProviderStatus. Expected: %+v, got: %+v", providerStatus, providerStatusDecoded) - } - } -} - -func TestEncodeAndDecodeProviderSpec(t *testing.T) { - codec, err := NewCodec() - if err != nil { - t.Fatal(err) - } - - publicIP := true - amiID := "id" - awsCredentialsSecretName := "test" - clusterID := "test" - - providerConfig := &AWSMachineProviderConfig{ - TypeMeta: metav1.TypeMeta{ - Kind: "AWSMachineProviderConfig", - APIVersion: "awsproviderconfig.openshift.io/v1beta1", - }, - AMI: AWSResourceReference{ - Filters: []Filter{ - { - Name: "tag:image_stage", - Values: []string{"base"}, - }, - { - Name: "tag:operating_system", - Values: []string{"rhel"}, - }, - { - Name: "tag:ready", - Values: []string{"yes"}, - }, - }, - ID: &amiID, - }, - CredentialsSecret: &corev1.LocalObjectReference{ - Name: awsCredentialsSecretName, - }, - InstanceType: "m4.xlarge", - Placement: Placement{ - Region: "us-east-1", - AvailabilityZone: "us-east-1a", - }, - Subnet: AWSResourceReference{ - Filters: []Filter{ - { - Name: "tag:Name", - Values: []string{clusterID}, - }, - }, - }, - Tags: []TagSpecification{ - { - Name: "openshift-node-group-config", - Value: "node-config-master", - }, - { - Name: "host-type", - Value: "master", - }, - { - Name: "sub-host-type", - Value: "default", - }, - }, - SecurityGroups: []AWSResourceReference{ - { - Filters: []Filter{ - { - Name: "tag:Name", - Values: []string{clusterID}, - }, - }, - }, - }, - PublicIP: &publicIP, - IAMInstanceProfile: &AWSResourceReference{ - ID: aws.String("IAMInstanceProfile"), - }, - UserDataSecret: &corev1.LocalObjectReference{ - Name: "userSecret", - }, - KeyName: aws.String("sshkeyname"), - LoadBalancers: []LoadBalancerReference{ - { - Name: "balancer", - Type: ClassicLoadBalancerType, - }, - }, - } - - providerConfigEncoded, err := codec.EncodeProviderSpec(providerConfig) - if err != nil { - t.Fatal(err) - } - - // Without deep copy - { - providerConfigDecoded := &AWSMachineProviderConfig{} - codec.DecodeProviderSpec(providerConfigEncoded, providerConfigDecoded) - - if !reflect.DeepEqual(providerConfig, providerConfigDecoded) { - t.Errorf("failed EncodeProviderSpec/DecodeProviderSpec. Expected: %+v, got: %+v", providerConfig, providerConfigDecoded) - } - } - - // With deep copy - { - providerConfigDecoded := &AWSMachineProviderConfig{} - codec.DecodeProviderSpec(providerConfigEncoded, providerConfigDecoded) - - if !reflect.DeepEqual(providerConfig.DeepCopy(), providerConfigDecoded) { - t.Errorf("failed EncodeProviderSpec/DecodeProviderSpec. Expected: %+v, got: %+v", providerConfig, providerConfigDecoded) - } - } -}