Skip to content

Commit

Permalink
Implement garbage collection action
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Burgazzoli <[email protected]>
  • Loading branch information
lburgazzoli committed Nov 19, 2024
1 parent e980df6 commit 3f6dda1
Show file tree
Hide file tree
Showing 34 changed files with 1,066 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ linters-settings:
# also allow generics
- generic
- EventHandler # for ToOwner
- discovery.DiscoveryInterface
- dynamic.Interface
revive:
rules:
- name: dot-imports
Expand Down
7 changes: 7 additions & 0 deletions controllers/components/dashboard/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/security"
Expand Down Expand Up @@ -99,6 +101,11 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.DashboardInstanceName),
)).
WithAction(updateStatus).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.DashboardInstanceName),
gc.WithUnremovables(gvk.OdhDashboardConfig),
)).
Build(ctx)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
Expand Down Expand Up @@ -84,6 +85,10 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.DataSciencePipelinesInstanceName),
)).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.DashboardInstanceName),
)).
Build(ctx)

if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/kueue/kueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
Expand Down Expand Up @@ -77,6 +78,10 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.KueueInstanceName),
)).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.KueueInstanceName),
)).
Build(ctx)

if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions controllers/components/modelregistry/modelregistry_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ import (
"sigs.k8s.io/controller-runtime/pkg/builder"

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/template"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/handlers"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/generation"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/predicates/resources"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/reconciler"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
Expand All @@ -60,6 +65,13 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
).
Owns(&admissionregistrationv1.MutatingWebhookConfiguration{}).
Owns(&admissionregistrationv1.ValidatingWebhookConfiguration{}).
// MR also depends on DSCInitialization to properly configure the SMM
// resource
WatchesH(
&dsciv1.DSCInitialization{},
handlers.ToNamed(componentsv1.ModelRegistryInstanceName),
builder.WithPredicates(generation.New()),
).
Watches(&corev1.Namespace{}).
Watches(&extv1.CustomResourceDefinition{}).
// Some ClusterRoles are part of the component deployment, but not owned by
Expand Down Expand Up @@ -95,6 +107,11 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.ModelRegistryInstanceName),
)).
WithAction(updateStatus).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.ModelRegistryInstanceName),
gc.WithUnremovables(gvk.ServiceMeshMember),
)).
Build(ctx)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func initialize(ctx context.Context, rr *odhtypes.ReconciliationRequest) error {
baseManifestInfo(BaseManifestsSourcePath),
extraManifestInfo(BaseManifestsSourcePath),
}

rr.Templates = []odhtypes.TemplateInfo{{
FS: resourcesFS,
Path: ServiceMeshMemberTemplate,
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/ray/ray_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
Expand Down Expand Up @@ -70,6 +71,10 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.RayInstanceName),
)).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.RayInstanceName),
)).
Build(ctx)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
Expand Down Expand Up @@ -67,6 +68,10 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.TrainingOperatorInstanceName),
)).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.TrainingOperatorInstanceName),
)).
Build(ctx)

if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions controllers/components/trustyai/trustyai_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

componentsv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/components/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/render/kustomize"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/actions/updatestatus"
Expand Down Expand Up @@ -68,6 +69,10 @@ func NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.TrustyAIInstanceName),
)).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.TrustyAIInstanceName),
)).
Build(ctx)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion controllers/dscinitialization/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

odhClient, err := odhClient.New(gCtx, cfg, k8sClient)
odhClient, err := odhClient.NewFromConfig(cfg, k8sClient)
Expect(err).NotTo(HaveOccurred())
Expect(odhClient).NotTo(BeNil())

Expand Down
23 changes: 22 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
appsv1 "k8s.io/api/apps/v1"
authorizationv1 "k8s.io/api/authorization/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -79,6 +80,7 @@ import (
odhClient "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/client"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/logger"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/services/gc"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/upgrade"
)

Expand Down Expand Up @@ -266,6 +268,7 @@ func main() { //nolint:funlen,maintidx
Cache: &client.CacheOptions{
DisableFor: []client.Object{
resources.GvkToUnstructured(gvk.OpenshiftIngress),
&authorizationv1.SelfSubjectRulesReview{},
},
// Set it to true so the cache-backed client reads unstructured objects
// or lists from the cache instead of a live lookup.
Expand All @@ -280,7 +283,7 @@ func main() { //nolint:funlen,maintidx

webhook.Init(mgr)

oc, err := odhClient.NewFromManager(ctx, mgr)
oc, err := odhClient.NewFromManager(mgr)
if err != nil {
setupLog.Error(err, "unable to create client")
os.Exit(1)
Expand Down Expand Up @@ -330,6 +333,24 @@ func main() { //nolint:funlen,maintidx
os.Exit(1)
}

ons, err := cluster.GetOperatorNamespace()
if err != nil {
setupLog.Error(err, "unable to determine Operator Namespace")
os.Exit(1)
}

gc.Instance = gc.New(
oc,
ons,
gc.WithUnremovables(gvk.CustomResourceDefinition, gvk.Lease),
)

err = mgr.Add(gc.Instance)
if err != nil {
setupLog.Error(err, "unable to register GC service")
os.Exit(1)
}

// Initialize component reconcilers
if err = CreateComponentReconcilers(ctx, mgr); err != nil {
os.Exit(1)
Expand Down
7 changes: 7 additions & 0 deletions pkg/cluster/gvk/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gvk

import (
appsv1 "k8s.io/api/apps/v1"
coordinationv1 "k8s.io/api/coordination/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -168,4 +169,10 @@ var (
Version: "v1",
Kind: "ServiceMeshMember",
}

Lease = schema.GroupVersionKind{
Group: coordinationv1.SchemeGroupVersion.Group,
Version: coordinationv1.SchemeGroupVersion.Version,
Kind: "Lease",
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func TestDeleteResourcesAction(t *testing.T) {
ns := xid.New().String()

cl, err := fakeclient.New(
ctx,
&appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
APIVersion: gvk.Deployment.GroupVersion().String(),
Expand Down
4 changes: 1 addition & 3 deletions pkg/controller/actions/deploy/action_deploy_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func TestDeployWithCacheAction(t *testing.T) {
g := NewWithT(t)
s := runtime.NewScheme()

ctx := context.Background()

utilruntime.Must(corev1.AddToScheme(s))
utilruntime.Must(appsv1.AddToScheme(s))
utilruntime.Must(apiextensionsv1.AddToScheme(s))
Expand Down Expand Up @@ -67,7 +65,7 @@ func TestDeployWithCacheAction(t *testing.T) {
envTestClient, err := ctrlCli.New(cfg, ctrlCli.Options{Scheme: s})
g.Expect(err).NotTo(HaveOccurred())

cli, err := client.New(ctx, cfg, envTestClient)
cli, err := client.NewFromConfig(cfg, envTestClient)
g.Expect(err).NotTo(HaveOccurred())

t.Run("ExistingResource", func(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/actions/deploy/action_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDeployAction(t *testing.T) {

ctx := context.Background()
ns := xid.New().String()
cl, err := fakeclient.New(ctx)
cl, err := fakeclient.New()
g.Expect(err).ShouldNot(HaveOccurred())

action := deploy.NewAction(
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestDeployNotOwnedSkip(t *testing.T) {

g.Expect(err).ShouldNot(HaveOccurred())

cl, err := fakeclient.New(ctx, oldObj)
cl, err := fakeclient.New(oldObj)
g.Expect(err).ShouldNot(HaveOccurred())

rr := types.ReconciliationRequest{
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestDeployNotOwnedCreate(t *testing.T) {

g.Expect(err).ShouldNot(HaveOccurred())

cl, err := fakeclient.New(ctx)
cl, err := fakeclient.New()
g.Expect(err).ShouldNot(HaveOccurred())

rr := types.ReconciliationRequest{
Expand Down
Loading

0 comments on commit 3f6dda1

Please sign in to comment.