Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MDBF-378: Create root secret
- Additinally remove redudant status variable that was used as a tweak
  • Loading branch information
an3l committed Jun 17, 2022
1 parent 337af0f commit 2f309f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
1 change: 0 additions & 1 deletion api/v1alpha1/mariadb_types.go
Expand Up @@ -90,7 +90,6 @@ type MariaDBStatus struct {
// +kubebuilder:default="NOT STARTED"

ShowState string `json:"showState"`
SecretSet int32 `json:"secretSet"`
}

//+kubebuilder:object:root=true
Expand Down
4 changes: 0 additions & 4 deletions config/crd/bases/mariak8g.mariadb.org_mariadbs.yaml
Expand Up @@ -106,17 +106,13 @@ spec:
type: integer
lastMessage:
type: string
secretSet:
format: int32
type: integer
showState:
default: NOT STARTED
type: string
required:
- dbState
- desiredReplicas
- lastMessage
- secretSet
type: object
type: object
served: true
Expand Down
39 changes: 23 additions & 16 deletions controllers/mariadb_controller.go
Expand Up @@ -23,7 +23,6 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -52,6 +51,9 @@ type MariaDBReconciler struct {
//+kubebuilder:rbac:groups=mariak8g.mariadb.org,resources=mariadbs/finalizers,verbs=update

func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
/*
----------- SETTING THE DEFAULTS FOR RECONCILIATION -----------
*/
// Create the object to reconcile
var app mariak8gv1alpha1.MariaDB // fetch resource
log := r.Log.WithValues("MariaDB: ", req.NamespacedName)
Expand All @@ -71,14 +73,17 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct

applyOpts := []client.PatchOption{client.ForceOwnership, client.FieldOwner("mariadb-controller")}

/*
----------- SETTING THE ROOT SECRET -----------
*/
// Check if secret exists (one cannot specify plain text rootpwd)
var root_secret *corev1.Secret = nil
if err := r.Get(ctx, req.NamespacedName, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: app.Name + "-secret",
Namespace: app.Namespace,
},
}); err != nil && errors.IsNotFound(err) {
root_secret := &corev1.Secret{}
err := r.Get(ctx, client.ObjectKey{
Namespace: req.Namespace,
Name: req.Name + "-secret",
}, root_secret)
// log.Info("Get()", "error: ", err, "root_secret", *root_secret)
if err != nil {
log.Info("Root secret doesn't exist, let me creat it:...")
// Create root secret if not exist root password
var err error
Expand All @@ -94,28 +99,30 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{RequeueAfter: 5}, nil
}

// This part is needed to be outside of reconciliation because of need to edit the secrets
if root_secret != nil {
err1 := r.Patch(ctx, root_secret, client.Apply, applyOpts...)
if err1 != nil {
return ctrl_res, err1
}
}

/*
----------- SETTING THE DEPLOYMENT -----------
*/
// Create the deployment
deployment, err := r.reconcile_deployment(ctx, app, log)
if err != nil {
log.Error(err, " failed to reconcile deployments!")
return ctrl_res, err
}

/*
----------- SETTING THE SERVICE -----------
*/
svc, err := r.reconcile_service(ctx, app, log)
// return if there is an error during service start
if err != nil {
log.Error(err, " failed to reconcile service!")
return ctrl_res, err
}

/*
----------- UPDATE INFORMATION IN CLUSTER -----------
*/

err = r.Patch(ctx, &deployment, client.Apply, applyOpts...)
if err != nil {
return ctrl_res, err
Expand Down

0 comments on commit 2f309f1

Please sign in to comment.