Skip to content

Commit

Permalink
add provisioning
Browse files Browse the repository at this point in the history
  • Loading branch information
jianrongzhang89 committed Dec 26, 2021
1 parent 014dad7 commit bdb1212
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 58 deletions.
7 changes: 6 additions & 1 deletion api/v1alpha1/dbaasprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
DBaaSInventoryProviderSyncType string = "SpecSynced"
DBaaSConnectionProviderSyncType string = "ReadyForBinding"
DBaaSConnectionReadyType string = "ConnectionReady"
DBaaSInstanceProviderSyncType string = "ProvisionReady"
DBaaSInstanceProviderSyncType string = "ClusterReady"
DBaaSInstanceReadyType string = "InstanceReady"

// DBaaS condition reasons
Expand Down Expand Up @@ -141,6 +141,8 @@ type DBaaSInventorySpec struct {
// DBaaSProvider CR (CredentialFields key). It is recommended to place the Secret in a
// namespace with limited accessibility.
CredentialsRef *NamespacedName `json:"credentialsRef"`
// RequestTimestamp request time for inventory status to be refreshed.
RequestTimestamp metav1.Time `json:"requestTimestamp,omitempty"`
}

// DBaaSInventoryStatus defines the Inventory status to be used by provider operators
Expand All @@ -149,6 +151,9 @@ type DBaaSInventoryStatus struct {

// A list of instances returned from querying the DB provider
Instances []Instance `json:"instances,omitempty"`

// ProcessedTimestamp time when the inventory gets refreshed
ProcessedTimestamp metav1.Time `json:"processedTimestamp,omitempty"`
}

type Instance struct {
Expand Down
3 changes: 2 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.

6 changes: 3 additions & 3 deletions bundle/manifests/dbaas-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ metadata:
operators.operatorframework.io/project_layout: go.kubebuilder.io/v3
repository: https://github.com/RHEcosystemAppEng/dbaas-operator
support: [email protected]
name: dbaas-operator.v0.5.3
name: dbaas-operator.v0.1.3
namespace: placeholder
spec:
apiservicedefinitions: {}
Expand Down Expand Up @@ -461,7 +461,7 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
image: quay.io/jianrzha/dbaas-operator:0.5.3
image: quay.io/ecosystem-appeng/dbaas-operator:v0.1.3
imagePullPolicy: Always
livenessProbe:
httpGet:
Expand Down Expand Up @@ -552,7 +552,7 @@ spec:
name: Red Hat
url: https://www.redhat.com
replaces: dbaas-operator.v0.1.2
version: 0.5.3
version: 0.1.3
webhookdefinitions:
- admissionReviewVersions:
- v1
Expand Down
9 changes: 9 additions & 0 deletions bundle/manifests/dbaas.redhat.com_dbaasinventories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ spec:
description: The namespace where object of known type is stored
type: string
type: object
requestTimestamp:
description: RequestTimestamp request time for inventory status to
be refreshed.
format: date-time
type: string
required:
- credentialsRef
- providerRef
Expand Down Expand Up @@ -172,6 +177,10 @@ spec:
- instanceID
type: object
type: array
processedTimestamp:
description: ProcessedTimestamp time when the inventory gets refreshed
format: date-time
type: string
type: object
type: object
served: true
Expand Down
9 changes: 9 additions & 0 deletions config/crd/bases/dbaas.redhat.com_dbaasinventories.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ spec:
description: The namespace where object of known type is stored
type: string
type: object
requestTimestamp:
description: RequestTimestamp request time for inventory status to
be refreshed.
format: date-time
type: string
required:
- credentialsRef
- providerRef
Expand Down Expand Up @@ -174,6 +179,10 @@ spec:
- instanceID
type: object
type: array
processedTimestamp:
description: ProcessedTimestamp time when the inventory gets refreshed
format: date-time
type: string
type: object
type: object
served: true
Expand Down
4 changes: 2 additions & 2 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: quay.io/jianrzha/dbaas-operator
newTag: 0.5.3
newName: quay.io/ecosystem-appeng/dbaas-operator
newTag: v0.1.3
67 changes: 17 additions & 50 deletions controllers/dbaasinstance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ package controllers
import (
"context"

appv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/pointer"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/RHEcosystemAppEng/dbaas-operator/api/v1alpha1"
)
Expand All @@ -49,7 +45,7 @@ type DBaaSInstanceReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *DBaaSInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, recErr error) {
logger := ctrl.LoggerFrom(ctx, "DBaaS Connection", req.NamespacedName)
logger := ctrl.LoggerFrom(ctx, "DBaaS Instance", req.NamespacedName)

var instance v1alpha1.DBaaSInstance
if err := r.Get(ctx, req.NamespacedName, &instance); err != nil {
Expand All @@ -67,15 +63,19 @@ func (r *DBaaSInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
var dbaasCond metav1.Condition
// This update will make sure the status is always updated in case of any errors or successful result
defer func(inst *v1alpha1.DBaaSInstance, cond *metav1.Condition) {
if len(dbaasCond.Type) == 0 {
// dbaasCond is not populated or updated. No status update is needed.
return
}
apimeta.SetStatusCondition(&inst.Status.Conditions, *cond)
if err := r.Client.Status().Update(ctx, inst); err != nil {
if errors.IsConflict(err) {
logger.V(1).Info("Connection modified, retry syncing spec")
logger.V(1).Info("Instance modified, retry syncing spec")
// Re-queue and preserve existing recErr
result = ctrl.Result{Requeue: true}
return
}
logger.Error(err, "Could not update connection status")
logger.Error(err, "Could not update instance status")
if recErr == nil {
// There is no existing recErr. Set it to the status update error
recErr = err
Expand All @@ -85,6 +85,12 @@ func (r *DBaaSInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques

var inventory v1alpha1.DBaaSInventory
if err := r.Get(ctx, types.NamespacedName{Namespace: instance.Spec.InventoryRef.Namespace, Name: instance.Spec.InventoryRef.Name}, &inventory); err != nil {
if errors.IsNotFound(err) {
logger.Error(err, "DBaaS Inventory resource not found", "DBaaS Inventory", instance.Spec.InventoryRef)
dbaasCond = metav1.Condition{Type: v1alpha1.DBaaSInstanceReadyType, Status: metav1.ConditionFalse, Reason: v1alpha1.DBaaSInventoryNotFound, Message: err.Error()}
result, recErr = ctrl.Result{}, err
return
}
logger.Error(err, "Error fetching DBaaS Inventory resource reference for DBaaS Connection", "DBaaS Inventory", instance.Spec.InventoryRef)
result, recErr = ctrl.Result{}, err
return
Expand All @@ -94,6 +100,7 @@ func (r *DBaaSInstanceReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if err != nil {
if errors.IsNotFound(err) {
logger.Error(err, "Requested DBaaS Provider is not configured in this environment", "DBaaS Provider", inventory.Spec.ProviderRef)
dbaasCond = metav1.Condition{Type: v1alpha1.DBaaSInstanceReadyType, Status: metav1.ConditionFalse, Reason: v1alpha1.DBaaSProviderNotFound, Message: err.Error()}
result, recErr = ctrl.Result{}, err
return
}
Expand Down Expand Up @@ -135,55 +142,15 @@ func (r *DBaaSInstanceReconciler) SetupWithManager(mgr ctrl.Manager) (controller
Build(r)
}

func (r *DBaaSInstanceReconciler) deploymentMutateFn(connection *v1alpha1.DBaaSInstance, deployment *appv1.Deployment) controllerutil.MutateFn {
return func() error {
deployment.ObjectMeta.Labels = map[string]string{
"managed-by": "dbaas-operator",
"owner": connection.Name,
"owner.kind": connection.Kind,
"owner.namespace": connection.Namespace,
}
deployment.Spec = appv1.DeploymentSpec{
Replicas: pointer.Int32Ptr(0),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"name": "bind-deploy",
},
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"name": "bind-deploy",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "bind-deploy",
Image: "quay.io/ecosystem-appeng/busybox",
ImagePullPolicy: v1.PullIfNotPresent,
Command: []string{"sh", "-c", "echo The app is running! && sleep 3600"},
},
},
},
},
}
deployment.OwnerReferences = nil
if err := ctrl.SetControllerReference(connection, deployment, r.Scheme); err != nil {
return err
}
return nil
}
}

// mergeInstanceStatus: merge the status from DBaaSProviderConnection into the current DBaaSInstance status
// mergeInstanceStatus: merge the status from DBaaSProviderInstance into the current DBaaSInstance status
func mergeInstanceStatus(inst *v1alpha1.DBaaSInstance, providerInst *v1alpha1.DBaaSProviderInstance) *metav1.Condition {
cond := apimeta.FindStatusCondition(inst.Status.Conditions, v1alpha1.DBaaSInstanceReadyType)
inst.Status.Phase = providerInst.Status.Phase
providerInst.Status.DeepCopyInto(&inst.Status)
if cond != nil {
inst.Status.Conditions = append(inst.Status.Conditions, *cond)
}
// Update connection status condition (type: DBaaSInstanceReadyType) based on the provider status
// Update instance status condition (type: DBaaSInstanceReadyType) based on the provider status
specSync := apimeta.FindStatusCondition(providerInst.Status.Conditions, v1alpha1.DBaaSInstanceProviderSyncType)
if cond != nil && specSync != nil && specSync.Status == metav1.ConditionTrue {
return &metav1.Condition{Type: v1alpha1.DBaaSInstanceReadyType, Status: metav1.ConditionTrue, Reason: v1alpha1.Ready, Message: v1alpha1.MsgProviderCRStatusSyncDone}
Expand Down
2 changes: 1 addition & 1 deletion controllers/reconcilers/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const (
INSTALL_NAMESPACE = "openshift-operators"
CATALOG_NAMESPACE = "openshift-marketplace"
// MONGODB_ATLAS_CATLOG_IMG = "quay.io/mongodb/mongodb-atlas-kubernetes-dbaas-catalog:latest"
MONGODB_ATLAS_CATLOG_IMG = "quay.io/ecosystem-appeng/mongodb-atlas-operator-catalog:0.7.7"
MONGODB_ATLAS_CATLOG_IMG = "quay.io/ecosystem-appeng/mongodb-atlas-operator-catalog:0.7.9"
CRUNCHY_BRIDGE_CATLOG_IMG = "registry.developers.crunchydata.com/crunchydata/crunchy-bridge-operator-catalog:v0.0.1"
DBAAS_DYNAMIC_PLUGIN_IMG = "quay.io/ecosystem-appeng/dbaas-dynamic-plugin:0.1.3"
DBAAS_DYNAMIC_PLUGIN_NAME = "dbaas-dynamic-plugin"
Expand Down
8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "DBaaSInventory")
os.Exit(1)
}
instanceCtrl, err := (&controllers.DBaaSInstanceReconciler{
DBaaSReconciler: DBaaSReconciler,
}).SetupWithManager(mgr)
if err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DBaaSIntance")
os.Exit(1)
}
if err = (&controllers.DBaaSDefaultTenantReconciler{
DBaaSReconciler: DBaaSReconciler,
}).SetupWithManager(mgr); err != nil {
Expand All @@ -139,6 +146,7 @@ func main() {
DBaaSReconciler: DBaaSReconciler,
ConnectionCtrl: connectionCtrl,
InventoryCtrl: inventoryCtrl,
InstanceCtrl: instanceCtrl,
}).SetupWithManager(mgr)
if err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DBaaSProvider")
Expand Down

0 comments on commit bdb1212

Please sign in to comment.