Skip to content

Commit

Permalink
add json tags and tests for providerconfig and provider status
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed Oct 30, 2018
1 parent 13042a4 commit 0626ab1
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 161 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ check: fmt vet lint test ## Check your code

.PHONY: unit
unit: # Run unit test
$(DOCKER_CMD) go test -race -cover ./cmd/... ./pkg/cloud/...
$(DOCKER_CMD) go test -race -cover ./cmd/... ./pkg/...

.PHONY: integration
integration: ## Run integration test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ const (
// It containsk AWS-specific status information.
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type AWSMachineProviderStatus struct {
metav1.TypeMeta
metav1.TypeMeta `json:",inline"`

// InstanceID is the instance ID of the machine created in AWS
InstanceID *string
InstanceID *string `json:"instanceId"`

// InstanceState is the state of the AWS instance for this machine
InstanceState *string
InstanceState *string `json:"instanceState"`

// Conditions is a set of conditions associated with the Machine to indicate
// errors or other status
Conditions []AWSMachineProviderCondition
Conditions []AWSMachineProviderCondition `json:"conditions"`
}

// AWSMachineProviderConditionType is a valid value for AWSMachineProviderCondition.Type
Expand All @@ -63,21 +63,21 @@ const (
// AWSMachineProviderCondition is a condition in a AWSMachineProviderStatus
type AWSMachineProviderCondition struct {
// Type is the type of the condition.
Type AWSMachineProviderConditionType
Type AWSMachineProviderConditionType `json:"type"`
// Status is the status of the condition.
Status corev1.ConditionStatus
Status corev1.ConditionStatus `json:"status"`
// LastProbeTime is the last time we probed the condition.
// +optional
LastProbeTime metav1.Time
LastProbeTime metav1.Time `json:"lastProbeTime"`
// LastTransitionTime is the last time the condition transitioned from one status to another.
// +optional
LastTransitionTime metav1.Time
LastTransitionTime metav1.Time `json:"lastTransitionTime"`
// Reason is a unique, one-word, CamelCase reason for the condition's last transition.
// +optional
Reason string
Reason string `json:"reason"`
// Message is a human-readable message indicating details about last transition.
// +optional
Message string
Message string `json:"message"`
}

// +genclient
Expand All @@ -90,93 +90,93 @@ type AWSMachineProviderConfig struct {
metav1.ObjectMeta `json:"metadata,omitempty"`

// AMI is the reference to the AMI from which to create the machine instance.
AMI AWSResourceReference
AMI AWSResourceReference `json:"ami"`

// InstanceType is the type of instance to create. Example: m4.xlarge
InstanceType string
InstanceType string `json:"instanceType"`

// Tags is the set of tags to add to apply to an instance, in addition to the ones
// added by default by the actuator. These tags are additive. The actuator will ensure
// these tags are present, but will not remove any other tags that may exist on the
// instance.
Tags []TagSpecification
Tags []TagSpecification `json:"tags"`

// IAMInstanceProfile is a reference to an IAM role to assign to the instance
IAMInstanceProfile *AWSResourceReference
IAMInstanceProfile *AWSResourceReference `json:"iamInstanceProfile"`

// UserDataSecret contains a local reference to a secret that contains the
// UserData to apply to the instance
UserDataSecret *corev1.LocalObjectReference
UserDataSecret *corev1.LocalObjectReference `json:"userDataSecret"`

// CredentialsSecret is a reference to the secret with AWS credentials. Otherwise, defaults to permissions
// provided by attached IAM role where the actuator is running.
CredentialsSecret *corev1.LocalObjectReference
CredentialsSecret *corev1.LocalObjectReference `json:"credentialsSecret"`

// KeyName is the name of the KeyPair to use for SSH
KeyName *string
KeyName *string `json:"keyName"`

// DeviceIndex is the index of the device on the instance for the network interface attachment.
// Defaults to 0.
DeviceIndex int64
DeviceIndex int64 `json:"deviceIndex"`

// PublicIP specifies whether the instance should get a public IP. If not present,
// it should use the default of its subnet.
PublicIP *bool
PublicIP *bool `json:"publicIp"`

// SecurityGroups is an array of references to security groups that should be applied to the
// instance.
SecurityGroups []AWSResourceReference
SecurityGroups []AWSResourceReference `json:"securityGroups"`

// Subnet is a reference to the subnet to use for this instance
Subnet AWSResourceReference
Subnet AWSResourceReference `json:"subnet"`

// Placement specifies where to create the instance in AWS
Placement Placement
Placement Placement `json:"placement"`

// LoadBalancerNames is the names of the load balancers to which the new instance
// should be added once it is created.
LoadBalancerNames []string
LoadBalancerNames []string `json:"loadBalancerIds"`
}

// AWSResourceReference is a reference to a specific AWS resource by ID, ARN, or filters.
// Only one of ID, ARN or Filters may be specified. Specifying more than one will result in
// a validation error.
type AWSResourceReference struct {
// ID of resource
ID *string
ID *string `json:"id"`

// ARN of resource
ARN *string
ARN *string `json:"arn"`

// Filters is a set of filters used to identify a resource
Filters []Filter
Filters []Filter `json:"filters"`
}

// Placement indicates where to create the instance in AWS
type Placement struct {
// Region is the region to use to create the instance
Region string
Region string `json:"region"`

// AvailabilityZone is the availability zone of the instance
AvailabilityZone string
AvailabilityZone string `json:"availabilityZone"`
}

// Filter is a filter used to identify an AWS resource
type Filter struct {
// Name of the filter. Filter names are case-sensitive.
Name string
Name string `json:"name"`

// Values includes one or more filter values. Filter values are case-sensitive.
Values []string
Values []string `json:"values"`
}

// TagSpecification is the name/value pair for a tag
type TagSpecification struct {
// Name of the tag
Name string
Name string `json:"name"`

// Value of the tag
Value string
Value string `json:"value"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down

This file was deleted.

8 changes: 4 additions & 4 deletions pkg/apis/awsproviderconfig/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func NewCodec() (*AWSProviderConfigCodec, error) {
return &codec, nil
}

// DecodeFromProviderConfig deserialises an object from the provider config
func (codec *AWSProviderConfigCodec) DecodeFromProviderConfig(providerConfig clusterv1.ProviderConfig, out runtime.Object) error {
// DecodeProviderConfig deserialises an object from the provider config
func (codec *AWSProviderConfigCodec) DecodeProviderConfig(providerConfig *clusterv1.ProviderConfig, out runtime.Object) error {
if providerConfig.Value != nil {
_, _, err := codec.decoder.Decode(providerConfig.Value.Raw, nil, out)
if err != nil {
Expand All @@ -83,8 +83,8 @@ func (codec *AWSProviderConfigCodec) DecodeFromProviderConfig(providerConfig clu
return nil
}

// EncodeToProviderConfig serialises an object to the provider config
func (codec *AWSProviderConfigCodec) EncodeToProviderConfig(in runtime.Object) (*clusterv1.ProviderConfig, error) {
// EncodeProviderConfig serialises an object to the provider config
func (codec *AWSProviderConfigCodec) EncodeProviderConfig(in runtime.Object) (*clusterv1.ProviderConfig, error) {
var buf bytes.Buffer
if err := codec.encoder.Encode(in, &buf); err != nil {
return nil, fmt.Errorf("encoding failed: %v", err)
Expand Down
139 changes: 139 additions & 0 deletions pkg/apis/awsproviderconfig/v1alpha1/register_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package v1alpha1

import (
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"testing"
"time"
)

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.k8s.io/v1alpha1",
},
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)
}

providerStatusDecoded := &AWSMachineProviderStatus{}
codec.DecodeProviderStatus(providerStatusEncoded, providerStatusDecoded)
if !reflect.DeepEqual(providerStatus, providerStatusDecoded) {
t.Errorf("failed EncodeProviderStatus/DecodeProviderStatus. Expected: %+v, got: %+v", providerStatus, providerStatusDecoded)
}
}

func TestEncodeAndDecodeProviderConfig(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.k8s.io/v1alpha1",
},
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: &apiv1.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,
}

providerConfigEncoded, err := codec.EncodeProviderConfig(providerConfig)
if err != nil {
t.Fatal(err)
}
providerConfigDecoded := &AWSMachineProviderConfig{}
codec.DecodeProviderConfig(providerConfigEncoded, providerConfigDecoded)

if !reflect.DeepEqual(providerConfig, providerConfigDecoded) {
t.Errorf("failed EncodeProviderConfig/DecodeProviderConfig. Expected: %+v, got: %+v", providerConfig, providerConfigDecoded)
}
}
Loading

0 comments on commit 0626ab1

Please sign in to comment.