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 10, 2023
1 parent 2733f11 commit 311a3fc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
4 changes: 2 additions & 2 deletions api/v1/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"

netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -19,8 +20,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"

netattdefv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
render "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/render"
)

const (
Expand Down
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 @@ -129,6 +129,13 @@ func (r *SriovIBNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return reconcile.Result{}, err
}
}

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
}

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

Expand Down
7 changes: 7 additions & 0 deletions controllers/sriovnetwork_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ func (r *SriovNetworkReconciler) Reconcile(ctx context.Context, req ctrl.Request
return reconcile.Result{}, err
}
}

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
}

// Check if this NetworkAttachmentDefinition already exists
found := &netattdefv1.NetworkAttachmentDefinition{}
err = r.Get(ctx, types.NamespacedName{Name: netAttDef.Name, Namespace: netAttDef.Namespace}, found)
Expand Down

0 comments on commit 311a3fc

Please sign in to comment.