Skip to content

Commit

Permalink
controlling existance of primary Project object
Browse files Browse the repository at this point in the history
  • Loading branch information
djkormo committed Jul 3, 2022
1 parent 80b3488 commit 15301c9
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 14 deletions.
2 changes: 1 addition & 1 deletion config/samples/project_v1alpha1_default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: project-operator
labels:
app: project-default
project-operator/pauseReconciliation: "false"
project-operator/pauseReconciliation: "true"
annotations:
"co.elastic.logs/multiline.type": "true"
"co.elastic.logs/multiline.pattern": "true"
Expand Down
2 changes: 1 addition & 1 deletion config/samples/project_v1alpha1_project-1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: project-operator
labels:
app: project-sample-label-1
project-operator/pauseReconciliation: "false"
project-operator/pauseReconciliation: "true"
annotations:
"co.elastic.logs/multiline.type": "true"
"co.elastic.logs/multiline.pattern": "true"
Expand Down
2 changes: 1 addition & 1 deletion config/samples/project_v1alpha1_project-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: project-operator
labels:
app: project-sample-label-2
project-operator/pauseReconciliation: "false"
project-operator/pauseReconciliation: "true"
annotations:
"co.elastic.logs/multiline.type": "true"
"co.elastic.logs/multiline.pattern": "true"
Expand Down
2 changes: 1 addition & 1 deletion controllers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package controllers

// for pausing operator loop
const (
pauseReconciliationLabel = "go-project-operator/pauseReconciliation"
pauseReconciliationLabel = "project-operator/pauseReconciliation"
)
16 changes: 8 additions & 8 deletions controllers/project_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ func (r *ProjectReconciler) limitRangeForProjectApp(m *projectv1alpha1.Project)
return limitRange
}

// SetupWithManager sets up the controller with the Manager.
func (r *ProjectReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&projectv1alpha1.Project{}).
WithEventFilter(ignoreDeletionPredicate()).
Complete(r)
}

type UpdateEvent struct {
// ObjectOld is the object from the event.
ObjectOld runtime.Object
Expand Down Expand Up @@ -354,11 +362,3 @@ func ignoreDeletionPredicate() predicate.Predicate {
},
}
}

// SetupWithManager sets up the controller with the Manager.
func (r *ProjectReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&projectv1alpha1.Project{}).
WithEventFilter(ignoreDeletionPredicate()).
Complete(r)
}
31 changes: 29 additions & 2 deletions controllers/projectnetworkpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ func (r *ProjectNetworkPolicyReconciler) Reconcile(ctx context.Context, req ctrl
return ctrl.Result{}, nil
}

// fetch Project CR instance

projectFound := &projectv1alpha1.Project{}
projectName := ProjectNetworkPolicy.Spec.ProjectName
projectNamespace := ProjectNetworkPolicy.ObjectMeta.Namespace
// get project template
project_err := r.Get(ctx, types.NamespacedName{
Name: projectName,
Namespace: projectNamespace,
}, projectFound)

// Checking if pause reconciliation label is set to true on Project LEVEL
if project_err == nil {
logger.Info("Found project:", projectName, ". That's ok")
logger.Info("Project labels:", projectFound.Labels[pauseReconciliationLabel], " That's ok")
if v, ok := projectFound.Labels[pauseReconciliationLabel]; ok && v == "true" {
logger.Info("Not reconciling ProjectNetworkPolicy: label on Project level", pauseReconciliationLabel, "is true")
return ctrl.Result{}, nil
}
}
// Checking if Project object exists
if project_err != nil {
if errors.IsNotFound(project_err) {

logger.Info("Project resource not found. Ignoring since primary object must be deleted:", projectName, projectNamespace)
return ctrl.Result{}, nil
}
}

// Get array of networkpolicies names
netpolnames := ProjectNetworkPolicy.Spec.NetworkPolicies
logger.Info("List of NetworkPolicy names", "NetworkPolicy.Name", netpolnames)
Expand Down Expand Up @@ -143,8 +172,6 @@ func (r *ProjectNetworkPolicyReconciler) Reconcile(ctx context.Context, req ctrl
continue
}

// checking if namespace is exluded

// Define new networkpolicy
netpol := r.networkpolicyForProjectApp(ProjectNetworkPolicy, netpolname, projectNetworkPolicyTemplateFound) // networkpolicyForProjectApp() returns a network policy
logger.Info("Creating a new NetworkPolicy", "NetworkPolicy.Name", netpolname, "NetworkPolicy.Namespace", netpol.Namespace)
Expand Down
29 changes: 29 additions & 0 deletions controllers/projectrole_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ func (r *ProjectRoleReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, nil
}

// fetch Project CR instance

projectFound := &projectv1alpha1.Project{}
projectName := ProjectRole.Spec.ProjectName
projectNamespace := ProjectRole.ObjectMeta.Namespace
// get project template
project_err := r.Get(ctx, types.NamespacedName{
Name: projectName,
Namespace: projectNamespace,
}, projectFound)

// Checking if pause reconciliation label is set to true on Project LEVEL
if project_err == nil {
logger.Info("Found project:", projectName, ". That's ok")
logger.Info("Project labels:", projectFound.Labels[pauseReconciliationLabel], " That's ok")
if v, ok := projectFound.Labels[pauseReconciliationLabel]; ok && v == "true" {
logger.Info("Not reconciling ProjectRole: label on Project level", pauseReconciliationLabel, "is true")
return ctrl.Result{}, nil
}
}
// Checking if Project object exists
if project_err != nil {
if errors.IsNotFound(project_err) {

logger.Info("Project resource not found. Ignoring since primary object must be deleted:", projectName, projectNamespace)
return ctrl.Result{}, nil
}
}

// Get array of role names
rolenames := ProjectRole.Spec.Roles
logger.Info("List of Role names", "Role.Name", rolenames)
Expand Down

0 comments on commit 15301c9

Please sign in to comment.