Skip to content

Commit

Permalink
Optimize SrivoNetwork CR update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pliurh committed Dec 27, 2019
1 parent 432454a commit a2b6100
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions pkg/controller/sriovnetwork/sriovnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package sriovnetwork
import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"strings"

"github.com/operator-framework/operator-sdk/pkg/k8sutil"
Expand Down Expand Up @@ -136,23 +136,26 @@ func (r *ReconcileSriovNetwork) Reconcile(request reconcile.Request) (reconcile.
err = r.client.Get(context.TODO(), types.NamespacedName{Name: netAttDef.Name, Namespace: netAttDef.Namespace}, found)
if err != nil {
if errors.IsNotFound(err) {
reqLogger.Info("NetworkAttachmentDefinition not exist, creating")
reqLogger.Info("NetworkAttachmentDefinition CR not exist, creating")
err = r.client.Create(context.TODO(), netAttDef)
if err != nil {
reqLogger.Error(err, "Couldn't create NetworkAttachmentDefinition", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
reqLogger.Error(err, "Couldn't create NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
return reconcile.Result{}, err
}
} else {
reqLogger.Error(err, "Couldn't get NetworkAttachmentDefinition", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
reqLogger.Error(err, "Couldn't get NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
return reconcile.Result{}, err
}
} else {
reqLogger.Info("NetworkAttachmentDefinition already exist, updating")
netAttDef.SetResourceVersion(found.GetResourceVersion())
err = r.client.Update(context.TODO(), netAttDef)
if err != nil {
reqLogger.Error(err, "Couldn't update NetworkAttachmentDefinition", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
return reconcile.Result{}, err
reqLogger.Info("NetworkAttachmentDefinition CR already exist")
if !reflect.DeepEqual(found.Spec, netAttDef.Spec) || !reflect.DeepEqual(found.GetAnnotations(), netAttDef.GetAnnotations()) {
reqLogger.Info("Update NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
netAttDef.SetResourceVersion(found.GetResourceVersion())
err = r.client.Update(context.TODO(), netAttDef)
if err != nil {
reqLogger.Error(err, "Couldn't update NetworkAttachmentDefinition CR", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
return reconcile.Result{}, err
}
}
}

Expand Down Expand Up @@ -271,7 +274,7 @@ func renderNetAttDef(cr *sriovnetworkv1.SriovNetwork) (*uns.Unstructured, error)
}
for _, obj := range objs {
raw, _ := json.Marshal(obj)
fmt.Printf("manifest %s\n", raw)
logger.Info("render NetworkAttachementDefinition output", "raw", string(raw))
}
return objs[0], nil
}

0 comments on commit a2b6100

Please sign in to comment.