Skip to content

Commit

Permalink
add cniSpec to HostDeviceRenderData and prettyPrint CNI JSON
Browse files Browse the repository at this point in the history
Signed-off-by: Tariq Ibrahim <tibrahim@nvidia.com>
  • Loading branch information
tariq1890 committed Aug 2, 2023
1 parent ae96ac3 commit 45fb85f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
8 changes: 2 additions & 6 deletions manifests/state-hostdevice-network/0010-hostdevice-net-cr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ metadata:
annotations:
k8s.v1.cni.cncf.io/resourceName: {{.ResourceName}}
spec:
config: '{
"cniVersion":"0.3.1",
"name":"{{.HostDeviceNetworkName}}",
"type":"host-device",
"ipam": {{.CrSpec.IPAM}}
}'
config:
'{{.CNISpec | json}}'
5 changes: 5 additions & 0 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package render

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -113,6 +114,10 @@ func (r *textTemplateRenderer) renderFile(filePath string, data *TemplatingData)
// Create a new template
tmpl := template.New(path.Base(filePath)).Option("missingkey=error")
tmpl.Funcs(template.FuncMap{
"json": func(obj interface{}) (string, error) {
jsonBytes, err := json.Marshal(obj)
return string(jsonBytes), err
},
"yaml": func(obj interface{}) (string, error) {
yamlBytes, err := yamlConverter.Marshal(obj)
return string(yamlBytes), err
Expand Down
11 changes: 11 additions & 0 deletions pkg/state/state_hostdevice_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
)

const (
stateHostDeviceCNIVersion = "0.3.1"
stateHostDeviceCNIType = "host-device"
stateHostDeviceNetworkName = "state-host-device-network"
stateHostDeviceNetworkDescription = "Host Device net-attach-def CR deployed in cluster"
resourceNamePrefix = "nvidia.com/"
Expand Down Expand Up @@ -70,6 +72,7 @@ type HostDeviceManifestRenderData struct {
CrSpec mellanoxv1alpha1.HostDeviceNetworkSpec
RuntimeSpec *runtimeSpec
ResourceName string
CNISpec *cniSpec
}

// Sync attempt to get the system to match the desired state which State represent.
Expand Down Expand Up @@ -135,9 +138,17 @@ func (s *stateHostDeviceNetwork) getManifestObjects(
resourceName = resourceNamePrefix + resourceName
}

cniSpec := &cniSpec{
CNIVersion: stateHostDeviceCNIVersion,
Name: cr.Name,
Type: stateHostDeviceCNIType,
IPAM: cr.Spec.IPAM,
}

renderData := &HostDeviceManifestRenderData{
HostDeviceNetworkName: cr.Name,
CrSpec: cr.Spec,
CNISpec: cniSpec,
RuntimeSpec: &runtimeSpec{
Namespace: config.FromEnv().State.NetworkOperatorResourceNamespace,
},
Expand Down
7 changes: 7 additions & 0 deletions pkg/state/state_skel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ import (
"github.com/Mellanox/network-operator/pkg/render"
)

type cniSpec struct {
CNIVersion string `json:"cniVersion,omitempty"`
Name string `json:"name,omitempty"`
IPAM string `json:"ipam,omitempty"`
Type string `json:"type,omitempty"`
}

type runtimeSpec struct {
Namespace string
}
Expand Down

0 comments on commit 45fb85f

Please sign in to comment.