Skip to content

Commit

Permalink
fix(): cleanup for project and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
RohanKumarMainali committed Nov 26, 2024
1 parent 7e05c5d commit 9bf4140
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
13 changes: 11 additions & 2 deletions cleanup/service/cleanup-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ var (
logger = util.NewLogger().With("controller", "GracefulCleanup")
)

type CleanupService struct {
}
type CleanupService struct{}

func (cs *CleanupService) CleanupResources(ctx context.Context) {
controllerManagerNamespace = os.Getenv("KUBESLICE_CONTROLLER_MANAGER_NAMESPACE")
Expand Down Expand Up @@ -63,6 +62,16 @@ func (cs *CleanupService) CleanupResources(ctx context.Context) {
for _, project := range projects.Items {

projectNamespace := project.Labels["kubeslice-project-namespace"]
if project.Spec.DefaultSliceCreation == true {
logger.Info("default slice creation is enabled for project %s. Disable defaultSliceCreation for cleanup", project.Name)
project.Spec.DefaultSliceCreation = false
err := util.CleanupUpdateResource(ctx, &project)
if err != nil {
hasErrors = true
logger.Errorf("%s Error disabling defaultSliceCreation in project ", util.Err, project.Name, err.Error())
}
}

// Delete all ServiceExports
logger.Infof("%s Fetching all ServiceExports for Project %s", util.Find, project.GetName())

Expand Down
4 changes: 2 additions & 2 deletions service/cluster_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func (c *ClusterService) ReconcileCluster(ctx context.Context, req ctrl.Request)
}
} else {
logger.Debug("starting delete for cluster", req.NamespacedName)
// Check if ClusterDeregisterFinalizer is added by worker cluster.
if shouldRequeue, result, reconErr := util.IsReconciled(DeregisterClusterFromDefaultSlice(ctx, req, logger, req.Name)); shouldRequeue {
return result, reconErr
}
logger.Info("after cluster deregister")

// Check if ClusterDeregisterFinalizer is added by worker cluster.
if !util.ContainsString(cluster.GetFinalizers(), ClusterDeregisterFinalizer) {
if shouldRequeue, result, reconErr := util.IsReconciled(c.cleanUpClusterResources(ctx, req, cluster)); shouldRequeue {
return result, reconErr
Expand Down
15 changes: 5 additions & 10 deletions service/default_slice_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
)

// TODO: Remove cluster object
func DefaultSliceOperations(ctx context.Context, req ctrl.Request, logger *zap.SugaredLogger, cluster *controllerv1alpha1.Cluster) (ctrl.Result, error) {
if cluster.Status.IsDeregisterInProgress {
logger.Info("cluster is in deregistration state, skipping default slice operations")
if cluster.Status.IsDeregisterInProgress || !cluster.ObjectMeta.DeletionTimestamp.IsZero() {
logger.Info("cluster is in deregistration state or already deleted, skipping default slice operations")
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -205,8 +204,6 @@ func DeregisterClusterFromDefaultSlice(ctx context.Context, req ctrl.Request, lo
}

defaultSliceName := fmt.Sprintf(util.DefaultProjectSliceName, projectName)
// also check for defaultSliceCreation flag
logger.Info("deregister worker cluster")
if present && project.Spec.DefaultSliceCreation {
// create default slice if not present
defaultProjectSlice := &controllerv1alpha1.SliceConfig{}
Expand All @@ -221,7 +218,7 @@ func DeregisterClusterFromDefaultSlice(ctx context.Context, req ctrl.Request, lo
}

if !foundDefaultSlice {
logger.Info("default slice not found")
logger.Info("default slice not found %s", defaultSliceName)
return ctrl.Result{}, nil
}

Expand All @@ -237,7 +234,6 @@ func DeregisterClusterFromDefaultSlice(ctx context.Context, req ctrl.Request, lo
logger.Info("worker %s is not present in default slice %s, returning without update....", clusterName, defaultSliceName)
return ctrl.Result{}, nil
}
logger.Info("default slice found %v", defaultProjectSlice)

// to deregisterCluster from default, assume it doesn't have any namespace, so every namespace attach to this clusterName will be removed
cluster := controllerv1alpha1.Cluster{
Expand Down Expand Up @@ -273,17 +269,16 @@ func DeregisterClusterFromDefaultSlice(ctx context.Context, req ctrl.Request, lo
modifiedDefaultSliceApplicationNamespace = append(modifiedDefaultSliceApplicationNamespace, appns)
}
}
logger.Info("modifiedDefaultSliceApplicationNamespace %v", modifiedDefaultSliceApplicationNamespace)

defaultProjectSlice.Spec.NamespaceIsolationProfile.ApplicationNamespaces = modifiedDefaultSliceApplicationNamespace
if isNamespaceRemovedFromCluster {
defaultProjectSlice.Spec.Clusters = util.RemoveElementFromArray(defaultProjectSlice.Spec.Clusters, cluster.Name)
err := util.UpdateResource(ctx, defaultProjectSlice)
if err != nil {
logger.Errorf("could not update default slice %v of clusterName", defaultProjectSlice, clusterName)
logger.Errorf("could not remove cluster %s from default slice %s", clusterName, defaultSliceName)
return ctrl.Result{}, err
}
logger.Infof("successfully deregisterd cluster %s from default slice %s", clusterName, defaultSliceName)
logger.Infof("successfully removed cluster %s from default slice %s", clusterName, defaultSliceName)
return ctrl.Result{}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion service/project_webhook_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func validateIfSliceConfigExists(ctx context.Context, project *controllerv1alpha
sliceConfig := &controllerv1alpha1.SliceConfigList{}
projectNamespace := fmt.Sprintf(ProjectNamespacePrefix, project.GetName())
err := util.ListResources(ctx, sliceConfig, client.InNamespace(projectNamespace))
// if the only existing slice config is the default slice config, the project can be deleted, such that we can handle cleanup of default slice in project finalizer
// if the only existing slice config is the default slice config, the project can be deleted
if err == nil && len(sliceConfig.Items) == 1 && sliceConfig.Items[0].Name == fmt.Sprintf(util.DefaultProjectSliceName, project.GetName()) {
return false
}
Expand Down

0 comments on commit 9bf4140

Please sign in to comment.