Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute number of L4 ILBs in error and success state #1176

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions pkg/l4/l4controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
Expand All @@ -31,6 +32,7 @@ import (
"k8s.io/ingress-gce/pkg/context"
"k8s.io/ingress-gce/pkg/controller/translator"
"k8s.io/ingress-gce/pkg/loadbalancers"
"k8s.io/ingress-gce/pkg/metrics"
negtypes "k8s.io/ingress-gce/pkg/neg/types"
"k8s.io/ingress-gce/pkg/utils"
"k8s.io/ingress-gce/pkg/utils/common"
Expand Down Expand Up @@ -132,18 +134,26 @@ func (l4c *L4Controller) processServiceCreateOrUpdate(key string, service *v1.Se
return nil
}

var serviceMetricsState metrics.L4ILBServiceState
// Mark the service InSuccess state as false to begin with.
// This will be updated to true if the VIP is configured successfully.
serviceMetricsState.InSuccess = false
defer func() {
l4c.ctx.ControllerMetrics.SetL4ILBService(types.NamespacedName{Name: service.Name, Namespace: service.Namespace}.String(), serviceMetricsState)
}()

// Ensure v2 finalizer
if err := common.EnsureServiceFinalizer(service, common.ILBFinalizerV2, l4c.ctx.KubeClient); err != nil {
return fmt.Errorf("Failed to attach finalizer to service %s/%s, err %v", service.Namespace, service.Name, err)
}
l4 := loadbalancers.NewL4Handler(service, l4c.ctx.Cloud, meta.Regional, l4c.ctx.ClusterNamer, l4c.ctx.Recorder(service.Namespace), &l4c.sharedResourcesLock, l4c.ctx.ControllerMetrics)
l4 := loadbalancers.NewL4Handler(service, l4c.ctx.Cloud, meta.Regional, l4c.ctx.ClusterNamer, l4c.ctx.Recorder(service.Namespace), &l4c.sharedResourcesLock)
nodeNames, err := utils.GetReadyNodeNames(l4c.nodeLister)
if err != nil {
return err
}
// Use the same function for both create and updates. If controller crashes and restarts,
// all existing services will show up as Service Adds.
status, err := l4.EnsureInternalLoadBalancer(nodeNames, service)
status, err := l4.EnsureInternalLoadBalancer(nodeNames, service, &serviceMetricsState)
if err != nil {
l4c.ctx.Recorder(service.Namespace).Eventf(service, v1.EventTypeWarning, "SyncLoadBalancerFailed",
"Error syncing load balancer: %v", err)
Expand Down Expand Up @@ -172,7 +182,7 @@ func (l4c *L4Controller) processServiceCreateOrUpdate(key string, service *v1.Se
}

func (l4c *L4Controller) processServiceDeletion(key string, svc *v1.Service) error {
l4 := loadbalancers.NewL4Handler(svc, l4c.ctx.Cloud, meta.Regional, l4c.ctx.ClusterNamer, l4c.ctx.Recorder(svc.Namespace), &l4c.sharedResourcesLock, l4c.ctx.ControllerMetrics)
l4 := loadbalancers.NewL4Handler(svc, l4c.ctx.Cloud, meta.Regional, l4c.ctx.ClusterNamer, l4c.ctx.Recorder(svc.Namespace), &l4c.sharedResourcesLock)
l4c.ctx.Recorder(svc.Namespace).Eventf(svc, v1.EventTypeNormal, "DeletingLoadBalancer", "Deleting load balancer for %s", key)
if err := l4.EnsureInternalLoadBalancerDeleted(svc); err != nil {
l4c.ctx.Recorder(svc.Namespace).Eventf(svc, v1.EventTypeWarning, "DeleteLoadBalancerFailed", "Error deleting load balancer: %v", err)
Expand All @@ -183,6 +193,11 @@ func (l4c *L4Controller) processServiceDeletion(key string, svc *v1.Service) err
"Error removing finalizer from load balancer: %v", err)
return err
}

namespacedName := types.NamespacedName{Name: svc.Name, Namespace: svc.Namespace}
klog.V(6).Infof("Internal L4 Loadbalancer for Service %s deleted, removing its state from metrics cache", namespacedName)
l4c.ctx.ControllerMetrics.DeleteL4ILBService(namespacedName.String())

// Reset the loadbalancer status, Ignore NotFound error since the service can already be deleted at this point.
if err := l4c.updateServiceStatus(svc, &v1.LoadBalancerStatus{}); utils.IgnoreHTTPNotFound(err) != nil {
l4c.ctx.Recorder(svc.Namespace).Eventf(svc, v1.EventTypeWarning, "DeleteLoadBalancer",
Expand Down
23 changes: 8 additions & 15 deletions pkg/loadbalancers/l4.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ type L4 struct {
ServicePort utils.ServicePort
NamespacedName types.NamespacedName
sharedResourcesLock *sync.Mutex
// metricsCollector exports L4 ILB service controller usage metrics.
metricsCollector metrics.L4ILBMetricsCollector
}

// NewL4Handler creates a new L4Handler for the given L4 service.
func NewL4Handler(service *corev1.Service, cloud *gce.Cloud, scope meta.KeyType, namer *namer.Namer, recorder record.EventRecorder, lock *sync.Mutex, collector metrics.L4ILBMetricsCollector) *L4 {
l := &L4{cloud: cloud, scope: scope, namer: namer, recorder: recorder, Service: service, sharedResourcesLock: lock, metricsCollector: collector}
func NewL4Handler(service *corev1.Service, cloud *gce.Cloud, scope meta.KeyType, namer *namer.Namer, recorder record.EventRecorder, lock *sync.Mutex) *L4 {
l := &L4{cloud: cloud, scope: scope, namer: namer, recorder: recorder, Service: service, sharedResourcesLock: lock}
l.NamespacedName = types.NamespacedName{Name: service.Name, Namespace: service.Namespace}
l.backendPool = backends.NewPool(l.cloud, l.namer)
l.ServicePort = utils.ServicePort{ID: utils.ServicePortID{Service: l.NamespacedName}, BackendNamer: l.namer,
Expand Down Expand Up @@ -146,10 +144,6 @@ func (l *L4) EnsureInternalLoadBalancerDeleted(svc *corev1.Service) error {
// This will be hit if this is a shared healthcheck.
klog.V(2).Infof("Failed to delete healthcheck %s: health check in use.", hcName)
}
if retErr == nil {
klog.V(6).Infof("Service %s deleted, removing its state from metrics cache", l.NamespacedName.String())
l.metricsCollector.DeleteL4ILBService(l.NamespacedName.String())
}
return retErr
}

Expand All @@ -168,7 +162,7 @@ func (l *L4) getFRNameWithProtocol(protocol string) string {

// EnsureInternalLoadBalancer ensures that all GCE resources for the given loadbalancer service have
// been created. It returns a LoadBalancerStatus with the updated ForwardingRule IP address.
func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service) (*corev1.LoadBalancerStatus, error) {
func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service, metricsState *metrics.L4ILBServiceState) (*corev1.LoadBalancerStatus, error) {
// Use the same resource name for NEG, BackendService as well as FR, FWRule.
l.Service = svc
name := l.namer.VMIPNEG(l.Service.Namespace, l.Service.Name)
Expand Down Expand Up @@ -252,18 +246,17 @@ func (l *L4) EnsureInternalLoadBalancer(nodeNames []string, svc *corev1.Service)
return nil, err
}

var serviceState metrics.L4ILBServiceState
metricsState.InSuccess = true
if options.AllowGlobalAccess {
serviceState.EnabledGlobalAccess = true
metricsState.EnabledGlobalAccess = true
}
// SubnetName is overrided to nil value if Alpha feature gate for custom subnet
// SubnetName is overwritten to nil value if Alpha feature gate for custom subnet
// is not enabled. So, a non empty subnet name at this point implies that the
// feature is in use.
if options.SubnetName != "" {
serviceState.EnabledCustomSubnet = true
metricsState.EnabledCustomSubnet = true
}
klog.V(6).Infof("Service %s ensured, updating its state %v in metrics cache", l.NamespacedName.String(), serviceState)
l.metricsCollector.SetL4ILBService(l.NamespacedName.String(), serviceState)
klog.V(6).Infof("Internal L4 Loadbalancer for Service %s ensured, updating its state %v in metrics cache", l.NamespacedName, metricsState)

return &corev1.LoadBalancerStatus{Ingress: []corev1.LoadBalancerIngress{{IP: fr.IPAddress}}}, nil
}
Loading