Files
my-rook-config/tests/framework/clients/block.go
T

162 lines
5.7 KiB
Go
Raw Normal View History

2017-06-26 21:52:06 -07:00
/*
Copyright 2016 The Rook Authors. All rights reserved.
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.
*/
2017-04-28 08:02:37 -07:00
package clients
import (
"fmt"
2017-06-26 21:52:06 -07:00
"github.com/rook/rook/pkg/model"
2017-06-26 21:52:06 -07:00
"github.com/rook/rook/tests/framework/contracts"
2017-07-21 12:07:15 -07:00
"github.com/rook/rook/tests/framework/utils"
2017-04-28 08:02:37 -07:00
)
2017-07-05 12:26:18 -07:00
//BlockOperation is wrapper for k8s rook block operations
type BlockOperation struct {
2017-07-21 12:07:15 -07:00
k8sClient *utils.K8sHelper
restClient contracts.RestAPIOperator
2017-04-28 08:02:37 -07:00
}
var (
writeDataToBlockPod = []string{"sh", "-c", "WRITE_DATA_CMD"}
readDataFromBlockPod = []string{"cat", "READ_DATA_CMD"}
)
2017-07-05 12:26:18 -07:00
// CreateK8BlockOperation - Constructor to create BlockOperation - client to perform rook Block operations on k8s
2017-07-21 12:07:15 -07:00
func CreateK8BlockOperation(k8shelp *utils.K8sHelper, rookRestClient contracts.RestAPIOperator) *BlockOperation {
return &BlockOperation{k8sClient: k8shelp, restClient: rookRestClient}
2017-04-28 08:02:37 -07:00
}
2017-07-05 12:26:18 -07:00
// BlockCreate Function to create a Block using Rook
2017-07-21 12:07:15 -07:00
// Input parameters -
//name - pod definition that creates a pvc in k8s - yaml should describe name and size of pvc being created
2017-04-28 08:02:37 -07:00
//size - not user for k8s implementation since its descried on the pvc yaml definition
//Output - k8s create pvc operation output and/or error
2017-07-05 12:26:18 -07:00
func (rb *BlockOperation) BlockCreate(name string, size int) (string, error) {
2017-07-21 12:07:15 -07:00
args := []string{"create", "-f", "-"}
result, err := rb.k8sClient.KubectlWithStdin(name, args...)
if err != nil {
return "", fmt.Errorf("Unable to create block -- : %s", err)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
return result, nil
2017-07-05 12:26:18 -07:00
2017-04-28 08:02:37 -07:00
}
2017-07-05 12:26:18 -07:00
// BlockDelete Function to delete a Block using Rook
2017-07-21 12:07:15 -07:00
// Input parameters -
//name - pod definition where pvc is described - delete is run on the the yaml definition
2017-04-28 08:02:37 -07:00
//Output - k8s delete pvc operation output and/or error
2017-07-05 12:26:18 -07:00
func (rb *BlockOperation) BlockDelete(name string) (string, error) {
2017-07-21 12:07:15 -07:00
args := []string{"delete", "-f", "-"}
result, err := rb.k8sClient.KubectlWithStdin(name, args...)
if err != nil {
return "", fmt.Errorf("Unable to delete block -- : %s", err)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
return result, nil
2017-07-05 12:26:18 -07:00
2017-04-28 08:02:37 -07:00
}
2017-07-05 12:26:18 -07:00
// BlockList Function to list all the blocks created/being managed by rook
func (rb *BlockOperation) BlockList() ([]model.BlockImage, error) {
return rb.restClient.GetBlockImages()
2017-04-28 08:02:37 -07:00
}
2017-07-05 12:26:18 -07:00
// BlockMap Function to map a Block using Rook
2017-07-21 12:07:15 -07:00
// Input parameters -
//name - Pod definition - pod should be defined to use a pvc that was created earlier
2017-04-28 08:02:37 -07:00
//mountpath - not used in this impl since mountpath is defined in the pod definition
//Output - k8s create pod operation output and/or error
2017-07-05 12:26:18 -07:00
func (rb *BlockOperation) BlockMap(name string, mountpath string) (string, error) {
2017-07-21 12:07:15 -07:00
args := []string{"create", "-f", "-"}
result, err := rb.k8sClient.KubectlWithStdin(name, args...)
if err != nil {
return "", fmt.Errorf("Unable to map block -- : %s", err)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
return result, nil
2017-04-28 08:02:37 -07:00
}
2017-07-05 12:26:18 -07:00
//BlockWrite Function to write data to block created by rook ,i.e. write data to a pod that is using a pvc
2017-07-21 12:07:15 -07:00
// Input parameters -
2017-04-28 08:02:37 -07:00
//name - path to a yaml file that creates a pod - pod should be defined to use a pvc that was created earlier
//mountpath - folder on the pod were data is supposed to be written(should match the mountpath described in the pod definition)
//data - data to be written
//filename - file where data is written to
//namespace - optional param - namespace of the pod
//Output - k8s exec pod operation output and/or error
2017-07-05 12:26:18 -07:00
func (rb *BlockOperation) BlockWrite(name string, mountpath string, data string, filename string, namespace string) (string, error) {
2017-04-28 08:02:37 -07:00
wt := "echo \"" + data + "\">" + mountpath + "/" + filename
2017-07-21 12:07:15 -07:00
args := []string{"exec", name}
2017-04-28 08:02:37 -07:00
if namespace != "" {
2017-07-21 12:07:15 -07:00
args = append(args, "-n", namespace)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
args = append(args, "--", "sh", "-c", wt)
result, err := rb.k8sClient.Kubectl(args...)
if err != nil {
return "", fmt.Errorf("Unable to write data to pod --: %s", err)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
return result, nil
2017-07-05 12:26:18 -07:00
2017-04-28 08:02:37 -07:00
}
2017-07-05 12:26:18 -07:00
// BlockRead Function to read from block created by rook ,i.e. Read data from a pod that is using a pvc
2017-07-21 12:07:15 -07:00
// Input parameters -
2017-04-28 08:02:37 -07:00
//name - path to a yaml file that creates a pod - pod should be defined to use a pvc that was created earlier
//mountpath - folder on the pod were data is supposed to be written(should match the mountpath described in the pod definition)
//filename - file to be read
//namespace - optional param - namespace of the pod
//Output - k8s exec pod operation output and/or error
2017-07-05 12:26:18 -07:00
func (rb *BlockOperation) BlockRead(name string, mountpath string, filename string, namespace string) (string, error) {
2017-04-28 08:02:37 -07:00
rd := mountpath + "/" + filename
2017-07-21 12:07:15 -07:00
args := []string{"exec", name}
2017-04-28 08:02:37 -07:00
if namespace != "" {
2017-07-21 12:07:15 -07:00
args = append(args, "-n", namespace)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
args = append(args, "--", "cat", rd)
result, err := rb.k8sClient.Kubectl(args...)
if err != nil {
return "", fmt.Errorf("Unable to read data to pod -- : %s", err)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
return result, nil
2017-07-05 12:26:18 -07:00
2017-04-28 08:02:37 -07:00
}
// BlockUnmap Function to unmap a Block using Rook
2017-07-21 12:07:15 -07:00
// Input parameters -
//name - pod definition - the pod described in yaml file is deleted
2017-04-28 08:02:37 -07:00
//mountpath - not used in this impl since mountpath is defined in the pod definition
//Output - k8s delete pod operation output and/or error
2017-07-05 12:26:18 -07:00
func (rb *BlockOperation) BlockUnmap(name string, mountpath string) (string, error) {
2017-07-21 12:07:15 -07:00
args := []string{"delete", "-f", "-"}
result, err := rb.k8sClient.KubectlWithStdin(name, args...)
if err != nil {
return "", fmt.Errorf("Unable to unmap block -- : %s", err)
2017-04-28 08:02:37 -07:00
}
2017-07-21 12:07:15 -07:00
return result, nil
2017-04-28 08:02:37 -07:00
}