Skip to content

Commit

Permalink
feat: add an event recorder to emit events on Service objects to info…
Browse files Browse the repository at this point in the history
…rm or warn users
  • Loading branch information
hrak committed Sep 20, 2024
1 parent 7679e44 commit 0ccfa4c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
23 changes: 16 additions & 7 deletions cloudstack/cloudstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ package cloudstack
import (
"errors"
"fmt"
"io"
"k8s.io/client-go/kubernetes"

"github.com/apache/cloudstack-go/v2/cloudstack"
"gopkg.in/gcfg.v1"
"io"
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 +54,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 +108,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
9 changes: 9 additions & 0 deletions cloudstack/cloudstack_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (cs *CSCloud) GetLoadBalancer(ctx context.Context, clusterName string, serv
// EnsureLoadBalancer creates a new load balancer, or updates the existing one. Returns the status of the balancer.
func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, service *corev1.Service, nodes []*corev1.Node) (status *corev1.LoadBalancerStatus, err error) {
klog.V(4).InfoS("EnsureLoadBalancer", "cluster", clusterName, "service", klog.KObj(service))
serviceName := fmt.Sprintf("%s/%s", service.Namespace, service.Name)

if len(service.Spec.Ports) == 0 {
return nil, errors.New("requested load balancer with no ports")
Expand Down Expand Up @@ -137,6 +138,10 @@ func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, s
return nil, err
}

msg := fmt.Sprintf("Created new load balancer for service %s with algorithm '%s' and IP address %s", serviceName, lb.algorithm, lb.ipAddr)
cs.eventRecorder.Event(service, corev1.EventTypeNormal, "LoadBalancerCreated", msg)
klog.Info(msg)

if lb.ipAddr != "" && lb.ipAddr != service.Spec.LoadBalancerIP {
defer func(lb *loadBalancer) {
if err != nil {
Expand Down Expand Up @@ -214,6 +219,10 @@ func (cs *CSCloud) EnsureLoadBalancer(ctx context.Context, clusterName string, s
if _, err := lb.updateFirewallRule(lbRule.Publicipid, int(port.Port), protocol, lbSourceRanges.StringSlice()); err != nil {
return nil, err
}
} else {
msg := fmt.Sprintf("LoadBalancerSourceRanges are ignored for Service %s because this CloudStack network does not support it", serviceName)
cs.eventRecorder.Event(service, corev1.EventTypeWarning, "LoadBalancerSourceRangesIgnored", msg)
klog.Warning(msg)
}
}

Expand Down

0 comments on commit 0ccfa4c

Please sign in to comment.