Skip to content

Commit

Permalink
Pretty print CNI config when rendering NetAttachDefs
Browse files Browse the repository at this point in the history
  • Loading branch information
tariq1890 committed Mar 13, 2023
1 parent 8c3bf58 commit 787ddd1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions controllers/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package controllers

import (
"bytes"
"encoding/json"
"os"
"strings"

Expand All @@ -38,3 +40,11 @@ func GetImagePullSecrets() []string {
return []string{}
}
}

func formatJSON(str string) (string, error) {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(str), "", " "); err != nil {
return "", err
}
return prettyJSON.String(), nil
}
7 changes: 7 additions & 0 deletions controllers/sriovibnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque
if err != nil {
return reconcile.Result{}, err
}
// format CNI config json in CR for easier readability
netAttDef.Spec.Config, err = formatJSON(netAttDef.Spec.Config)
if err != nil {
reqLogger.Error(err, "Couldn't process rendered NetworkAttachmentDefinition config", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
return reconcile.Result{}, err
}
if lnns, ok := instance.GetAnnotations()[sriovnetworkv1.LASTNETWORKNAMESPACE]; ok && netAttDef.GetNamespace() != lnns {
err = r.Delete(ctx, &netattdefv1.NetworkAttachmentDefinition{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -129,6 +135,7 @@ func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return reconcile.Result{}, err
}
}

// Check if this NetworkAttachmentDefinition already exists
found := &netattdefv1.NetworkAttachmentDefinition{}

Expand Down
6 changes: 6 additions & 0 deletions controllers/sriovnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ func (r *SriovNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request
if err != nil {
return reconcile.Result{}, err
}
// format CNI config json in CR for easier readability
netAttDef.Spec.Config, err = formatJSON(netAttDef.Spec.Config)
if err != nil {
reqLogger.Error(err, "Couldn't process rendered NetworkAttachmentDefinition config", "Namespace", netAttDef.Namespace, "Name", netAttDef.Name)
return reconcile.Result{}, err
}
if lnns, ok := instance.GetAnnotations()[sriovnetworkv1.LASTNETWORKNAMESPACE]; ok && netAttDef.GetNamespace() != lnns {
err = r.Delete(ctx, &netattdefv1.NetworkAttachmentDefinition{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit 787ddd1

Please sign in to comment.