Skip to content

Commit

Permalink
Remove the disableGitOpts from fleet-controller
Browse files Browse the repository at this point in the history
It also renames the `finalizerutils` to just `finalizer` and documents
its functions

Signed-off-by: Xavi Garcia <[email protected]>
  • Loading branch information
0xavi0 committed Jun 25, 2024
1 parent aa435ee commit 3a1a210
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
3 changes: 0 additions & 3 deletions charts/fleet/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ spec:
{{- end }}
command:
- fleetcontroller
{{- if not $.Values.gitops.enabled }}
- --disable-gitops
{{- end }}
{{- if . }}
- --shard-id
- {{ quote . }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package finalizeutil
package finalize

import (
"context"
Expand All @@ -20,6 +20,9 @@ const (
BundleDeploymentFinalizer = "fleet.cattle.io/bundle-deployment-finalizer"
)

// PurgeBundles deletes all bundles related to the given GitRepo namespaced name
// It deletes resources in cascade. Deleting Bundles, its BundleDeployments, and
// the related namespace if Bundle.Spec.DeleteNamespace is set to true.
func PurgeBundles(ctx context.Context, c client.Client, gitrepo types.NamespacedName) error {
bundles := &v1alpha1.BundleList{}
err := c.List(ctx, bundles, client.MatchingLabels{v1alpha1.RepoLabel: gitrepo.Name}, client.InNamespace(gitrepo.Namespace))
Expand Down Expand Up @@ -61,6 +64,7 @@ func PurgeBundles(ctx context.Context, c client.Client, gitrepo types.Namespaced
return nil
}

// PurgeBundleDeployments deletes all BundleDeployments related with the given Bundle namespaced name.
func PurgeBundleDeployments(ctx context.Context, c client.Client, bundle types.NamespacedName) error {
list := &v1alpha1.BundleDeploymentList{}
err := c.List(
Expand Down Expand Up @@ -101,6 +105,7 @@ func PurgeBundleDeployments(ctx context.Context, c client.Client, bundle types.N
return nil
}

// PurgeImageScans deletes all ImageScan resources related with the given GitRepo namespaces name.
func PurgeImageScans(ctx context.Context, c client.Client, gitrepo types.NamespacedName) error {
images := &v1alpha1.ImageScanList{}
err := c.List(ctx, images, client.InNamespace(gitrepo.Namespace))
Expand All @@ -120,6 +125,9 @@ func PurgeImageScans(ctx context.Context, c client.Client, gitrepo types.Namespa
return nil
}

// PurgeNamespace deletes the given namespace if deleteNamespace is set to true.
// It ignores the following namespaces, that are considered as default by fleet or kubernetes:
// fleet-local, cattle-fleet-system, fleet-default, cattle-fleet-clusters-system, default
func PurgeNamespace(ctx context.Context, c client.Client, deleteNamespace bool, ns string) error {
if !deleteNamespace {
return nil
Expand Down
14 changes: 7 additions & 7 deletions internal/cmd/controller/gitops/reconciler/gitjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/rancher/fleet/internal/cmd/controller/finalizeutil"
"github.com/rancher/fleet/internal/cmd/controller/finalize"
"github.com/rancher/fleet/internal/cmd/controller/grutil"
"github.com/rancher/fleet/internal/cmd/controller/imagescan"
"github.com/rancher/fleet/internal/metrics"
Expand Down Expand Up @@ -138,7 +138,7 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

if !gitrepo.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(gitrepo, finalizeutil.GitRepoFinalizer) {
if controllerutil.ContainsFinalizer(gitrepo, finalize.GitRepoFinalizer) {
if err := r.cleanupGitRepo(ctx, logger, gitrepo); err != nil {
return ctrl.Result{}, err
}
Expand All @@ -147,7 +147,7 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, nil
}

if !controllerutil.ContainsFinalizer(gitrepo, finalizeutil.GitRepoFinalizer) {
if !controllerutil.ContainsFinalizer(gitrepo, finalize.GitRepoFinalizer) {
err := r.addGitRepoFinalizer(ctx, req.NamespacedName)
if client.IgnoreNotFound(err) != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -264,14 +264,14 @@ func (r *GitJobReconciler) cleanupGitRepo(ctx context.Context, logger logr.Logge
metrics.GitRepoCollector.Delete(gitrepo.Name, gitrepo.Namespace)

nsName := types.NamespacedName{Name: gitrepo.Name, Namespace: gitrepo.Namespace}
if err := finalizeutil.PurgeBundles(ctx, r.Client, nsName); err != nil {
if err := finalize.PurgeBundles(ctx, r.Client, nsName); err != nil {
return err
}

// remove the job scheduled by imagescan, if any
_ = r.Scheduler.DeleteJob(imagescan.GitCommitKey(gitrepo.Namespace, gitrepo.Name))

if err := finalizeutil.PurgeImageScans(ctx, r.Client, nsName); err != nil {
if err := finalize.PurgeImageScans(ctx, r.Client, nsName); err != nil {
return err
}

Expand All @@ -280,7 +280,7 @@ func (r *GitJobReconciler) cleanupGitRepo(ctx context.Context, logger logr.Logge
return err
}

controllerutil.RemoveFinalizer(gitrepo, finalizeutil.GitRepoFinalizer)
controllerutil.RemoveFinalizer(gitrepo, finalize.GitRepoFinalizer)

return r.Update(ctx, gitrepo)
})
Expand All @@ -299,7 +299,7 @@ func (r *GitJobReconciler) addGitRepoFinalizer(ctx context.Context, nsName types
return err
}

controllerutil.AddFinalizer(gitrepo, finalizeutil.GitRepoFinalizer)
controllerutil.AddFinalizer(gitrepo, finalize.GitRepoFinalizer)

return r.Update(ctx, gitrepo)
})
Expand Down
2 changes: 0 additions & 2 deletions internal/cmd/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ func start(
leaderOpts LeaderElectionOptions,
workersOpts ControllerReconcilerWorkers,
bindAddresses BindAddresses,
disableGitops bool,
disableMetrics bool,
shardID string,
) error {
setupLog.Info("listening for changes on local cluster",
"disableGitops", disableGitops,
"disableMetrics", disableMetrics,
)

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/controller/reconciler/bundle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package reconciler
import (
"context"

"github.com/rancher/fleet/internal/cmd/controller/finalizeutil"
"github.com/rancher/fleet/internal/cmd/controller/finalize"
"github.com/rancher/fleet/internal/cmd/controller/summary"
"github.com/rancher/fleet/internal/cmd/controller/target"
"github.com/rancher/fleet/internal/manifest"
Expand Down Expand Up @@ -90,7 +90,7 @@ func (r *BundleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
metrics.BundleCollector.Delete(req.Name, req.Namespace)

logger.V(1).Info("Bundle not found, purging bundle deployments")
if err := finalizeutil.PurgeBundleDeployments(ctx, r.Client, req.NamespacedName); err != nil {
if err := finalize.PurgeBundleDeployments(ctx, r.Client, req.NamespacedName); err != nil {
// A bundle deployment may have been purged by the GitRepo reconciler, hence we ignore
// not-found errors here.
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand Down
2 changes: 0 additions & 2 deletions internal/cmd/controller/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type FleetManager struct {
command.DebugConfig
Kubeconfig string `usage:"Kubeconfig file"`
Namespace string `usage:"namespace to watch" default:"cattle-fleet-system" env:"NAMESPACE"`
DisableGitops bool `usage:"disable gitops components" name:"disable-gitops"`
DisableMetrics bool `usage:"disable metrics" name:"disable-metrics"`
ShardID string `usage:"only manage resources labeled with a specific shard ID" name:"shard-id"`
}
Expand Down Expand Up @@ -162,7 +161,6 @@ func (f *FleetManager) Run(cmd *cobra.Command, args []string) error {
leaderOpts,
workersOpts,
bindAddresses,
f.DisableGitops,
f.DisableMetrics,
f.ShardID,
); err != nil {
Expand Down

0 comments on commit 3a1a210

Please sign in to comment.