Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce component reconciler's action verbosity #1388

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
9 changes: 2 additions & 7 deletions controllers/components/dashboard/dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
componentName := computeComponentName()

_, err := reconciler.ComponentReconcilerFor(mgr, componentsv1.DashboardInstanceName, &componentsv1.Dashboard{}).
_, err := reconciler.ComponentReconcilerFor(mgr, &componentsv1.Dashboard{}).
// operands - owned
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Expand Down Expand Up @@ -105,16 +105,11 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
WithAction(customizeResources).
WithAction(deploy.NewAction(
deploy.WithCache(),
deploy.WithFieldOwner(componentsv1.DashboardInstanceName),
deploy.WithLabel(labels.ComponentPartOf, componentsv1.DashboardInstanceName),
)).
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.DashboardInstanceName),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a tangential question - Is there a reason for labels to be components.opendatahub.io/part-of:default-dashboard instead of components.opendatahub.io/part-of:dashboard ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess the question is:
should the value be the name of the component CR or the name of the component kind ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the way we use labels should be cleaned up a little bit :) , so as today we use the instance name because that label is used by predicates and event mapper and some actions but I think the right path would be to:

  • use the Kind for the part-of
  • add some additional annotations for other stuffs (predicated, mapper, etc)

)).
WithAction(updatestatus.NewAction()).
WithAction(updateStatus).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.DashboardInstanceName),
gc.WithUnremovables(gvk.OdhDashboardConfig),
)).
Build(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"

routev1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -110,7 +111,7 @@ func updateStatus(ctx context.Context, rr *odhtypes.ReconciliationRequest) error
&rl,
client.InNamespace(rr.DSCI.Spec.ApplicationsNamespace),
client.MatchingLabels(map[string]string{
labels.ComponentPartOf: componentsv1.DashboardInstanceName,
labels.ComponentPartOf: strings.ToLower(componentsv1.DashboardKind),
}),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ var (
)

func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.DataSciencePipelinesInstanceName,
&componentsv1.DataSciencePipelines{},
).
_, err := reconciler.ComponentReconcilerFor(mgr, &componentsv1.DataSciencePipelines{}).
// customized Owns() for Component with new predicates
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Expand All @@ -77,16 +73,10 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
)).
WithAction(deploy.NewAction(
deploy.WithCache(),
deploy.WithFieldOwner(componentsv1.DataSciencePipelinesInstanceName),
deploy.WithLabel(labels.ComponentPartOf, componentsv1.DataSciencePipelinesInstanceName),
)).
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.DataSciencePipelinesInstanceName),
)).
WithAction(updatestatus.NewAction()).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.DataSciencePipelinesInstanceName),
)).
WithAction(gc.NewAction()).
Build(ctx)

if err != nil {
Expand Down
16 changes: 3 additions & 13 deletions controllers/components/kueue/kueue_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ import (
)

func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.KueueInstanceName,
&componentsv1.Kueue{},
).
_, err := reconciler.ComponentReconcilerFor(mgr, &componentsv1.Kueue{}).
// customized Owns() for Component with new predicates
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Expand All @@ -70,16 +66,10 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
)).
WithAction(deploy.NewAction(
deploy.WithCache(),
deploy.WithFieldOwner(componentsv1.KueueInstanceName),
deploy.WithLabel(labels.ComponentPartOf, componentsv1.KueueInstanceName),
)).
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.KueueInstanceName),
)).
WithAction(updatestatus.NewAction()).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.KueueInstanceName),
)).
WithAction(gc.NewAction()).
Build(ctx)

if err != nil {
Expand Down
13 changes: 2 additions & 11 deletions controllers/components/modelregistry/modelregistry_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ import (
)

func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.ModelRegistryInstanceName,
&componentsv1.ModelRegistry{},
).
_, err := reconciler.ComponentReconcilerFor(mgr, &componentsv1.ModelRegistry{}).
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Owns(&rbacv1.Role{}).
Expand Down Expand Up @@ -91,16 +87,11 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
WithAction(customizeResources).
WithAction(deploy.NewAction(
deploy.WithCache(),
deploy.WithFieldOwner(componentsv1.ModelRegistryInstanceName),
deploy.WithLabel(labels.ComponentPartOf, componentsv1.ModelRegistryInstanceName),
)).
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.ModelRegistryInstanceName),
)).
WithAction(updatestatus.NewAction()).
WithAction(updateStatus).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.ModelRegistryInstanceName),
gc.WithUnremovables(gvk.ServiceMeshMember),
)).
Build(ctx)
Expand Down
16 changes: 3 additions & 13 deletions controllers/components/ray/ray_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ import (
)

func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.RayInstanceName,
&componentsv1.Ray{},
).
_, err := reconciler.ComponentReconcilerFor(mgr, &componentsv1.Ray{}).
// customized Owns() for Component with new predicates
Owns(&corev1.ConfigMap{}).
Owns(&corev1.Secret{}).
Expand All @@ -63,16 +59,10 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
)).
WithAction(deploy.NewAction(
deploy.WithCache(),
deploy.WithFieldOwner(componentsv1.RayInstanceName),
deploy.WithLabel(labels.ComponentPartOf, componentsv1.RayInstanceName),
)).
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.RayInstanceName),
)).
WithAction(updatestatus.NewAction()).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.RayInstanceName),
)).
WithAction(gc.NewAction()).
Build(ctx)

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

func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.TrainingOperatorInstanceName,
&componentsv1.TrainingOperator{},
).
_, err := reconciler.ComponentReconcilerFor(mgr, &componentsv1.TrainingOperator{}).
// customized Owns() for Component with new predicates
Owns(&corev1.ConfigMap{}).
Owns(&promv1.PodMonitor{}).
Expand All @@ -60,16 +56,10 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
)).
WithAction(deploy.NewAction(
deploy.WithCache(),
deploy.WithFieldOwner(componentsv1.TrainingOperatorInstanceName),
deploy.WithLabel(labels.ComponentPartOf, componentsv1.TrainingOperatorInstanceName),
)).
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.TrainingOperatorInstanceName),
)).
WithAction(updatestatus.NewAction()).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.TrainingOperatorInstanceName),
)).
WithAction(gc.NewAction()).
Build(ctx)

if err != nil {
Expand Down
16 changes: 3 additions & 13 deletions controllers/components/trustyai/trustyai_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ import (
)

func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.Manager) error {
_, err := reconciler.ComponentReconcilerFor(
mgr,
componentsv1.TrustyAIInstanceName,
&componentsv1.TrustyAI{},
).
_, err := reconciler.ComponentReconcilerFor(mgr, &componentsv1.TrustyAI{}).
// customized Owns() for Component with new predicates
Owns(&corev1.ConfigMap{}).
Owns(&corev1.ServiceAccount{}).
Expand All @@ -61,16 +57,10 @@ func (s *componentHandler) NewComponentReconciler(ctx context.Context, mgr ctrl.
)).
WithAction(deploy.NewAction(
deploy.WithCache(),
deploy.WithFieldOwner(componentsv1.TrustyAIInstanceName),
deploy.WithLabel(labels.ComponentPartOf, componentsv1.TrustyAIInstanceName),
)).
WithAction(updatestatus.NewAction(
updatestatus.WithSelectorLabel(labels.ComponentPartOf, componentsv1.TrustyAIInstanceName),
)).
WithAction(updatestatus.NewAction()).
// must be the final action
WithAction(gc.NewAction(
gc.WithLabel(labels.ComponentPartOf, componentsv1.TrustyAIInstanceName),
)).
WithAction(gc.NewAction()).
Build(ctx)

if err != nil {
Expand Down
65 changes: 44 additions & 21 deletions pkg/controller/actions/deploy/action_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
odhClient "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/client"
odhTypes "github.com/opendatahub-io/opendatahub-operator/v2/pkg/controller/types"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/annotations"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/metadata/labels"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
)

Expand Down Expand Up @@ -111,22 +112,15 @@
a.cache.Sync()
}

controllerName := strings.ToLower(rr.Instance.GetObjectKind().GroupVersionKind().Kind)

deployHash, err := odhTypes.HashStr(rr)
kind, err := resources.KindForObject(rr.Client.Scheme(), rr.Instance)
if err != nil {
return fmt.Errorf("unable to compute reauest hash: %w", err)
return err

Check warning on line 117 in pkg/controller/actions/deploy/action_deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/actions/deploy/action_deploy.go#L117

Added line #L117 was not covered by tests
}

deployAnnotations := map[string]string{
annotations.ComponentGeneration: strconv.FormatInt(rr.Instance.GetGeneration(), 10),
annotations.ComponentHash: deployHash,
annotations.PlatformType: string(rr.Release.Name),
annotations.PlatformVersion: rr.Release.Version.String(),
}
controllerName := strings.ToLower(kind)

for i := range rr.Resources {
ok, err := a.deploy(ctx, rr, rr.Resources[i], deployAnnotations)
ok, err := a.deploy(ctx, rr, rr.Resources[i])
if err != nil {
return fmt.Errorf("failure deploying %s: %w", rr.Resources[i], err)
}
Expand All @@ -143,7 +137,6 @@
ctx context.Context,
rr *odhTypes.ReconciliationRequest,
obj unstructured.Unstructured,
deployAnnotations map[string]string,
) (bool, error) {
current, lookupErr := a.lookup(ctx, rr.Client, obj)
if lookupErr != nil {
Expand All @@ -156,9 +149,27 @@
return false, nil
}

fo := a.fieldOwner
if fo == "" {
kind, err := resources.KindForObject(rr.Client.Scheme(), rr.Instance)
if err != nil {
return false, err
}

Check warning on line 157 in pkg/controller/actions/deploy/action_deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/actions/deploy/action_deploy.go#L156-L157

Added lines #L156 - L157 were not covered by tests

fo = strings.ToLower(kind)
}

resources.SetLabels(&obj, a.labels)
resources.SetAnnotations(&obj, a.annotations)
resources.SetAnnotations(&obj, deployAnnotations)
resources.SetAnnotation(&obj, annotations.InstanceGeneration, strconv.FormatInt(rr.Instance.GetGeneration(), 10))
resources.SetAnnotation(&obj, annotations.InstanceName, rr.Instance.GetName())
resources.SetAnnotation(&obj, annotations.InstanceUID, string(rr.Instance.GetUID()))
resources.SetAnnotation(&obj, annotations.PlatformType, string(rr.Release.Name))
resources.SetAnnotation(&obj, annotations.PlatformVersion, rr.Release.Version.String())

if resources.GetLabel(&obj, labels.ComponentPartOf) == "" && fo != "" {
zdtsw marked this conversation as resolved.
Show resolved Hide resolved
resources.SetLabel(&obj, labels.ComponentPartOf, fo)
}

// backup copy for caching
origObj := obj.DeepCopy()
Expand Down Expand Up @@ -205,9 +216,21 @@

switch a.deployMode {
case ModePatch:
deployedObj, err = a.patch(ctx, rr.Client, &obj, current)
deployedObj, err = a.patch(
ctx,
rr.Client,
&obj,
current,
client.ForceOwnership,
client.FieldOwner(fo))
case ModeSSA:
deployedObj, err = a.apply(ctx, rr.Client, &obj, current)
deployedObj, err = a.apply(
ctx,
rr.Client,
&obj,
current,
client.ForceOwnership,
client.FieldOwner(fo))
default:
err = fmt.Errorf("unsupported deploy mode %s", a.deployMode)
}
Expand Down Expand Up @@ -278,6 +301,7 @@
c *odhClient.Client,
obj *unstructured.Unstructured,
old *unstructured.Unstructured,
opts ...client.PatchOption,
) (*unstructured.Unstructured, error) {
logf.FromContext(ctx).V(3).Info("patch",
"gvk", obj.GroupVersionKind(),
Expand Down Expand Up @@ -305,7 +329,7 @@
}

default:
// do noting
// do nothing

Check warning on line 332 in pkg/controller/actions/deploy/action_deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/actions/deploy/action_deploy.go#L332

Added line #L332 was not covered by tests
break
}

Expand All @@ -324,8 +348,7 @@
ctx,
old,
client.RawPatch(types.ApplyPatchType, data),
client.ForceOwnership,
client.FieldOwner(a.fieldOwner),
opts...,

Check warning on line 351 in pkg/controller/actions/deploy/action_deploy.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/actions/deploy/action_deploy.go#L351

Added line #L351 was not covered by tests
)

if err != nil {
Expand All @@ -341,6 +364,7 @@
c *odhClient.Client,
obj *unstructured.Unstructured,
old *unstructured.Unstructured,
opts ...client.PatchOption,
) (*unstructured.Unstructured, error) {
logf.FromContext(ctx).V(3).Info("apply",
"gvk", obj.GroupVersionKind(),
Expand Down Expand Up @@ -369,15 +393,14 @@
return nil, fmt.Errorf("failed to merge Deployment %s/%s: %w", obj.GetNamespace(), obj.GetName(), err)
}
default:
// do noting
// do nothing
break
}

err := c.Apply(
ctx,
obj,
client.ForceOwnership,
client.FieldOwner(a.fieldOwner),
opts...,
)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/actions/deploy/action_deploy_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestDeployWithCacheAction(t *testing.T) {
utilruntime.Must(corev1.AddToScheme(s))
utilruntime.Must(appsv1.AddToScheme(s))
utilruntime.Must(apiextensionsv1.AddToScheme(s))
utilruntime.Must(componentsv1.AddToScheme(s))

projectDir, err := envtestutil.FindProjectRoot()
g.Expect(err).NotTo(HaveOccurred())
Expand Down
Loading