Skip to content

Commit

Permalink
set gwSvcType during workersliceconfig creation
Browse files Browse the repository at this point in the history
Signed-off-by: Mridul <[email protected]>
  • Loading branch information
mridulgain committed Oct 20, 2023
1 parent aec07c4 commit a7a4691
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 49 deletions.
2 changes: 1 addition & 1 deletion service/mocks/IVpnKeyRotationService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions service/mocks/IWorkerSliceConfigService.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion service/slice_config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package service
import (
"context"
"fmt"

"github.com/kubeslice/kubeslice-controller/metrics"

"github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
Expand Down Expand Up @@ -132,7 +133,18 @@ func (s *SliceConfigService) ReconcileSliceConfig(ctx context.Context, req ctrl.
clusterCidr := util.FindCIDRByMaxClusters(sliceConfig.Spec.MaxClusters)
completeResourceName := fmt.Sprintf(util.LabelValue, util.GetObjectKind(sliceConfig), sliceConfig.GetName())
ownershipLabel := util.GetOwnerLabel(completeResourceName)
clusterMap, err := s.ms.CreateMinimalWorkerSliceConfig(ctx, sliceConfig.Spec.Clusters, req.Namespace, ownershipLabel, sliceConfig.Name, sliceConfig.Spec.SliceSubnet, clusterCidr)
// create cluster wise slice gw svc info
var sliceGwSvcTypeMap = make(map[string]*v1alpha1.SliceGatewayServiceType)
for _, gwSvctype := range sliceConfig.Spec.SliceGatewayProvider.SliceGatewayServiceType {
if gwSvctype.Cluster == "*" {
for _, cluster := range sliceConfig.Spec.Clusters {
sliceGwSvcTypeMap[cluster] = &gwSvctype
}
} else {
sliceGwSvcTypeMap[gwSvctype.Cluster] = &gwSvctype
}
}
clusterMap, err := s.ms.CreateMinimalWorkerSliceConfig(ctx, sliceConfig.Spec.Clusters, req.Namespace, ownershipLabel, sliceConfig.Name, sliceConfig.Spec.SliceSubnet, clusterCidr, sliceGwSvcTypeMap)
if err != nil {
return ctrl.Result{}, err
}
Expand Down
13 changes: 7 additions & 6 deletions service/slice_config_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"context"
"errors"
"fmt"
"testing"

"github.com/kubeslice/kubeslice-controller/metrics"
metricMock "github.com/kubeslice/kubeslice-controller/metrics/mocks"
"testing"

"github.com/kubeslice/kubeslice-monitoring/pkg/events"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -99,7 +100,7 @@ func SliceConfigReconciliationCompleteHappyCase(t *testing.T) {
"cluster-2": 2,
}

workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
workerSliceGatewayMock.On("CreateMinimumWorkerSliceGateways", ctx, mock.Anything, mock.Anything, requestObj.Namespace, mock.Anything, clusterMap, mock.Anything, mock.Anything).Return(ctrl.Result{}, nil).Once()
label := map[string]string{
"original-slice-name": sliceConfig.Name,
Expand Down Expand Up @@ -279,7 +280,7 @@ func SliceConfigErrorOnCreateWorkerSliceConfig(t *testing.T) {
"cluster-2": 2,
}
err1 := errors.New("internal_error")
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, err1).Once()
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, err1).Once()
result, err2 := sliceConfigService.ReconcileSliceConfig(ctx, requestObj)
expectedResult := ctrl.Result{}
require.Error(t, err2)
Expand Down Expand Up @@ -310,7 +311,7 @@ func SliceConfigErrorOnCreateWorkerSliceGateway(t *testing.T) {
"cluster-1": 1,
"cluster-2": 2,
}
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
err1 := errors.New("internal_error")
workerSliceGatewayMock.On("CreateMinimumWorkerSliceGateways", ctx, mock.Anything, mock.Anything, requestObj.Namespace, mock.Anything, clusterMap, mock.Anything, mock.Anything).Return(ctrl.Result{}, err1).Once()
result, err2 := sliceConfigService.ReconcileSliceConfig(ctx, requestObj)
Expand Down Expand Up @@ -586,7 +587,7 @@ func SliceConfigErrorOnListingServiceExport(t *testing.T) {
"cluster-1": 1,
"cluster-2": 2,
}
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
workerSliceGatewayMock.On("CreateMinimumWorkerSliceGateways", ctx, mock.Anything, mock.Anything, requestObj.Namespace, mock.Anything, clusterMap, mock.Anything, mock.Anything).Return(ctrl.Result{}, nil).Once()
label := map[string]string{
"original-slice-name": sliceConfig.Name,
Expand Down Expand Up @@ -625,7 +626,7 @@ func SliceConfigErrorOnCreateOrUpdateServiceImport(t *testing.T) {
"cluster-1": 1,
"cluster-2": 2,
}
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
workerSliceConfigMock.On("CreateMinimalWorkerSliceConfig", ctx, mock.Anything, requestObj.Namespace, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(clusterMap, nil).Once()
workerSliceGatewayMock.On("CreateMinimumWorkerSliceGateways", ctx, mock.Anything, mock.Anything, requestObj.Namespace, mock.Anything, clusterMap, mock.Anything, mock.Anything).Return(ctrl.Result{}, nil).Once()
label := map[string]string{
"original-slice-name": sliceConfig.Name,
Expand Down
59 changes: 32 additions & 27 deletions service/worker_slice_config_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ import (
"fmt"
"time"

"github.com/kubeslice/kubeslice-controller/metrics"

"github.com/kubeslice/kubeslice-controller/events"

"go.uber.org/zap"

"github.com/jinzhu/copier"
controllerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
workerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/worker/v1alpha1"
"github.com/kubeslice/kubeslice-controller/util"
"go.uber.org/zap"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

controllerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/controller/v1alpha1"
workerv1alpha1 "github.com/kubeslice/kubeslice-controller/apis/worker/v1alpha1"
"github.com/kubeslice/kubeslice-controller/events"
"github.com/kubeslice/kubeslice-controller/metrics"
"github.com/kubeslice/kubeslice-controller/util"
)

const workerSliceConfigNameFormat = "%s-%s"
Expand All @@ -44,7 +42,7 @@ type IWorkerSliceConfigService interface {
DeleteWorkerSliceConfigByLabel(ctx context.Context, label map[string]string, namespace string) error
ListWorkerSliceConfigs(ctx context.Context, ownerLabel map[string]string, namespace string) ([]workerv1alpha1.WorkerSliceConfig, error)
ComputeClusterMap(clusterNames []string, workerSliceConfigs []workerv1alpha1.WorkerSliceConfig) map[string]int
CreateMinimalWorkerSliceConfig(ctx context.Context, clusters []string, namespace string, label map[string]string, name, sliceSubnet string, clusterCidr string) (map[string]int, error)
CreateMinimalWorkerSliceConfig(ctx context.Context, clusters []string, namespace string, label map[string]string, name, sliceSubnet string, clusterCidr string, sliceGwSvcTypeMap map[string]*controllerv1alpha1.SliceGatewayServiceType) (map[string]int, error)
}

// WorkerSliceConfigService implements the IWorkerSliceConfigService interface
Expand Down Expand Up @@ -214,21 +212,21 @@ outer:
logger.With(zap.Error(err)).Errorf("Failed to deep copy external gateway configuration")
}

// Reconcile Slice gateway service type
sliceGatewayProvider := workerv1alpha1.WorkerSliceGatewayProvider{
SliceGatewayType: sliceConfig.Spec.SliceGatewayProvider.SliceGatewayType,
SliceCaType: sliceConfig.Spec.SliceGatewayProvider.SliceCaType,
}
gwSvcTypePresent := false
for _, gwSvcType := range sliceConfig.Spec.SliceGatewayProvider.SliceGatewayServiceType {
if gwSvcType.Cluster == "*" || gwSvcType.Cluster == workerSliceConfig.Labels["worker-cluster"] {
sliceGatewayProvider.SliceGatewayServiceType = gwSvcType.Type
gwSvcTypePresent = true
}
}
if !gwSvcTypePresent {
sliceGatewayProvider.SliceGatewayServiceType = defaultSliceGatewayServiceType
}
// // Reconcile Slice gateway service type
// sliceGatewayProvider := workerv1alpha1.WorkerSliceGatewayProvider{
// SliceGatewayType: sliceConfig.Spec.SliceGatewayProvider.SliceGatewayType,
// SliceCaType: sliceConfig.Spec.SliceGatewayProvider.SliceCaType,
// }
// gwSvcTypePresent := false
// for _, gwSvcType := range sliceConfig.Spec.SliceGatewayProvider.SliceGatewayServiceType {
// if gwSvcType.Cluster == "*" || gwSvcType.Cluster == workerSliceConfig.Labels["worker-cluster"] {
// sliceGatewayProvider.SliceGatewayServiceType = gwSvcType.Type
// gwSvcTypePresent = true
// }
// }
// if !gwSvcTypePresent {
// sliceGatewayProvider.SliceGatewayServiceType = defaultSliceGatewayServiceType
// }

// Reconcile the Namespace Isolation Profile
controllerIsolationProfile := sliceConfig.Spec.NamespaceIsolationProfile
Expand Down Expand Up @@ -256,7 +254,7 @@ outer:
}

workerSliceConfig.Spec.ExternalGatewayConfig = externalGatewayConfig
workerSliceConfig.Spec.SliceGatewayProvider = sliceGatewayProvider
// workerSliceConfig.Spec.SliceGatewayProvider = sliceGatewayProvider
workerSliceConfig.Spec.NamespaceIsolationProfile = workerIsolationProfile
workerSliceConfig.Spec.SliceName = sliceConfig.Name
workerSliceConfig.Spec.Octet = octet
Expand All @@ -270,7 +268,7 @@ outer:

// CreateMinimalWorkerSliceConfig CreateWorkerSliceConfig is a function to create the worker slice configs with minimum number of fields.
// More fields are added in reconciliation loop.
func (s *WorkerSliceConfigService) CreateMinimalWorkerSliceConfig(ctx context.Context, clusters []string, namespace string, label map[string]string, name, sliceSubnet string, clusterCidr string) (map[string]int, error) {
func (s *WorkerSliceConfigService) CreateMinimalWorkerSliceConfig(ctx context.Context, clusters []string, namespace string, label map[string]string, name, sliceSubnet string, clusterCidr string, sliceGwSvcTypeMap map[string]*controllerv1alpha1.SliceGatewayServiceType) (map[string]int, error) {
logger := util.CtxLogger(ctx)

//Load Event Recorder with project name, slice name and namespace
Expand Down Expand Up @@ -307,6 +305,11 @@ func (s *WorkerSliceConfigService) CreateMinimalWorkerSliceConfig(ctx context.Co
}
ipamOctet := clusterMap[cluster]
clusterSubnetCIDR := fmt.Sprintf(util.GetClusterPrefixPool(sliceSubnet, ipamOctet, clusterCidr))
// determine gw svc type
sliceGwSvcType := defaultSliceGatewayServiceType
if val, exists := sliceGwSvcTypeMap[cluster]; exists {
sliceGwSvcType = val.Type
}
if !found {
label["project-namespace"] = namespace
label["original-slice-name"] = name
Expand All @@ -325,6 +328,7 @@ func (s *WorkerSliceConfigService) CreateMinimalWorkerSliceConfig(ctx context.Co
expectedSlice.Spec.Octet = &ipamOctet
expectedSlice.Spec.ClusterSubnetCIDR = clusterSubnetCIDR
expectedSlice.Spec.SliceSubnet = sliceSubnet
expectedSlice.Spec.SliceGatewayProvider.SliceGatewayServiceType = sliceGwSvcType
err = util.CreateResource(ctx, &expectedSlice)
if err != nil {
//Register an event for worker slice config creation failure
Expand Down Expand Up @@ -357,6 +361,7 @@ func (s *WorkerSliceConfigService) CreateMinimalWorkerSliceConfig(ctx context.Co
existingSlice.UID = ""
existingSlice.Spec.Octet = &ipamOctet
existingSlice.Spec.ClusterSubnetCIDR = clusterSubnetCIDR
existingSlice.Spec.SliceGatewayProvider.SliceGatewayServiceType = sliceGwSvcType
logger.Debug("updating slice with new octet", existingSlice)
if existingSlice.Annotations == nil {
existingSlice.Annotations = make(map[string]string)
Expand Down
11 changes: 6 additions & 5 deletions service/worker_slice_config_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"context"
"errors"
"fmt"
"testing"

"github.com/kubeslice/kubeslice-controller/metrics"
metricMock "github.com/kubeslice/kubeslice-controller/metrics/mocks"
"testing"

"github.com/kubeslice/kubeslice-monitoring/pkg/events"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -338,7 +339,7 @@ func testCreateWorkerSliceConfigNewClusterSuccess(t *testing.T) {
clientMock.On("Create", ctx, mock.Anything).Return(nil).Once()
clientMock.On("Create", ctx, mock.AnythingOfType("*v1.Event")).Return(nil).Once()
mMock.On("RecordCounterMetric", mock.Anything, mock.Anything).Return().Once()
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20")
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20", nil)
require.Equal(t, len(result), 2)
require.NoError(t, nil)
require.Nil(t, err)
Expand Down Expand Up @@ -405,7 +406,7 @@ func testCreateWorkerSliceConfigNewClusterFails(t *testing.T) {
clientMock.On("Create", ctx, mock.Anything).Return(err1).Once()
clientMock.On("Create", ctx, mock.AnythingOfType("*v1.Event")).Return(nil).Once()
mMock.On("RecordCounterMetric", mock.Anything, mock.Anything).Return().Once()
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20")
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20", nil)
require.Error(t, err)
require.Equal(t, len(result), 2)
require.Equal(t, err, err1)
Expand Down Expand Up @@ -472,7 +473,7 @@ func testCreateWorkerSliceConfigUpdateClusterSuccess(t *testing.T) {
mMock.On("RecordCounterMetric", mock.Anything, mock.Anything).Return().Once()
clientMock.On("Update", ctx, mock.AnythingOfType("*v1.Event")).Return(nil).Once()
mMock.On("RecordCounterMetric", mock.Anything, mock.Anything).Return().Once()
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20")
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20", nil)
require.Equal(t, len(result), 2)
require.NoError(t, nil)
require.Nil(t, err)
Expand Down Expand Up @@ -538,7 +539,7 @@ func testCreateWorkerSliceConfigUpdateClusterFails(t *testing.T) {
clientMock.On("Update", ctx, mock.Anything).Return(err1).Once()
clientMock.On("Create", ctx, mock.AnythingOfType("*v1.Event")).Return(nil).Once()
mMock.On("RecordCounterMetric", mock.Anything, mock.Anything).Return().Once()
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20")
result, err := WorkerSliceService.CreateMinimalWorkerSliceConfig(ctx, []string{"cluster-1", "cluster-2"}, requestObj.Namespace, label, "red", "198.23.54.47/16", "/20", nil)
require.Error(t, err)
require.Equal(t, len(result), 2)
require.Equal(t, err, err1)
Expand Down

0 comments on commit a7a4691

Please sign in to comment.