Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PVC Provisioning Job #916

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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
80 changes: 80 additions & 0 deletions operators/pkg/tenant-controller/tenant_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"
"time"

batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
netv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand All @@ -30,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -53,6 +55,18 @@ const (
NFSSecretServerNameKey = "server-name"
// NFSSecretPathKey -> NFS path key in NFS secret.
NFSSecretPathKey = "path"
// ProvisionJobBaseImage -> Base container image for Personal Drive provision job.
ProvisionJobBaseImage = "busybox"
// ProvisionJobLabel -> Key of the label added by the Provision Job to flag the PVC.
ProvisionJobLabel = "mydrive-provisioning"
AleTopp marked this conversation as resolved.
Show resolved Hide resolved
// ProvisionJobValueOk -> Value of the label added by the Provision Job to flag the PVC when everything worked fine.
ProvisionJobValueOk = "completed"
// ProvisionJobValuePending -> Value of the label added by the Provision Job to flag the PVC when it hasn't completed yet.
ProvisionJobValuePending = "pending"
// ProvisionJobMaxRetries -> Maximum number of retries for Provision jobs.
ProvisionJobMaxRetries = 3
// ProvisionJobTTLSeconds -> Seconds for Provision jobs before deletion (either failure or success).
ProvisionJobTTLSeconds = 604800
)

// TenantReconciler reconciles a Tenant object.
Expand Down Expand Up @@ -243,6 +257,7 @@ func (r *TenantReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&rbacv1.ClusterRole{}).
Owns(&rbacv1.ClusterRoleBinding{}).
Owns(&netv1.NetworkPolicy{}).
Owns(&batchv1.Job{}).
Watches(&crownlabsv1alpha1.Workspace{},
handler.EnqueueRequestsFromMapFunc(r.workspaceToEnrolledTenants)).
WithOptions(controller.Options{
Expand Down Expand Up @@ -517,6 +532,32 @@ func (r *TenantReconciler) createOrUpdateTnPersonalNFSVolume(ctx context.Context
return err
}
klog.Infof("PVC Secret for tenant %s %s", tn.Name, pvcSecOpRes)

val, ok := pvc.Labels[ProvisionJobLabel]
if !ok || val != ProvisionJobValueOk {
AleTopp marked this conversation as resolved.
Show resolved Hide resolved
chownJob := batchv1.Job{ObjectMeta: metav1.ObjectMeta{Name: pvc.Name + "-provision", Namespace: pvc.Namespace}}

chownJobOpRes, err := ctrl.CreateOrUpdate(ctx, r.Client, &chownJob, func() error {
r.updateTnProvisioningJob(&chownJob, &pvc)
return ctrl.SetControllerReference(tn, &chownJob, r.Scheme)
})
if err != nil {
klog.Errorf("Unable to create or update PVC Provisioning Job for tenant %s -> %s", tn.Name, err)
return err
}
klog.Infof("PVC Provisioning Job for tenant %s %s", tn.Name, chownJobOpRes)

if chownJob.Status.Succeeded == 1 {
pvc.Labels[ProvisionJobLabel] = ProvisionJobValueOk
if err := r.Update(ctx, &pvc); err != nil {
klog.Errorf("PVC Provisioning Job failed to update PVC labels for tenant %s", tn.Name)
}

klog.Infof("PVC Provisioning Job completed for tenant %s", tn.Name)
} else if chownJob.Status.Failed == 1 {
klog.Errorf("PVC Provisioning Job failed for tenant %s", tn.Name)
AleTopp marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else if pvc.Status.Phase == v1.ClaimPending {
klog.Infof("PVC pending for tenant %s", tn.Name)
}
Expand Down Expand Up @@ -570,12 +611,51 @@ func (r *TenantReconciler) updateTnNetPolAllow(np *netv1.NetworkPolicy) {
func (r *TenantReconciler) updateTnPersistentVolumeClaim(pvc *v1.PersistentVolumeClaim) {
scName := r.MyDrivePVCsStorageClassName
pvc.Labels = r.updateTnResourceCommonLabels(pvc.Labels)
pvc.Labels[ProvisionJobLabel] = ProvisionJobValuePending

pvc.Spec.AccessModes = []v1.PersistentVolumeAccessMode{v1.ReadWriteMany}
pvc.Spec.Resources.Requests = v1.ResourceList{v1.ResourceStorage: r.MyDrivePVCsSize}
pvc.Spec.StorageClassName = &scName
}

func (r *TenantReconciler) updateTnProvisioningJob(chownJob *batchv1.Job, pvc *v1.PersistentVolumeClaim) {
if chownJob.CreationTimestamp.IsZero() {
chownJob.Spec.BackoffLimit = ptr.To[int32](ProvisionJobMaxRetries)
chownJob.Spec.TTLSecondsAfterFinished = ptr.To[int32](ProvisionJobTTLSeconds)
chownJob.Spec.Template.Spec.RestartPolicy = v1.RestartPolicyOnFailure
chownJob.Spec.Template.Spec.Containers = []v1.Container{{
Name: "chown-container",
Image: ProvisionJobBaseImage,
Command: []string{"chown", "-R", fmt.Sprintf("%d:%d", forge.CrownLabsUserID, forge.CrownLabsUserID), forge.MyDriveVolumeMountPath},
VolumeMounts: []v1.VolumeMount{{
Name: "mydrive",
MountPath: forge.MyDriveVolumeMountPath,
},
},
Resources: v1.ResourceRequirements{
Requests: v1.ResourceList{
"cpu": resource.MustParse("100m"),
"memory": resource.MustParse("128Mi"),
},
Limits: v1.ResourceList{
"cpu": resource.MustParse("100m"),
"memory": resource.MustParse("128Mi"),
},
},
},
}
chownJob.Spec.Template.Spec.Volumes = []v1.Volume{{
Name: "mydrive",
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvc.Name,
},
},
},
}
}
}

func (r *TenantReconciler) handleKeycloakSubscription(ctx context.Context, tn *crownlabsv1alpha2.Tenant, tenantExistingWorkspaces []crownlabsv1alpha2.TenantWorkspaceEntry) error {
// KcA could be nil for local testing skipping the keycloak subscription
if r.KcA == nil {
Expand Down
Loading