-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal: add sidecar service and client setup for volume group
added sidecar service and client setup code for volume group feature Signed-off-by: Nikhil-Ladha <[email protected]>
- Loading branch information
1 parent
cc4b105
commit 32e811c
Showing
8 changed files
with
1,956 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2024 The Kubernetes-CSI-Addons 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 fake | ||
|
||
import "github.com/csi-addons/kubernetes-csi-addons/internal/proto" | ||
|
||
// VolumeGroupClient to fake grouping operations. | ||
type VolumeGroupClient struct { | ||
// CreateVolumeGroupMock mocks CreateVolumeGroup RPC call. | ||
CreateVolumeGroupMock func(volumeGroupName string, volumeIDs []string) (*proto.CreateVolumeGroupResponse, error) | ||
// ModifyVolumeGroupMembershipMock mock ModifyVolumeGroupMembership RPC call. | ||
ModifyVolumeGroupMembershipMock func(volumeGroupID string, volumeIDs []string) (*proto.ModifyVolumeGroupMembershipResponse, error) | ||
// DeleteVolumeGroupMock mocks DeleteVolumeGroup RPC call. | ||
DeleteVolumeGroupMock func(volumeGroupID string) (*proto.DeleteVolumeGroupResponse, error) | ||
// ControllerGetVolumeGroupMock mocks ControllerGetVolumeGroup RPC call. | ||
ControllerGetVolumeGroupMock func(volumeGroupID string) (*proto.ControllerGetVolumeGroupResponse, error) | ||
} | ||
|
||
// CreateVolumeGroup calls CreateVolumeGroupMock mock function. | ||
func (vg *VolumeGroupClient) CreateVolumeGroup(volumeGroupName string, volumeIDs []string) (*proto.CreateVolumeGroupResponse, error) { | ||
return vg.CreateVolumeGroupMock(volumeGroupName, volumeIDs) | ||
} | ||
|
||
// ModifyVolumeGroupMembership calls ModifyVolumeGroupMembership mock function. | ||
func (vg *VolumeGroupClient) ModifyVolumeGroupMembership(volumeGroupID string, volumeIDs []string) (*proto.ModifyVolumeGroupMembershipResponse, error) { | ||
return vg.ModifyVolumeGroupMembershipMock(volumeGroupID, volumeIDs) | ||
} | ||
|
||
// DeleteVolumeGroup calls DeleteVolumeGroup mock function. | ||
func (vg *VolumeGroupClient) DeleteVolumeGroup(volumeGroupID string) (*proto.DeleteVolumeGroupResponse, error) { | ||
return vg.DeleteVolumeGroupMock(volumeGroupID) | ||
} | ||
|
||
// ControllerGetVolumeGroup calls ControllerGetVolumeGroup mock function. | ||
func (vg *VolumeGroupClient) ControllerGetVolumeGroup(volumeGroupID string) (*proto.ControllerGetVolumeGroupResponse, error) { | ||
return vg.ControllerGetVolumeGroupMock(volumeGroupID) | ||
} |
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,104 @@ | ||
/* | ||
Copyright 2024 The Kubernetes-CSI-Addons 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" | ||
"time" | ||
|
||
"github.com/csi-addons/kubernetes-csi-addons/internal/proto" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
type volumeGroupClient struct { | ||
client proto.VolumeGroupControllerClient | ||
timeout time.Duration | ||
} | ||
|
||
// VolumeGroup holds the methods required for volume grouping. | ||
type VolumeGroup interface { | ||
// CreateVolumeGroup RPC call to create a volume group. | ||
CreateVolumeGroup(volumeGroupName string, volumeIDs []string, secretName, secretNamespace string, parameters map[string]string) (*proto.CreateVolumeGroupResponse, error) | ||
// ModifyVolumeGroupMembership RPC call to modify the volume group. | ||
ModifyVolumeGroupMembership(volumeGroupID string, volumeIDs []string, secretName, secretNamespace string) (*proto.ModifyVolumeGroupMembershipResponse, error) | ||
// DeleteVolumeGroup RPC call to delete the volume group. | ||
DeleteVolumeGroup(volumeGroupID string, secretName, secretNamespace string) (*proto.DeleteVolumeGroupResponse, error) | ||
// ControllerGetVolumeGroup RPC call to fetch the volume group. | ||
ControllerGetVolumeGroup(volumeGroupID string, secretName, secretNamespace string) (*proto.ControllerGetVolumeGroupResponse, error) | ||
} | ||
|
||
// NewReplicationClient returns VolumeGroup interface which has the RPC | ||
// calls for grouping. | ||
func NewVolumeGroupClient(cc *grpc.ClientConn, timeout time.Duration) VolumeGroup { | ||
return &volumeGroupClient{client: proto.NewVolumeGroupControllerClient(cc), timeout: timeout} | ||
} | ||
|
||
func (vg *volumeGroupClient) CreateVolumeGroup(volumeGroupName string, volumeIDs []string, secretName, secretNamespace string, | ||
parameters map[string]string) (*proto.CreateVolumeGroupResponse, error) { | ||
req := &proto.CreateVolumeGroupRequest{ | ||
Name: volumeGroupName, | ||
VolumeIds: volumeIDs, | ||
Parameters: parameters, | ||
SecretName: secretName, | ||
SecretNamespace: secretNamespace, | ||
} | ||
|
||
createCtx, cancel := context.WithTimeout(context.Background(), vg.timeout) | ||
defer cancel() | ||
return vg.client.CreateVolumeGroup(createCtx, req) | ||
} | ||
|
||
func (vg *volumeGroupClient) ModifyVolumeGroupMembership(volumeGroupID string, volumeIDs []string, | ||
secretName, secretNamespace string) (*proto.ModifyVolumeGroupMembershipResponse, error) { | ||
req := &proto.ModifyVolumeGroupMembershipRequest{ | ||
VolumeGroupId: volumeGroupID, | ||
VolumeIds: volumeIDs, | ||
SecretName: secretName, | ||
SecretNamespace: secretNamespace, | ||
} | ||
|
||
createCtx, cancel := context.WithTimeout(context.Background(), vg.timeout) | ||
defer cancel() | ||
return vg.client.ModifyVolumeGroupMembership(createCtx, req) | ||
} | ||
|
||
func (vg *volumeGroupClient) DeleteVolumeGroup(volumeGroupID string, | ||
secretName, secretNamespace string) (*proto.DeleteVolumeGroupResponse, error) { | ||
req := &proto.DeleteVolumeGroupRequest{ | ||
VolumeGroupId: volumeGroupID, | ||
SecretName: secretName, | ||
SecretNamespace: secretNamespace, | ||
} | ||
|
||
createCtx, cancel := context.WithTimeout(context.Background(), vg.timeout) | ||
defer cancel() | ||
return vg.client.DeleteVolumeGroup(createCtx, req) | ||
} | ||
|
||
func (vg *volumeGroupClient) ControllerGetVolumeGroup(volumeGroupID string, | ||
secretName, secretNamespace string) (*proto.ControllerGetVolumeGroupResponse, error) { | ||
req := &proto.ControllerGetVolumeGroupRequest{ | ||
VolumeGroupId: volumeGroupID, | ||
SecretName: secretName, | ||
SecretNamespace: secretNamespace, | ||
} | ||
|
||
createCtx, cancel := context.WithTimeout(context.Background(), vg.timeout) | ||
defer cancel() | ||
return vg.client.ControllerGetVolumeGroup(createCtx, req) | ||
} |
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,125 @@ | ||
/* | ||
Copyright 2024 The Kubernetes-CSI-Addons 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 ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/csi-addons/kubernetes-csi-addons/internal/client/fake" | ||
"github.com/csi-addons/kubernetes-csi-addons/internal/proto" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateVolumeGroup(t *testing.T) { | ||
t.Parallel() | ||
mockedCreateVolumeGroup := &fake.VolumeGroupClient{ | ||
CreateVolumeGroupMock: func(volumeGroupName string, volumeIDs []string) (*proto.CreateVolumeGroupResponse, error) { | ||
return &proto.CreateVolumeGroupResponse{}, nil | ||
}, | ||
} | ||
client := mockedCreateVolumeGroup | ||
resp, err := client.CreateVolumeGroup("", nil) | ||
assert.Equal(t, &proto.CreateVolumeGroupResponse{}, resp) | ||
assert.Nil(t, err) | ||
|
||
// return error | ||
mockedCreateVolumeGroup = &fake.VolumeGroupClient{ | ||
CreateVolumeGroupMock: func(volumeGroupName string, volumeIDs []string) (*proto.CreateVolumeGroupResponse, error) { | ||
return nil, errors.New("failed to create volume group") | ||
}, | ||
} | ||
client = mockedCreateVolumeGroup | ||
resp, err = client.CreateVolumeGroup("", nil) | ||
assert.Nil(t, resp) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestDeleteVolumeGroup(t *testing.T) { | ||
t.Parallel() | ||
mockedDeleteVolumeGroup := &fake.VolumeGroupClient{ | ||
DeleteVolumeGroupMock: func(volumeGroupID string) (*proto.DeleteVolumeGroupResponse, error) { | ||
return &proto.DeleteVolumeGroupResponse{}, nil | ||
}, | ||
} | ||
client := mockedDeleteVolumeGroup | ||
resp, err := client.DeleteVolumeGroup("") | ||
assert.Equal(t, &proto.DeleteVolumeGroupResponse{}, resp) | ||
assert.Nil(t, err) | ||
|
||
// return error | ||
mockedDeleteVolumeGroup = &fake.VolumeGroupClient{ | ||
DeleteVolumeGroupMock: func(volumeGroupID string) (*proto.DeleteVolumeGroupResponse, error) { | ||
return nil, errors.New("failed to delete volume group") | ||
}, | ||
} | ||
client = mockedDeleteVolumeGroup | ||
resp, err = client.DeleteVolumeGroup("") | ||
assert.Nil(t, resp) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestModifyVolumeGroupMembership(t *testing.T) { | ||
t.Parallel() | ||
// return success response | ||
mockedModifyVolumeGroup := &fake.VolumeGroupClient{ | ||
ModifyVolumeGroupMembershipMock: func(volumeGroupID string, volumeIDs []string) (*proto.ModifyVolumeGroupMembershipResponse, error) { | ||
return &proto.ModifyVolumeGroupMembershipResponse{}, nil | ||
}, | ||
} | ||
client := mockedModifyVolumeGroup | ||
resp, err := client.ModifyVolumeGroupMembership("", nil) | ||
assert.Equal(t, &proto.ModifyVolumeGroupMembershipResponse{}, resp) | ||
assert.Nil(t, err) | ||
|
||
// return error | ||
mockedModifyVolumeGroup = &fake.VolumeGroupClient{ | ||
ModifyVolumeGroupMembershipMock: func(volumeGroupID string, volumeIDs []string) (*proto.ModifyVolumeGroupMembershipResponse, error) { | ||
return nil, errors.New("failed to modify volume group") | ||
}, | ||
} | ||
client = mockedModifyVolumeGroup | ||
resp, err = client.ModifyVolumeGroupMembership("", nil) | ||
assert.Nil(t, resp) | ||
assert.NotNil(t, err) | ||
} | ||
|
||
func TestControllerGetVolumeGroup(t *testing.T) { | ||
t.Parallel() | ||
// return success response | ||
mockedGetVolumeGroup := &fake.VolumeGroupClient{ | ||
ControllerGetVolumeGroupMock: func(volumeGroupID string) (*proto.ControllerGetVolumeGroupResponse, error) { | ||
return &proto.ControllerGetVolumeGroupResponse{}, nil | ||
}, | ||
} | ||
client := mockedGetVolumeGroup | ||
resp, err := client.ControllerGetVolumeGroup("") | ||
assert.Equal(t, &proto.ControllerGetVolumeGroupResponse{}, resp) | ||
assert.Nil(t, err) | ||
|
||
// return error | ||
mockedGetVolumeGroup = &fake.VolumeGroupClient{ | ||
ControllerGetVolumeGroupMock: func(volumeGroupID string) (*proto.ControllerGetVolumeGroupResponse, error) { | ||
return nil, errors.New("failed to get volume group") | ||
}, | ||
} | ||
client = mockedGetVolumeGroup | ||
resp, err = client.ControllerGetVolumeGroup("") | ||
assert.Nil(t, resp) | ||
assert.NotNil(t, err) | ||
} |
Oops, something went wrong.