Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6388,6 +6388,14 @@ spec:
sha256sum:
description: SHA256Sum of the Content field
type: string
status:
description: ContentStatus defines the observed state of Content
properties:
referenceCount:
description: ReferenceCount is the number of BundleDeployments that
currently reference this Content resource.
type: integer
type: object
type: object
served: true
storage: true
Expand Down
1 change: 1 addition & 0 deletions charts/fleet/ci/debug-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ controller:
bundle: "1"
bundledeployment: "1"
schedule: "1"
content: "1"

shards:
- id: shard0
Expand Down
1 change: 1 addition & 0 deletions charts/fleet/ci/nobootstrap-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ controller:
bundle: "1"
bundledeployment: "1"
schedule: "1"
content: "1"

shards:
- id: shard0
Expand Down
1 change: 1 addition & 0 deletions charts/fleet/ci/nodebug-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ controller:
bundle: "1"
bundledeployment: "1"
schedule: "1"
content: "1"

shards:
- id: shard0
Expand Down
1 change: 1 addition & 0 deletions charts/fleet/ci/nogitops-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ controller:
bundle: "1"
bundledeployment: "1"
schedule: "1"
content: "1"

shards:
- id: shard0
Expand Down
1 change: 1 addition & 0 deletions charts/fleet/ci/nohelmops-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ controller:
bundle: "1"
bundledeployment: "1"
schedule: "1"
content: "1"

shards:
- id: shard0
Expand Down
4 changes: 4 additions & 0 deletions charts/fleet/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ spec:
- name: SCHEDULE_RECONCILER_WORKERS
value: {{ quote $.Values.controller.reconciler.workers.schedule }}
{{- end }}
{{- if $.Values.controller.reconciler.workers.content }}
- name: CONTENT_RECONCILER_WORKERS
value: {{ quote $.Values.controller.reconciler.workers.content }}
{{- end }}
{{- if $.Values.extraEnv }}
{{ toYaml $.Values.extraEnv | indent 8}}
{{- end }}
Expand Down
1 change: 1 addition & 0 deletions charts/fleet/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ controller:
clustergroup: "50"
imagescan: "50"
schedule: "50"
content: "50"

gitjob:
replicas: 1
Expand Down
4 changes: 4 additions & 0 deletions integrationtests/gitjob/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/rancher/fleet/internal/cmd/controller/gitops"
"github.com/rancher/fleet/internal/cmd/controller/gitops/reconciler"
ctrlreconciler "github.com/rancher/fleet/internal/cmd/controller/reconciler"
"github.com/rancher/fleet/internal/cmd/controller/target"
Expand Down Expand Up @@ -89,6 +90,9 @@ var _ = BeforeSuite(func() {
})
Expect(err).ToNot(HaveOccurred())

Expect(gitops.AddRepoNameLabelIndexer(ctx, mgr)).ToNot(HaveOccurred())
Expect(gitops.AddImageScanGitRepoIndexer(ctx, mgr)).ToNot(HaveOccurred())

ctlr := gomock.NewController(GinkgoT())

// redirect logs to a buffer that we can read in the tests
Expand Down
73 changes: 11 additions & 62 deletions internal/cmd/controller/finalize/finalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import (
"strings"

"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/v3/pkg/kv"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)

const (
Expand Down Expand Up @@ -66,64 +62,6 @@ func PurgeBundles(ctx context.Context, c client.Client, gitrepo types.Namespaced
return nil
}

// PurgeContent tries to delete the content resource related with the given bundle deployment.
func PurgeContent(ctx context.Context, c client.Client, name, deplID string) error {
contentID, _ := kv.Split(deplID, ":")
content := &v1alpha1.Content{}
if err := c.Get(ctx, types.NamespacedName{Name: contentID}, content); err != nil {
return client.IgnoreNotFound(err)
}

logger := log.FromContext(ctx).WithName("purge-content").WithValues("contentID", contentID, "finalizerName", name)

nn := types.NamespacedName{Name: content.Name}
if controllerutil.ContainsFinalizer(content, name) {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
if err := c.Get(ctx, nn, content); err != nil {
return client.IgnoreNotFound(err)
}

controllerutil.RemoveFinalizer(content, name)

return c.Update(ctx, content)
})
if err != nil {
return err
}

logger.V(1).Info("Removed finalizer from content resource")
}

if len(content.Finalizers) == 0 {
if err := c.Delete(ctx, content); err != nil {
return err
}
logger.V(1).Info("Deleted content resource")
}

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))
if err != nil {
return err
}

for _, image := range images.Items {
if image.Spec.GitRepoName == gitrepo.Name {
err := c.Delete(ctx, &image)
if err != nil {
return err
}
}

}
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
Expand Down Expand Up @@ -168,3 +106,14 @@ func EnsureFinalizer(ctx context.Context, c client.Client, obj client.Object, fi
controllerutil.AddFinalizer(obj, finalizer)
return c.Update(ctx, obj)
}

func PurgeTargetNamespaceIfNeeded(ctx context.Context, c client.Client, gitrepo *v1alpha1.GitRepo) error {
deleteNamespace := gitrepo.Spec.DeleteNamespace
namespace := gitrepo.Spec.TargetNamespace

if gitrepo.Spec.KeepResources {
deleteNamespace = false
}

return PurgeNamespace(ctx, c, deleteNamespace, namespace)
}
51 changes: 51 additions & 0 deletions internal/cmd/controller/gitops/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
clog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

command "github.com/rancher/fleet/internal/cmd"
"github.com/rancher/fleet/internal/cmd/controller/gitops/reconciler"
fcreconciler "github.com/rancher/fleet/internal/cmd/controller/reconciler"
"github.com/rancher/fleet/internal/config"
"github.com/rancher/fleet/internal/metrics"
"github.com/rancher/fleet/internal/ssh"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
Expand Down Expand Up @@ -140,6 +142,18 @@ func (g *GitOperator) Run(cmd *cobra.Command, args []string) error {

kh := ssh.KnownHosts{EnforceHostKeyChecks: !g.SkipHostKeyChecks}

// Add an indexer for the Gitrepo name label as that will make accesses in the cache
// faster
if err := AddRepoNameLabelIndexer(ctx, mgr); err != nil {
return err
}

// Add an indexer for the GitRepo name field in ImageScans as that will make accesses in the cache
// faster
if err := AddImageScanGitRepoIndexer(ctx, mgr); err != nil {
return err
}

gitJobReconciler := &reconciler.GitJobReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down Expand Up @@ -245,3 +259,40 @@ func startWebhook(ctx context.Context, namespace string, addr string, client cli

return nil
}

func AddRepoNameLabelIndexer(ctx context.Context, mgr manager.Manager) error {
return mgr.GetFieldIndexer().IndexField(
ctx,
&fleet.Bundle{},
config.RepoNameIndex,
func(obj client.Object) []string {
content, ok := obj.(*fleet.Bundle)
if !ok {
return nil
}
if name, exists := content.Labels[fleet.RepoLabel]; exists {
return []string{name}
}

return nil
},
)
}

func AddImageScanGitRepoIndexer(ctx context.Context, mgr manager.Manager) error {
return mgr.GetFieldIndexer().IndexField(
ctx,
&fleet.ImageScan{},
config.ImageScanGitRepoIndex,
func(obj client.Object) []string {
content, ok := obj.(*fleet.ImageScan)
if !ok {
return nil
}
if content.Spec.GitRepoName == "" {
return nil
}
return []string{content.Spec.GitRepoName}
},
)
}
Loading