Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add features
- Return `Age` column to be visible by getter method
- Add `Image` column for wide option in getter method of `mariadb` resource
- Add `status.showState` default value - after testing couldn't see default values,
  however it is visible in the generated manifest
- Append to the `db.name-server` `-deployment` to be visible from looking for deployemnt
- Append `-server-service` to the db.name for internal service
- Change default of envioronment variable `MARIADB_ALLOW_EMPTY_ROOT_PASSWORD` (default 0)
- Add env `MARIADB_DATABASE` to be created on server startup for `MARIADB_USER`
- Results
```
$ kubectl get mariadb
NAME             MARIADB STATE   PORT   AGE
mariadb-sample   RUNNING         3306   44s
$ kubectl get mariadb -o wide
NAME             MARIADB STATE   PORT   IMAGE                                           AGE
mariadb-sample   RUNNING         3306   quay.io/mariadb-foundation/mariadb-devel:10.5   48s

$ kubectl get pods
NAME                                                READY   STATUS    RESTARTS   AGE
mariadb-sample-server-deployment-68bc5c6f9f-kcqt7   1/1     Running   0          94s
mariadb-sample-server-deployment-68bc5c6f9f-ls9d9   1/1     Running   0          94s

$ kubectl get svc
NAME                            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes                      ClusterIP   10.96.0.1      <none>        443/TCP    116d
mariadb-sample-server-service   ClusterIP   10.109.68.55   <none>        3306/TCP   108s

$ kubectl exec -it mariadb-sample-server-deployment-68bc5c6f9f-ls9d9 -- mariadb -uexample-user -pmy_cool_secret -e "show grants for current_user(); show databases;"
+-------------------------------------------------------------------------------------------------------------+
| Grants for example-user@%                                                                                   |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `example-user`@`%` IDENTIFIED BY PASSWORD '*4EC7004AFBA4CA916C70D9A264CC2B6356AD9AEB' |
|                                           GRANT ALL PRIVILEGES ON `testDB-operator`.* TO `example-user`@`%` |
+-------------------------------------------------------------------------------------------------------------+
+--------------------+
| Database           |
+--------------------+
| information_schema |
|    testDB-operator |
+--------------------+
```

- Future work - add secrets and validate entries
  • Loading branch information
an3l committed Feb 22, 2022
1 parent 234e3bd commit 9fb056c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
9 changes: 8 additions & 1 deletion api/v1alpha1/mariadb_types.go
Expand Up @@ -85,12 +85,19 @@ type MariaDBStatus struct {
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"`

// +optional
// +kubebuilder:default="NOT STARTED"

ShowState string `json:"showState"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
// +kubebuilder:printcolumn:priority=0,name=MariaDB State,type=string,JSONPath=".status.dbState",description="State of the MariaDB instance",format=""
// +kubebuilder:printcolumn:priority=0,name=MariaDB State,type=string,JSONPath=".status.showState",description="State of the MariaDB instance",format=""
// +kubebuilder:printcolumn:priority=0,name=Port,type=string,JSONPath=".spec.port",description="Port of the MariaDB instance",format=""
// +kubebuilder:printcolumn:priority=1,name=Image,type=string,JSONPath=".spec.image",description="Image of the MariaDB instance",format=""
// +kubebuilder:printcolumn:priority=0,name=Age, type=date,JSONPath=".metadata.creationTimestamp"

// MariaDB is the Schema for the mariadbs API
type MariaDB struct {
Expand Down
13 changes: 12 additions & 1 deletion config/crd/bases/mariak8g.mariadb.org_mariadbs.yaml
Expand Up @@ -18,13 +18,21 @@ spec:
versions:
- additionalPrinterColumns:
- description: State of the MariaDB instance
jsonPath: .status.dbState
jsonPath: .status.showState
name: MariaDB State
type: string
- description: Port of the MariaDB instance
jsonPath: .spec.port
name: Port
type: string
- description: Image of the MariaDB instance
jsonPath: .spec.image
name: Image
priority: 1
type: string
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1alpha1
schema:
openAPIV3Schema:
Expand Down Expand Up @@ -99,6 +107,9 @@ spec:
type: integer
lastMessage:
type: string
showState:
default: NOT STARTED
type: string
required:
- dbState
- desiredReplicas
Expand Down
7 changes: 4 additions & 3 deletions controllers/common.go
Expand Up @@ -33,7 +33,7 @@ func (r *MariaDBReconciler) desiredDeployment(database mariak8gv1alpha1.MariaDB)
depl := appsv1.Deployment{
TypeMeta: metav1.TypeMeta{APIVersion: appsv1.SchemeGroupVersion.String(), Kind: "Deployment"},
ObjectMeta: metav1.ObjectMeta{
Name: database.Name + "-server",
Name: database.Name + "-server-deployment",
Namespace: database.Namespace,
},
Spec: appsv1.DeploymentSpec{
Expand All @@ -51,11 +51,12 @@ func (r *MariaDBReconciler) desiredDeployment(database mariak8gv1alpha1.MariaDB)
Name: "mariadb",
Image: mariaImage,
Env: []corev1.EnvVar{
{Name: "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD", Value: "1"},
{Name: "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD", Value: "0"},
// root password should be set from secret - test
{Name: "MARIADB_ROOT_PASSWORD", Value: database.Spec.Rootpwd},
{Name: "MARIADB_USER", Value: database.Spec.Username},
{Name: "MARIADB_PASSWORD", Value: database.Spec.Password},
{Name: "MARIADB_DATABASE", Value: database.Spec.Database},
},
Ports: []corev1.ContainerPort{
{ContainerPort: mariaPort, Name: "mariadb-port", Protocol: "TCP"},
Expand All @@ -79,7 +80,7 @@ func (r *MariaDBReconciler) desiredService(database mariak8gv1alpha1.MariaDB) (c
svc := corev1.Service{
TypeMeta: metav1.TypeMeta{APIVersion: corev1.SchemeGroupVersion.String(), Kind: "Service"},
ObjectMeta: metav1.ObjectMeta{
Name: database.Name,
Name: database.Name + "-server-service",
Namespace: database.Namespace,
},
Spec: corev1.ServiceSpec{
Expand Down
7 changes: 6 additions & 1 deletion controllers/mariadb_controller.go
Expand Up @@ -65,8 +65,13 @@ func (r *MariaDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
log.Error(err, "unable to fetch MariaDB")
return ctrl.Result{}, err
}
app.Status.DbState = mariak8gv1alpha1.RunningStatusPhase

deployment, err := r.desiredDeployment(app)
if err == nil {
app.Status.DbState = mariak8gv1alpha1.RunningStatusPhase
app.Status.ShowState = string(app.Status.DbState)
}

// return if there is an error during deployment start
if err != nil {
return ctrl.Result{}, err
Expand Down

0 comments on commit 9fb056c

Please sign in to comment.