Skip to content

Commit ad869cc

Browse files
authored
refactor code into multiple files (#91)
* refactor code into multiple files Signed-off-by: Felix Breuer <f.breuer94@gmail.com> * remove license comments Signed-off-by: Felix Breuer <f.breuer94@gmail.com> * move mock client to its own package Signed-off-by: Felix Breuer <f.breuer94@gmail.com> --------- Signed-off-by: Felix Breuer <f.breuer94@gmail.com>
1 parent 2ebd5ff commit ad869cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+854
-1001
lines changed

OWNERS_ALIASES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ aliases:
1818
- jamand
1919
- breuerfelix
2020
- aniruddha2000
21+
2122
machine-controller-manager-provider-stackit-approvers:
2223
- dergeberl
2324
- JuliusSte

cmd/machine-controller/main.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,3 @@
1-
/*
2-
Copyright 2014 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-
This file was copied and modified from the kubernetes/kubernetes project
17-
https://github.com/kubernetes/kubernetes/release-1.8/cmd/kube-controller-manager/controller_manager.go
18-
19-
Modifications Copyright (c) 2017 SAP SE or an SAP affiliate company. All rights reserved.
20-
*/
21-
221
package main
232

243
import (

hack/rename-project

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#!/bin/bash -eu
2-
#
3-
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
4-
#
5-
# SPDX-License-Identifier: Apache-2.0
62

73
project_name=$1
84
provider_name=$2

pkg/client/helper.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package client
2+
3+
// ptr returns a pointer to the given value
4+
// This helper is needed because the STACKIT SDK uses pointers for optional fields
5+
func ptr[T any](v T) *T {
6+
return &v
7+
}
8+
9+
// convertLabelsToSDK converts map[string]string to *map[string]interface{} for SDK
10+
//
11+
//nolint:gocritic // SDK requires *map
12+
func convertLabelsToSDK(labels map[string]string) *map[string]interface{} {
13+
if labels == nil {
14+
return nil
15+
}
16+
17+
result := make(map[string]interface{}, len(labels))
18+
for k, v := range labels {
19+
result[k] = v
20+
}
21+
return &result
22+
}
23+
24+
// convertLabelsFromSDK converts *map[string]interface{} from SDK to map[string]string
25+
//
26+
//nolint:gocritic // SDK requires *map
27+
func convertLabelsFromSDK(labels *map[string]interface{}) map[string]string {
28+
if labels == nil {
29+
return nil
30+
}
31+
32+
result := make(map[string]string, len(*labels))
33+
for k, v := range *labels {
34+
if strVal, ok := v.(string); ok {
35+
result[k] = strVal
36+
}
37+
}
38+
return result
39+
}
40+
41+
// convertStringSliceToSDK converts []string to *[]string for SDK
42+
func convertStringSliceToSDK(slice []string) *[]string {
43+
if slice == nil {
44+
return nil
45+
}
46+
return &slice
47+
}
48+
49+
// convertMetadataToSDK converts map[string]interface{} to *map[string]interface{} for SDK
50+
//
51+
//nolint:gocritic // SDK requires *map
52+
func convertMetadataToSDK(metadata map[string]interface{}) *map[string]interface{} {
53+
if metadata == nil {
54+
return nil
55+
}
56+
return &metadata
57+
}

pkg/client/mock/client.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package mock
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
7+
"github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/client"
8+
api "github.com/stackitcloud/machine-controller-manager-provider-stackit/pkg/provider/apis"
9+
)
10+
11+
// StackitClient is a mock implementation of StackitClient for testing
12+
// Note: Single-tenant design - each client is bound to one set of credentials
13+
type StackitClient struct {
14+
CreateServerFunc func(ctx context.Context, projectID, region string, req *client.CreateServerRequest) (*client.Server, error)
15+
GetServerFunc func(ctx context.Context, projectID, region, serverID string) (*client.Server, error)
16+
DeleteServerFunc func(ctx context.Context, projectID, region, serverID string) error
17+
ListServersFunc func(ctx context.Context, projectID, region string, labelSelector map[string]string) ([]*client.Server, error)
18+
GetNICsFunc func(ctx context.Context, projectID, region, serverID string) ([]*client.NIC, error)
19+
UpdateNICFunc func(ctx context.Context, projectID, region, networkID, nicID string, allowedAddresses []string) (*client.NIC, error)
20+
}
21+
22+
func (m *StackitClient) CreateServer(ctx context.Context, projectID, region string, req *client.CreateServerRequest) (*client.Server, error) {
23+
if m.CreateServerFunc != nil {
24+
return m.CreateServerFunc(ctx, projectID, region, req)
25+
}
26+
return &client.Server{
27+
ID: "550e8400-e29b-41d4-a716-446655440000",
28+
Name: req.Name,
29+
Status: "CREATING",
30+
}, nil
31+
}
32+
33+
func (m *StackitClient) GetServer(ctx context.Context, projectID, region, serverID string) (*client.Server, error) {
34+
if m.GetServerFunc != nil {
35+
return m.GetServerFunc(ctx, projectID, region, serverID)
36+
}
37+
return &client.Server{
38+
ID: serverID,
39+
Name: "test-machine",
40+
Status: "ACTIVE",
41+
}, nil
42+
}
43+
44+
func (m *StackitClient) DeleteServer(ctx context.Context, projectID, region, serverID string) error {
45+
if m.DeleteServerFunc != nil {
46+
return m.DeleteServerFunc(ctx, projectID, region, serverID)
47+
}
48+
return nil
49+
}
50+
51+
func (m *StackitClient) ListServers(ctx context.Context, projectID, region string, labelSelector map[string]string) ([]*client.Server, error) {
52+
if m.ListServersFunc != nil {
53+
return m.ListServersFunc(ctx, projectID, region, labelSelector)
54+
}
55+
return []*client.Server{}, nil
56+
}
57+
58+
func (m *StackitClient) GetNICsForServer(ctx context.Context, projectID, region, serverID string) ([]*client.NIC, error) {
59+
if m.GetNICsFunc != nil {
60+
return m.GetNICsFunc(ctx, projectID, region, serverID)
61+
}
62+
return []*client.NIC{}, nil
63+
}
64+
65+
func (m *StackitClient) UpdateNIC(ctx context.Context, projectID, region, networkID, nicID string, allowedAddresses []string) (*client.NIC, error) {
66+
if m.UpdateNICFunc != nil {
67+
return m.UpdateNICFunc(ctx, projectID, region, networkID, nicID, allowedAddresses)
68+
}
69+
return &client.NIC{}, nil
70+
}
71+
72+
// UpdateNIC updates a network interface
73+
74+
// encodeProviderSpec is a helper function to encode ProviderSpec for tests
75+
func EncodeProviderSpec(spec *api.ProviderSpec) ([]byte, error) {
76+
return json.Marshal(spec)
77+
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
2-
//
3-
// SPDX-License-Identifier: Apache-2.0
4-
5-
package provider
1+
package client
62

73
import (
84
"context"
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
2-
//
3-
// SPDX-License-Identifier: Apache-2.0
4-
5-
package provider
1+
package client
62

73
import (
84
"errors"
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
2-
//
3-
// SPDX-License-Identifier: Apache-2.0
4-
5-
package provider
1+
package client
62

73
import (
84
"context"
@@ -18,7 +14,6 @@ import (
1814
//
1915
// Note: region parameter is required by STACKIT SDK v1.0.0+
2016
// It must be extracted from the Secret (e.g., "eu01-1", "eu01-2")
21-
// nolint:dupl // the duplicates are mock functions
2217
type StackitClient interface {
2318
// CreateServer creates a new server in STACKIT
2419
CreateServer(ctx context.Context, projectID, region string, req *CreateServerRequest) (*Server, error)

pkg/provider/apis/provider_spec.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
2-
//
3-
// SPDX-License-Identifier: Apache-2.0
4-
51
package api
62

73
// ProviderSpec is the spec to be used while parsing the calls.

pkg/provider/apis/validation/validation.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
2-
//
3-
// SPDX-License-Identifier: Apache-2.0
4-
5-
// Package validation - validation is used to validate cloud specific ProviderSpec
61
package validation
72

83
import (

0 commit comments

Comments
 (0)