Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add Controller (1) - initial setup
- It (`reconcile` function/loop) works for create and delete of sample resource
  • Loading branch information
an3l committed Dec 14, 2021
1 parent cd34e98 commit 61805b2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
11 changes: 5 additions & 6 deletions api/v1alpha1/mariadb_types.go
Expand Up @@ -63,25 +63,24 @@ type MariaDBSpec struct {
// Port number exposed for Database service
// +optional
// +kubebuilder:default=3306
// +kubebuilder:validation:Minimum=0

Port int32 `json:"port"`
}

type StatusPhase string

const (
RunningStatusPhase StatusPhase = "RUNNING"
RunningStatusPhase StatusPhase = "RUNNING"
BootstrapingStatusPhase StatusPhase = "BOOTSTRAP"
ErrorStatusPhase StatusPhase = "ERROR"
ErrorStatusPhase StatusPhase = "ERROR"
)

// MariaDBStatus defines the observed state of MariaDB
type MariaDBStatus struct {
CurrentReplicas *int32 `json:"currentReplicas,omitempty"` // If it's nil, it is unset, we'll use a default. If it is 0 than it is set to 0
DesiredReplicas int32 `json:"desiredReplicas"` // 0 is the same as unset (no value) and default will be applied even if user applies 0.
DesiredReplicas int32 `json:"desiredReplicas"` // 0 is the same as unset (no value) and default will be applied even if user applies 0.
LastMessage string `json:"lastMessage"`
DbState StatusPhase `json:"dbState"`

DbState StatusPhase `json:"dbState"`
}

//+kubebuilder:object:root=true
Expand Down
7 changes: 6 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

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

2 changes: 0 additions & 2 deletions config/crd/bases/mariak8g.mariadb.org_mariadbs.yaml
Expand Up @@ -62,9 +62,7 @@ spec:
type: string
port:
default: 3306
description: Port number exposed for Database service
format: int32
minimum: 0
type: integer
rootpwd:
description: Root user password
Expand Down
9 changes: 8 additions & 1 deletion config/samples/mariak8g_v1alpha1_mariadb.yaml
Expand Up @@ -3,4 +3,11 @@ kind: MariaDB
metadata:
name: mariadb-sample
spec:
# Add fields here
# Add required fields:
dataStoragePath: "/tmp/datadir"
database: "testDB-operator"
image: "docker-image"
password: "test!123"
rootpwd: "root!123"
username: "anel"

38 changes: 26 additions & 12 deletions controllers/mariadb_controller.go
Expand Up @@ -19,38 +19,52 @@ package controllers
import (
"context"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

mariak8gv1alpha1 "github.com/mariadb/mariadb.org-tools/mariadb-operator/api/v1alpha1"
)

func ignoreNotFound(err error) error {
if errors.IsNotFound(err) {
return nil
}
return err
}

// MariaDBReconciler reconciles a MariaDB object
type MariaDBReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
}

//+kubebuilder:rbac:groups=mariak8g.mariadb.org,resources=mariadbs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=mariak8g.mariadb.org,resources=mariadbs/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=mariak8g.mariadb.org,resources=mariadbs/finalizers,verbs=update

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the MariaDB object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.10.0/pkg/reconcile
func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
_ = log.FromContext(ctx)

// your logic here
log := r.Log.WithValues("MariaDB: ", req.NamespacedName)

var app mariak8gv1alpha1.MariaDB // fetch resource
log.Info("Reconciling MariaDB kind", "mariadb", app.Name)

if err := r.Get(ctx, req.NamespacedName, &app); err != nil {
// it might be not found if this is a delete request
if ignoreNotFound(err) == nil {
log.Info("Reconciled MariaDB kind after delete")
return ctrl.Result{}, nil
}
log.Error(err, "unable to fetch MariaDB")
return ctrl.Result{}, err
}

log.Info("Reconciled MariaDB kind", "mariadb", app.Name)
return ctrl.Result{}, nil
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -3,6 +3,7 @@ module github.com/mariadb/mariadb.org-tools/mariadb-operator
go 1.16

require (
github.com/go-logr/logr v0.4.0 // indirect
github.com/onsi/ginkgo v1.16.4
github.com/onsi/gomega v1.15.0
k8s.io/apimachinery v0.22.1
Expand Down
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -80,6 +80,7 @@ func main() {

if err = (&controllers.MariaDBReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MariaDB1"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MariaDB")
Expand Down

0 comments on commit 61805b2

Please sign in to comment.