Skip to content

Commit 133871a

Browse files
Add ability to add Events by OCCM (#2539)
We often find situations when it's useful to use events to warn user about implicit choices that the OCCM makes, e.g. when a provided option is ignored because Octavia does not support it. This commit adds `eventRecorder` to the `LbaasV2` interface that can be used to create events in these cases. In addition a few places when we just logged a warning are now extended to create events on Services that users will be able to read. Co-authored-by: Michał Dulko <[email protected]>
1 parent f2a57de commit 133871a

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

pkg/openstack/events.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2023 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+
17+
package openstack
18+
19+
const (
20+
eventLBForceInternal = "LoadBalancerForcedInternal"
21+
eventLBExternalNetworkSearchFailed = "LoadBalancerExternalNetworkSearchFailed"
22+
eventLBSourceRangesIgnored = "LoadBalancerSourceRangesIgnored"
23+
eventLBAZIgnored = "LoadBalancerAvailabilityZonesIgnored"
24+
)

pkg/openstack/loadbalancer.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,9 +1050,10 @@ func (lbaas *LbaasV2) ensureFloatingIP(clusterName string, service *corev1.Servi
10501050
}
10511051
klog.V(2).Infof("Successfully created floating IP %s for loadbalancer %s", floatIP.FloatingIP, lb.ID)
10521052
}
1053-
10541053
} else {
1055-
klog.Warningf("Floating network configuration not provided for Service %s, forcing to ensure an internal load balancer service", serviceName)
1054+
msg := "Floating network configuration not provided for Service %s, forcing to ensure an internal load balancer service"
1055+
lbaas.eventRecorder.Eventf(service, corev1.EventTypeWarning, eventLBForceInternal, msg, serviceName)
1056+
klog.Warningf(msg, serviceName)
10561057
}
10571058
}
10581059

@@ -1755,7 +1756,9 @@ func (lbaas *LbaasV2) checkService(service *corev1.Service, nodes []*corev1.Node
17551756
if floatingNetworkID == "" {
17561757
floatingNetworkID, err = openstackutil.GetFloatingNetworkID(lbaas.network)
17571758
if err != nil {
1758-
klog.Warningf("Failed to find floating-network-id for Service %s: %v", serviceName, err)
1759+
msg := "Failed to find floating-network-id for Service %s: %v"
1760+
lbaas.eventRecorder.Eventf(service, corev1.EventTypeWarning, eventLBExternalNetworkSearchFailed, msg, serviceName, err)
1761+
klog.Warningf(msg, serviceName, err)
17591762
}
17601763
}
17611764

@@ -1828,7 +1831,9 @@ func (lbaas *LbaasV2) checkService(service *corev1.Service, nodes []*corev1.Node
18281831
klog.V(4).Info("LoadBalancerSourceRanges will be enforced on the SG created and attached to LB members")
18291832
svcConf.allowedCIDR = sourceRanges.StringSlice()
18301833
} else {
1831-
klog.Warning("LoadBalancerSourceRanges are ignored")
1834+
msg := "LoadBalancerSourceRanges are ignored for Service %s because Octavia provider does not support it"
1835+
lbaas.eventRecorder.Eventf(service, corev1.EventTypeWarning, eventLBSourceRangesIgnored, msg, serviceName)
1836+
klog.Warningf(msg, serviceName)
18321837
}
18331838

18341839
if openstackutil.IsOctaviaFeatureSupported(lbaas.lb, openstackutil.OctaviaFeatureFlavors, lbaas.opts.LBProvider) {
@@ -1839,7 +1844,9 @@ func (lbaas *LbaasV2) checkService(service *corev1.Service, nodes []*corev1.Node
18391844
if openstackutil.IsOctaviaFeatureSupported(lbaas.lb, openstackutil.OctaviaFeatureAvailabilityZones, lbaas.opts.LBProvider) {
18401845
svcConf.availabilityZone = availabilityZone
18411846
} else if availabilityZone != "" {
1842-
klog.Warning("LoadBalancer Availability Zones aren't supported. Please, upgrade Octavia API to version 2.14 or later (Ussuri release) to use them")
1847+
msg := "LoadBalancer Availability Zones aren't supported. Please, upgrade Octavia API to version 2.14 or later (Ussuri release) to use them for Service %s"
1848+
lbaas.eventRecorder.Eventf(service, corev1.EventTypeWarning, eventLBAZIgnored, msg, serviceName)
1849+
klog.Warningf(msg, serviceName)
18431850
}
18441851

18451852
svcConf.enableMonitor = getBoolFromServiceAnnotation(service, ServiceAnnotationLoadBalancerEnableHealthMonitor, lbaas.opts.CreateMonitor)

pkg/openstack/openstack.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ import (
3737
cloudprovider "k8s.io/cloud-provider"
3838
"k8s.io/klog/v2"
3939

40+
"k8s.io/api/core/v1"
4041
"k8s.io/client-go/informers"
4142
coreinformers "k8s.io/client-go/informers/core/v1"
43+
"k8s.io/client-go/kubernetes/scheme"
44+
v1core "k8s.io/client-go/kubernetes/typed/core/v1"
45+
"k8s.io/client-go/tools/record"
4246
"k8s.io/cloud-provider-openstack/pkg/client"
4347
"k8s.io/cloud-provider-openstack/pkg/metrics"
4448
"k8s.io/cloud-provider-openstack/pkg/util"
@@ -82,11 +86,12 @@ type PortWithPortSecurity struct {
8286

8387
// LoadBalancer is used for creating and maintaining load balancers
8488
type LoadBalancer struct {
85-
secret *gophercloud.ServiceClient
86-
network *gophercloud.ServiceClient
87-
lb *gophercloud.ServiceClient
88-
opts LoadBalancerOpts
89-
kclient kubernetes.Interface
89+
secret *gophercloud.ServiceClient
90+
network *gophercloud.ServiceClient
91+
lb *gophercloud.ServiceClient
92+
opts LoadBalancerOpts
93+
kclient kubernetes.Interface
94+
eventRecorder record.EventRecorder
9095
}
9196

9297
// LoadBalancerOpts have the options to talk to Neutron LBaaSV2 or Octavia
@@ -166,6 +171,9 @@ type OpenStack struct {
166171
useV1Instances bool // TODO: v1 instance apis can be deleted after the v2 is verified enough
167172
nodeInformer coreinformers.NodeInformer
168173
nodeInformerHasSynced func() bool
174+
175+
eventBroadcaster record.EventBroadcaster
176+
eventRecorder record.EventRecorder
169177
}
170178

171179
// Config is used to read and store information from the cloud configuration file
@@ -199,6 +207,9 @@ func init() {
199207
func (os *OpenStack) Initialize(clientBuilder cloudprovider.ControllerClientBuilder, stop <-chan struct{}) {
200208
clientset := clientBuilder.ClientOrDie("cloud-controller-manager")
201209
os.kclient = clientset
210+
os.eventBroadcaster = record.NewBroadcaster()
211+
os.eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: os.kclient.CoreV1().Events("")})
212+
os.eventRecorder = os.eventBroadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: "cloud-provider-openstack"})
202213
}
203214

204215
// ReadConfig reads values from the cloud.conf
@@ -373,7 +384,7 @@ func (os *OpenStack) LoadBalancer() (cloudprovider.LoadBalancer, bool) {
373384

374385
klog.V(1).Info("Claiming to support LoadBalancer")
375386

376-
return &LbaasV2{LoadBalancer{secret, network, lb, os.lbOpts, os.kclient}}, true
387+
return &LbaasV2{LoadBalancer{secret, network, lb, os.lbOpts, os.kclient, os.eventRecorder}}, true
377388
}
378389

379390
// Zones indicates that we support zones

0 commit comments

Comments
 (0)