Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reconciler default timout, cluster and machine scopes, machine Reconcile function, and InstanceStatus enums #24

Merged
merged 12 commits into from
Nov 10, 2023
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ RUN go mod download
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY controller/ controller/
COPY cloud/ cloud/
COPY util/ util/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
34 changes: 34 additions & 0 deletions api/v1alpha1/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2023 Akamai Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

type InstanceStatus string

var (
InstanceStatusRunning = InstanceStatus("running")
InstanceStatusOffline = InstanceStatus("offline")
InstanceStatusBooting = InstanceStatus("booting")
InstanceStatusRebooting = InstanceStatus("rebooting")
InstanceStatusShuttingDown = InstanceStatus("shutting_down")
InstanceStatusProvisioning = InstanceStatus("provisioning")
InstanceStatusDeleting = InstanceStatus("deleting")
InstanceStatusMigrating = InstanceStatus("migrating")
InstanceStatusRebuilding = InstanceStatus("rebuilding")
InstanceStatusCloning = InstanceStatus("cloning")
InstanceStatusRestoring = InstanceStatus("restoring")
InstanceStatusStopped = InstanceStatus("stopped")
)
105 changes: 105 additions & 0 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
Copyright 2023 Akamai Technologies, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package scope

import (
"context"
"fmt"
"net/http"
"os"

infrav1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
"github.com/linode/linodego"
"golang.org/x/oauth2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// ClusterScopeParams defines the input parameters used to create a new Scope.
type ClusterScopeParams struct {
Client client.Client
Cluster *clusterv1.Cluster
LinodeClient *linodego.Client
LinodeCluster *infrav1.LinodeCluster
}

func validateClusterScopeParams(params ClusterScopeParams) error {
if params.Cluster == nil {
return fmt.Errorf("Cluster is required when creating a ClusterScope")
}
if params.LinodeCluster == nil {
return fmt.Errorf("LinodeCluster is required when creating a ClusterScope")
}
return nil
}

func createLinodeClient() (*linodego.Client, error) {
apiKey, ok := os.LookupEnv("LINODE_TOKEN")
if !ok {
return nil, fmt.Errorf("failed to get LINODE_TOKEN environment variable")
}
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: apiKey})

oauth2Client := &http.Client{
Transport: &oauth2.Transport{
Source: tokenSource,
},
}
linodeClient := linodego.NewClient(oauth2Client)
return &linodeClient, nil
}

// NewClusterScope creates a new Scope from the supplied parameters.
// This is meant to be called for each reconcile iteration.
func NewClusterScope(ctx context.Context, params ClusterScopeParams) (*ClusterScope, error) {
// TODO
if err := validateClusterScopeParams(params); err != nil {
return nil, err
}

if params.LinodeClient == nil {
if linodeClient, err := createLinodeClient(); err != nil {
return nil, err
} else {
params.LinodeClient = linodeClient
}
}

helper, err := patch.NewHelper(params.LinodeCluster, params.Client)
if err != nil {
return nil, fmt.Errorf("failed to init patch helper: %w", err)
}

return &ClusterScope{
client: params.Client,
Cluster: params.Cluster,
LinodeClient: params.LinodeClient,
LinodeCluster: params.LinodeCluster,
patchHelper: helper,
}, nil
}

// ClusterScope defines the basic context for an actuator to operate upon.
type ClusterScope struct {
client client.Client
patchHelper *patch.Helper

LinodeClient *linodego.Client
Cluster *clusterv1.Cluster
LinodeCluster *infrav1.LinodeCluster
}
64 changes: 64 additions & 0 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package scope

import (
"fmt"

infrav1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type MachineScopeParams struct {
Client client.Client
Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeCluster *infrav1.LinodeCluster
LinodeMachine *infrav1.LinodeMachine
}

type MachineScope struct {
client client.Client
patchHelper *patch.Helper

Cluster *clusterv1.Cluster
Machine *clusterv1.Machine
LinodeCluster *infrav1.LinodeCluster
LinodeMachine *infrav1.LinodeMachine
}

func validateMachineScopeParams(params MachineScopeParams) error {
if params.Cluster == nil {
return fmt.Errorf("Cluster is required when creating a MachineScope")
}
if params.Machine == nil {
return fmt.Errorf("Machine is required when creating a MachineScope")
}
if params.LinodeCluster == nil {
return fmt.Errorf("LinodeCluster is required when creating a MachineScope")
}
if params.LinodeMachine == nil {
return fmt.Errorf("LinodeMachine is required when creating a MachineScope")
}
return nil
}

func NewMachineScope(params MachineScopeParams) (*MachineScope, error) {
if err := validateMachineScopeParams(params); err != nil {
return nil, err
}

helper, err := patch.NewHelper(params.LinodeMachine, params.Client)
if err != nil {
return nil, fmt.Errorf("failed to init patch helper: %w", err)
}

return &MachineScope{
client: params.Client,
patchHelper: helper,
Cluster: params.Cluster,
Machine: params.Machine,
LinodeCluster: params.LinodeCluster,
LinodeMachine: params.LinodeMachine,
}, nil
}
86 changes: 81 additions & 5 deletions controller/linodemachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ package controller

import (
"context"
"fmt"
"time"

"github.com/linode/cluster-api-provider-linode/cloud/scope"
"github.com/linode/cluster-api-provider-linode/util/reconciler"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/annotations"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

infrastructurev1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
infrav1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1"
)

// LinodeMachineReconciler reconciles a LinodeMachine object
type LinodeMachineReconciler struct {
client.Client
Scheme *runtime.Scheme
Scheme *runtime.Scheme
ReconcileTimeout time.Duration
}

//+kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=linodemachines,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -47,16 +53,86 @@ type LinodeMachineReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *LinodeMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)
ctx, cancel := context.WithTimeout(ctx, reconciler.DefaultedLoopTimeout(r.ReconcileTimeout))
defer cancel()

log := ctrl.LoggerFrom(ctx)

// TODO(user): your logic here
linodeMachine := &infrav1.LinodeMachine{}
if err := r.Get(ctx, req.NamespacedName, linodeMachine); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

machine, err := util.GetOwnerMachine(ctx, r.Client, linodeMachine.ObjectMeta)
if err != nil {
return ctrl.Result{}, err
}
if machine == nil {
log.Info("Machine Controller has not yet set OwnerRef")
return ctrl.Result{}, nil
}

log = log.WithValues("Linode machine: ", machine.Name)
cluster, err := util.GetClusterFromMetadata(ctx, r.Client, machine.ObjectMeta)
if err != nil {
log.Info("Machine is missing cluster label or cluster does not exist")
return ctrl.Result{}, nil
}

linodeCluster := &infrav1.LinodeCluster{}
linodeClusterKey := client.ObjectKey{
Namespace: linodeMachine.Namespace,
Name: cluster.Spec.InfrastructureRef.Name,
}

if err := r.Client.Get(ctx, linodeClusterKey, linodeCluster); err != nil {
log.Info("LinodeCluster is not available yet")
return ctrl.Result{}, nil
}

if annotations.IsPaused(cluster, linodeCluster) {
log.Info("LinodeMachine or linked Cluster is marked as paused. Won't reconcile")
return ctrl.Result{}, nil
}

clusterScope, err := scope.NewClusterScope(
ctx,
scope.ClusterScopeParams{
Client: r.Client,
Cluster: cluster,
LinodeCluster: linodeCluster,
},
)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create scope: %w", err)
}

machineScope, err := scope.NewMachineScope(
scope.MachineScopeParams{
Client: r.Client,
Cluster: cluster,
Machine: machine,
LinodeCluster: linodeCluster,
LinodeMachine: linodeMachine,
},
)

return r.reconcile(ctx, machineScope, clusterScope)
}

func (r *LinodeMachineReconciler) reconcile(
ctx context.Context,
machineScope *scope.MachineScope,
clusterScope *scope.ClusterScope,
) (ctrl.Result, error) {
// TODO
return ctrl.Result{}, nil
}

// SetupWithManager sets up the controller with the Manager.
func (r *LinodeMachineReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&infrastructurev1alpha1.LinodeMachine{}).
For(&infrav1.LinodeMachine{}).
Complete(r)
}
13 changes: 10 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ module github.com/linode/cluster-api-provider-linode
go 1.20

require (
github.com/linode/linodego v1.24.1
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.28.1
golang.org/x/oauth2 v0.10.0
k8s.io/apimachinery v0.28.3
k8s.io/client-go v0.28.3
sigs.k8s.io/cluster-api v1.5.3
sigs.k8s.io/controller-runtime v0.16.3
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
Expand All @@ -22,7 +27,9 @@ require (
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-resty/resty/v2 v2.10.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gobuffalo/flect v1.0.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -31,7 +38,7 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -49,16 +56,16 @@ require (
go.uber.org/zap v1.25.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.3 // indirect
Expand Down
Loading