Skip to content

Commit c939508

Browse files
hrakGambler13
andauthored
Update to Kubernetes 1.24.12 + Use Go 1.19 (#44)
This PR is a continuation of the work in #32 and contains the following changes: Use Go 1.19 Update Kubernetes deps to 1.24.12 Move the cloudstack-go dep to its new home in the apache org (and update to v2.15.0) switch to klog v2 I can provide a test container image if needed. Signed-off-by: Hans Rakers <[email protected]> Co-authored-by: DESKTOP-AG9326G\Schlatter <[email protected]>
1 parent 0835702 commit c939508

File tree

10 files changed

+715
-650
lines changed

10 files changed

+715
-650
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
- name: Set up Go
3434
uses: actions/setup-go@v3
3535
with:
36-
go-version: 1.13
36+
go-version: 1.19
3737
- name: Run Script
3838
run: make test
3939

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
FROM golang:1.13 as builder
18+
FROM golang:1.19 as builder
1919
COPY . /go/src/github.com/apache/cloudstack-kubernetes-provider
2020
WORKDIR /go/src/github.com/apache/cloudstack-kubernetes-provider
2121
RUN make clean && CGO_ENABLED=0 GOOS=linux make

cloudstack.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ import (
2626
"io"
2727
"os"
2828

29-
"github.com/xanzy/go-cloudstack/v2/cloudstack"
29+
"github.com/apache/cloudstack-go/v2/cloudstack"
3030
"gopkg.in/gcfg.v1"
3131
"k8s.io/apimachinery/pkg/types"
3232
cloudprovider "k8s.io/cloud-provider"
33-
"k8s.io/klog"
33+
"k8s.io/klog/v2"
3434
)
3535

3636
// ProviderName is the name of this cloud provider.
@@ -120,6 +120,14 @@ func (cs *CSCloud) Instances() (cloudprovider.Instances, bool) {
120120
return cs, true
121121
}
122122

123+
func (cs *CSCloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
124+
if cs.client == nil {
125+
return nil, false
126+
}
127+
128+
return cs, true
129+
}
130+
123131
// Zones returns an implementation of Zones for CloudStack.
124132
func (cs *CSCloud) Zones() (cloudprovider.Zones, bool) {
125133
if cs.client == nil {

cloudstack_instances.go

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ import (
2525
"fmt"
2626
"regexp"
2727

28-
"github.com/xanzy/go-cloudstack/v2/cloudstack"
29-
"k8s.io/api/core/v1"
28+
"github.com/apache/cloudstack-go/v2/cloudstack"
29+
corev1 "k8s.io/api/core/v1"
3030
"k8s.io/apimachinery/pkg/types"
3131
cloudprovider "k8s.io/cloud-provider"
32-
"k8s.io/klog"
32+
"k8s.io/klog/v2"
3333
)
3434

3535
var labelInvalidCharsRegex *regexp.Regexp = regexp.MustCompile(`([^A-Za-z0-9][^-A-Za-z0-9_.]*)?[^A-Za-z0-9]`)
3636

3737
// NodeAddresses returns the addresses of the specified instance.
38-
func (cs *CSCloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1.NodeAddress, error) {
38+
func (cs *CSCloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]corev1.NodeAddress, error) {
3939
instance, count, err := cs.client.VirtualMachine.GetVirtualMachineByName(
4040
string(name),
4141
cloudstack.WithProject(cs.projectID),
@@ -51,7 +51,7 @@ func (cs *CSCloud) NodeAddresses(ctx context.Context, name types.NodeName) ([]v1
5151
}
5252

5353
// NodeAddressesByProviderID returns the addresses of the specified instance.
54-
func (cs *CSCloud) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]v1.NodeAddress, error) {
54+
func (cs *CSCloud) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]corev1.NodeAddress, error) {
5555
instance, count, err := cs.client.VirtualMachine.GetVirtualMachineByID(
5656
providerID,
5757
cloudstack.WithProject(cs.projectID),
@@ -66,21 +66,21 @@ func (cs *CSCloud) NodeAddressesByProviderID(ctx context.Context, providerID str
6666
return cs.nodeAddresses(instance)
6767
}
6868

69-
func (cs *CSCloud) nodeAddresses(instance *cloudstack.VirtualMachine) ([]v1.NodeAddress, error) {
69+
func (cs *CSCloud) nodeAddresses(instance *cloudstack.VirtualMachine) ([]corev1.NodeAddress, error) {
7070
if len(instance.Nic) == 0 {
7171
return nil, errors.New("instance does not have an internal IP")
7272
}
7373

74-
addresses := []v1.NodeAddress{
75-
{Type: v1.NodeInternalIP, Address: instance.Nic[0].Ipaddress},
74+
addresses := []corev1.NodeAddress{
75+
{Type: corev1.NodeInternalIP, Address: instance.Nic[0].Ipaddress},
7676
}
7777

7878
if instance.Hostname != "" {
79-
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeHostName, Address: instance.Hostname})
79+
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeHostName, Address: instance.Hostname})
8080
}
8181

8282
if instance.Publicip != "" {
83-
addresses = append(addresses, v1.NodeAddress{Type: v1.NodeExternalIP, Address: instance.Publicip})
83+
addresses = append(addresses, corev1.NodeAddress{Type: corev1.NodeExternalIP, Address: instance.Publicip})
8484
} else {
8585
// Since there is no sane way to determine the external IP if the host isn't
8686
// using static NAT, we will just fire a log message and omit the external IP.
@@ -168,3 +168,43 @@ func (cs *CSCloud) InstanceExistsByProviderID(ctx context.Context, providerID st
168168
func (cs *CSCloud) InstanceShutdownByProviderID(ctx context.Context, providerID string) (bool, error) {
169169
return false, cloudprovider.NotImplemented
170170
}
171+
172+
func (cs *CSCloud) InstanceExists(ctx context.Context, node *corev1.Node) (bool, error) {
173+
nodeName := types.NodeName(node.Name)
174+
providerID, err := cs.InstanceID(ctx, nodeName)
175+
if err != nil {
176+
return false, err
177+
}
178+
179+
return cs.InstanceExistsByProviderID(ctx, providerID)
180+
}
181+
182+
func (cs *CSCloud) InstanceShutdown(ctx context.Context, node *corev1.Node) (bool, error) {
183+
return false, cloudprovider.NotImplemented
184+
}
185+
186+
func (cs *CSCloud) InstanceMetadata(ctx context.Context, node *corev1.Node) (*cloudprovider.InstanceMetadata, error) {
187+
188+
instanceType, err := cs.InstanceType(ctx, types.NodeName(node.Name))
189+
if err != nil {
190+
return nil, err
191+
}
192+
193+
addresses, err := cs.NodeAddresses(ctx, types.NodeName(node.Name))
194+
if err != nil {
195+
return nil, err
196+
}
197+
198+
zone, err := cs.GetZone(ctx)
199+
if err != nil {
200+
return nil, err
201+
}
202+
203+
return &cloudprovider.InstanceMetadata{
204+
ProviderID: cs.ProviderName(),
205+
InstanceType: instanceType,
206+
NodeAddresses: addresses,
207+
Zone: cs.zone,
208+
Region: zone.Region,
209+
}, nil
210+
}

cloudstack_loadbalancer.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import (
2525
"strconv"
2626
"strings"
2727

28-
"github.com/xanzy/go-cloudstack/v2/cloudstack"
29-
"k8s.io/klog"
28+
"github.com/apache/cloudstack-go/v2/cloudstack"
29+
"k8s.io/klog/v2"
3030

31-
v1 "k8s.io/api/core/v1"
31+
corev1 "k8s.io/api/core/v1"
3232
cloudprovider "k8s.io/cloud-provider"
3333
)
3434

@@ -50,7 +50,7 @@ type loadBalancer struct {
5050
}
5151

5252
// GetLoadBalancer returns whether the specified load balancer exists, and if so, what its status is.
53-
func (cs *CSCloud) GetLoadBalancer(ctx context.Context, clusterName string, service *v1.Service) (*v1.LoadBalancerStatus, bool, error) {
53+
func (cs *CSCloud) GetLoadBalancer(ctx context.Context, clusterName string, service *corev1.Service) (*corev1.LoadBalancerStatus, bool, error) {
5454
klog.V(4).Infof("GetLoadBalancer(%v, %v, %v)", clusterName, service.Namespace, service.Name)
5555

5656
// Get the load balancer details and existing rules.
@@ -66,14 +66,14 @@ func (cs *CSCloud) GetLoadBalancer(ctx context.Context, clusterName string, serv
6666

6767
klog.V(4).Infof("Found a load balancer associated with IP %v", lb.ipAddr)
6868

69-
status := &v1.LoadBalancerStatus{}
70-
status.Ingress = append(status.Ingress, v1.LoadBalancerIngress{IP: lb.ipAddr})
69+
status := &corev1.LoadBalancerStatus{}
70+
status.Ingress = append(status.Ingress, corev1.LoadBalancerIngress{IP: lb.ipAddr})
7171

7272
return status, true, nil
7373
}
7474

7575
// EnsureLoadBalancer creates a new load balancer, or updates the existing one. Returns the status of the balancer.
76-
func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) (status *v1.LoadBalancerStatus, err error) {
76+
func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, service *corev1.Service, nodes []*corev1.Node) (status *corev1.LoadBalancerStatus, err error) {
7777
klog.V(4).Infof("EnsureLoadBalancer(%v, %v, %v, %v, %v, %v)", clusterName, service.Namespace, service.Name, service.Spec.LoadBalancerIP, service.Spec.Ports, nodes)
7878

7979
if len(service.Spec.Ports) == 0 {
@@ -88,9 +88,9 @@ func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, s
8888

8989
// Set the load balancer algorithm.
9090
switch service.Spec.SessionAffinity {
91-
case v1.ServiceAffinityNone:
91+
case corev1.ServiceAffinityNone:
9292
lb.algorithm = "roundrobin"
93-
case v1.ServiceAffinityClientIP:
93+
case corev1.ServiceAffinityClientIP:
9494
lb.algorithm = "source"
9595
default:
9696
return nil, fmt.Errorf("unsupported load balancer affinity: %v", service.Spec.SessionAffinity)
@@ -193,14 +193,14 @@ func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, s
193193
}
194194
}
195195

196-
status = &v1.LoadBalancerStatus{}
197-
status.Ingress = []v1.LoadBalancerIngress{{IP: lb.ipAddr}}
196+
status = &corev1.LoadBalancerStatus{}
197+
status.Ingress = []corev1.LoadBalancerIngress{{IP: lb.ipAddr}}
198198

199199
return status, nil
200200
}
201201

202202
// UpdateLoadBalancer updates hosts under the specified load balancer.
203-
func (cs *CSCloud) UpdateLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) error {
203+
func (cs *CSCloud) UpdateLoadBalancer(ctx context.Context, clusterName string, service *corev1.Service, nodes []*corev1.Node) error {
204204
klog.V(4).Infof("UpdateLoadBalancer(%v, %v, %v, %v)", clusterName, service.Namespace, service.Name, nodes)
205205

206206
// Get the load balancer details and existing rules.
@@ -246,7 +246,7 @@ func (cs *CSCloud) UpdateLoadBalancer(ctx context.Context, clusterName string, s
246246

247247
// EnsureLoadBalancerDeleted deletes the specified load balancer if it exists, returning
248248
// nil if the load balancer specified either didn't exist or was successfully deleted.
249-
func (cs *CSCloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName string, service *v1.Service) error {
249+
func (cs *CSCloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName string, service *corev1.Service) error {
250250
klog.V(4).Infof("EnsureLoadBalancerDeleted(%v, %v, %v)", clusterName, service.Namespace, service.Name)
251251

252252
// Get the load balancer details and existing rules.
@@ -286,12 +286,12 @@ func (cs *CSCloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName st
286286
}
287287

288288
// GetLoadBalancerName retrieves the name of the LoadBalancer.
289-
func (cs *CSCloud) GetLoadBalancerName(ctx context.Context, clusterName string, service *v1.Service) string {
289+
func (cs *CSCloud) GetLoadBalancerName(ctx context.Context, clusterName string, service *corev1.Service) string {
290290
return cloudprovider.DefaultLoadBalancerName(service)
291291
}
292292

293293
// getLoadBalancer retrieves the IP address and ID and all the existing rules it can find.
294-
func (cs *CSCloud) getLoadBalancer(service *v1.Service) (*loadBalancer, error) {
294+
func (cs *CSCloud) getLoadBalancer(service *corev1.Service) (*loadBalancer, error) {
295295
lb := &loadBalancer{
296296
CloudStackClient: cs.client,
297297
name: cs.GetLoadBalancerName(context.TODO(), "", service),
@@ -329,7 +329,7 @@ func (cs *CSCloud) getLoadBalancer(service *v1.Service) (*loadBalancer, error) {
329329
}
330330

331331
// verifyHosts verifies if all hosts belong to the same network, and returns the host ID's and network ID.
332-
func (cs *CSCloud) verifyHosts(nodes []*v1.Node) ([]string, string, error) {
332+
func (cs *CSCloud) verifyHosts(nodes []*corev1.Node) ([]string, string, error) {
333333
hostNames := map[string]bool{}
334334
for _, node := range nodes {
335335
hostNames[strings.ToLower(node.Name)] = true
@@ -456,7 +456,7 @@ func (lb *loadBalancer) releaseLoadBalancerIP() error {
456456

457457
// checkLoadBalancerRule checks if the rule already exists and if it does, if it can be updated. If
458458
// it does exist but cannot be updated, it will delete the existing rule so it can be created again.
459-
func (lb *loadBalancer) checkLoadBalancerRule(lbRuleName string, port v1.ServicePort, protocol LoadBalancerProtocol) (*cloudstack.LoadBalancerRule, bool, error) {
459+
func (lb *loadBalancer) checkLoadBalancerRule(lbRuleName string, port corev1.ServicePort, protocol LoadBalancerProtocol) (*cloudstack.LoadBalancerRule, bool, error) {
460460
lbRule, ok := lb.rules[lbRuleName]
461461
if !ok {
462462
return nil, false, nil
@@ -490,7 +490,7 @@ func (lb *loadBalancer) updateLoadBalancerRule(lbRuleName string, protocol LoadB
490490
}
491491

492492
// createLoadBalancerRule creates a new load balancer rule and returns it's ID.
493-
func (lb *loadBalancer) createLoadBalancerRule(lbRuleName string, port v1.ServicePort, protocol LoadBalancerProtocol) (*cloudstack.LoadBalancerRule, error) {
493+
func (lb *loadBalancer) createLoadBalancerRule(lbRuleName string, port corev1.ServicePort, protocol LoadBalancerProtocol) (*cloudstack.LoadBalancerRule, error) {
494494
p := lb.LoadBalancer.NewCreateLoadBalancerRuleParams(
495495
lb.algorithm,
496496
lbRuleName,

cloudstack_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"strings"
2727
"testing"
2828

29-
"k8s.io/api/core/v1"
29+
corev1 "k8s.io/api/core/v1"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
)
3232

@@ -111,7 +111,7 @@ func TestLoadBalancer(t *testing.T) {
111111
t.Fatalf("LoadBalancer() returned false")
112112
}
113113

114-
_, exists, err := lb.GetLoadBalancer(context.TODO(), testClusterName, &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "noexist"}})
114+
_, exists, err := lb.GetLoadBalancer(context.TODO(), testClusterName, &corev1.Service{ObjectMeta: metav1.ObjectMeta{Name: "noexist"}})
115115
if err != nil {
116116
t.Fatalf("GetLoadBalancer(\"noexist\") returned error: %s", err)
117117
}

cmd/cloudstack-ccm/main.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@ import (
2424
"math/rand"
2525
"time"
2626

27-
"k8s.io/klog"
27+
"k8s.io/apimachinery/pkg/util/wait"
28+
cloudprovider "k8s.io/cloud-provider"
2829

30+
"k8s.io/cloud-provider/app"
31+
"k8s.io/cloud-provider/app/config"
32+
"k8s.io/cloud-provider/options"
2933
cliflag "k8s.io/component-base/cli/flag"
3034
"k8s.io/component-base/logs"
31-
"k8s.io/kubernetes/cmd/cloud-controller-manager/app"
3235

3336
_ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins
3437
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
38+
"k8s.io/klog/v2"
3539

3640
"github.com/spf13/pflag"
3741

@@ -41,7 +45,14 @@ import (
4145
func main() {
4246
rand.Seed(time.Now().UnixNano())
4347

44-
command := app.NewCloudControllerManagerCommand()
48+
ccmOptions, err := options.NewCloudControllerManagerOptions()
49+
if err != nil {
50+
klog.Fatalf("unable to initialize command options: %v", err)
51+
}
52+
53+
fss := cliflag.NamedFlagSets{}
54+
55+
command := app.NewCloudControllerManagerCommand(ccmOptions, cloudInitializer, app.DefaultInitFuncConstructors, fss, wait.NeverStop)
4556

4657
// TODO: once we switch everything over to Cobra commands, we can go back to calling
4758
// cliflag.InitFlags() (by removing its pflag.Parse() call). For now, we have to set the
@@ -56,3 +67,25 @@ func main() {
5667
klog.Fatalf("error: %v\n", err)
5768
}
5869
}
70+
71+
func cloudInitializer(config *config.CompletedConfig) cloudprovider.Interface {
72+
cloudConfig := config.ComponentConfig.KubeCloudShared.CloudProvider
73+
74+
// initialize cloud provider with the cloud provider name and config file provided
75+
cloud, err := cloudprovider.InitCloudProvider(cloudConfig.Name, cloudConfig.CloudConfigFile)
76+
if err != nil {
77+
klog.Fatalf("Cloud provider could not be initialized: %v", err)
78+
}
79+
if cloud == nil {
80+
klog.Fatalf("Cloud provider is nil")
81+
}
82+
83+
if !cloud.HasClusterID() {
84+
if config.ComponentConfig.KubeCloudShared.AllowUntaggedCloud {
85+
klog.Warning("detected a cluster without a ClusterID. A ClusterID will be required in the future. Please tag your cluster to avoid any future issues")
86+
} else {
87+
klog.Fatalf("no ClusterID found. A ClusterID is required for the cloud provider to function properly. This check can be bypassed by setting the allow-untagged-cloud option")
88+
}
89+
}
90+
return cloud
91+
}

0 commit comments

Comments
 (0)