Commit 9f130619 authored by Francisco Borges Aurindo Barros's avatar Francisco Borges Aurindo Barros Committed by Alexandre Lossent
Browse files

Labels set on Project based on Ownership/admin/category

parent f16ac510
......@@ -80,10 +80,12 @@ type ProjectLifecyclePolicySpec struct {
// +optional
ApplicationCategoryLink bool `json:"applicationCategoryLink,omitempty"`
// Overwrite the description of the ApplicationRegistration's parent project with the description
// set in the Application Portal.
// Sync the parent Openshift project's metadata (annotations and labels) with the information from the Application Portal.
// Description goes to the standard Openshift annotation for project description. Owner, Admin Group and category are
// exposed with custom labels.
// +optional
SyncProjectDescription bool `json:"syncProjectDescription,omitempty"`
// +kubebuilder:default:=true
SyncProjectMetadata bool `json:"syncProjectMetadata,omitempty"`
}
// ProjectLifecyclePolicyStatus defines the observed state of ProjectLifecyclePolicy
......
......@@ -75,9 +75,12 @@ spec:
NamespaceDashboard (the only type of link that can be specified
per namespace).
type: boolean
syncProjectDescription:
description: Overwrite the description of the ApplicationRegistration's
parent project with the description set in the Application Portal.
syncProjectMetadata:
default: true
description: Sync the parent Openshift project's metadata (annotations
and labels) with the information from the Application Portal. Description
goes to the standard Openshift annotation for project description.
Owner, Admin Group and category are exposed with custom labels.
type: boolean
required:
- applicationDeletedFromAuthApiPolicy
......
......@@ -45,6 +45,13 @@ import (
consolev1 "github.com/openshift/api/console/v1"
)
// Variables used in function `ensureProjectMetadata`
const (
labelOwner string = "lifecycle.webservices.cern.ch/owner"
labelResourceCategory string = "lifecycle.webservices.cern.ch/resourceCategory"
labelAdminGroup string = "lifecycle.webservices.cern.ch/adminGroup"
)
// ProjectLifecyclePolicyReconciler reconciles a ProjectLifecyclePolicy object
type ProjectLifecyclePolicyReconciler struct {
client.Client
......@@ -160,9 +167,10 @@ func (r *ProjectLifecyclePolicyReconciler) Reconcile(ctx context.Context, req ct
return ctrl.Result{}, err
}
// if user updated application's Description in app portal, propagate the new description
// to the Openshift project description
if err := r.ensureProjectDescription(*policy, appreg); err != nil {
// if user updated application's Description in app portal,
// or changed ownership, adminGroup or Resoruce category,
// propagate this change to the Openshift project metadata
if err := r.ensureProjectMetadata(*policy, appreg); err != nil {
r.logErrorAndSetFailedStatus(ctx, log, policy, err, "Failed to sync project description")
return ctrl.Result{}, err
}
......@@ -285,8 +293,8 @@ func (r *ProjectLifecyclePolicyReconciler) logInfoAndSetCannotApplyStatus(ctx co
return r.Status().Update(ctx, policy)
}
func (r *ProjectLifecyclePolicyReconciler) ensureProjectDescription(policy webservicesv1alpha1.ProjectLifecyclePolicy, appreg webservicesv1alpha1.ApplicationRegistration) error {
if !policy.Spec.SyncProjectDescription {
func (r *ProjectLifecyclePolicyReconciler) ensureProjectMetadata(policy webservicesv1alpha1.ProjectLifecyclePolicy, appreg webservicesv1alpha1.ApplicationRegistration) error {
if !policy.Spec.SyncProjectMetadata {
// nothing to do
return nil
}
......@@ -302,20 +310,30 @@ func (r *ProjectLifecyclePolicyReconciler) ensureProjectDescription(policy webse
// Does nothing if there's no actual change to the resource (here, the namespace)
_, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, &namespace, func() error {
// Mutate function that sets the desired state of the RoleBinding resource.
ModifyProjectDescription(policy, appreg, &namespace)
ModifyProjectMetadata(policy, appreg, &namespace)
return nil
})
return err
}
// Function that sets the desired value for the namespace annotation holding Openshift project description
func ModifyProjectDescription(policy webservicesv1alpha1.ProjectLifecyclePolicy, appreg webservicesv1alpha1.ApplicationRegistration, namespace *v1.Namespace) {
// ModifyProjectMetadata sets the desired value for the namespace annotation and adds custom labels, CurrentOwner, CurrentAdminGroup and ResourceCategory, to the Openshift project
func ModifyProjectMetadata(policy webservicesv1alpha1.ProjectLifecyclePolicy, appreg webservicesv1alpha1.ApplicationRegistration, namespace *v1.Namespace) {
//Set Annotations
a := namespace.Annotations
if a == nil {
a = make(map[string]string)
}
a[annotations.OpenShiftDescription] = appreg.Status.CurrentDescription
namespace.SetAnnotations(a)
//Set Labels
labels := namespace.Labels
if labels == nil {
labels = make(map[string]string)
}
labels[labelOwner] = appreg.Status.CurrentOwnerUsername
labels[labelResourceCategory] = string(appreg.Status.CurrentResourceCategory)
labels[labelAdminGroup] = appreg.Status.CurrentAdminGroup
namespace.SetLabels(labels)
}
func (r *ProjectLifecyclePolicyReconciler) ensureOwnerRoleBinding(policy webservicesv1alpha1.ProjectLifecyclePolicy, appreg webservicesv1alpha1.ApplicationRegistration) error {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment