Skip to content

Commit 49e8e30

Browse files
authored
refactor: remove hand-written interfaces (#1208)
Remove hand-written interfaces in favour of generated interfaces from libraries we use.
1 parent 5d3c411 commit 49e8e30

File tree

16 files changed

+45
-176
lines changed

16 files changed

+45
-176
lines changed

hcloud/cloud.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var providerVersion = "unknown"
5050

5151
type cloud struct {
5252
client *hcloud.Client
53-
robotClient robot.Client
53+
robotClient hrobot.RobotClient
5454
cfg config.HCCMConfiguration
5555
recorder record.EventRecorder
5656
networkID int64
@@ -95,7 +95,7 @@ func NewCloud(cidr string) (cloudprovider.Interface, error) {
9595
client := hcloud.NewClient(opts...)
9696
metadataClient := metadata.NewClient()
9797

98-
var robotClient robot.Client
98+
var robotClient hrobot.RobotClient
9999
if cfg.Robot.Enabled && cfg.Robot.User != "" && cfg.Robot.Password != "" {
100100
c := hrobot.NewBasicAuthClientWithCustomHttpClient(
101101
cfg.Robot.User,

hcloud/instances.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net"
2424

25+
hrobot "github.com/syself/hrobot-go"
2526
hrobotmodels "github.com/syself/hrobot-go/models"
2627
corev1 "k8s.io/api/core/v1"
2728
"k8s.io/client-go/tools/record"
@@ -32,7 +33,6 @@ import (
3233
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/legacydatacenter"
3334
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/metrics"
3435
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/providerid"
35-
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/robot"
3636
"github.com/hetznercloud/hcloud-go/v2/hcloud"
3737
)
3838

@@ -43,7 +43,7 @@ const (
4343

4444
type instances struct {
4545
client *hcloud.Client
46-
robotClient robot.Client
46+
robotClient hrobot.RobotClient
4747
recorder record.EventRecorder
4848
networkID int64
4949
cfg config.HCCMConfiguration
@@ -56,7 +56,7 @@ var (
5656

5757
func newInstances(
5858
client *hcloud.Client,
59-
robotClient robot.Client,
59+
robotClient hrobot.RobotClient,
6060
recorder record.EventRecorder,
6161
networkID int64,
6262
cfg config.HCCMConfiguration,
@@ -359,7 +359,7 @@ func (s hcloudServer) Metadata(networkID int64, _ *corev1.Node, cfg config.HCCMC
359359

360360
type robotServer struct {
361361
*hrobotmodels.Server
362-
robotClient robot.Client
362+
robotClient hrobot.RobotClient
363363
recorder record.EventRecorder
364364
}
365365

hcloud/instances_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"regexp"
2323
"strings"
2424

25+
hrobot "github.com/syself/hrobot-go"
2526
hrobotmodels "github.com/syself/hrobot-go/models"
2627
corev1 "k8s.io/api/core/v1"
2728
"k8s.io/apimachinery/pkg/runtime"
2829

2930
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/metrics"
30-
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/robot"
3131
"github.com/hetznercloud/hcloud-go/v2/hcloud"
3232
)
3333

@@ -74,7 +74,7 @@ func getCloudServerByID(ctx context.Context, c *hcloud.Client, id int64) (*hclou
7474
return server, nil
7575
}
7676

77-
func getRobotServerByName(c robot.Client, node *corev1.Node) (server *hrobotmodels.Server, err error) {
77+
func getRobotServerByName(c hrobot.RobotClient, node *corev1.Node) (server *hrobotmodels.Server, err error) {
7878
const op = "hcloud/getRobotServerByName"
7979

8080
if c == nil {

internal/hcops/action.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/hcops/certificates.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,10 @@ import (
88
"github.com/hetznercloud/hcloud-go/v2/hcloud"
99
)
1010

11-
// HCloudCertificateClient defines the hcloud-go function related to
12-
// certificate management.
13-
type HCloudCertificateClient interface {
14-
AllWithOpts(context.Context, hcloud.CertificateListOpts) ([]*hcloud.Certificate, error)
15-
Get(ctx context.Context, idOrName string) (*hcloud.Certificate, *hcloud.Response, error)
16-
CreateCertificate(
17-
ctx context.Context, opts hcloud.CertificateCreateOpts,
18-
) (hcloud.CertificateCreateResult, *hcloud.Response, error)
19-
}
20-
2111
// CertificateOps implements all operations regarding Hetzner Cloud Certificates.
2212
type CertificateOps struct {
23-
ActionClient HCloudActionClient
24-
CertClient HCloudCertificateClient
13+
ActionClient hcloud.IActionClient
14+
CertClient hcloud.ICertificateClient
2515
}
2616

2717
// GetCertificateByNameOrID obtains a certificate from the Hetzner Cloud

internal/hcops/load_balancer.go

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sync"
99
"time"
1010

11+
hrobot "github.com/syself/hrobot-go"
1112
corev1 "k8s.io/api/core/v1"
1213
"k8s.io/client-go/tools/record"
1314
"k8s.io/klog/v2"
@@ -16,65 +17,19 @@ import (
1617
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/config"
1718
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/metrics"
1819
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/providerid"
19-
"github.com/hetznercloud/hcloud-cloud-controller-manager/internal/robot"
2020
"github.com/hetznercloud/hcloud-go/v2/hcloud"
2121
)
2222

2323
// LabelServiceUID is a label added to the Hetzner Cloud backend to uniquely
2424
// identify a load balancer managed by Hetzner Cloud Cloud Controller Manager.
2525
const LabelServiceUID = "hcloud-ccm/service-uid"
2626

27-
// HCloudLoadBalancerClient defines the hcloud-go functions required by the
28-
// Load Balancer operations type.
29-
type HCloudLoadBalancerClient interface {
30-
GetByID(ctx context.Context, id int64) (*hcloud.LoadBalancer, *hcloud.Response, error)
31-
GetByName(ctx context.Context, name string) (*hcloud.LoadBalancer, *hcloud.Response, error)
32-
33-
Create(ctx context.Context, opts hcloud.LoadBalancerCreateOpts) (hcloud.LoadBalancerCreateResult, *hcloud.Response, error)
34-
Update(
35-
ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerUpdateOpts,
36-
) (*hcloud.LoadBalancer, *hcloud.Response, error)
37-
Delete(ctx context.Context, lb *hcloud.LoadBalancer) (*hcloud.Response, error)
38-
39-
AddService(
40-
ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerAddServiceOpts,
41-
) (*hcloud.Action, *hcloud.Response, error)
42-
UpdateService(
43-
ctx context.Context, lb *hcloud.LoadBalancer, listenPort int, opts hcloud.LoadBalancerUpdateServiceOpts,
44-
) (*hcloud.Action, *hcloud.Response, error)
45-
DeleteService(
46-
ctx context.Context, lb *hcloud.LoadBalancer, listenPort int,
47-
) (*hcloud.Action, *hcloud.Response, error)
48-
49-
ChangeAlgorithm(ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerChangeAlgorithmOpts) (*hcloud.Action, *hcloud.Response, error)
50-
ChangeType(ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerChangeTypeOpts) (*hcloud.Action, *hcloud.Response, error)
51-
ChangeDNSPtr(ctx context.Context, lb *hcloud.LoadBalancer, ip string, ptr *string) (*hcloud.Action, *hcloud.Response, error)
52-
53-
AddServerTarget(ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerAddServerTargetOpts) (*hcloud.Action, *hcloud.Response, error)
54-
RemoveServerTarget(ctx context.Context, lb *hcloud.LoadBalancer, server *hcloud.Server) (*hcloud.Action, *hcloud.Response, error)
55-
56-
AddIPTarget(ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerAddIPTargetOpts) (*hcloud.Action, *hcloud.Response, error)
57-
RemoveIPTarget(ctx context.Context, lb *hcloud.LoadBalancer, server net.IP) (*hcloud.Action, *hcloud.Response, error)
58-
59-
AttachToNetwork(ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerAttachToNetworkOpts) (*hcloud.Action, *hcloud.Response, error)
60-
DetachFromNetwork(ctx context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerDetachFromNetworkOpts) (*hcloud.Action, *hcloud.Response, error)
61-
62-
EnablePublicInterface(
63-
ctx context.Context, loadBalancer *hcloud.LoadBalancer,
64-
) (*hcloud.Action, *hcloud.Response, error)
65-
DisablePublicInterface(
66-
ctx context.Context, loadBalancer *hcloud.LoadBalancer,
67-
) (*hcloud.Action, *hcloud.Response, error)
68-
69-
AllWithOpts(ctx context.Context, opts hcloud.LoadBalancerListOpts) ([]*hcloud.LoadBalancer, error)
70-
}
71-
7227
// LoadBalancerOps implements all operations regarding Hetzner Cloud Load Balancers.
7328
type LoadBalancerOps struct {
74-
LBClient HCloudLoadBalancerClient
75-
ActionClient HCloudActionClient
76-
NetworkClient HCloudNetworkClient
77-
RobotClient robot.Client
29+
LBClient hcloud.ILoadBalancerClient
30+
ActionClient hcloud.IActionClient
31+
NetworkClient hcloud.INetworkClient
32+
RobotClient hrobot.RobotClient
7833
CertOps *CertificateOps
7934
RetryDelay time.Duration
8035
NetworkID int64

internal/hcops/network.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/mocks/action.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type ActionClient struct {
1212
mock.Mock
13+
hcloud.IActionClient // embedded for compile-time interface satisfaction
1314
}
1415

1516
func (m *ActionClient) WaitFor(ctx context.Context, actions ...*hcloud.Action) error {

internal/mocks/casts.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func getRobotServers(args mock.Arguments, i int) []hrobotmodels.Server {
5555
return v.([]hrobotmodels.Server)
5656
}
5757

58+
func getReset(args mock.Arguments, i int) *hrobotmodels.Reset {
59+
v := args.Get(i)
60+
if v == nil {
61+
return nil
62+
}
63+
return v.(*hrobotmodels.Reset)
64+
}
65+
5866
func getNetworkPtr(args mock.Arguments, i int) *hcloud.Network {
5967
v := args.Get(i)
6068
if v == nil {

internal/mocks/certificates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type CertificateClient struct {
1212
mock.Mock
13+
hcloud.ICertificateClient // embedded for compile-time interface satisfaction
1314
}
1415

1516
func (m *CertificateClient) AllWithOpts(

0 commit comments

Comments
 (0)