Skip to content

Commit

Permalink
Merge pull request #52 from Leaseweb/develop
Browse files Browse the repository at this point in the history
Add support for loadBalancerSourceRanges annotation, better lb rule names, fix panic, update to K8s 1.29.9
  • Loading branch information
hrak authored Sep 24, 2024
2 parents 6ec363b + bd1fd06 commit cb800fa
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.21"
go-version: "1.22"
- name: Run Script
run: make test

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

FROM golang:1.21 as builder
FROM golang:1.22 as builder
COPY . /go/src/github.com/apache/cloudstack-kubernetes-provider
WORKDIR /go/src/github.com/apache/cloudstack-kubernetes-provider
RUN make clean && CGO_ENABLED=0 GOOS=linux make
Expand Down
20 changes: 15 additions & 5 deletions cloudstack/cloudstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ import (
"errors"
"fmt"
"io"
"k8s.io/client-go/kubernetes"

"github.com/apache/cloudstack-go/v2/cloudstack"
"gopkg.in/gcfg.v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/tools/record"
cloudprovider "k8s.io/cloud-provider"
)

Expand All @@ -51,10 +55,11 @@ var _ cloudprovider.LoadBalancer = (*CSCloud)(nil)

// CSCloud is an implementation of Interface for CloudStack.
type CSCloud struct {
client *cloudstack.CloudStackClient
projectID string // If non-"", all resources will be created within this project
zone string
kclient kubernetes.Interface
client *cloudstack.CloudStackClient
projectID string // If non-"", all resources will be created within this project
zone string
kclient kubernetes.Interface
eventRecorder record.EventRecorder
}

func init() {
Expand Down Expand Up @@ -104,6 +109,11 @@ func newCSCloud(cfg *CSConfig) (*CSCloud, error) {
func (cs *CSCloud) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
clientset := clientBuilder.ClientOrDie("cloud-controller-manager")
cs.kclient = clientset
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{
Interface: cs.kclient.CoreV1().Events(""),
})
cs.eventRecorder = eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: "cloud-provider-cloudstack"})
}

// LoadBalancer returns an implementation of LoadBalancer for CloudStack.
Expand Down
146 changes: 116 additions & 30 deletions cloudstack/cloudstack_loadbalancer.go

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions cloudstack/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
package cloudstack

import (
v1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
)

// LoadBalancerProtocol represents a specific network protocol supported by the CloudStack load balancer.
Expand Down Expand Up @@ -81,16 +81,16 @@ func (p LoadBalancerProtocol) IPProtocol() string {
// -> "tcp-proxy" (CloudStack 4.6 and later)
//
// Other values return LoadBalancerProtocolInvalid.
func ProtocolFromServicePort(port v1.ServicePort, service *v1.Service) LoadBalancerProtocol {
func ProtocolFromServicePort(port corev1.ServicePort, service *corev1.Service) LoadBalancerProtocol {
proxy := getBoolFromServiceAnnotation(service, ServiceAnnotationLoadBalancerProxyProtocol, false)
switch port.Protocol {
case v1.ProtocolTCP:
case corev1.ProtocolTCP:
if proxy {
return LoadBalancerProtocolTCPProxy
} else {
return LoadBalancerProtocolTCP
}
case v1.ProtocolUDP:
case corev1.ProtocolUDP:
return LoadBalancerProtocolUDP
default:
return LoadBalancerProtocolInvalid
Expand Down
13 changes: 6 additions & 7 deletions cloudstack/service_patcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ import (
"context"
"encoding/json"
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"reflect"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/client-go/kubernetes"
"reflect"
)

type servicePatcher struct {
Expand Down Expand Up @@ -60,7 +59,7 @@ func (sp *servicePatcher) Patch(ctx context.Context, err error) error {
}

// patchService makes patch request to the Service object.
func patchService(ctx context.Context, client kubernetes.Interface, cur, mod *v1.Service) error {
func patchService(ctx context.Context, client kubernetes.Interface, cur, mod *corev1.Service) error {
curJSON, err := json.Marshal(cur)
if err != nil {
return fmt.Errorf("failed to serialize current service object: %v", err)
Expand All @@ -71,7 +70,7 @@ func patchService(ctx context.Context, client kubernetes.Interface, cur, mod *v1
return fmt.Errorf("failed to serialize modified service object: %v", err)
}

patch, err := strategicpatch.CreateTwoWayMergePatch(curJSON, modJSON, v1.Service{})
patch, err := strategicpatch.CreateTwoWayMergePatch(curJSON, modJSON, corev1.Service{})
if err != nil {
return fmt.Errorf("failed to create 2-way merge patch: %v", err)
}
Expand Down
10 changes: 10 additions & 0 deletions cloudstack/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ func sanitizeLabel(value string) string {

return value
}

// Sprintf255 behaves like fmt.Sprintf but limits the output to 255 characters.
func Sprintf255(format string, a ...interface{}) string {
result := fmt.Sprintf(format, a...)
if len(result) > 255 {
return result[:255]
}

return result
}
24 changes: 12 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ require (
github.com/google/go-cmp v0.6.0
github.com/stretchr/testify v1.9.0
gopkg.in/gcfg.v1 v1.2.3
k8s.io/api v0.29.5
k8s.io/apimachinery v0.29.5
k8s.io/cloud-provider v0.29.5
k8s.io/component-base v0.29.5
k8s.io/api v0.29.9
k8s.io/apimachinery v0.29.9
k8s.io/client-go v0.29.9
k8s.io/cloud-provider v0.29.9
k8s.io/component-base v0.29.9
k8s.io/klog/v2 v2.110.1
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
)

require (
Expand Down Expand Up @@ -99,20 +101,18 @@ require (
gopkg.in/warnings.v0 v0.1.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiserver v0.29.5 // indirect
k8s.io/client-go v0.29.5 // indirect
k8s.io/component-helpers v0.29.5 // indirect
k8s.io/controller-manager v0.29.5 // indirect
k8s.io/kms v0.29.5 // indirect
k8s.io/apiserver v0.29.9 // indirect
k8s.io/component-helpers v0.29.9 // indirect
k8s.io/controller-manager v0.29.9 // indirect
k8s.io/kms v0.29.9 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.28.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace (
k8s.io/component-helpers => k8s.io/component-helpers v0.29.5
k8s.io/controller-manager => k8s.io/controller-manager v0.29.5
k8s.io/component-helpers => k8s.io/component-helpers v0.29.9
k8s.io/controller-manager => k8s.io/controller-manager v0.29.9
)
36 changes: 18 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -324,26 +324,26 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
k8s.io/api v0.29.5 h1:levS+umUigHCfI3riD36pMY1vQEbrzh4r1ivVWAhHaI=
k8s.io/api v0.29.5/go.mod h1:7b18TtPcJzdjk7w5zWyIHgoAtpGeRvGGASxlS7UZXdQ=
k8s.io/apimachinery v0.29.5 h1:Hofa2BmPfpoT+IyDTlcPdCHSnHtEQMoJYGVoQpRTfv4=
k8s.io/apimachinery v0.29.5/go.mod h1:i3FJVwhvSp/6n8Fl4K97PJEP8C+MM+aoDq4+ZJBf70Y=
k8s.io/apiserver v0.29.5 h1:223C+JkRnGmudEU00GfpX6quDSrzwwP0DuXOYTyUYb0=
k8s.io/apiserver v0.29.5/go.mod h1:zN9xdatz5g7XwL1Xoz9hD4QQON1GN0c+1kV5e/NHejM=
k8s.io/client-go v0.29.5 h1:nlASXmPQy190qTteaVP31g3c/wi2kycznkTP7Sv1zPc=
k8s.io/client-go v0.29.5/go.mod h1:aY5CnqUUvXYccJhm47XHoPcRyX6vouHdIBHaKZGTbK4=
k8s.io/cloud-provider v0.29.5 h1:xzgnKrXK8NgMVBGCAZmupzA5WALLTTQkGgg36k/Zey4=
k8s.io/cloud-provider v0.29.5/go.mod h1:cKT9OzdMUK2/2rF3dWOD/w6Oqg/X4MTxrNVv4GTappQ=
k8s.io/component-base v0.29.5 h1:Ptj8AzG+p8c2a839XriHwxakDpZH9uvIgYz+o1agjg8=
k8s.io/component-base v0.29.5/go.mod h1:9nBUoPxW/yimISIgAG7sJDrUGJlu7t8HnDafIrOdU8Q=
k8s.io/component-helpers v0.29.5 h1:aDwzJoQWK4zreZPKun6H2c3QIwy1F1G5hMU5YR1s8yA=
k8s.io/component-helpers v0.29.5/go.mod h1:5Hwtbhs8I9DdVvV8Lh2cKaHbGuqgqqE18iO5XyFzGVE=
k8s.io/controller-manager v0.29.5 h1:KOzas+Az8egEsTIajKdjVmlY0MzvAaYDREmo7+Kyyzg=
k8s.io/controller-manager v0.29.5/go.mod h1:WXsnVH7yjXG6jyjEQ/B0DTe71m1tC+TohTjt6/9RErs=
k8s.io/api v0.29.9 h1:FwdflpNsfMUYUOblMZNWJ4K/q0OSL5A4jGa0iOgcJco=
k8s.io/api v0.29.9/go.mod h1:fNhmzRfKaSEHCmczA/jRx6CiDKhYOnFLJBERMJAXEk8=
k8s.io/apimachinery v0.29.9 h1:YZ8HUid1TzQVz94cnNlsQjLdH0VoAhWSqz7t0q6B12A=
k8s.io/apimachinery v0.29.9/go.mod h1:i3FJVwhvSp/6n8Fl4K97PJEP8C+MM+aoDq4+ZJBf70Y=
k8s.io/apiserver v0.29.9 h1:BiHTZbAYcNYHTE9RlWMBe5AX2XoZbRujy6oo5krU4V8=
k8s.io/apiserver v0.29.9/go.mod h1:3zmBeYworciVZHs8jSfzt/naTpDQb3AM++aYJ5Pkqqo=
k8s.io/client-go v0.29.9 h1:4f/Wz6li3rEyIPFj32XAQMtOGMM1tg7KQi1oeS6ibPg=
k8s.io/client-go v0.29.9/go.mod h1:2N1drQEZ5yiYrWVaE2Un8JiISUhl47D8pyZlYLszke4=
k8s.io/cloud-provider v0.29.9 h1:sbu1is+Hq6/l7SlgBdy6Vc9fEtQysLBPTu3qgmPXU44=
k8s.io/cloud-provider v0.29.9/go.mod h1:fTOTtMu+SMa9oTeAsPtkliICKp2t/4a0DEXeOwrrhnc=
k8s.io/component-base v0.29.9 h1:lPENvp3CCwdeMEWGjiTfn5b287qQYuK7gX32OBOovmA=
k8s.io/component-base v0.29.9/go.mod h1:NGDa6Ih0EdcLA2G4K2ZYySoiB+2Tn+rmSqPyudCPgDY=
k8s.io/component-helpers v0.29.9 h1:+3dLb8nHWPeNwrL6whkpvKv46o90EW5O6Aju/hLQW70=
k8s.io/component-helpers v0.29.9/go.mod h1:sUtoToDUdZDku0Kj4i3dRWyb1jmZLxXO2cvWvD27A40=
k8s.io/controller-manager v0.29.9 h1:hKGqtTf7IDmN5csLVgf6VUXPJqosajAnMsqw2jCDh6s=
k8s.io/controller-manager v0.29.9/go.mod h1:OZVIb2E9QFoSaiaVt4DFOsH/kZpCaEgFSYr/mCF9zuk=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
k8s.io/kms v0.29.5 h1:DcR0hBeEcuLmKTpriezu7kyR4sJcHeeYle/WGdQWi2o=
k8s.io/kms v0.29.5/go.mod h1:vWVImKkJd+1BQY4tBwdfSwjQBiLrnbNtHADcDEDQFtk=
k8s.io/kms v0.29.9 h1:7B9VGhFAzNfBmfXeQ0Xi+OzIcNPw/xazPnLm/J3vNT4=
k8s.io/kms v0.29.9/go.mod h1:vWVImKkJd+1BQY4tBwdfSwjQBiLrnbNtHADcDEDQFtk=
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 h1:aVUu9fTY98ivBPKR9Y5w/AuzbMm96cd3YHRTU83I780=
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00/go.mod h1:AsvuZPBlUDVuCdzJ87iajxtXuR9oktsTctW/R9wwouA=
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
Expand Down

0 comments on commit cb800fa

Please sign in to comment.