Skip to content

Commit

Permalink
Fit machine creation to CAPI flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Kovacs committed Dec 12, 2023
1 parent d63ee72 commit 262324d
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 135 deletions.
156 changes: 116 additions & 40 deletions api/v1alpha1/linodemachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,143 @@ package v1alpha1
import (
"github.com/linode/linodego"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/errors"
)

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

// LinodeMachineSpec defines the desired state of LinodeMachine
type LinodeMachineSpec struct {
Region string `json:"region"`
Type string `json:"type"`
Label string `json:"label,omitempty"`
Group string `json:"group,omitempty"`
RootPass string `json:"rootPass,omitempty"`
AuthorizedKeys []string `json:"authorizedKeys,omitempty"`
AuthorizedUsers []string `json:"authorizedUsers,omitempty"`
StackScriptID int `json:"stackscriptId,omitempty"`
StackScriptData map[string]string `json:"stackscriptData,omitempty"`
BackupID int `json:"backupId,omitempty"`
Image string `json:"image,omitempty"`
Interfaces []InstanceConfigInterfaceCreateOptions `json:"interfaces,omitempty"`
BackupsEnabled bool `json:"backupsEnabled,omitempty"`
PrivateIP bool `json:"privateIp,omitempty"`
Tags []string `json:"tags,omitempty"`
Metadata *InstanceMetadataOptions `json:"metadata,omitempty"`
FirewallID int `json:"firewallId,omitempty"`
// ProviderID is the unique identifier as specified by the cloud provider.
ProviderID *string `json:"providerID,omitempty"`
// InstanceID is the Linode instance ID for this machine.
InstanceID *int `json:"instanceID,omitempty"`

// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Region string `json:"region"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Type string `json:"type"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Label string `json:"label,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Group string `json:"group,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
RootPass string `json:"rootPass,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
AuthorizedKeys []string `json:"authorizedKeys,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
AuthorizedUsers []string `json:"authorizedUsers,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
StackScriptID int `json:"stackscriptId,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
StackScriptData map[string]string `json:"stackscriptData,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
BackupID int `json:"backupId,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Image string `json:"image,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Interfaces []InstanceConfigInterfaceCreateOptions `json:"interfaces,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
BackupsEnabled bool `json:"backupsEnabled,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
PrivateIP bool `json:"privateIp,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Tags []string `json:"tags,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Metadata *InstanceMetadataOptions `json:"metadata,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
FirewallID int `json:"firewallId,omitempty"`
}

// InstanceMetadataOptions defines metadata of instance
type InstanceMetadataOptions struct {
// UserData expects a Base64-encoded string
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
UserData string `json:"userData,omitempty"`
}

// InstanceConfigInterfaceCreateOptions defines network interface config
type InstanceConfigInterfaceCreateOptions struct {
IPAMAddress string `json:"ipamAddress,omitempty"`
Label string `json:"label,omitempty"`
Purpose linodego.ConfigInterfacePurpose `json:"purpose,omitempty"`
Primary bool `json:"primary,omitempty"`
SubnetID *int `json:"subnetId,omitempty"`
IPv4 *VPCIPv4 `json:"ipv4,omitempty"`
IPRanges []string `json:"ipRanges,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
IPAMAddress string `json:"ipamAddress,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Label string `json:"label,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Purpose linodego.ConfigInterfacePurpose `json:"purpose,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
Primary bool `json:"primary,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
SubnetID *int `json:"subnetId,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
IPv4 *VPCIPv4 `json:"ipv4,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
IPRanges []string `json:"ipRanges,omitempty"`
}

// VPCIPv4 defines VPC IPV4 settings
type VPCIPv4 struct {
VPC string `json:"vpc,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
VPC string `json:"vpc,omitempty"`
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Value is immutable"
NAT1To1 string `json:"nat1to1,omitempty"`
}

// LinodeMachineStatus defines the observed state of LinodeMachine
type LinodeMachineStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Provisioning Provisioning `json:"provisioning,omitempty"`
}

// Provisioning defines the last known stage of provisioning
type Provisioning struct {
LinodeID *int `json:"linodeId,omitempty"`
LastKnownState linodego.InstanceStatus `json:"lastKnownState,omitempty"`
// Ready is true when the provider resource is ready.
// +optional
Ready bool `json:"ready"`

// Addresses contains the Linode instance associated addresses.
Addresses []clusterv1.MachineAddress `json:"addresses,omitempty"`

// InstanceState is the state of the Linode instance for this machine.
// +optional
InstanceState *linodego.InstanceStatus `json:"instanceState,omitempty"`

// FailureReason will be set in the event that there is a terminal problem
// reconciling the Machine and will contain a succinct value suitable
// for machine interpretation.
//
// This field should not be set for transitive errors that a controller
// faces that are expected to be fixed automatically over
// time (like service outages), but instead indicate that something is
// fundamentally wrong with the Machine's spec or the configuration of
// the controller, and that manual intervention is required. Examples
// of terminal errors would be invalid combinations of settings in the
// spec, values that are unsupported by the controller, or the
// responsible controller itself being critically misconfigured.
//
// Any transient errors that occur during the reconciliation of Machines
// can be added as events to the Machine object and/or logged in the
// controller's output.
// +optional
FailureReason *errors.MachineStatusError `json:"failureReason,omitempty"`

// FailureMessage will be set in the event that there is a terminal problem
// reconciling the Machine and will contain a more verbose string suitable
// for logging and human consumption.
//
// This field should not be set for transitive errors that a controller
// faces that are expected to be fixed automatically over
// time (like service outages), but instead indicate that something is
// fundamentally wrong with the Machine's spec or the configuration of
// the controller, and that manual intervention is required. Examples
// of terminal errors would be invalid combinations of settings in the
// spec, values that are unsupported by the controller, or the
// responsible controller itself being critically misconfigured.
//
// Any transient errors that occur during the reconciliation of Machines
// can be added as events to the Machine object and/or logged in the
// controller's output.
// +optional
FailureMessage *string `json:"failureMessage,omitempty"`

// Conditions defines current service state of the LinodeMachine.
// +optional
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
Expand All @@ -93,13 +170,12 @@ type LinodeMachine struct {
Status LinodeMachineStatus `json:"status,omitempty"`
}

// GetInstanceID returns the instance ID or 0 if missing
func (lm *LinodeMachine) GetInstanceID() int {
if lm.Status.Provisioning.LinodeID == nil {
return 0
}
func (lm *LinodeMachine) GetConditions() clusterv1.Conditions {
return lm.Status.Conditions
}

return *lm.Status.Provisioning.LinodeID
func (lm *LinodeMachine) SetConditions(conditions clusterv1.Conditions) {
lm.Status.Conditions = conditions
}

//+kubebuilder:object:root=true
Expand Down
61 changes: 40 additions & 21 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@ func main() {
os.Exit(1)
}

var machineWatchFilter string
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flag.StringVar(&machineWatchFilter, "machine-watch-filter", "", "The machines to watch by label.")
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
Expand Down Expand Up @@ -106,9 +108,11 @@ func main() {
os.Exit(1)
}
if err = (&controller2.LinodeMachineReconciler{
LinodeApiKey: linodeToken,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("LinodeMachineReconciler"),
WatchFilterValue: machineWatchFilter,
LinodeApiKey: linodeToken,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "LinodeMachine")
os.Exit(1)
Expand Down
Loading

0 comments on commit 262324d

Please sign in to comment.