diff --git a/pkg/cloud/azure/services/virtualnetworks/service.go b/pkg/cloud/azure/services/virtualnetworks/service.go deleted file mode 100644 index b0532e779..000000000 --- a/pkg/cloud/azure/services/virtualnetworks/service.go +++ /dev/null @@ -1,50 +0,0 @@ -/* -Copyright 2019 The Kubernetes 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 virtualnetworks - -import ( - "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network" - "github.com/Azure/go-autorest/autorest" - "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure" - "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/actuators" -) - -// Service provides operations on resource groups -type Service struct { - Client network.VirtualNetworksClient - Scope *actuators.MachineScope -} - -// getVirtualNetworksClient creates a new groups client from subscriptionid. -func getVirtualNetworksClient(resourceManagerEndpoint, subscriptionID string, authorizer autorest.Authorizer) network.VirtualNetworksClient { - vnetsClient := network.NewVirtualNetworksClientWithBaseURI(resourceManagerEndpoint, subscriptionID) - vnetsClient.Authorizer = authorizer - vnetsClient.AddToUserAgent(azure.UserAgent) - return vnetsClient -} - -// NewService creates a new groups service. -func NewService(scope *actuators.MachineScope) azure.Service { - if scope.IsStackHub() { - return NewStackHubService(scope) - } - - return &Service{ - Client: getVirtualNetworksClient(scope.ResourceManagerEndpoint, scope.SubscriptionID, scope.Authorizer), - Scope: scope, - } -} diff --git a/pkg/cloud/azure/services/virtualnetworks/service_stack.go b/pkg/cloud/azure/services/virtualnetworks/service_stack.go deleted file mode 100644 index 431f2e72c..000000000 --- a/pkg/cloud/azure/services/virtualnetworks/service_stack.go +++ /dev/null @@ -1,46 +0,0 @@ -/* -Copyright 2019 The Kubernetes 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 virtualnetworks - -import ( - "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/network/mgmt/network" - "github.com/Azure/go-autorest/autorest" - "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure" - "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure/actuators" -) - -// StackHubService provides operations on resource groups -type StackHubService struct { - Client network.VirtualNetworksClient - Scope *actuators.MachineScope -} - -// getVirtualNetworksClientStackHub creates a new groups client from subscriptionid. -func getVirtualNetworksClientStackHub(resourceManagerEndpoint, subscriptionID string, authorizer autorest.Authorizer) network.VirtualNetworksClient { - vnetsClient := network.NewVirtualNetworksClientWithBaseURI(resourceManagerEndpoint, subscriptionID) - vnetsClient.Authorizer = authorizer - vnetsClient.AddToUserAgent(azure.UserAgent) - return vnetsClient -} - -// NewService creates a new groups service. -func NewStackHubService(scope *actuators.MachineScope) azure.Service { - return &StackHubService{ - Client: getVirtualNetworksClientStackHub(scope.ResourceManagerEndpoint, scope.SubscriptionID, scope.Authorizer), - Scope: scope, - } -} diff --git a/pkg/cloud/azure/services/virtualnetworks/virtualnetworks.go b/pkg/cloud/azure/services/virtualnetworks/virtualnetworks.go deleted file mode 100644 index 02f05f825..000000000 --- a/pkg/cloud/azure/services/virtualnetworks/virtualnetworks.go +++ /dev/null @@ -1,123 +0,0 @@ -/* -Copyright 2019 The Kubernetes 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 virtualnetworks - -import ( - "context" - "errors" - "fmt" - - "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network" - "github.com/Azure/go-autorest/autorest/to" - "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure" - "k8s.io/klog/v2" -) - -// Spec input specification for Get/CreateOrUpdate/Delete calls -type Spec struct { - Name string - CIDR string -} - -// Get provides information about a virtual network. -func (s *Service) Get(ctx context.Context, spec azure.Spec) (interface{}, error) { - vnetSpec, ok := spec.(*Spec) - if !ok { - return network.VirtualNetwork{}, errors.New("Invalid VNET Specification") - } - vnet, err := s.Client.Get(ctx, s.Scope.MachineConfig.NetworkResourceGroup, vnetSpec.Name, "") - if err != nil && azure.ResourceNotFound(err) { - return nil, fmt.Errorf("vnet %s not found: %w", vnetSpec.Name, err) - } else if err != nil { - return vnet, err - } - return vnet, nil -} - -// CreateOrUpdate creates or updates a virtual network. -func (s *Service) CreateOrUpdate(ctx context.Context, spec azure.Spec) error { - // Following should be created upstream and provided as an input to NewService - // A vnet has following dependencies - // * Vnet Cidr - // * Control Plane Subnet Cidr - // * Node Subnet Cidr - // * Control Plane NSG - // * Node NSG - // * Node Routetable - vnetSpec, ok := spec.(*Spec) - if !ok { - return errors.New("Invalid VNET Specification") - } - - if _, err := s.Get(ctx, vnetSpec); err == nil { - // vnet already exists, cannot update since its immutable - return nil - } - - klog.V(2).Infof("creating vnet %s ", vnetSpec.Name) - f, err := s.Client.CreateOrUpdate(ctx, s.Scope.MachineConfig.NetworkResourceGroup, vnetSpec.Name, - network.VirtualNetwork{ - Location: to.StringPtr(s.Scope.MachineConfig.Location), - VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{ - AddressSpace: &network.AddressSpace{ - AddressPrefixes: &[]string{vnetSpec.CIDR}, - }, - }, - Tags: s.Scope.Tags, - }) - if err != nil { - return err - } - err = f.WaitForCompletionRef(ctx, s.Client.Client) - if err != nil { - return err - } - - _, err = f.Result(s.Client) - if err != nil { - return err - } - klog.V(2).Infof("successfully created vnet %s ", vnetSpec.Name) - return err -} - -// Delete deletes the virtual network with the provided name. -func (s *Service) Delete(ctx context.Context, spec azure.Spec) error { - vnetSpec, ok := spec.(*Spec) - if !ok { - return errors.New("Invalid VNET Specification") - } - klog.V(2).Infof("deleting vnet %s ", vnetSpec.Name) - future, err := s.Client.Delete(ctx, s.Scope.MachineConfig.NetworkResourceGroup, vnetSpec.Name) - if err != nil && azure.ResourceNotFound(err) { - // already deleted - return nil - } - if err != nil { - return fmt.Errorf("failed to delete vnet %s in resource group %s: %w", vnetSpec.Name, s.Scope.MachineConfig.NetworkResourceGroup, err) - } - - err = future.WaitForCompletionRef(ctx, s.Client.Client) - if err != nil { - return fmt.Errorf("cannot delete, future response: %w", err) - } - - _, err = future.Result(s.Client) - - klog.V(2).Infof("successfully deleted vnet %s ", vnetSpec.Name) - return err -} diff --git a/pkg/cloud/azure/services/virtualnetworks/virtualnetworks_stack.go b/pkg/cloud/azure/services/virtualnetworks/virtualnetworks_stack.go deleted file mode 100644 index f3de2684e..000000000 --- a/pkg/cloud/azure/services/virtualnetworks/virtualnetworks_stack.go +++ /dev/null @@ -1,117 +0,0 @@ -/* -Copyright 2019 The Kubernetes 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 virtualnetworks - -import ( - "context" - "errors" - "fmt" - - "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/network/mgmt/network" - "github.com/Azure/go-autorest/autorest/to" - "github.com/openshift/machine-api-provider-azure/pkg/cloud/azure" - "k8s.io/klog/v2" -) - -// Get provides information about a virtual network. -func (s *StackHubService) Get(ctx context.Context, spec azure.Spec) (interface{}, error) { - vnetSpec, ok := spec.(*Spec) - if !ok { - return network.VirtualNetwork{}, errors.New("Invalid VNET Specification") - } - vnet, err := s.Client.Get(ctx, s.Scope.MachineConfig.NetworkResourceGroup, vnetSpec.Name, "") - if err != nil && azure.ResourceNotFound(err) { - return nil, fmt.Errorf("vnet %s not found: %w", vnetSpec.Name, err) - } else if err != nil { - return vnet, err - } - return vnet, nil -} - -// CreateOrUpdate creates or updates a virtual network. -func (s *StackHubService) CreateOrUpdate(ctx context.Context, spec azure.Spec) error { - // Following should be created upstream and provided as an input to NewService - // A vnet has following dependencies - // * Vnet Cidr - // * Control Plane Subnet Cidr - // * Node Subnet Cidr - // * Control Plane NSG - // * Node NSG - // * Node Routetable - vnetSpec, ok := spec.(*Spec) - if !ok { - return errors.New("Invalid VNET Specification") - } - - if _, err := s.Get(ctx, vnetSpec); err == nil { - // vnet already exists, cannot update since its immutable - return nil - } - - klog.V(2).Infof("creating vnet %s ", vnetSpec.Name) - f, err := s.Client.CreateOrUpdate(ctx, s.Scope.MachineConfig.NetworkResourceGroup, vnetSpec.Name, - network.VirtualNetwork{ - Location: to.StringPtr(s.Scope.MachineConfig.Location), - VirtualNetworkPropertiesFormat: &network.VirtualNetworkPropertiesFormat{ - AddressSpace: &network.AddressSpace{ - AddressPrefixes: &[]string{vnetSpec.CIDR}, - }, - }, - }) - if err != nil { - return err - } - - err = f.WaitForCompletionRef(ctx, s.Client.Client) - if err != nil { - return err - } - - _, err = f.Result(s.Client) - if err != nil { - return err - } - klog.V(2).Infof("successfully created vnet %s ", vnetSpec.Name) - return err -} - -// Delete deletes the virtual network with the provided name. -func (s *StackHubService) Delete(ctx context.Context, spec azure.Spec) error { - vnetSpec, ok := spec.(*Spec) - if !ok { - return errors.New("Invalid VNET Specification") - } - klog.V(2).Infof("deleting vnet %s ", vnetSpec.Name) - future, err := s.Client.Delete(ctx, s.Scope.MachineConfig.NetworkResourceGroup, vnetSpec.Name) - if err != nil && azure.ResourceNotFound(err) { - // already deleted - return nil - } - if err != nil { - return fmt.Errorf("failed to delete vnet %s in resource group %s: %w", vnetSpec.Name, s.Scope.MachineConfig.NetworkResourceGroup, err) - } - - err = future.WaitForCompletionRef(ctx, s.Client.Client) - if err != nil { - return fmt.Errorf("cannot delete, future response: %w", err) - } - - _, err = future.Result(s.Client) - - klog.V(2).Infof("successfully deleted vnet %s ", vnetSpec.Name) - return err -}