Skip to content

Commit

Permalink
Merge pull request #79 from srl-labs/feat/implement-clab-dir-persistence
Browse files Browse the repository at this point in the history
Feat/implement clab dir persistence
  • Loading branch information
carlmontanari authored Nov 25, 2023
2 parents 126e484 + 2d51344 commit 9f7e548
Show file tree
Hide file tree
Showing 19 changed files with 1,319 additions and 209 deletions.
8 changes: 6 additions & 2 deletions apis/topology/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ type Persistence struct {
// a mounted PVC.
Enabled bool `json:"enabled"`
// ClaimSize is the size of the PVC for this topology -- if not provided this defaults to 5Gi.
// If provided, the string value must be a valid kubernetes storage requests style string.
// If provided, the string value must be a valid kubernetes storage requests style string. Note
// the claim size *cannot be made smaller* once created, but it *can* be expanded. If you need
// to make the claim smaller you must delete the topology (or the node from the topology) and
// re-add it.
// +optional
ClaimSize string `json:"claimSize"`
// StorageClassName is the storage class to set in the PVC -- if not provided this will be left
// empty which will end up using your default storage class.
// empty which will end up using your default storage class. Note that currently we assume you
// have (as default) or provide a dynamically provisionable storage class, hence no selector.
// +optional
StorageClassName string `json:"storageClassName"`
}
Expand Down
8 changes: 7 additions & 1 deletion assets/crd/topology.clabernetes_containerlabs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ spec:
description: ClaimSize is the size of the PVC for this topology
-- if not provided this defaults to 5Gi. If provided, the string
value must be a valid kubernetes storage requests style string.
Note the claim size *cannot be made smaller* once created, but
it *can* be expanded. If you need to make the claim smaller
you must delete the topology (or the node from the topology)
and re-add it.
type: string
enabled:
description: Enabled indicates if persistence of hte containerlab
Expand All @@ -190,7 +194,9 @@ spec:
storageClassName:
description: StorageClassName is the storage class to set in the
PVC -- if not provided this will be left empty which will end
up using your default storage class.
up using your default storage class. Note that currently we
assume you have (as default) or provide a dynamically provisionable
storage class, hence no selector.
type: string
required:
- enabled
Expand Down
8 changes: 7 additions & 1 deletion assets/crd/topology.clabernetes_knes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ spec:
description: ClaimSize is the size of the PVC for this topology
-- if not provided this defaults to 5Gi. If provided, the string
value must be a valid kubernetes storage requests style string.
Note the claim size *cannot be made smaller* once created, but
it *can* be expanded. If you need to make the claim smaller
you must delete the topology (or the node from the topology)
and re-add it.
type: string
enabled:
description: Enabled indicates if persistence of hte containerlab
Expand All @@ -186,7 +190,9 @@ spec:
storageClassName:
description: StorageClassName is the storage class to set in the
PVC -- if not provided this will be left empty which will end
up using your default storage class.
up using your default storage class. Note that currently we
assume you have (as default) or provide a dynamically provisionable
storage class, hence no selector.
type: string
required:
- enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ spec:
description: ClaimSize is the size of the PVC for this topology
-- if not provided this defaults to 5Gi. If provided, the string
value must be a valid kubernetes storage requests style string.
Note the claim size *cannot be made smaller* once created, but
it *can* be expanded. If you need to make the claim smaller
you must delete the topology (or the node from the topology)
and re-add it.
type: string
enabled:
description: Enabled indicates if persistence of hte containerlab
Expand All @@ -190,7 +194,9 @@ spec:
storageClassName:
description: StorageClassName is the storage class to set in the
PVC -- if not provided this will be left empty which will end
up using your default storage class.
up using your default storage class. Note that currently we
assume you have (as default) or provide a dynamically provisionable
storage class, hence no selector.
type: string
required:
- enabled
Expand Down
8 changes: 7 additions & 1 deletion charts/clabernetes/crds/topology.clabernetes_knes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ spec:
description: ClaimSize is the size of the PVC for this topology
-- if not provided this defaults to 5Gi. If provided, the string
value must be a valid kubernetes storage requests style string.
Note the claim size *cannot be made smaller* once created, but
it *can* be expanded. If you need to make the claim smaller
you must delete the topology (or the node from the topology)
and re-add it.
type: string
enabled:
description: Enabled indicates if persistence of hte containerlab
Expand All @@ -186,7 +190,9 @@ spec:
storageClassName:
description: StorageClassName is the storage class to set in the
PVC -- if not provided this will be left empty which will end
up using your default storage class.
up using your default storage class. Note that currently we
assume you have (as default) or provide a dynamically provisionable
storage class, hence no selector.
type: string
required:
- enabled
Expand Down
1 change: 1 addition & 0 deletions charts/clabernetes/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rules:
- configmaps
- services
- pods
- persistentvolumeclaims
verbs:
- get
- list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ rules:
- configmaps
- services
- pods
- persistentvolumeclaims
verbs:
- get
- list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ rules:
- configmaps
- services
- pods
- persistentvolumeclaims
verbs:
- get
- list
Expand Down
3 changes: 3 additions & 0 deletions constants/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const (
// KubernetesService is a const to use for "service".
KubernetesService = "service"

// KubernetesPVC is a const to use for "persistentvolumeclaim".
KubernetesPVC = "persistentvolumeclaim"

// KubernetesDeployment is a const to use for "deployment".
KubernetesDeployment = "deployment"

Expand Down
12 changes: 12 additions & 0 deletions controllers/topology/containerlab/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ func (c *Controller) Reconcile(
reconcileData,
)
if err != nil {
// error already logged
return ctrlruntime.Result{}, err
}

err = c.TopologyReconciler.ReconcilePersistentVolumeClaim(
ctx,
containerlab,
reconcileData,
)
if err != nil {
c.BaseController.Log.Criticalf("failed reconciling clabernetes pvcs, error: %s", err)

return ctrlruntime.Result{}, err
}

Expand Down
11 changes: 11 additions & 0 deletions controllers/topology/kne/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ func (c *Controller) Reconcile(
return ctrlruntime.Result{}, err
}

err = c.TopologyReconciler.ReconcilePersistentVolumeClaim(
ctx,
kne,
reconcileData,
)
if err != nil {
c.BaseController.Log.Criticalf("failed reconciling clabernetes pvcs, error: %s", err)

return ctrlruntime.Result{}, err
}

err = c.TopologyReconciler.ReconcileDeployments(
ctx,
kne,
Expand Down
42 changes: 42 additions & 0 deletions controllers/topology/reconciler/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,41 @@ func (r *DeploymentReconciler) renderDeploymentDevices(
)
}

func (r *DeploymentReconciler) renderDeploymentPersistence(
deployment *k8sappsv1.Deployment,
nodeName,
owningTopologyName string,
owningTopologyCommonSpec *clabernetesapistopologyv1alpha1.TopologyCommonSpec,
) {
if !owningTopologyCommonSpec.Persistence.Enabled {
return
}

volumeName := "containerlab-directory-persistence"

deployment.Spec.Template.Spec.Volumes = append(
deployment.Spec.Template.Spec.Volumes,
k8scorev1.Volume{
Name: volumeName,
VolumeSource: k8scorev1.VolumeSource{
PersistentVolumeClaim: &k8scorev1.PersistentVolumeClaimVolumeSource{
ClaimName: fmt.Sprintf("%s-%s", owningTopologyName, nodeName),
ReadOnly: false,
},
},
},
)

deployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(
deployment.Spec.Template.Spec.Containers[0].VolumeMounts,
k8scorev1.VolumeMount{
Name: volumeName,
ReadOnly: false,
MountPath: fmt.Sprintf("/clabernetes/clab-clabernetes-%s", nodeName),
},
)
}

// Render accepts the owning topology a mapping of clabernetes sub-topology configs and a node name
// and renders the final deployment for this node.
func (r *DeploymentReconciler) Render(
Expand Down Expand Up @@ -707,6 +742,13 @@ func (r *DeploymentReconciler) Render(
&owningTopologyCommonSpec,
)

r.renderDeploymentPersistence(
deployment,
nodeName,
owningTopologyName,
&owningTopologyCommonSpec,
)

return deployment
}

Expand Down
Loading

0 comments on commit 9f7e548

Please sign in to comment.