turbonomic / orm

Operator Resource Mapping
Apache License 2.0
5 stars 13 forks source link

Omitted path support #36

Closed kuanf closed 1 year ago

kuanf commented 1 year ago

This issue is part of [OM-97052]: ORM v2 resize action.

All tasks in issue are completed with this PR.

Event recording needs to be done in kubeturbo outside orm, coz it must be done after Updating the owner resource.

I saw kubeturbo already initialized its own event recorder, you can pass it to kuberentes.Toolbox during initialization or use it directly (pass nil to toolbox).

Example of doing updating is in compareAndEnforce function in controllers/advicemapping_controller.go You have your own way to construct the valueFromAdvisor, ignore that part.

        if m.Owner.Namespace == "" {
            m.Owner.Namespace = am.Namespace
        }
        obj, err = kubernetes.Toolbox.GetResourceWithObjectReference(m.Owner.ObjectReference)
        if err != nil {
            acLog.Error(err, "finding true owner", "owner", m.Owner)
            continue
        }

        valueInOwner, found, err := ormutils.NestedField(obj.Object, m.Owner.Path)
        if err != nil {
            acLog.Error(err, "finding path in owner", "path", m.Owner.Path)
            continue
        }
        var valueFromAdvisor interface{}
        vMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(m.Value)
        if err != nil {
            acLog.Error(err, "converting value", "value", m.Value)
            continue
        }
        for _, v := range vMap {
            valueFromAdvisor = v
            break
        }

        if !reflect.DeepEqual(valueInOwner, valueFromAdvisor) {
            ormutils.SetNestedField(obj.Object, valueFromAdvisor, m.Owner.Path)

            err = kubernetes.Toolbox.UpdateResourceWithGVK(obj.GroupVersionKind(), obj)
            if err != nil {
                acLog.Error(err, "updating true owner", "owner", m.Owner)
                continue
            }
            if !found {
                kubernetes.Toolbox.RecordEventf(obj, "Normal", "Updated", "Updated %s as advised by %s/%s and completed omitted path", m.Owner.Path, am.GetNamespace(), am.GetName())
            } else {
                kubernetes.Toolbox.RecordEventf(obj, "Normal", "Updated", "Updated %s as advised by %s/%s", m.Owner.Path, am.GetNamespace(), am.GetName())
            }
        }
tian-ma commented 1 year ago

Will you please add a link to a ticket in jira? Thanks.

kuanf commented 1 year ago

@tian-ma this issue is part of [OM-97052]: ORM v2 resize action. updated PR description