Skip to content

Commit 3370274

Browse files
awesomenixk8s-ci-robot
authored andcommitted
fix: Create a new Generic Service interface which provides an individual azure service easy for unittesting (#138)
Create Generic Service interface for all azureclients to be built on top of Move groups to Service architecture, easier to write a Fake Service and simulate results Fix accidental use of an older version of azure resources, use consistent versions across Apply suggestions from code review Co-Authored-By: awesomenix <[email protected]> Address review comments, use the top level context defined at scope level Rename services to a reconciler which hosts groups of services Revert any changes made to network svc
1 parent 4b67c07 commit 3370274

File tree

23 files changed

+487
-191
lines changed

23 files changed

+487
-191
lines changed

Gopkg.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cloud/azure/actuators/clients.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import (
2727

2828
// AzureClients contains all the Azure clients used by the scopes.
2929
type AzureClients struct {
30+
SubscriptionID string
31+
32+
Authorizer autorest.Authorizer
3033
// TODO: Remove legacy clients once interfaces are reimplemented
3134
Compute AzureComputeClient
3235
Network AzureNetworkClient
@@ -44,7 +47,6 @@ type AzureClients struct {
4447
PublicIPAddresses network.PublicIPAddressesClient
4548

4649
// Resources
47-
Groups resources.GroupsClient
4850
Deployments resources.DeploymentsClient
4951
Tags resources.TagsClient
5052
}
@@ -86,12 +88,6 @@ type AzureNetworkClient interface {
8688

8789
// AzureResourcesClient defines the operations that will interact with the Azure Resources API
8890
type AzureResourcesClient interface {
89-
// Resource Groups Operations
90-
CreateOrUpdateGroup(resourceGroupName string, location string) (resources.Group, error)
91-
DeleteGroup(resourceGroupName string) (resources.GroupsDeleteFuture, error)
92-
CheckGroupExistence(rgName string) (autorest.Response, error)
93-
WaitForGroupsDeleteFuture(future resources.GroupsDeleteFuture) error
94-
9591
// Deployment Operations
9692
CreateOrUpdateDeployment(machine *clusterv1.Machine, clusterConfig *providerv1.AzureClusterProviderSpec, machineConfig *providerv1.AzureMachineProviderSpec, startupScript string) (*resources.DeploymentsCreateOrUpdateFuture, error)
9793
GetDeploymentResult(future resources.DeploymentsCreateOrUpdateFuture) (de resources.DeploymentExtended, err error)

pkg/cloud/azure/actuators/cluster/BUILD.bazel

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
5-
srcs = ["actuator.go"],
5+
srcs = [
6+
"actuator.go",
7+
"reconciler.go",
8+
],
69
importpath = "sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators/cluster",
710
visibility = ["//visibility:public"],
811
deps = [
912
"//pkg/cloud/azure/actuators:go_default_library",
13+
"//pkg/cloud/azure/services:go_default_library",
1014
"//pkg/cloud/azure/services/certificates:go_default_library",
15+
"//pkg/cloud/azure/services/groups:go_default_library",
1116
"//pkg/cloud/azure/services/network:go_default_library",
12-
"//pkg/cloud/azure/services/resources:go_default_library",
1317
"//pkg/deployer:go_default_library",
1418
"//vendor/github.com/pkg/errors:go_default_library",
1519
"//vendor/k8s.io/klog:go_default_library",
@@ -23,5 +27,10 @@ go_test(
2327
name = "go_default_test",
2428
srcs = ["actuator_test.go"],
2529
embed = [":go_default_library"],
26-
deps = ["//vendor/sigs.k8s.io/cluster-api/pkg/controller/cluster:go_default_library"],
30+
deps = [
31+
"//pkg/cloud/azure/actuators:go_default_library",
32+
"//pkg/cloud/azure/services:go_default_library",
33+
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
34+
"//vendor/sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1:go_default_library",
35+
],
2736
)

pkg/cloud/azure/actuators/cluster/actuator.go

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import (
2020
"github.com/pkg/errors"
2121
"k8s.io/klog"
2222
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
23-
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/certificates"
2423
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/network"
25-
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/resources"
2624
"sigs.k8s.io/cluster-api-provider-azure/pkg/deployer"
2725
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
2826
client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
@@ -63,32 +61,27 @@ func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
6361

6462
defer scope.Close()
6563

66-
networkSvc := network.NewService(scope)
67-
resourcesSvc := resources.NewService(scope)
68-
certSvc := certificates.NewService(scope)
69-
70-
// Store cert material in spec.
71-
if err := certSvc.ReconcileCertificates(); err != nil {
72-
return errors.Wrapf(err, "failed to reconcile certificates for cluster %q", cluster.Name)
73-
}
74-
75-
if err := resourcesSvc.ReconcileResourceGroup(); err != nil {
76-
return errors.Wrapf(err, "failed to reconcile resource group for cluster %q", cluster.Name)
64+
err = NewReconciler(scope).Reconcile()
65+
if err != nil {
66+
return errors.Wrap(err, "failed to reconcile cluster services")
7767
}
7868

79-
if err := networkSvc.ReconcileNetwork(); err != nil {
80-
return errors.Wrapf(err, "failed to reconcile network for cluster %q", cluster.Name)
69+
networkSvc := network.NewService(scope)
70+
err = networkSvc.ReconcileNetwork()
71+
if err != nil {
72+
return errors.Wrapf(err, "failed to reconcile network for cluster %s", scope.Cluster.Name)
8173
}
8274

8375
// TODO: Add bastion method
8476
/*
8577
if err := resourcesSvc.ReconcileBastion(); err != nil {
86-
return errors.Wrapf(err, "failed to reconcile bastion host for cluster %q", cluster.Name)
78+
return errors.Wrapf(err, "failed to reconcile bastion host for cluster %q", scope.Cluster.Name)
8779
}
8880
*/
8981

90-
if err := networkSvc.ReconcileLoadBalancer("api"); err != nil {
91-
return errors.Wrapf(err, "failed to reconcile load balancers for cluster %q", cluster.Name)
82+
err = networkSvc.ReconcileLoadBalancer("api")
83+
if err != nil {
84+
return errors.Wrapf(err, "failed to reconcile load balancers for cluster %s", scope.Cluster.Name)
9285
}
9386

9487
return nil
@@ -106,7 +99,6 @@ func (a *Actuator) Delete(cluster *clusterv1.Cluster) error {
10699
defer scope.Close()
107100

108101
//networkSvc := network.NewService(scope)
109-
resourcesSvc := resources.NewService(scope)
110102

111103
// TODO: Add load balancer method
112104
/*
@@ -131,8 +123,7 @@ func (a *Actuator) Delete(cluster *clusterv1.Cluster) error {
131123
}
132124
}
133125
*/
134-
135-
if err := resourcesSvc.DeleteResourceGroup(); err != nil {
126+
if err := NewReconciler(scope).Delete(); err != nil {
136127
klog.Errorf("Error deleting resource group: %v.", err)
137128
return &controllerError.RequeueAfterError{
138129
RequeueAfter: 5 * 1000 * 1000 * 1000,

pkg/cloud/azure/actuators/cluster/actuator_test.go

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,80 @@ limitations under the License.
1717
package cluster
1818

1919
import (
20-
"sigs.k8s.io/cluster-api/pkg/controller/cluster"
21-
)
20+
"context"
21+
"testing"
2222

23-
var (
24-
_ cluster.Actuator = (*Actuator)(nil)
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
25+
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services"
26+
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
2527
)
2628

29+
func createFakeScope() *actuators.Scope {
30+
return &actuators.Scope{
31+
Context: context.Background(),
32+
Cluster: &clusterv1.Cluster{
33+
ObjectMeta: metav1.ObjectMeta{
34+
Name: "dummyClusterName",
35+
},
36+
},
37+
}
38+
}
39+
40+
func TestReconcileSuccess(t *testing.T) {
41+
fakeSuccessSvc := &services.FakeSuccessService{}
42+
fakeNotFoundSvc := &services.FakeNotFoundService{}
43+
44+
fakeReconciler := &Reconciler{
45+
scope: createFakeScope(),
46+
groupsSvc: fakeSuccessSvc,
47+
certificatesSvc: fakeSuccessSvc,
48+
}
49+
50+
if err := fakeReconciler.Reconcile(); err != nil {
51+
t.Errorf("failed to reconcile cluster services: %+v", err)
52+
}
53+
54+
if err := fakeReconciler.Delete(); err != nil {
55+
t.Errorf("failed to delete cluster services: %+v", err)
56+
}
57+
58+
fakeReconciler.groupsSvc = fakeNotFoundSvc
59+
60+
if err := fakeReconciler.Delete(); err != nil {
61+
t.Errorf("failed to delete cluster services: %+v", err)
62+
}
63+
}
64+
65+
func TestReconcileFailure(t *testing.T) {
66+
fakeSuccessSvc := &services.FakeSuccessService{}
67+
fakeFailureSvc := &services.FakeFailureService{}
68+
69+
fakeReconciler := &Reconciler{
70+
scope: createFakeScope(),
71+
groupsSvc: fakeSuccessSvc,
72+
certificatesSvc: fakeFailureSvc,
73+
}
74+
75+
if err := fakeReconciler.Reconcile(); err == nil {
76+
t.Errorf("expected reconcile to fail")
77+
}
78+
79+
if err := fakeReconciler.Delete(); err != nil {
80+
t.Errorf("expected delete to succeed, since we delete groupssvc only")
81+
}
82+
83+
fakeReconciler.groupsSvc = fakeFailureSvc
84+
85+
if err := fakeReconciler.Reconcile(); err == nil {
86+
t.Errorf("expected reconcile to fail")
87+
}
88+
89+
if err := fakeReconciler.Delete(); err == nil {
90+
t.Errorf("expected delete to fail")
91+
}
92+
}
93+
2794
// TODO: Reimplement tests
2895
/*
2996
import (
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cluster
18+
19+
import (
20+
"github.com/pkg/errors"
21+
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/actuators"
22+
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services"
23+
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/certificates"
24+
"sigs.k8s.io/cluster-api-provider-azure/pkg/cloud/azure/services/groups"
25+
)
26+
27+
// Reconciler are list of services required by cluster actuator, easy to create a fake
28+
type Reconciler struct {
29+
scope *actuators.Scope
30+
groupsSvc services.Service
31+
certificatesSvc services.Service
32+
}
33+
34+
// NewReconciler populates all the services based on input scope
35+
func NewReconciler(scope *actuators.Scope) *Reconciler {
36+
return &Reconciler{
37+
scope: scope,
38+
groupsSvc: groups.NewService(scope),
39+
certificatesSvc: certificates.NewService(scope),
40+
}
41+
}
42+
43+
// Reconcile reconciles all the services in pre determined order
44+
func (s *Reconciler) Reconcile() error {
45+
// Store cert material in spec.
46+
if err := s.certificatesSvc.CreateOrUpdate(s.scope.Context); err != nil {
47+
return errors.Wrapf(err, "failed to CreateOrUpdate certificates for cluster %s", s.scope.Cluster.Name)
48+
}
49+
50+
if err := s.groupsSvc.CreateOrUpdate(s.scope.Context); err != nil {
51+
return errors.Wrapf(err, "failed to CreateOrUpdate resource group for cluster %s", s.scope.Cluster.Name)
52+
}
53+
return nil
54+
}
55+
56+
// Delete reconciles all the services in pre determined order
57+
func (s *Reconciler) Delete() error {
58+
if err := s.groupsSvc.Delete(s.scope.Context); err != nil {
59+
if services.ResourceNotFound(err) {
60+
return nil
61+
}
62+
return errors.Wrapf(err, "failed to Delete resource group for cluster %s", s.scope.Cluster.Name)
63+
}
64+
return nil
65+
}

0 commit comments

Comments
 (0)