Skip to content

Commit

Permalink
move provider config for new layout
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed Oct 17, 2018
1 parent b4dd7a0 commit 9d5de45
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 1,310 deletions.
144 changes: 133 additions & 11 deletions pkg/apis/awsproviderconfig/v1alpha1/awsmachineproviderconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,58 @@ limitations under the License.
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// 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.

// AWSMachineProviderConfigSpec defines the desired state of AWSMachineProviderConfig
type AWSMachineProviderConfigSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
// AWSMachineProviderStatus is the type that will be embedded in a Machine.Status.ProviderStatus field.
// It containsk AWS-specific status information.
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
type AWSMachineProviderStatus struct {
metav1.TypeMeta

// InstanceID is the instance ID of the machine created in AWS
InstanceID *string

// InstanceState is the state of the AWS instance for this machine
InstanceState *string

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

// AWSMachineProviderConfigStatus defines the observed state of AWSMachineProviderConfig
type AWSMachineProviderConfigStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
// AWSMachineProviderConditionType is a valid value for AWSMachineProviderCondition.Type
type AWSMachineProviderConditionType string

// Valid conditions for an AWS machine instance
const (
// MachineCreation indicates whether the machine has been created or not. If not,
// it should include a reason and message for the failure.
MachineCreation AWSMachineProviderConditionType = "MachineCreation"
)

// AWSMachineProviderCondition is a condition in a AWSMachineProviderStatus
type AWSMachineProviderCondition struct {
// Type is the type of the condition.
Type AWSMachineProviderConditionType
// Status is the status of the condition.
Status corev1.ConditionStatus
// LastProbeTime is the last time we probed the condition.
// +optional
LastProbeTime metav1.Time
// LastTransitionTime is the last time the condition transitioned from one status to another.
// +optional
LastTransitionTime metav1.Time
// Reason is a unique, one-word, CamelCase reason for the condition's last transition.
// +optional
Reason string
// Message is a human-readable message indicating details about last transition.
// +optional
Message string
}

// +genclient
Expand All @@ -44,8 +80,94 @@ type AWSMachineProviderConfig struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec AWSMachineProviderConfigSpec `json:"spec,omitempty"`
Status AWSMachineProviderConfigStatus `json:"status,omitempty"`
// AMI is the reference to the AMI from which to create the machine instance.
AMI AWSResourceReference

// InstanceType is the type of instance to create. Example: m4.xlarge
InstanceType string

// 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

// IAMInstanceProfile is a reference to an IAM role to assign to the instance
IAMInstanceProfile *AWSResourceReference

// UserDataSecret contains a local reference to a secret that contains the
// UserData to apply to the instance
UserDataSecret *corev1.LocalObjectReference

// 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

// KeyName is the name of the KeyPair to use for SSH
KeyName *string

// DeviceIndex is the index of the device on the instance for the network interface attachment.
// Defaults to 0.
DeviceIndex int64

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

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

// Subnet is a reference to the subnet to use for this instance
Subnet AWSResourceReference

// Placement specifies where to create the instance in AWS
Placement Placement

// LoadBalancerIDs is the IDs of the load balancers to which the new instance
// should be added once it is created.
LoadBalancerIDs []AWSResourceReference
}

// 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

// ARN of resource
ARN *string

// Filters is a set of filters used to identify a resource
Filters []Filter
}

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

// AvailabilityZone is the availability zone of the instance
AvailabilityZone string
}

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

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

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

// Value of the tag
Value string
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand All @@ -58,5 +180,5 @@ type AWSMachineProviderConfigList struct {
}

func init() {
SchemeBuilder.Register(&AWSMachineProviderConfig{}, &AWSMachineProviderConfigList{})
SchemeBuilder.Register(&AWSMachineProviderConfig{}, &AWSMachineProviderConfigList{}, AWSMachineProviderStatus{})
}
87 changes: 87 additions & 0 deletions pkg/apis/awsproviderconfig/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ limitations under the License.
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/runtime/scheme"
"fmt"
"bytes"
)

var (
Expand All @@ -36,3 +41,85 @@ var (
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
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
}
codecFactory := serializer.NewCodecFactory(scheme)
encoder, err := newEncoder(&codecFactory)
if err != nil {
return nil, err
}
codec := AWSProviderConfigCodec{
encoder: encoder,
decoder: codecFactory.UniversalDecoder(SchemeGroupVersion),
}
return &codec, nil
}

// DecodeFromProviderConfig deserialises an object from the provider config
func (codec *AWSProviderConfigCodec) DecodeFromProviderConfig(providerConfig clusterv1.ProviderConfig, out runtime.Object) error {
if providerConfig.Value != nil {
_, _, err := codec.decoder.Decode(providerConfig.Value.Raw, nil, out)
if err != nil {
return fmt.Errorf("decoding failure: %v", err)
}
}
return nil
}

// EncodeToProviderConfig serialises an object to the provider config
func (codec *AWSProviderConfigCodec) EncodeToProviderConfig(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)
}
return &clusterv1.ProviderConfig{
Value: &runtime.RawExtension{Raw: buf.Bytes()},
}, 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)
}

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

// DecodeProviderStatus serialises the provider status
func (codec *AWSProviderConfigCodec) DecodeProviderStatus(providerStatus *runtime.RawExtension, out runtime.Object) error {
if providerStatus != nil {
_, _, err := codec.decoder.Decode(providerStatus.Raw, nil, out)
if err != nil {
return fmt.Errorf("decoding failure: %v", err)
}
}
return 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")
}
encoder := codecFactory.EncoderForVersion(serializerInfos[0].Serializer, SchemeGroupVersion)
return encoder, nil
}
Loading

0 comments on commit 9d5de45

Please sign in to comment.