-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lvmlocalpv vg listing & zfspool listing (#53)
* Make zfslocalpv client testable Signed-off-by: Harsh Vardhan <[email protected]>
- Loading branch information
Harsh Vardhan
authored
Jul 15, 2021
1 parent
7c26ad5
commit 340fefc
Showing
12 changed files
with
327 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright 2020-2021 The OpenEBS Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/openebs/openebsctl/pkg/util" | ||
zfs "github.com/openebs/zfs-localpv/pkg/apis/openebs.io/zfs/v1" | ||
zvolclient "github.com/openebs/zfs-localpv/pkg/generated/clientset/internalclientset" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
// getLVMClient returns OpenEBS clientset by taking kubeconfig as an | ||
// argument | ||
func getZFSclient(kubeconfig string) (*zvolclient.Clientset, error) { | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not build config from flags: %v", err) | ||
} | ||
client, err := zvolclient.NewForConfig(config) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get new config: %v", err) | ||
} | ||
return client, nil | ||
} | ||
|
||
// GetZFSVols returns a list or a map of ZFSVolume depending upon rType & options | ||
func (k K8sClient) GetZFSVols(volNames []string, rType util.ReturnType, labelSelector string, options util.MapOptions) (*zfs.ZFSVolumeList, map[string]zfs.ZFSVolume, error) { | ||
zvols, err := k.ZFCS.ZfsV1().ZFSVolumes("").List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
var list []zfs.ZFSVolume | ||
if len(volNames) == 0 { | ||
list = zvols.Items | ||
} else { | ||
zvsMap := make(map[string]zfs.ZFSVolume) | ||
for _, zv := range zvols.Items { | ||
zvsMap[zv.Name] = zv | ||
} | ||
for _, name := range volNames { | ||
if zv, ok := zvsMap[name]; ok { | ||
list = append(list, zv) | ||
} else { | ||
fmt.Printf("Error from server (NotFound): zfsVolume %s not found\n", name) | ||
} | ||
} | ||
} | ||
if rType == util.List { | ||
return &zfs.ZFSVolumeList{ | ||
Items: list, | ||
}, nil, nil | ||
} | ||
if rType == util.Map { | ||
zvMap := make(map[string]zfs.ZFSVolume) | ||
switch options.Key { | ||
case util.Label: | ||
for _, zv := range list { | ||
if vol, ok := zv.Labels[options.LabelKey]; ok { | ||
zvMap[vol] = zv | ||
} | ||
} | ||
return nil, zvMap, nil | ||
case util.Name: | ||
for _, zv := range list { | ||
zvMap[zv.Name] = zv | ||
} | ||
return nil, zvMap, nil | ||
default: | ||
return nil, nil, fmt.Errorf("invalid map options") | ||
} | ||
} | ||
return nil, nil, fmt.Errorf("invalid return type") | ||
} | ||
|
||
// GetZFSNodes return a list of ZFSNodes | ||
func (k K8sClient) GetZFSNodes() (*zfs.ZFSNodeList, error) { | ||
return k.ZFCS.ZfsV1().ZFSNodes("").List(context.TODO(), metav1.ListOptions{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright 2020-2021 The OpenEBS Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package storage | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/openebs/openebsctl/pkg/client" | ||
"github.com/openebs/openebsctl/pkg/util" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/cli-runtime/pkg/printers" | ||
) | ||
|
||
const ( | ||
firstElemPrefix = `├─` | ||
lastElemPrefix = `└─` | ||
) | ||
|
||
// GetVolumeGroups lists all volume groups by node | ||
func GetVolumeGroups(c *client.K8sClient, vgs []string) error { | ||
lvmNodes, err := c.GetLVMNodes() | ||
if err != nil { | ||
return err | ||
} | ||
var rows []metav1.TableRow | ||
for _, lv := range lvmNodes.Items { | ||
rows = append(rows, metav1.TableRow{Cells: []interface{}{lv.Name, "", "", ""}}) | ||
for i, vg := range lv.VolumeGroups { | ||
var prefix string | ||
if i < len(lv.VolumeGroups)-1 { | ||
prefix = firstElemPrefix | ||
} else { | ||
prefix = lastElemPrefix | ||
} | ||
rows = append(rows, metav1.TableRow{Cells: []interface{}{prefix + vg.Name, | ||
util.ConvertToIBytes(vg.Free.String()), util.ConvertToIBytes(vg.Size.String())}}) | ||
} | ||
rows = append(rows, metav1.TableRow{Cells: []interface{}{"", "", ""}}) | ||
} | ||
// 3. Actually print the table or return an error | ||
if len(rows) == 0 { | ||
return fmt.Errorf("no lvm volumegroups found") | ||
} | ||
util.TablePrinter(util.LVMvolgroupListColumnDefinitions, rows, printers.PrintOptions{Wide: true}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
Copyright 2020-2021 The OpenEBS Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package storage | ||
|
||
import ( | ||
"testing" | ||
|
||
fakelvmclient "github.com/openebs/lvm-localpv/pkg/generated/clientset/internalclientset/fake" | ||
"github.com/openebs/openebsctl/pkg/client" | ||
) | ||
|
||
func TestGetVolumeGroup(t *testing.T) { | ||
type args struct { | ||
c *client.K8sClient | ||
vg []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
"no LVM volumegroups present", | ||
args{ | ||
c: &client.K8sClient{ | ||
Ns: "lvmlocalpv", | ||
K8sCS: nil, | ||
OpenebsCS: nil, | ||
LVMCS: fakelvmclient.NewSimpleClientset(), | ||
}, | ||
vg: nil, | ||
}, | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := GetVolumeGroups(tt.args.c, tt.args.vg); (err != nil) != tt.wantErr { | ||
t.Errorf("GetVolumeGroups() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.