From d9d53b19e2e781cdcff92a8d9963c8521a9a3ceb Mon Sep 17 00:00:00 2001 From: Praveen M Date: Mon, 18 Dec 2023 16:34:31 +0530 Subject: [PATCH] deploy: added json field tags for csi config map This commit adds the json field tags for csi config map for encoding and decoding JSON. Signed-off-by: Praveen M --- .../kubernetes/cephfs/csi-config-map_test.go | 2 +- api/deploy/kubernetes/csi-config-map.go | 30 ++++++------ e2e/configmap.go | 16 ++---- internal/cephfs/nodeserver_test.go | 8 +-- internal/cephfs/store/volumeoptions.go | 5 +- internal/rbd/nodeserver_test.go | 18 ++----- internal/util/cluster_mapping_test.go | 4 +- internal/util/csiconfig.go | 49 ++----------------- internal/util/csiconfig_test.go | 36 +++++++------- .../api/deploy/kubernetes/csi-config-map.go | 30 ++++++------ 10 files changed, 74 insertions(+), 124 deletions(-) diff --git a/api/deploy/kubernetes/cephfs/csi-config-map_test.go b/api/deploy/kubernetes/cephfs/csi-config-map_test.go index 42b46be8aeb..a8ce434251f 100644 --- a/api/deploy/kubernetes/cephfs/csi-config-map_test.go +++ b/api/deploy/kubernetes/cephfs/csi-config-map_test.go @@ -35,4 +35,4 @@ func TestNewCSIConfigMapYAML(t *testing.T) { require.NoError(t, err) require.NotEqual(t, "", yaml) -} \ No newline at end of file +} diff --git a/api/deploy/kubernetes/csi-config-map.go b/api/deploy/kubernetes/csi-config-map.go index 5114e666057..4d237b3c51f 100644 --- a/api/deploy/kubernetes/csi-config-map.go +++ b/api/deploy/kubernetes/csi-config-map.go @@ -18,42 +18,42 @@ package kubernetes type ClusterInfo struct { // ClusterID is used for unique identification - ClusterID string + ClusterID string `json:"clusterID"` // Monitors is monitor list for corresponding cluster ID - Monitors []string + Monitors []string `json:"monitors"` // CephFS contains CephFS specific options - CephFS CephFS + CephFS CephFS `json:"cephFS"` // RBD Contains RBD specific options - RBD RBD + RBD RBD `json:"rbd"` // NFS contains NFS specific options - NFS NFS + NFS NFS `json:"nfs"` // Read affinity map options - ReadAffinity ReadAffinity + ReadAffinity ReadAffinity `json:"readAffinity"` } type CephFS struct { // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string + NetNamespaceFilePath string `json:"netNamespaceFilePath"` // SubvolumeGroup contains the name of the SubvolumeGroup for CSI volumes - SubvolumeGroup string + SubvolumeGroup string `json:"subvolumeGroup"` // KernelMountOptions contains the kernel mount options for CephFS volumes - KernelMountOptions string + KernelMountOptions string `json:"kernelMountOptions"` // FuseMountOptions contains the fuse mount options for CephFS volumes - FuseMountOptions string + FuseMountOptions string `json:"fuseMountOptions"` } type RBD struct { // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string + NetNamespaceFilePath string `json:"netNamespaceFilePath"` // RadosNamespace is a rados namespace in the pool - RadosNamespace string + RadosNamespace string `json:"radosNamespace"` } type NFS struct { // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string + NetNamespaceFilePath string `json:"netNamespaceFilePath"` } type ReadAffinity struct { - Enabled bool - CrushLocationLabels []string + Enabled bool `json:"enabled"` + CrushLocationLabels []string `json:"crushLocationLabels"` } diff --git a/e2e/configmap.go b/e2e/configmap.go index a52626af7ca..6ad3978c510 100644 --- a/e2e/configmap.go +++ b/e2e/configmap.go @@ -21,7 +21,7 @@ import ( "encoding/json" "fmt" - "github.com/ceph/ceph-csi/internal/util" + cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes" v1 "k8s.io/api/core/v1" apierrs "k8s.io/apimachinery/pkg/api/errors" @@ -54,19 +54,13 @@ func createConfigMap(pluginPath string, c kubernetes.Interface, f *framework.Fra if err != nil { return err } - conmap := []util.ClusterInfo{{ + conmap := []cephcsi.ClusterInfo{{ ClusterID: fsID, Monitors: mons, - RBD: struct { - NetNamespaceFilePath string `json:"netNamespaceFilePath"` - RadosNamespace string `json:"radosNamespace"` - }{ + RBD: cephcsi.RBD{ RadosNamespace: radosNamespace, }, - ReadAffinity: struct { - Enabled bool `json:"enabled"` - CrushLocationLabels []string `json:"crushLocationLabels"` - }{ + ReadAffinity: cephcsi.ReadAffinity{ Enabled: true, CrushLocationLabels: []string{ crushLocationRegionLabel, @@ -123,7 +117,7 @@ func createCustomConfigMap( for key := range clusterInfo { clusterID = append(clusterID, key) } - conmap := make([]util.ClusterInfo, len(clusterID)) + conmap := make([]cephcsi.ClusterInfo, len(clusterID)) for i, j := range clusterID { conmap[i].ClusterID = j diff --git a/internal/cephfs/nodeserver_test.go b/internal/cephfs/nodeserver_test.go index 34095529eae..d35c1e2cf98 100644 --- a/internal/cephfs/nodeserver_test.go +++ b/internal/cephfs/nodeserver_test.go @@ -24,10 +24,10 @@ import ( "github.com/container-storage-interface/spec/lib/go/csi" + cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes" "github.com/ceph/ceph-csi/internal/cephfs/mounter" "github.com/ceph/ceph-csi/internal/cephfs/store" csicommon "github.com/ceph/ceph-csi/internal/csi-common" - "github.com/ceph/ceph-csi/internal/util" ) func Test_setMountOptions(t *testing.T) { @@ -39,17 +39,17 @@ func Test_setMountOptions(t *testing.T) { configKernelMountOptions := "crc" configFuseMountOptions := "allow_other" - csiConfig := []util.ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", - CephFS: util.CephFS{ + CephFS: cephcsi.CephFS{ KernelMountOptions: configKernelMountOptions, FuseMountOptions: configFuseMountOptions, }, }, { ClusterID: "cluster-2", - CephFS: util.CephFS{ + CephFS: cephcsi.CephFS{ KernelMountOptions: "", FuseMountOptions: "", }, diff --git a/internal/cephfs/store/volumeoptions.go b/internal/cephfs/store/volumeoptions.go index 349e9db6c98..569b17ae93a 100644 --- a/internal/cephfs/store/volumeoptions.go +++ b/internal/cephfs/store/volumeoptions.go @@ -26,6 +26,7 @@ import ( "github.com/container-storage-interface/spec/lib/go/csi" + cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes" "github.com/ceph/ceph-csi/internal/cephfs/core" cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors" fsutil "github.com/ceph/ceph-csi/internal/cephfs/util" @@ -164,7 +165,7 @@ func extractMounter(dest *string, options map[string]string) error { return nil } -func GetClusterInformation(options map[string]string) (*util.ClusterInfo, error) { +func GetClusterInformation(options map[string]string) (*cephcsi.ClusterInfo, error) { clusterID, ok := options["clusterID"] if !ok { err := fmt.Errorf("clusterID must be set") @@ -189,7 +190,7 @@ func GetClusterInformation(options map[string]string) (*util.ClusterInfo, error) return nil, err } - clusterData := &util.ClusterInfo{ + clusterData := &cephcsi.ClusterInfo{ ClusterID: clusterID, Monitors: strings.Split(monitors, ","), } diff --git a/internal/rbd/nodeserver_test.go b/internal/rbd/nodeserver_test.go index 59c2c274cdf..097769e74b9 100644 --- a/internal/rbd/nodeserver_test.go +++ b/internal/rbd/nodeserver_test.go @@ -22,6 +22,7 @@ import ( "os" "testing" + cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes" csicommon "github.com/ceph/ceph-csi/internal/csi-common" "github.com/ceph/ceph-csi/internal/util" @@ -209,13 +210,10 @@ func TestReadAffinity_GetReadAffinityMapOptions(t *testing.T) { } topology := map[string]string{} - csiConfig := []util.ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", - ReadAffinity: struct { - Enabled bool `json:"enabled"` - CrushLocationLabels []string `json:"crushLocationLabels"` - }{ + ReadAffinity: cephcsi.ReadAffinity{ Enabled: true, CrushLocationLabels: []string{ "topology.kubernetes.io/region", @@ -224,10 +222,7 @@ func TestReadAffinity_GetReadAffinityMapOptions(t *testing.T) { }, { ClusterID: "cluster-2", - ReadAffinity: struct { - Enabled bool `json:"enabled"` - CrushLocationLabels []string `json:"crushLocationLabels"` - }{ + ReadAffinity: cephcsi.ReadAffinity{ Enabled: false, CrushLocationLabels: []string{ "topology.kubernetes.io/region", @@ -236,10 +231,7 @@ func TestReadAffinity_GetReadAffinityMapOptions(t *testing.T) { }, { ClusterID: "cluster-3", - ReadAffinity: struct { - Enabled bool `json:"enabled"` - CrushLocationLabels []string `json:"crushLocationLabels"` - }{ + ReadAffinity: cephcsi.ReadAffinity{ Enabled: true, CrushLocationLabels: []string{}, }, diff --git a/internal/util/cluster_mapping_test.go b/internal/util/cluster_mapping_test.go index 6f6d5a9d85b..b97328e99fe 100644 --- a/internal/util/cluster_mapping_test.go +++ b/internal/util/cluster_mapping_test.go @@ -24,6 +24,8 @@ import ( "reflect" "strings" "testing" + + cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes" ) func TestGetClusterMappingInfo(t *testing.T) { @@ -304,7 +306,7 @@ func TestFetchMappedClusterIDAndMons(t *testing.T) { mappingBasePath := t.TempDir() csiConfigFile := mappingBasePath + "/config.json" clusterMappingConfigFile := mappingBasePath + "/cluster-mapping.json" - csiConfig := []ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", Monitors: []string{"ip-1", "ip-2"}, diff --git a/internal/util/csiconfig.go b/internal/util/csiconfig.go index f2355fabdfb..0982172d867 100644 --- a/internal/util/csiconfig.go +++ b/internal/util/csiconfig.go @@ -22,6 +22,8 @@ import ( "fmt" "os" "strings" + + "github.com/ceph/ceph-csi/api/deploy/kubernetes" ) const ( @@ -36,49 +38,6 @@ const ( ClusterIDKey = "clusterID" ) -// ClusterInfo strongly typed JSON spec for the below JSON structure. -type ClusterInfo struct { - // ClusterID is used for unique identification - ClusterID string `json:"clusterID"` - // Monitors is monitor list for corresponding cluster ID - Monitors []string `json:"monitors"` - // CephFS contains CephFS specific options - CephFS CephFS `json:"cephFS"` - // RBD Contains RBD specific options - RBD RBD `json:"rbd"` - // NFS contains NFS specific options - NFS NFS `json:"nfs"` - // Read affinity map options - ReadAffinity ReadAffinity `json:"readAffinity"` -} - -type CephFS struct { - // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string `json:"netNamespaceFilePath"` - // SubvolumeGroup contains the name of the SubvolumeGroup for CSI volumes - SubvolumeGroup string `json:"subvolumeGroup"` - // KernelMountOptions contains the kernel mount options for CephFS volumes - KernelMountOptions string `json:"kernelMountOptions"` - // FuseMountOptions contains the fuse mount options for CephFS volumes - FuseMountOptions string `json:"fuseMountOptions"` -} -type RBD struct { - // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string `json:"netNamespaceFilePath"` - // RadosNamespace is a rados namespace in the pool - RadosNamespace string `json:"radosNamespace"` -} - -type NFS struct { - // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string `json:"netNamespaceFilePath"` -} - -type ReadAffinity struct { - Enabled bool `json:"enabled"` - CrushLocationLabels []string `json:"crushLocationLabels"` -} - // Expected JSON structure in the passed in config file is, //nolint:godot // example json content should not contain unwanted dot. /* @@ -96,8 +55,8 @@ type ReadAffinity struct { } }] */ -func readClusterInfo(pathToConfig, clusterID string) (*ClusterInfo, error) { - var config []ClusterInfo +func readClusterInfo(pathToConfig, clusterID string) (*kubernetes.ClusterInfo, error) { + var config []kubernetes.ClusterInfo // #nosec content, err := os.ReadFile(pathToConfig) diff --git a/internal/util/csiconfig_test.go b/internal/util/csiconfig_test.go index 1e00f80aa7a..8da09907689 100644 --- a/internal/util/csiconfig_test.go +++ b/internal/util/csiconfig_test.go @@ -20,6 +20,8 @@ import ( "encoding/json" "os" "testing" + + cephcsi "github.com/ceph/ceph-csi/api/deploy/kubernetes" ) var ( @@ -164,18 +166,18 @@ func TestGetRBDNetNamespaceFilePath(t *testing.T) { }, } - csiConfig := []ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", Monitors: []string{"ip-1", "ip-2"}, - RBD: RBD{ + RBD: cephcsi.RBD{ NetNamespaceFilePath: "/var/lib/kubelet/plugins/rbd.ceph.csi.com/cluster1-net", }, }, { ClusterID: "cluster-2", Monitors: []string{"ip-3", "ip-4"}, - RBD: RBD{ + RBD: cephcsi.RBD{ NetNamespaceFilePath: "/var/lib/kubelet/plugins/rbd.ceph.csi.com/cluster2-net", }, }, @@ -234,18 +236,18 @@ func TestGetCephFSNetNamespaceFilePath(t *testing.T) { }, } - csiConfig := []ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", Monitors: []string{"ip-1", "ip-2"}, - CephFS: CephFS{ + CephFS: cephcsi.CephFS{ NetNamespaceFilePath: "/var/lib/kubelet/plugins/cephfs.ceph.csi.com/cluster1-net", }, }, { ClusterID: "cluster-2", Monitors: []string{"ip-3", "ip-4"}, - CephFS: CephFS{ + CephFS: cephcsi.CephFS{ NetNamespaceFilePath: "/var/lib/kubelet/plugins/cephfs.ceph.csi.com/cluster2-net", }, }, @@ -304,18 +306,18 @@ func TestGetNFSNetNamespaceFilePath(t *testing.T) { }, } - csiConfig := []ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", Monitors: []string{"ip-1", "ip-2"}, - NFS: NFS{ + NFS: cephcsi.NFS{ NetNamespaceFilePath: "/var/lib/kubelet/plugins/nfs.ceph.csi.com/cluster1-net", }, }, { ClusterID: "cluster-2", Monitors: []string{"ip-3", "ip-4"}, - NFS: NFS{ + NFS: cephcsi.NFS{ NetNamespaceFilePath: "/var/lib/kubelet/plugins/nfs.ceph.csi.com/cluster2-net", }, }, @@ -394,10 +396,10 @@ func TestGetReadAffinityOptions(t *testing.T) { }, } - csiConfig := []ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", - ReadAffinity: ReadAffinity{ + ReadAffinity: cephcsi.ReadAffinity{ Enabled: true, CrushLocationLabels: []string{ "topology.kubernetes.io/region", @@ -408,7 +410,7 @@ func TestGetReadAffinityOptions(t *testing.T) { }, { ClusterID: "cluster-2", - ReadAffinity: ReadAffinity{ + ReadAffinity: cephcsi.ReadAffinity{ Enabled: true, CrushLocationLabels: []string{ "topology.kubernetes.io/region", @@ -417,7 +419,7 @@ func TestGetReadAffinityOptions(t *testing.T) { }, { ClusterID: "cluster-3", - ReadAffinity: ReadAffinity{ + ReadAffinity: cephcsi.ReadAffinity{ Enabled: false, CrushLocationLabels: []string{ "topology.io/rack", @@ -482,24 +484,24 @@ func TestGetCephFSMountOptions(t *testing.T) { }, } - csiConfig := []ClusterInfo{ + csiConfig := []cephcsi.ClusterInfo{ { ClusterID: "cluster-1", - CephFS: CephFS{ + CephFS: cephcsi.CephFS{ KernelMountOptions: "crc", FuseMountOptions: "ro", }, }, { ClusterID: "cluster-2", - CephFS: CephFS{ + CephFS: cephcsi.CephFS{ KernelMountOptions: "", FuseMountOptions: "", }, }, { ClusterID: "cluster-3", - CephFS: CephFS{}, + CephFS: cephcsi.CephFS{}, }, } csiConfigFileContent, err := json.Marshal(csiConfig) diff --git a/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/csi-config-map.go b/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/csi-config-map.go index 5114e666057..4d237b3c51f 100644 --- a/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/csi-config-map.go +++ b/vendor/github.com/ceph/ceph-csi/api/deploy/kubernetes/csi-config-map.go @@ -18,42 +18,42 @@ package kubernetes type ClusterInfo struct { // ClusterID is used for unique identification - ClusterID string + ClusterID string `json:"clusterID"` // Monitors is monitor list for corresponding cluster ID - Monitors []string + Monitors []string `json:"monitors"` // CephFS contains CephFS specific options - CephFS CephFS + CephFS CephFS `json:"cephFS"` // RBD Contains RBD specific options - RBD RBD + RBD RBD `json:"rbd"` // NFS contains NFS specific options - NFS NFS + NFS NFS `json:"nfs"` // Read affinity map options - ReadAffinity ReadAffinity + ReadAffinity ReadAffinity `json:"readAffinity"` } type CephFS struct { // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string + NetNamespaceFilePath string `json:"netNamespaceFilePath"` // SubvolumeGroup contains the name of the SubvolumeGroup for CSI volumes - SubvolumeGroup string + SubvolumeGroup string `json:"subvolumeGroup"` // KernelMountOptions contains the kernel mount options for CephFS volumes - KernelMountOptions string + KernelMountOptions string `json:"kernelMountOptions"` // FuseMountOptions contains the fuse mount options for CephFS volumes - FuseMountOptions string + FuseMountOptions string `json:"fuseMountOptions"` } type RBD struct { // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string + NetNamespaceFilePath string `json:"netNamespaceFilePath"` // RadosNamespace is a rados namespace in the pool - RadosNamespace string + RadosNamespace string `json:"radosNamespace"` } type NFS struct { // symlink filepath for the network namespace where we need to execute commands. - NetNamespaceFilePath string + NetNamespaceFilePath string `json:"netNamespaceFilePath"` } type ReadAffinity struct { - Enabled bool - CrushLocationLabels []string + Enabled bool `json:"enabled"` + CrushLocationLabels []string `json:"crushLocationLabels"` }