Skip to content

Commit

Permalink
Start building out template controller
Browse files Browse the repository at this point in the history
  • Loading branch information
detiber committed Dec 11, 2020
1 parent 0886904 commit 19a3cce
Show file tree
Hide file tree
Showing 11 changed files with 769 additions and 34 deletions.
39 changes: 39 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,42 @@ rules:
- get
- patch
- update
- apiGroups:
- tinkerbell.org
resources:
- hardware
- hardware/status
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- tinkerbell.org
resources:
- templates
- templates/status
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- tinkerbell.org
resources:
- workflows
- workflows/status
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ go 1.15

require (
github.com/go-logr/logr v0.1.0
github.com/tinkerbell/tink v0.0.0-20201210163923-6d9159b63857
google.golang.org/grpc v1.32.0
k8s.io/api v0.17.14
k8s.io/apimachinery v0.17.14
k8s.io/client-go v0.17.14
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
k8s.io/utils v0.0.0-20201110183641-67b214c5f920
sigs.k8s.io/cluster-api v0.3.11
sigs.k8s.io/controller-runtime v0.5.11
)
370 changes: 370 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tilt-provider.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"config": {
"image": "quay.io/tinkerbell/cluster-api-provider-tinkerbell",
"live_reload_deps": [
"main.go", "go.mod", "go.sum", "api", "controllers"
"main.go", "go.mod", "go.sum", "api", "controllers", "tink"
]
}
}
10 changes: 7 additions & 3 deletions tink/api/v1alpha1/hardware_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// HardwareIDAnnotation is used by the controller to store the
// ID assigned to the workflow by Tinkerbell.
const HardwareIDAnnotation = "hardware.tinkerbell.org/id"
const (
// HardwareIDAnnotation is used by the controller to store the
// ID assigned to the workflow by Tinkerbell.
HardwareIDAnnotation = "hardware.tinkerbell.org/id"

HardwareFinalizer = "hardware.tinkerbell.org"
)

// HardwareSpec defines the desired state of Hardware.
type HardwareSpec struct {
Expand Down
3 changes: 2 additions & 1 deletion tink/api/v1alpha1/template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ const (

// TemplateSpec defines the desired state of Template.
type TemplateSpec struct {
Data string `json:"data,omitempty"`
// +optional
Data *string `json:"data,omitempty"`
}

// TemplateStatus defines the observed state of Template.
Expand Down
10 changes: 7 additions & 3 deletions tink/api/v1alpha1/workflow_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// WorkflowIDAnnotation is used by the controller to store the
// ID assigned to the workflow by Tinkerbell.
const WorkflowIDAnnotation = "workflow.tinkerbell.org/id"
const (
// WorkflowIDAnnotation is used by the controller to store the
// ID assigned to the workflow by Tinkerbell.
WorkflowIDAnnotation = "workflow.tinkerbell.org/id"

WorkflowFinalizer = "workflow.tinkerbell.org"
)

// WorkflowSpec defines the desired state of Workflow.
type WorkflowSpec struct {
Expand Down
7 changes: 6 additions & 1 deletion tink/api/v1alpha1/zz_generated.deepcopy.go

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

108 changes: 108 additions & 0 deletions tink/controllers/hardware_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2020 The Kubernetes Authors.
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 controllers contains controllers for Tinkerbell.
package controllers

import (
"context"
"fmt"

"github.com/go-logr/logr"
tinkv1alpha1 "github.com/tinkerbell/cluster-api-provider-tinkerbell/tink/api/v1alpha1"
"github.com/tinkerbell/tink/protos/hardware"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// HardwareReconciler implements Reconciler interface by managing Tinkerbell hardware.
type HardwareReconciler struct {
client.Client
HardwareClient hardware.HardwareServiceClient
Log logr.Logger
Recorder record.EventRecorder
Scheme *runtime.Scheme
}

// SetupWithManager configures reconciler with a given manager.
func (r *HardwareReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&tinkv1alpha1.Hardware{}).
Complete(r)
}

// +kubebuilder:rbac:groups=tinkerbell.org,resources=hardware;hardware/status,verbs=get;list;watch;create;update;patch;delete

// Reconcile ensures state of Tinkerbell hardware.
func (r *HardwareReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
logger := r.Log.WithValues("hardware", req.NamespacedName.Name)

// Fetch the hardware.
hardware := &tinkv1alpha1.Hardware{}
if err := r.Get(ctx, req.NamespacedName, hardware); err != nil {
if apierrors.IsNotFound(err) {
return ctrl.Result{}, nil
}

return ctrl.Result{}, fmt.Errorf("getting hardware: %w", err)
}

// Add finalizer first if not exist to avoid the race condition between init and delete
if !controllerutil.ContainsFinalizer(hardware, tinkv1alpha1.HardwareFinalizer) {
before := hardware.DeepCopy()
controllerutil.AddFinalizer(hardware, tinkv1alpha1.HardwareFinalizer)

if err := r.Client.Patch(ctx, hardware, client.MergeFrom(before)); err != nil {
logger.Error(err, "Failed to add finalizer to hardware")

return ctrl.Result{}, fmt.Errorf("failed to add finalizer: %w", err)
}

return ctrl.Result{}, nil
}

// Handle deleted hardware.
if !hardware.DeletionTimestamp.IsZero() {
return r.reconcileDelete(hardware)
}

return r.reconcileNormal(hardware)
}

func (r *HardwareReconciler) reconcileNormal(hardware *tinkv1alpha1.Hardware) (ctrl.Result, error) {
logger := r.Log.WithValues("hardware", hardware.Name)
err := ErrNotImplemented

logger.Error(err, "Not yet implemented")

return ctrl.Result{}, err
}

func (r *HardwareReconciler) reconcileDelete(hardware *tinkv1alpha1.Hardware) (ctrl.Result, error) {
logger := r.Log.WithValues("hardware", hardware.Name)
err := ErrNotImplemented

logger.Error(err, "Not yet implemented")

// controllerutil.RemoveFinalizer(hardware, tinkv1alpha1.HardwareFinalizer)

return ctrl.Result{}, err
}
Loading

0 comments on commit 19a3cce

Please sign in to comment.