Skip to content

Commit

Permalink
feat: added cluster and machine api definitions
Browse files Browse the repository at this point in the history
The initial api definitions for the capi infrastructure implementations
of cluster, machine and machinetemplate have been added.

The type follow the capi requirements:
*[cluster](https://cluster-api.sigs.k8s.io/developer/providers/cluster-infrastructure.html)
*[machine](https://cluster-api.sigs.k8s.io/developer/providers/machine-infrastructure.html)

The api types are based on a simplified version of the flintlock grpc
api. In capi provider we will make some assumptions/defaults.

Signed-off-by: Richard Case <[email protected]>
  • Loading branch information
richardcase committed Nov 11, 2021
1 parent d8a96a7 commit 1014a50
Show file tree
Hide file tree
Showing 22 changed files with 1,342 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ CRD_OPTIONS ?= "crd:Versions=v1"

.PHONY: generate
generate: ## Runs code generation tooling
$(MAKE) generate-api
$(MAKE) generate-go
$(MAKE) generate-manifests

generate-go: $(CONTROLLER_GEN) $(DEFAULTER_GEN)
$(CONTROLLER_GEN) \
Expand Down
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ resources:
kind: MicrovmMachine
path: github.com/weaveworks/cluster-api-provider-microvm/api/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: false
domain: cluster.x-k8s.io
group: infrastructure
kind: MicrovmMachineTemplate
path: github.com/weaveworks/cluster-api-provider-microvm/api/v1alpha1
version: v1alpha1
version: "3"
51 changes: 46 additions & 5 deletions api/v1alpha1/microvmcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,49 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

const (
// ClusterFinalizer allows ReconcileMicrovmCluster to clean up esources associated with MicrovmCluster before
// removing it from the apiserver.
ClusterFinalizer = "microvmcluster.infrastructure.cluster.x-k8s.io"
)

// MicrovmClusterSpec defines the desired state of MicrovmCluster.
type MicrovmClusterSpec struct {
// Foo is an example field of MicrovmCluster. Edit microvmcluster_types.go to remove/update
Foo string `json:"foo,omitempty"`
// ControlPlaneEndpoint represents the endpoint used to communicate with the control plane.
//
// See https://cluster-api.sigs.k8s.io/developer/architecture/controllers/cluster.html
// for more details.
//
// +optional
ControlPlaneEndpoint clusterv1.APIEndpoint `json:"controlPlaneEndpoint"`
}

// MicrovmClusterStatus defines the observed state of MicrovmCluster.
type MicrovmClusterStatus struct{}
type MicrovmClusterStatus struct {
// Ready indicates that the cluster is ready.
// +optional
// +kubebuilder:default=false
Ready bool `json:"ready"`

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// Conditions defines current service state of the MicrovmCluster.
// +optional
Conditions clusterv1.Conditions `json:"conditions,omitempty"`

// FailureDomains is a list of failure domain objects synced from the infrastructure provider.
FailureDomains clusterv1.FailureDomains `json:"failureDomains,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:storageversion
// +kubebuilder:resource:path=microvmclusters,scope=Namespaced,categories=cluster-api,shortName=mvmc
// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".metadata.labels.cluster\\.x-k8s\\.io/cluster-name",description="Cluster to which this MicrovmCluster belongs"
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.ready",description="Cluster infrastructure is ready"
// +kubebuilder:printcolumn:name="ControlPlaneEndpoint",type="string",JSONPath=".spec.controlPlaneEndpoint[0]",description="API Endpoint",priority=1
// +k8s:defaulter-gen=true

// MicrovmCluster is the Schema for the microvmclusters API.
type MicrovmCluster struct {
Expand All @@ -28,9 +58,20 @@ type MicrovmCluster struct {
Status MicrovmClusterStatus `json:"status,omitempty"`
}

// GetConditions returns the observations of the operational state of the MicrovmCluster resource.
func (r *MicrovmCluster) GetConditions() clusterv1.Conditions {
return r.Status.Conditions
}

// SetConditions sets the underlying service state of the MicrovmCluster to the predescribed clusterv1.Conditions.
func (r *MicrovmCluster) SetConditions(conditions clusterv1.Conditions) {
r.Status.Conditions = conditions
}

//+kubebuilder:object:root=true

// MicrovmClusterList contains a list of MicrovmCluster.
// +k8s:defaulter-gen=true
type MicrovmClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Expand Down
73 changes: 70 additions & 3 deletions api/v1alpha1/microvmmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,83 @@ package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/errors"
)

const (
// MachineFinalizer allows ReconcileMicrovmMachine to clean up resources associated with MicrovmMachine
// before removing it from the apiserver.
MachineFinalizer = "microvmmachine.infrastructure.cluster.x-k8s.io"
)

// MicrovmMachineSpec defines the desired state of MicrovmMachine.
type MicrovmMachineSpec struct {
// Foo is an example field of MicrovmMachine. Edit microvmmachine_types.go to remove/update
Foo string `json:"foo,omitempty"`
MicrovmSpec `json:",inline"`

// ProviderID is the unique identifier as specified by the cloud provider.
ProviderID *string `json:"providerID,omitempty"`

// FailureDomain is the failure domain unique identifier this Machine should be attached to, as defined in Cluster API.
// For the microvm provider, the name is equivalent to the name of the host running flintlock.
FailureDomain *string `json:"failureDomain,omitempty"`
}

// MicrovmMachineStatus defines the observed state of MicrovmMachine.
type MicrovmMachineStatus struct{}
type MicrovmMachineStatus struct {
// Ready is true when the provider resource is ready.
// +optional
// +kubebuilder:default=false
Ready bool `json:"ready"`

// VMState indicates the state of the microvm.
VMState *VMState `json:"vmState,omitempty"`

// Addresses contains the microvm associated addresses.
Addresses []clusterv1.MachineAddress `json:"addresses,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 MicrovmMachine.
// +optional
Conditions clusterv1.Conditions `json:"conditions,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
Expand Down
13 changes: 12 additions & 1 deletion api/v1alpha1/microvmmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
package v1alpha1

import (
"reflect"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -38,7 +41,15 @@ func (r *MicrovmMachine) ValidateDelete() error {

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *MicrovmMachine) ValidateUpdate(old runtime.Object) error {
return nil
var allErrs field.ErrorList

previous, _ := old.(*MicrovmMachine)

if !reflect.DeepEqual(r.Spec, previous.Spec) {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "microvm machine spec is immutable"))
}

return aggregateObjErrors(r.GroupVersionKind().GroupKind(), r.Name, allErrs)
}

// Default satisfies the defaulting webhook interface.
Expand Down
39 changes: 39 additions & 0 deletions api/v1alpha1/microvmmachinetemplate_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2021 Weaveworks or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MPL-2.0

package v1alpha1

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

// MicrovmMachineTemplateSpec defines the desired state of MicrovmMachineTemplate.
type MicrovmMachineTemplateSpec struct {
Template MicrovmMachineTemplateResource `json:"template"`
}

// +kubebuilder:object:root=true
// +kubebuilder:storageversion
// +kubebuilder:resource:path=microvmmachinetemplates,scope=Namespaced,categories=cluster-api,shortName=mvmmt
// +k8s:defaulter-gen=true

// MicrovmMachineTemplate is the Schema for the microvmmachinetemplates API.
type MicrovmMachineTemplate struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec MicrovmMachineTemplateSpec `json:"spec,omitempty"`
}

//+kubebuilder:object:root=true

// MicrovmMachineTemplateList contains a list of MicrovmMachineTemplate.
type MicrovmMachineTemplateList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []MicrovmMachineTemplate `json:"items"`
}

func init() {
SchemeBuilder.Register(&MicrovmMachineTemplate{}, &MicrovmMachineTemplateList{})
}
38 changes: 38 additions & 0 deletions api/v1alpha1/microvmmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2021 Weaveworks or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MPL-2.0

package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

var _ = logf.Log.WithName("microvmmachinetemplate-resource")

func (r *MicrovmMachineTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha1-microvmmachinetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=microvmmachinetemplates,versions=v1alpha1,name=validation.microvmmachinetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1beta1

var _ webhook.Validator = &MicrovmMachineTemplate{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *MicrovmMachineTemplate) ValidateCreate() error {
return nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *MicrovmMachineTemplate) ValidateDelete() error {
return nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *MicrovmMachineTemplate) ValidateUpdate(old runtime.Object) error {
return nil
}
Loading

0 comments on commit 1014a50

Please sign in to comment.