Skip to content
This repository was archived by the owner on Dec 18, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type MobileSecurityServiceSpec struct {
OAuthPort int32 `json:"oAuthPort"`

//CR optional configuration values
ConfigMapName string `json:"configMapName,omitempty"`
RouteName string `json:"routeName,omitempty"`
ConfigMapName string `json:"configMapName,omitempty"`
RouteName string `json:"routeName,omitempty"`
SkipNamespaceValidation bool `json:"skipNamespaceValidation,omitempty"`
}

// MobileSecurityServiceStatus defines the observed state of MobileSecurityService
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ type MobileSecurityServiceAppSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
AppName string `json:"appName"`
AppId string `json:"appId"`
AppName string `json:"appName"`
AppId string `json:"appId"`

// Optional configuration values
SkipNamespaceValidation bool `json:"skipNamespaceValidation,omitempty"`
}

// MobileSecurityServiceAppStatus defines the observed state of MobileSecurityServiceApp
Expand All @@ -24,7 +27,7 @@ type MobileSecurityServiceAppStatus struct {
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
SDKConfigMapName string `json:"sdkConfigMapName"`
BindStatus string `json:"bindStatus"`
BindStatus string `json:"bindStatus"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package v1alpha1

import (
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/api/extensions/v1beta1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
Expand All @@ -18,13 +17,13 @@ type MobileSecurityServiceDBSpec struct {
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html

//Enviroment Variables for the DB when the Service configMap was not found
DatabaseName string `json:"databaseName,omitempty"`
DatabasePassword string `json:"databasePassword,omitempty"`
DatabaseUser string `json:"databaseUser,omitempty"`
DatabaseNameParam string `json:"databaseNameParam"`
DatabasePasswordParam string `json:"databasePasswordParam"`
DatabaseUserParam string `json:"databaseUserParam"`
DatabasePort int32 `json:"databasePort"`
DatabaseName string `json:"databaseName,omitempty"`
DatabasePassword string `json:"databasePassword,omitempty"`
DatabaseUser string `json:"databaseUser,omitempty"`
DatabaseNameParam string `json:"databaseNameParam"`
DatabasePasswordParam string `json:"databasePasswordParam"`
DatabaseUserParam string `json:"databaseUserParam"`
DatabasePort int32 `json:"databasePort"`

//CR mandatory configuration values
Size int32 `json:"size"`
Expand All @@ -33,6 +32,9 @@ type MobileSecurityServiceDBSpec struct {
DatabaseMemoryLimit string `json:"databaseMemoryLimit"`
DatabaseMemoryRequest string `json:"databaseMemoryRequest"`
DatabaseStorageRequest string `json:"databaseStorageRequest"`

// Optional configuration values
SkipNamespaceValidation bool `json:"skipNamespaceValidation,omitempty"`
}

// MobileSecurityServiceDBStatus defines the observed state of MobileSecurityServiceDB
Expand All @@ -41,12 +43,12 @@ type MobileSecurityServiceDBStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
PersistentVolumeClaimName string `json:"persistentVolumeClaimName"`
DeploymentName string `json:"deploymentName"`
DeploymentStatus v1beta1.DeploymentStatus `json:"deploymentStatus"`
ServiceName string `json:"serviceName"`
ServiceStatus v1.ServiceStatus `json:"serviceStatus"`
DatabaseStatus string `json:databaseStatus"`
PersistentVolumeClaimName string `json:"persistentVolumeClaimName"`
DeploymentName string `json:"deploymentName"`
DeploymentStatus v1beta1.DeploymentStatus `json:"deploymentStatus"`
ServiceName string `json:"serviceName"`
ServiceStatus v1.ServiceStatus `json:"serviceStatus"`
DatabaseStatus string `json:databaseStatus"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
41 changes: 20 additions & 21 deletions pkg/controller/mobilesecurityservice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mobilesecurityservice

import (
"context"

mobilesecurityservicev1alpha1 "github.com/aerogear/mobile-security-service-operator/pkg/apis/mobilesecurityservice/v1alpha1"
"github.com/aerogear/mobile-security-service-operator/pkg/utils"
"github.com/go-logr/logr"
Expand All @@ -18,10 +19,10 @@ import (
)

const (
CONFIGMAP = "ConfigMap"
DEEPLOYMENT = "Deployment"
SERVICE = "Service"
ROUTE = "Route"
CONFIGMAP = "ConfigMap"
DEEPLOYMENT = "Deployment"
SERVICE = "Service"
ROUTE = "Route"
)

var log = logf.Log.WithName("controller_mobilesecurityservice")
Expand Down Expand Up @@ -69,7 +70,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
}

//Route
if err:= watchRoute(c); err != nil {
if err := watchRoute(c); err != nil {
return err
}

Expand Down Expand Up @@ -98,7 +99,7 @@ func (r *ReconcileMobileSecurityService) update(obj runtime.Object, reqLogger lo
}

//Create the factory object and requeue
func (r *ReconcileMobileSecurityService) create( instance *mobilesecurityservicev1alpha1.MobileSecurityService, reqLogger logr.Logger, kind string, err error) (reconcile.Result, error) {
func (r *ReconcileMobileSecurityService) create(instance *mobilesecurityservicev1alpha1.MobileSecurityService, reqLogger logr.Logger, kind string, err error) (reconcile.Result, error) {
obj, errBuildObject := r.buildFactory(reqLogger, instance, kind)
if errBuildObject != nil {
return reconcile.Result{}, errBuildObject
Expand All @@ -118,7 +119,7 @@ func (r *ReconcileMobileSecurityService) create( instance *mobilesecurityservice

}

//buildFactory will return the resource according to the kind defined
// buildFactory will return the resource according to the kind defined
func (r *ReconcileMobileSecurityService) buildFactory(reqLogger logr.Logger, instance *mobilesecurityservicev1alpha1.MobileSecurityService, kind string) (runtime.Object, error) {
reqLogger.Info("Check "+kind, "into the namespace", instance.Namespace)
switch kind {
Expand All @@ -136,8 +137,6 @@ func (r *ReconcileMobileSecurityService) buildFactory(reqLogger logr.Logger, ins
}
}



// Reconcile reads that state of the cluster for a MobileSecurityService object and makes changes based on the state read
// and what is in the MobileSecurityService.Spec
// Note:
Expand All @@ -147,28 +146,28 @@ func (r *ReconcileMobileSecurityService) Reconcile(request reconcile.Request) (r
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling Mobile Security Service ...")

instance := &mobilesecurityservicev1alpha1.MobileSecurityService{}

//Fetch the MobileSecurityService instance
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
return fetch(r, reqLogger, err)
}

// FIXME: Check if is a valid namespace
// We should not checked if the namespace is valid or not. It is an workaround since currently is not possible watch/cache a List of Namespaces
// The impl to allow do it is done and merged in the master branch of the lib but not released in an stable version. It should be removed when this feature be impl.
// See the PR which we are working on to update the deps and have this feature: https://github.com/operator-framework/operator-sdk/pull/1388
if isValidNamespace, err:= utils.IsValidOperatorNamespace(request.Namespace); err != nil || isValidNamespace == false {
if isValidNamespace, err := utils.IsValidOperatorNamespace(request.Namespace, instance.Spec.SkipNamespaceValidation); err != nil || isValidNamespace == false {
// Stop reconcile
operatorNamespace, _ := k8sutil.GetOperatorNamespace();
operatorNamespace, _ := k8sutil.GetOperatorNamespace()
reqLogger.Error(err, "Unable to reconcile Mobile Security Service", "Request.Namespace", request.Namespace, "isValidNamespace", isValidNamespace, "Operator.Namespace", operatorNamespace)
return reconcile.Result{}, nil
}

reqLogger.Info("Valid namespace for Mobile Security Service", "Namespace", request.Namespace)
reqLogger.Info("Start Reconciling Mobile Security Service ...")

instance := &mobilesecurityservicev1alpha1.MobileSecurityService{}

//Fetch the MobileSecurityService instance
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
return fetch(r, reqLogger, err)
}

//Check specs
if !hasMandatorySpecs(instance, reqLogger) {
return reconcile.Result{Requeue: true}, nil
Expand Down Expand Up @@ -209,7 +208,7 @@ func (r *ReconcileMobileSecurityService) Reconcile(request reconcile.Request) (r
}

//Update status for deployment
deploymentStatus, err := r.updateDeploymentStatus(reqLogger,instance)
deploymentStatus, err := r.updateDeploymentStatus(reqLogger, instance)
if err != nil {
return reconcile.Result{}, err
}
Expand All @@ -227,7 +226,7 @@ func (r *ReconcileMobileSecurityService) Reconcile(request reconcile.Request) (r
}

//Update status for App
if err:= r.updateStatus(reqLogger, configMapStatus, deploymentStatus, serviceStatus, routeStatus, instance); err != nil {
if err := r.updateStatus(reqLogger, configMapStatus, deploymentStatus, serviceStatus, routeStatus, instance); err != nil {
return reconcile.Result{}, err
}

Expand Down
27 changes: 14 additions & 13 deletions pkg/controller/mobilesecurityserviceapp/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mobilesecurityserviceapp

import (
"context"

mobilesecurityservicev1alpha1 "github.com/aerogear/mobile-security-service-operator/pkg/apis/mobilesecurityservice/v1alpha1"
"github.com/aerogear/mobile-security-service-operator/pkg/models"
"github.com/aerogear/mobile-security-service-operator/pkg/service"
Expand Down Expand Up @@ -109,31 +110,31 @@ func (r *ReconcileMobileSecurityServiceApp) Reconcile(request reconcile.Request)
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling MobileSecurityServiceApp")

instance := &mobilesecurityservicev1alpha1.MobileSecurityServiceApp{}

//Fetch the MobileSecurityService instance
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
return fetch(r, reqLogger, err)
}

// FIXME: Check if is a valid namespace
// We should not checked if the namespace is valid or not. It is an workaround since currently is not possible watch/cache a List of Namespaces
// The impl to allow do it is done and merged in the master branch of the lib but not released in an stable version. It should be removed when this feature be impl.
// See the PR which we are working on to update the deps and have this feature: https://github.com/operator-framework/operator-sdk/pull/1388
if isValidNamespace, err:= utils.IsValidAppNamespace(request.Namespace); err != nil || isValidNamespace == false {
if isValidNamespace, err := utils.IsValidAppNamespace(request.Namespace); err != nil || isValidNamespace == false {
// Stop reconcile
envVar, _ := utils.GetAppNamespaces();
envVar, _ := utils.GetAppNamespaces()
reqLogger.Error(err, "Unable to reconcile Mobile Security Service App", "Request.Namespace", request.Namespace, "isValidNamespace", isValidNamespace, "EnvVar.APP_NAMESPACES", envVar)
return reconcile.Result{}, nil
}

reqLogger.Info("Valid namespace for MobileSecurityServiceApp", "Namespace", request.Namespace)
reqLogger.Info("Start Reconciling MobileSecurityServiceApp ...")

instance := &mobilesecurityservicev1alpha1.MobileSecurityServiceApp{}

//Fetch the MobileSecurityService instance
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
return fetch(r, reqLogger, err)
}

reqLogger.Info("Checking for service instance ...")
serviceInstance := &mobilesecurityservicev1alpha1.MobileSecurityService{}
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: utils.SERVICE_INSTANCE_NAME, Namespace: utils.SERVICE_INSTANCE_NAMESPACE }, serviceInstance); err != nil {
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: utils.SERVICE_INSTANCE_NAME, Namespace: utils.SERVICE_INSTANCE_NAMESPACE}, serviceInstance); err != nil {
// Return and don't create
reqLogger.Info("Mobile Security Service instance resource not found. Ignoring since object must be deleted")
return reconcile.Result{}, nil
Expand All @@ -146,7 +147,7 @@ func (r *ReconcileMobileSecurityServiceApp) Reconcile(request reconcile.Request)

reqLogger.Info("Checking if the route already exists ...")
route := &routev1.Route{}
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: utils.GetRouteName(serviceInstance), Namespace: utils.SERVICE_INSTANCE_NAMESPACE }, route); err != nil {
if err := r.client.Get(context.TODO(), types.NamespacedName{Name: utils.GetRouteName(serviceInstance), Namespace: utils.SERVICE_INSTANCE_NAMESPACE}, route); err != nil {
return reconcile.Result{}, err
}

Expand All @@ -155,7 +156,7 @@ func (r *ReconcileMobileSecurityServiceApp) Reconcile(request reconcile.Request)

//Check if ConfigMap for the app exist, if not create one.
if _, err := r.fetchSDKConfigMap(reqLogger, instance); err != nil {
return r.create(instance, CONFIGMAP, serviceAPI , reqLogger, err)
return r.create(instance, CONFIGMAP, serviceAPI, reqLogger, err)
}

//Check if App is Bind in the REST Service, if not then bind it
Expand Down
34 changes: 18 additions & 16 deletions pkg/controller/mobilesecurityservicedb/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package mobilesecurityservicedb

import (
"context"
"time"

mobilesecurityservicev1alpha1 "github.com/aerogear/mobile-security-service-operator/pkg/apis/mobilesecurityservice/v1alpha1"
"github.com/aerogear/mobile-security-service-operator/pkg/utils"
"github.com/go-logr/logr"
Expand All @@ -16,7 +18,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/source"
"time"
)

var log = logf.Log.WithName("controller_mobilesecurityservicedb")
Expand Down Expand Up @@ -82,7 +83,7 @@ type ReconcileMobileSecurityServiceDB struct {
}

//Update the object and reconcile it
func (r *ReconcileMobileSecurityServiceDB) update( obj runtime.Object, reqLogger logr.Logger) (reconcile.Result, error) {
func (r *ReconcileMobileSecurityServiceDB) update(obj runtime.Object, reqLogger logr.Logger) (reconcile.Result, error) {
err := r.client.Update(context.TODO(), obj)
if err != nil {
reqLogger.Error(err, "Failed to update Spec")
Expand Down Expand Up @@ -119,7 +120,7 @@ func (r *ReconcileMobileSecurityServiceDB) buildFactory(instance *mobilesecurity
case PVC:
return r.buildPVCForDB(instance), nil
case DEEPLOYMENT:
return r.buildDBDeployment(instance,serviceInstance), nil
return r.buildDBDeployment(instance, serviceInstance), nil
case SERVICE:
return r.buildDBService(instance), nil
default:
Expand All @@ -137,38 +138,39 @@ func (r *ReconcileMobileSecurityServiceDB) Reconcile(request reconcile.Request)
reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
reqLogger.Info("Reconciling Mobile Security Service Database")

instance := &mobilesecurityservicev1alpha1.MobileSecurityServiceDB{}

//Fetch the MobileSecurityService instance
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
return fetch(r, reqLogger, err)
}

// FIXME: Check if is a valid namespace
// We should not checked if the namespace is valid or not. It is an workaround since currently is not possible watch/cache a List of Namespaces
// The impl to allow do it is done and merged in the master branch of the lib but not released in an stable version. It should be removed when this feature be impl.
// See the PR which we are working on to update the deps and have this feature: https://github.com/operator-framework/operator-sdk/pull/1388
if isValidNamespace, err:= utils.IsValidOperatorNamespace(request.Namespace); err != nil || isValidNamespace == false {
if isValidNamespace, err := utils.IsValidOperatorNamespace(request.Namespace, instance.Spec.SkipNamespaceValidation); err != nil || isValidNamespace == false {
// Stop reconcile
operatorNamespace, _ := k8sutil.GetOperatorNamespace();
operatorNamespace, _ := k8sutil.GetOperatorNamespace()
reqLogger.Error(err, "Unable to reconcile Mobile Security Service Database", "Request.Namespace", request.Namespace, "isValidNamespace", isValidNamespace, "Operator.Namespace", operatorNamespace)
return reconcile.Result{}, nil
}

reqLogger.Info("Valid namespace for Mobile Security Service DB", "Namespace", request.Namespace)
reqLogger.Info("Start Reconciling Mobile Security Service DB ...")

instance := &mobilesecurityservicev1alpha1.MobileSecurityServiceDB{}

//Fetch the MobileSecurityService instance
err := r.client.Get(context.TODO(), request.NamespacedName, instance)
if err != nil {
return fetch(r, reqLogger, err)
}

//Check if Deployment for the app exist, if not create one
deployment, err := r.fetchDBDeployment(reqLogger, instance)

if err != nil {
// To give time for the mobile security service be created
time.Sleep(30 * time.Second)
// It will fetch the service instance for the DB type be able to get the configMap config created by it, however,
// if the Instance cannot be found and/or its configMap was not created than the default values specified in its CR will be used
reqLogger.Info("Checking for service instance ...")
serviceInstance := &mobilesecurityservicev1alpha1.MobileSecurityService{}
r.client.Get(context.TODO(), types.NamespacedName{Name: utils.SERVICE_INSTANCE_NAME, Namespace: utils.SERVICE_INSTANCE_NAMESPACE }, serviceInstance)
r.client.Get(context.TODO(), types.NamespacedName{Name: utils.SERVICE_INSTANCE_NAME, Namespace: utils.SERVICE_INSTANCE_NAMESPACE}, serviceInstance)
return r.create(instance, serviceInstance, DEEPLOYMENT, reqLogger, err)
}

Expand All @@ -191,7 +193,7 @@ func (r *ReconcileMobileSecurityServiceDB) Reconcile(request reconcile.Request)
}

//Update status for deployment
deploymentStatus, err := r.updateDeploymentStatus(reqLogger,instance)
deploymentStatus, err := r.updateDeploymentStatus(reqLogger, instance)
if err != nil {
return reconcile.Result{}, err
}
Expand All @@ -209,7 +211,7 @@ func (r *ReconcileMobileSecurityServiceDB) Reconcile(request reconcile.Request)
}

//Update status for DB
if err:= r.updateDBStatus(reqLogger, deploymentStatus, serviceStatus, pvcStatus, instance); err != nil {
if err := r.updateDBStatus(reqLogger, deploymentStatus, serviceStatus, pvcStatus, instance); err != nil {
return reconcile.Result{}, err
}

Expand Down
Loading