Skip to content

Commit

Permalink
Update Service interface to use Reconcile/Delete methods (#180)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <[email protected]>
  • Loading branch information
justaugustus authored and k8s-ci-robot committed Apr 11, 2019
1 parent f347984 commit ce3edcd
Show file tree
Hide file tree
Showing 35 changed files with 336 additions and 1,586 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,17 @@ gazelle: ## Run Bazel Gazelle
generate: ## Generate mocks, CRDs and runs `go generate` through Bazel
GOPATH=$(shell go env GOPATH) bazel run //:generate $(BAZEL_ARGS)
$(MAKE) dep-ensure
$(MAKE) generate-deepcopy
# bazel build $(BAZEL_ARGS) //pkg/cloud/azure/services/mocks:go_mock_interfaces \
# //pkg/cloud/azure/services/ec2/mock_ec2iface:go_default_library \
# //pkg/cloud/azure/services/elb/mock_elbiface:go_default_library
# cp -Rf bazel-genfiles/pkg/* pkg/
$(MAKE) generate-crds

.PHONY: generate-deepcopy
generate-deepcopy:
cd pkg/apis && go run ../../vendor/k8s.io/code-generator/cmd/deepcopy-gen/main.go -O zz_generated.deepcopy -i ./... -h ../../hack/boilerplate/boilerplate.go.txt

.PHONY: generate-crds
generate-crds:
bazel build //config
Expand Down Expand Up @@ -248,12 +253,11 @@ create-cluster: binaries-dev ## Create a development Kubernetes cluster on Azure
-a ./cmd/clusterctl/examples/azure/out/addons.yaml

.PHONY: delete-cluster
delete-cluster: binaries-dev ## Deletes the development Kubernetes Cluster (CLUSTER_NAME required)
delete-cluster: binaries-dev ## Deletes the development Kubernetes Cluster
clusterctl delete cluster -v 4 \
--bootstrap-type kind \
--cluster $(CLUSTER_NAME) \
--kubeconfig ./kubeconfig \
-p ./cmd/clusterctl/examples/azure/out/provider-components.yaml \
-p ./cmd/clusterctl/examples/azure/out/provider-components.yaml

kind-reset: ## Destroys the "clusterapi" kind cluster.
kind delete cluster --name=clusterapi || true
Expand Down
8 changes: 8 additions & 0 deletions pkg/cloud/azure/actuators/cluster/actuator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,35 @@ func TestServicesCreatedCount(t *testing.T) {
if cache[azure.GenerateVnetName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GenerateVnetName(fakeReconciler.scope.Cluster.Name))
}

if cache[azure.GenerateControlPlaneSecurityGroupName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GenerateControlPlaneSecurityGroupName(fakeReconciler.scope.Cluster.Name))
}

if cache[azure.GenerateNodeSecurityGroupName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GenerateNodeSecurityGroupName(fakeReconciler.scope.Cluster.Name))
}

if cache[azure.GenerateNodeRouteTableName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GenerateNodeRouteTableName(fakeReconciler.scope.Cluster.Name))
}

if cache[azure.GenerateControlPlaneSubnetName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GenerateControlPlaneSubnetName(fakeReconciler.scope.Cluster.Name))
}

if cache[azure.GenerateNodeSubnetName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GenerateNodeSubnetName(fakeReconciler.scope.Cluster.Name))
}

if cache[azure.GenerateInternalLBName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GenerateInternalLBName(fakeReconciler.scope.Cluster.Name))
}

if cache[azure.GeneratePublicLBName(fakeReconciler.scope.Cluster.Name)] != 1 {
t.Errorf("Expected 1 count of %s service", azure.GeneratePublicLBName(fakeReconciler.scope.Cluster.Name))
}

if cache[fakeReconciler.scope.Network().APIServerIP.Name] != 1 {
t.Errorf("Expected 1 count of %s service", fakeReconciler.scope.Network().APIServerIP.Name)
}
Expand Down
166 changes: 86 additions & 80 deletions pkg/cloud/azure/actuators/cluster/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cluster

import (
"github.com/pkg/errors"
"k8s.io/klog"
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure"
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/certificates"
Expand Down Expand Up @@ -62,206 +63,211 @@ func NewReconciler(scope *actuators.Scope) *Reconciler {
}

// Reconcile reconciles all the services in pre determined order
func (s *Reconciler) Reconcile() error {
actuators.CreateOrUpdateNetworkAPIServerIP(s.scope)
func (r *Reconciler) Reconcile() error {
klog.V(2).Infof("reconciling cluster %s", r.scope.Cluster.Name)
actuators.CreateOrUpdateNetworkAPIServerIP(r.scope)

// Store cert material in spec.
if err := s.certificatesSvc.CreateOrUpdate(s.scope.Context, nil); err != nil {
return errors.Wrapf(err, "failed to createorupdate certificates for cluster %s", s.scope.Cluster.Name)
if err := r.certificatesSvc.Reconcile(r.scope.Context, nil); err != nil {
return errors.Wrapf(err, "failed to reconcile certificates for cluster %s", r.scope.Cluster.Name)
}

if err := s.groupsSvc.CreateOrUpdate(s.scope.Context, nil); err != nil {
return errors.Wrapf(err, "failed to createorupdate resource group for cluster %s", s.scope.Cluster.Name)
if err := r.groupsSvc.Reconcile(r.scope.Context, nil); err != nil {
return errors.Wrapf(err, "failed to reconcile resource group for cluster %s", r.scope.Cluster.Name)
}

vnetSpec := &virtualnetworks.Spec{
Name: azure.GenerateVnetName(s.scope.Cluster.Name),
Name: azure.GenerateVnetName(r.scope.Cluster.Name),
CIDR: azure.DefaultVnetCIDR,
}
if err := s.vnetSvc.CreateOrUpdate(s.scope.Context, vnetSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate virtual network for cluster %s", s.scope.Cluster.Name)
if err := r.vnetSvc.Reconcile(r.scope.Context, vnetSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile virtual network for cluster %s", r.scope.Cluster.Name)
}

sgSpec := &securitygroups.Spec{
Name: azure.GenerateControlPlaneSecurityGroupName(s.scope.Cluster.Name),
Name: azure.GenerateControlPlaneSecurityGroupName(r.scope.Cluster.Name),
IsControlPlane: true,
}
if err := s.securityGroupSvc.CreateOrUpdate(s.scope.Context, sgSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate control plane network security group for cluster %s", s.scope.Cluster.Name)
if err := r.securityGroupSvc.Reconcile(r.scope.Context, sgSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile control plane network security group for cluster %s", r.scope.Cluster.Name)
}

sgSpec = &securitygroups.Spec{
Name: azure.GenerateNodeSecurityGroupName(s.scope.Cluster.Name),
Name: azure.GenerateNodeSecurityGroupName(r.scope.Cluster.Name),
IsControlPlane: false,
}
if err := s.securityGroupSvc.CreateOrUpdate(s.scope.Context, sgSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate node network security group for cluster %s", s.scope.Cluster.Name)
if err := r.securityGroupSvc.Reconcile(r.scope.Context, sgSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile node network security group for cluster %s", r.scope.Cluster.Name)
}

rtSpec := &routetables.Spec{
Name: azure.GenerateNodeRouteTableName(s.scope.Cluster.Name),
Name: azure.GenerateNodeRouteTableName(r.scope.Cluster.Name),
}
if err := s.routeTableSvc.CreateOrUpdate(s.scope.Context, rtSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate node route table for cluster %s", s.scope.Cluster.Name)
if err := r.routeTableSvc.Reconcile(r.scope.Context, rtSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile node route table for cluster %s", r.scope.Cluster.Name)
}

subnetSpec := &subnets.Spec{
Name: azure.GenerateControlPlaneSubnetName(s.scope.Cluster.Name),
Name: azure.GenerateControlPlaneSubnetName(r.scope.Cluster.Name),
CIDR: azure.DefaultControlPlaneSubnetCIDR,
VnetName: azure.GenerateVnetName(s.scope.Cluster.Name),
SecurityGroupName: azure.GenerateControlPlaneSecurityGroupName(s.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(r.scope.Cluster.Name),
SecurityGroupName: azure.GenerateControlPlaneSecurityGroupName(r.scope.Cluster.Name),
}
if err := s.subnetsSvc.CreateOrUpdate(s.scope.Context, subnetSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate control plane subnet for cluster %s", s.scope.Cluster.Name)
if err := r.subnetsSvc.Reconcile(r.scope.Context, subnetSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile control plane subnet for cluster %s", r.scope.Cluster.Name)
}

subnetSpec = &subnets.Spec{
Name: azure.GenerateNodeSubnetName(s.scope.Cluster.Name),
Name: azure.GenerateNodeSubnetName(r.scope.Cluster.Name),
CIDR: azure.DefaultNodeSubnetCIDR,
VnetName: azure.GenerateVnetName(s.scope.Cluster.Name),
SecurityGroupName: azure.GenerateNodeSecurityGroupName(s.scope.Cluster.Name),
RouteTableName: azure.GenerateNodeRouteTableName(s.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(r.scope.Cluster.Name),
SecurityGroupName: azure.GenerateNodeSecurityGroupName(r.scope.Cluster.Name),
RouteTableName: azure.GenerateNodeRouteTableName(r.scope.Cluster.Name),
}
if err := s.subnetsSvc.CreateOrUpdate(s.scope.Context, subnetSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate node subnet for cluster %s", s.scope.Cluster.Name)
if err := r.subnetsSvc.Reconcile(r.scope.Context, subnetSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile node subnet for cluster %s", r.scope.Cluster.Name)
}

internalLBSpec := &internalloadbalancers.Spec{
Name: azure.GenerateInternalLBName(s.scope.Cluster.Name),
SubnetName: azure.GenerateControlPlaneSubnetName(s.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(s.scope.Cluster.Name),
Name: azure.GenerateInternalLBName(r.scope.Cluster.Name),
SubnetName: azure.GenerateControlPlaneSubnetName(r.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(r.scope.Cluster.Name),
IPAddress: azure.DefaultInternalLBIPAddress,
}
if err := s.internalLBSvc.CreateOrUpdate(s.scope.Context, internalLBSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate control plane internal load balancer for cluster %s", s.scope.Cluster.Name)
if err := r.internalLBSvc.Reconcile(r.scope.Context, internalLBSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile control plane internal load balancer for cluster %s", r.scope.Cluster.Name)
}

publicIPSpec := &publicips.Spec{
Name: s.scope.Network().APIServerIP.Name,
Name: r.scope.Network().APIServerIP.Name,
}
if err := s.publicIPSvc.CreateOrUpdate(s.scope.Context, publicIPSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate control plane public ip for cluster %s", s.scope.Cluster.Name)
if err := r.publicIPSvc.Reconcile(r.scope.Context, publicIPSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile control plane public ip for cluster %s", r.scope.Cluster.Name)
}

publicLBSpec := &publicloadbalancers.Spec{
Name: azure.GeneratePublicLBName(s.scope.Cluster.Name),
PublicIPName: s.scope.Network().APIServerIP.Name,
Name: azure.GeneratePublicLBName(r.scope.Cluster.Name),
PublicIPName: r.scope.Network().APIServerIP.Name,
}
if err := s.publicLBSvc.CreateOrUpdate(s.scope.Context, publicLBSpec); err != nil {
return errors.Wrapf(err, "failed to createorupdate control plane public load balancer for cluster %s", s.scope.Cluster.Name)
if err := r.publicLBSvc.Reconcile(r.scope.Context, publicLBSpec); err != nil {
return errors.Wrapf(err, "failed to reconcile control plane public load balancer for cluster %s", r.scope.Cluster.Name)
}

klog.V(2).Infof("successfully reconciled cluster %s", r.scope.Cluster.Name)
return nil
}

// Delete reconciles all the services in pre determined order
func (s *Reconciler) Delete() error {
if err := s.deleteLB(); err != nil {
func (r *Reconciler) Delete() error {
if err := r.deleteLB(); err != nil {
return errors.Wrap(err, "failed to delete load balancer")
}

if err := s.deleteSubnets(); err != nil {
if err := r.deleteSubnets(); err != nil {
return errors.Wrap(err, "failed to delete subnets")
}

rtSpec := &routetables.Spec{
Name: azure.GenerateNodeRouteTableName(s.scope.Cluster.Name),
Name: azure.GenerateNodeRouteTableName(r.scope.Cluster.Name),
}
if err := s.routeTableSvc.Delete(s.scope.Context, rtSpec); err != nil {
if err := r.routeTableSvc.Delete(r.scope.Context, rtSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete route table %s for cluster %s", azure.GenerateNodeRouteTableName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete route table %s for cluster %s", azure.GenerateNodeRouteTableName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}

if err := s.deleteNSG(); err != nil {
if err := r.deleteNSG(); err != nil {
return errors.Wrap(err, "failed to delete network security group")
}

vnetSpec := &virtualnetworks.Spec{
Name: azure.GenerateVnetName(s.scope.Cluster.Name),
Name: azure.GenerateVnetName(r.scope.Cluster.Name),
}
if err := s.vnetSvc.Delete(s.scope.Context, vnetSpec); err != nil {
if err := r.vnetSvc.Delete(r.scope.Context, vnetSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete virtual network %s for cluster %s", azure.GenerateVnetName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete virtual network %s for cluster %s", azure.GenerateVnetName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}

if err := s.groupsSvc.Delete(s.scope.Context, nil); err != nil {
if err := r.groupsSvc.Delete(r.scope.Context, nil); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete resource group for cluster %s", s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete resource group for cluster %s", r.scope.Cluster.Name)
}
}

return nil
}

func (s *Reconciler) deleteLB() error {
func (r *Reconciler) deleteLB() error {
publicLBSpec := &publicloadbalancers.Spec{
Name: azure.GeneratePublicLBName(s.scope.Cluster.Name),
Name: azure.GeneratePublicLBName(r.scope.Cluster.Name),
}
if err := s.publicLBSvc.Delete(s.scope.Context, publicLBSpec); err != nil {
if err := r.publicLBSvc.Delete(r.scope.Context, publicLBSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete lb %s for cluster %s", azure.GeneratePublicLBName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete lb %s for cluster %s", azure.GeneratePublicLBName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}
publicIPSpec := &publicips.Spec{
Name: s.scope.Network().APIServerIP.Name,
Name: r.scope.Network().APIServerIP.Name,
}
if err := s.publicIPSvc.Delete(s.scope.Context, publicIPSpec); err != nil {
if err := r.publicIPSvc.Delete(r.scope.Context, publicIPSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete public ip %s for cluster %s", s.scope.Network().APIServerIP.Name, s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete public ip %s for cluster %s", r.scope.Network().APIServerIP.Name, r.scope.Cluster.Name)
}
}

internalLBSpec := &internalloadbalancers.Spec{
Name: azure.GenerateInternalLBName(s.scope.Cluster.Name),
Name: azure.GenerateInternalLBName(r.scope.Cluster.Name),
}
if err := s.internalLBSvc.Delete(s.scope.Context, internalLBSpec); err != nil {
if err := r.internalLBSvc.Delete(r.scope.Context, internalLBSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to internal load balancer %s for cluster %s", azure.GenerateInternalLBName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to internal load balancer %s for cluster %s", azure.GenerateInternalLBName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}

return nil
}

func (s *Reconciler) deleteSubnets() error {
func (r *Reconciler) deleteSubnets() error {
subnetSpec := &subnets.Spec{
Name: azure.GenerateNodeSubnetName(s.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(s.scope.Cluster.Name),
Name: azure.GenerateNodeSubnetName(r.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(r.scope.Cluster.Name),
}
if err := s.subnetsSvc.Delete(s.scope.Context, subnetSpec); err != nil {
if err := r.subnetsSvc.Delete(r.scope.Context, subnetSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete %s subnet for cluster %s", azure.GenerateNodeSubnetName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete %s subnet for cluster %s", azure.GenerateNodeSubnetName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}

subnetSpec = &subnets.Spec{
Name: azure.GenerateControlPlaneSubnetName(s.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(s.scope.Cluster.Name),
Name: azure.GenerateControlPlaneSubnetName(r.scope.Cluster.Name),
VnetName: azure.GenerateVnetName(r.scope.Cluster.Name),
}
if err := s.subnetsSvc.Delete(s.scope.Context, subnetSpec); err != nil {
if err := r.subnetsSvc.Delete(r.scope.Context, subnetSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete %s subnet for cluster %s", azure.GenerateControlPlaneSubnetName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete %s subnet for cluster %s", azure.GenerateControlPlaneSubnetName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}

return nil
}

func (s *Reconciler) deleteNSG() error {
func (r *Reconciler) deleteNSG() error {
sgSpec := &securitygroups.Spec{
Name: azure.GenerateNodeSecurityGroupName(s.scope.Cluster.Name),
Name: azure.GenerateNodeSecurityGroupName(r.scope.Cluster.Name),
}
if err := s.securityGroupSvc.Delete(s.scope.Context, sgSpec); err != nil {
if err := r.securityGroupSvc.Delete(r.scope.Context, sgSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete security group %s for cluster %s", azure.GenerateNodeSecurityGroupName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete security group %s for cluster %s", azure.GenerateNodeSecurityGroupName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}
sgSpec = &securitygroups.Spec{
Name: azure.GenerateControlPlaneSecurityGroupName(s.scope.Cluster.Name),
Name: azure.GenerateControlPlaneSecurityGroupName(r.scope.Cluster.Name),
}
if err := s.securityGroupSvc.Delete(s.scope.Context, sgSpec); err != nil {
if err := r.securityGroupSvc.Delete(r.scope.Context, sgSpec); err != nil {
if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to delete security group %s for cluster %s", azure.GenerateControlPlaneSecurityGroupName(s.scope.Cluster.Name), s.scope.Cluster.Name)
return errors.Wrapf(err, "failed to delete security group %s for cluster %s", azure.GenerateControlPlaneSecurityGroupName(r.scope.Cluster.Name), r.scope.Cluster.Name)
}
}

return nil
}
Loading

0 comments on commit ce3edcd

Please sign in to comment.