-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reconciler default timout, cluster and machine scopes, machine `R…
…econcile` function, and `InstanceStatus` enums (#24) * Add `LinodeClients` and `InstanceStatus` * Add default reconciler timeout * go mod tidy * Add cluster scope * Correct naming * Add linode client directly to * Bump linodego to 1.24.1 * Implemented multiple functions for the cluster scope * Added MachineScope * Fill up Reconcile function * Add newly implemented packages to docker file * go mod tidy --------- Co-authored-by: Zhiwei Liang <[email protected]>
- Loading branch information
1 parent
ba2b0a9
commit 2b883e9
Showing
8 changed files
with
404 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.