This commit is contained in:
Mangirdas Judeikis 2021-08-17 10:15:03 +01:00
Родитель 4d34802e2b
Коммит 6aeae2b2e0
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: AA071F630E926BBD
10 изменённых файлов: 72 добавлений и 35 удалений

Просмотреть файл

@ -146,7 +146,7 @@ func New(ctx context.Context, log *logrus.Entry, _env env.Interface, db database
dns: dns.NewManager(_env, localFPAuthorizer),
storage: storage,
subnet: subnet.NewManager(nil, _env.Environment(), r.SubscriptionID, fpAuthorizer),
subnet: subnet.NewManager(_env.Environment(), r.SubscriptionID, fpAuthorizer),
graph: graph.NewManager(log, aead, storage),
}, nil
}

Просмотреть файл

@ -16,7 +16,7 @@ import (
func (r *reconcileManager) reconcileSubnets(ctx context.Context) error {
// the main logic starts here
subnets, err := r.subnets.ListFromCluster(ctx)
subnets, err := r.kSubnets.ListFromCluster(ctx)
if err != nil {
return err
}

Просмотреть файл

@ -93,7 +93,7 @@ func TestReconcileManager(t *testing.T) {
for _, tt := range []struct {
name string
subnetMock func(*mock_subnet.MockManager)
subnetMock func(*mock_subnet.MockManager, *mock_subnet.MockKubeManager)
maocli func() (*maofake.Clientset, error)
instance func(*arov1alpha1.Cluster)
wantErr error
@ -113,11 +113,11 @@ func TestReconcileManager(t *testing.T) {
return maofake.NewSimpleClientset(machine1, machine2), nil
},
subnetMock: func(mock *mock_subnet.MockManager) {
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {
resourceIdMaster := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameMaster
resourceIdWorker := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameWorker
mock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
kmock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
{
ResourceID: resourceIdMaster,
IsMaster: true,
@ -151,12 +151,12 @@ func TestReconcileManager(t *testing.T) {
return maofake.NewSimpleClientset(machine1, machine2), nil
},
subnetMock: func(mock *mock_subnet.MockManager) {
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {
resourceIdMaster := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameMaster
resourceIdWorker := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameWorker
mock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
kmock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
{
ResourceID: resourceIdMaster,
IsMaster: true,
@ -199,12 +199,12 @@ func TestReconcileManager(t *testing.T) {
return maofake.NewSimpleClientset(machine1, machine2), nil
},
subnetMock: func(mock *mock_subnet.MockManager) {
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {
resourceIdMaster := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameMaster
resourceIdWorker := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameWorker
mock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
kmock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
{
ResourceID: resourceIdMaster,
IsMaster: true,
@ -242,12 +242,12 @@ func TestReconcileManager(t *testing.T) {
return maofake.NewSimpleClientset(machine1, machine2), nil
},
subnetMock: func(mock *mock_subnet.MockManager) {
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {
resourceIdMaster := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameMaster
resourceIdWorker := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameWorker
mock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
kmock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
{
ResourceID: resourceIdMaster,
IsMaster: true,
@ -285,12 +285,12 @@ func TestReconcileManager(t *testing.T) {
return maofake.NewSimpleClientset(machine1, machine2), nil
},
subnetMock: func(mock *mock_subnet.MockManager) {
subnetMock: func(mock *mock_subnet.MockManager, kmock *mock_subnet.MockKubeManager) {
resourceIdMaster := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameMaster
resourceIdWorker := "/subscriptions/" + subscriptionId + "/resourceGroups/" + vnetResourceGroup + "/providers/Microsoft.Network/virtualNetworks/" + vnetName + "/subnets/" + subnetNameWorker
mock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
kmock.EXPECT().ListFromCluster(gomock.Any()).Return([]subnet.Subnet{
{
ResourceID: resourceIdMaster,
IsMaster: true,
@ -327,8 +327,9 @@ func TestReconcileManager(t *testing.T) {
defer controller.Finish()
subnets := mock_subnet.NewMockManager(controller)
kSubnets := mock_subnet.NewMockKubeManager(controller)
if tt.subnetMock != nil {
tt.subnetMock(subnets)
tt.subnetMock(subnets, kSubnets)
}
instance := getValidClusterInstance()
@ -341,6 +342,7 @@ func TestReconcileManager(t *testing.T) {
instance: instance,
subscriptionID: subscriptionId,
subnets: subnets,
kSubnets: kSubnets,
}
err := r.reconcileSubnets(context.Background())

Просмотреть файл

@ -46,7 +46,8 @@ type reconcileManager struct {
instance *arov1alpha1.Cluster
subscriptionID string
subnets subnet.Manager
subnets subnet.Manager
kSubnets subnet.KubeManager
}
// NewReconciler creates a new Reconciler
@ -100,7 +101,8 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
log: r.log,
instance: instance,
subscriptionID: resource.SubscriptionID,
subnets: subnet.NewManager(r.maocli, &azEnv, resource.SubscriptionID, authorizer),
kSubnets: subnet.NewKubeManager(r.maocli, resource.SubscriptionID),
subnets: subnet.NewManager(&azEnv, resource.SubscriptionID, authorizer),
}
return reconcile.Result{}, manager.reconcileSubnets(ctx)

Просмотреть файл

@ -1,5 +1,5 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/Azure/ARO-RP/pkg/util/subnet (interfaces: Manager)
// Source: github.com/Azure/ARO-RP/pkg/util/subnet (interfaces: Manager,KubeManager)
// Package mock_subnet is a generated GoMock package.
package mock_subnet
@ -81,8 +81,31 @@ func (mr *MockManagerMockRecorder) GetHighestFreeIP(arg0, arg1 interface{}) *gom
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetHighestFreeIP", reflect.TypeOf((*MockManager)(nil).GetHighestFreeIP), arg0, arg1)
}
// MockKubeManager is a mock of KubeManager interface.
type MockKubeManager struct {
ctrl *gomock.Controller
recorder *MockKubeManagerMockRecorder
}
// MockKubeManagerMockRecorder is the mock recorder for MockKubeManager.
type MockKubeManagerMockRecorder struct {
mock *MockKubeManager
}
// NewMockKubeManager creates a new mock instance.
func NewMockKubeManager(ctrl *gomock.Controller) *MockKubeManager {
mock := &MockKubeManager{ctrl: ctrl}
mock.recorder = &MockKubeManagerMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockKubeManager) EXPECT() *MockKubeManagerMockRecorder {
return m.recorder
}
// ListFromCluster mocks base method.
func (m *MockManager) ListFromCluster(arg0 context.Context) ([]subnet.Subnet, error) {
func (m *MockKubeManager) ListFromCluster(arg0 context.Context) ([]subnet.Subnet, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ListFromCluster", arg0)
ret0, _ := ret[0].([]subnet.Subnet)
@ -91,7 +114,7 @@ func (m *MockManager) ListFromCluster(arg0 context.Context) ([]subnet.Subnet, er
}
// ListFromCluster indicates an expected call of ListFromCluster.
func (mr *MockManagerMockRecorder) ListFromCluster(arg0 interface{}) *gomock.Call {
func (mr *MockKubeManagerMockRecorder) ListFromCluster(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListFromCluster", reflect.TypeOf((*MockManager)(nil).ListFromCluster), arg0)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListFromCluster", reflect.TypeOf((*MockKubeManager)(nil).ListFromCluster), arg0)
}

Просмотреть файл

@ -49,7 +49,7 @@ func NewResourceCleaner(log *logrus.Entry, env env.Core, shouldDelete checkFn, d
privatelinkservicescli: network.NewPrivateLinkServicesClient(env.Environment(), env.SubscriptionID(), authorizer),
securitygroupscli: network.NewSecurityGroupsClient(env.Environment(), env.SubscriptionID(), authorizer),
subnet: subnet.NewManager(nil, env.Environment(), env.SubscriptionID(), authorizer),
subnet: subnet.NewManager(env.Environment(), env.SubscriptionID(), authorizer),
// ShouldDelete decides whether the resource group gets deleted
shouldDelete: shouldDelete,

Просмотреть файл

@ -9,15 +9,34 @@ import (
"fmt"
"github.com/Azure/go-autorest/autorest/azure"
maoclient "github.com/openshift/machine-api-operator/pkg/generated/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
azureproviderv1beta1 "sigs.k8s.io/cluster-api-provider-azure/pkg/apis/azureprovider/v1beta1"
)
// KubeManager interface interact with kubernetes layer to extract required information
type KubeManager interface {
ListFromCluster(ctx context.Context) ([]Subnet, error)
}
type kubeManager struct {
maocli maoclient.Interface
subscriptionID string
}
func NewKubeManager(maocli maoclient.Interface, subscriptionID string) KubeManager {
return &kubeManager{
maocli: maocli,
subscriptionID: subscriptionID,
}
}
// ListFromCluster reconstructs subnetId used in machines object in the cluster
// In cases we interat with customer vnets, we don't know which subnets are used in ARO.
// Example : /subscriptions/{subscriptionID}/resourceGroups/{vnet-resource-group}/providers/Microsoft.Network/virtualNetworks/{vnet-name}/subnets/{subnet-name}
func (m *manager) ListFromCluster(ctx context.Context) ([]Subnet, error) {
func (m *kubeManager) ListFromCluster(ctx context.Context) ([]Subnet, error) {
if m.maocli == nil {
return nil, fmt.Errorf("subnets.Manager was initiated without kubernetes client")
}
@ -53,7 +72,7 @@ func (m *manager) ListFromCluster(ctx context.Context) ([]Subnet, error) {
return unique(subnetMap), nil
}
func (m *manager) getDescriptorFromProviderSpec(providerSpec *runtime.RawExtension) (*Subnet, error) {
func (m *kubeManager) getDescriptorFromProviderSpec(providerSpec *runtime.RawExtension) (*Subnet, error) {
var spec azureproviderv1beta1.AzureMachineProviderSpec
err := json.Unmarshal(providerSpec.Raw, &spec)
if err != nil {

Просмотреть файл

@ -92,7 +92,7 @@ func TestListFromCluster(t *testing.T) {
}
tt.modify(&workerMachine, &masterMachine)
m := manager{
m := kubeManager{
maocli: maofake.NewSimpleClientset(&workerMachine, &masterMachine),
}

Просмотреть файл

@ -4,5 +4,5 @@ package subnet
// Licensed under the Apache License 2.0.
//go:generate rm -rf ../mocks/$GOPACKAGE
//go:generate go run ../../../vendor/github.com/golang/mock/mockgen -destination=../mocks/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/util/$GOPACKAGE Manager
//go:generate go run ../../../vendor/github.com/golang/mock/mockgen -destination=../mocks/$GOPACKAGE/$GOPACKAGE.go github.com/Azure/ARO-RP/pkg/util/$GOPACKAGE Manager,KubeManager
//go:generate go run ../../../vendor/golang.org/x/tools/cmd/goimports -local=github.com/Azure/ARO-RP -e -w ../mocks/$GOPACKAGE/$GOPACKAGE.go

Просмотреть файл

@ -13,7 +13,6 @@ import (
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/apparentlymart/go-cidr/cidr"
maoclient "github.com/openshift/machine-api-operator/pkg/generated/clientset/versioned"
"github.com/Azure/ARO-RP/pkg/api"
"github.com/Azure/ARO-RP/pkg/util/azureclient"
@ -29,26 +28,18 @@ type Manager interface {
Get(ctx context.Context, subnetID string) (*mgmtnetwork.Subnet, error)
GetHighestFreeIP(ctx context.Context, subnetID string) (string, error)
CreateOrUpdate(ctx context.Context, subnetID string, subnet *mgmtnetwork.Subnet) error
// ListFromCluster uses kube clients, instead of azure go get cluster
// subnets
ListFromCluster(ctx context.Context) ([]Subnet, error)
}
type manager struct {
subnets network.SubnetsClient
virtualNetworks network.VirtualNetworksClient
maocli maoclient.Interface
subscriptionID string
}
func NewManager(maocli maoclient.Interface, environment *azureclient.AROEnvironment, subscriptionID string, spAuthorizer autorest.Authorizer) Manager {
func NewManager(environment *azureclient.AROEnvironment, subscriptionID string, spAuthorizer autorest.Authorizer) Manager {
return &manager{
subnets: network.NewSubnetsClient(environment, subscriptionID, spAuthorizer),
virtualNetworks: network.NewVirtualNetworksClient(environment, subscriptionID, spAuthorizer),
maocli: maocli,
subscriptionID: subscriptionID,
}
}