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

Cherry Pick #1222 [Neg crd metrics] #1229

Merged
merged 2 commits into from
Aug 25, 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
1 change: 1 addition & 0 deletions pkg/metrics/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ const (
vmIpNeg = feature("VmIpNEG")
vmIpNegLocal = feature("VmIpNegLocal")
vmIpNegCluster = feature("VmIpNegCluster")
customNamedNeg = feature("CustomNamedNEG")

l4ILBService = feature("L4ILBService")
l4ILBGlobalAccess = feature("L4ILBGlobalAccess")
Expand Down
2 changes: 2 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ func (im *ControllerMetrics) computeNegMetrics() map[feature]int {
vmIpNeg: 0,
vmIpNegLocal: 0,
vmIpNegCluster: 0,
customNamedNeg: 0,
}

for key, negState := range im.negMap {
Expand All @@ -295,6 +296,7 @@ func (im *ControllerMetrics) computeNegMetrics() map[feature]int {
counts[ingressNeg] += negState.IngressNeg
counts[asmNeg] += negState.AsmNeg
counts[neg] += negState.AsmNeg + negState.StandaloneNeg + negState.IngressNeg
counts[customNamedNeg] += negState.CustomNamedNeg
if negState.VmIpNeg != nil {
counts[neg] += 1
counts[vmIpNeg] += 1
Expand Down
43 changes: 32 additions & 11 deletions pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,12 +789,13 @@ func TestComputeNegMetrics(t *testing.T) {
vmIpNeg: 0,
vmIpNegLocal: 0,
vmIpNegCluster: 0,
customNamedNeg: 0,
},
},
{
"one neg service",
[]NegServiceState{
newNegState(0, 0, 1, nil),
newNegState(0, 0, 1, 0, nil),
},
map[feature]int{
standaloneNeg: 0,
Expand All @@ -804,12 +805,13 @@ func TestComputeNegMetrics(t *testing.T) {
vmIpNeg: 0,
vmIpNegLocal: 0,
vmIpNegCluster: 0,
customNamedNeg: 0,
},
},
{
"vm primary ip neg in traffic policy cluster mode",
[]NegServiceState{
newNegState(0, 0, 1, &VmIpNegType{trafficPolicyLocal: false}),
newNegState(0, 0, 1, 0, &VmIpNegType{trafficPolicyLocal: false}),
},
map[feature]int{
standaloneNeg: 0,
Expand All @@ -819,15 +821,32 @@ func TestComputeNegMetrics(t *testing.T) {
vmIpNeg: 1,
vmIpNegLocal: 0,
vmIpNegCluster: 1,
customNamedNeg: 0,
},
},
{
"custom named neg",
[]NegServiceState{
newNegState(1, 0, 0, 1, nil),
},
map[feature]int{
standaloneNeg: 1,
ingressNeg: 0,
asmNeg: 0,
neg: 1,
vmIpNeg: 0,
vmIpNegLocal: 0,
vmIpNegCluster: 0,
customNamedNeg: 1,
},
},
{
"many neg services",
[]NegServiceState{
newNegState(0, 0, 1, nil),
newNegState(0, 1, 0, &VmIpNegType{trafficPolicyLocal: false}),
newNegState(5, 0, 0, &VmIpNegType{trafficPolicyLocal: true}),
newNegState(5, 3, 2, nil),
newNegState(0, 0, 1, 0, nil),
newNegState(0, 1, 0, 0, &VmIpNegType{trafficPolicyLocal: false}),
newNegState(5, 0, 0, 0, &VmIpNegType{trafficPolicyLocal: true}),
newNegState(5, 3, 2, 0, nil),
},
map[feature]int{
standaloneNeg: 10,
Expand All @@ -837,6 +856,7 @@ func TestComputeNegMetrics(t *testing.T) {
vmIpNeg: 2,
vmIpNegLocal: 1,
vmIpNegCluster: 1,
customNamedNeg: 0,
},
},
} {
Expand All @@ -856,12 +876,13 @@ func TestComputeNegMetrics(t *testing.T) {
}
}

func newNegState(standalone, ingress, asm int, negType *VmIpNegType) NegServiceState {
func newNegState(standalone, ingress, asm, customNamed int, negType *VmIpNegType) NegServiceState {
return NegServiceState{
IngressNeg: ingress,
StandaloneNeg: standalone,
AsmNeg: asm,
VmIpNeg: negType,
IngressNeg: ingress,
StandaloneNeg: standalone,
AsmNeg: asm,
VmIpNeg: negType,
CustomNamedNeg: customNamed,
}
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type NegServiceState struct {
AsmNeg int
// VmIpNeg specifies if a service uses GCE_VM_IP NEG.
VmIpNeg *VmIpNegType
// CustomNamedNeg is the count of standalone negs with custom names
CustomNamedNeg int
}

// VmIpNegType contains whether a GCE_VM_IP NEG is requesting for
Expand Down
11 changes: 6 additions & 5 deletions pkg/neg/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (c *Controller) nodeWorker() {
func (c *Controller) processNode() {
defer func() {
now := c.nodeSyncTracker.Track()
metrics.LastSyncTimestamp.WithLabelValues().Set(float64(now.UTC().UnixNano()))
metrics.LastSyncTimestamp.Set(float64(now.UTC().UnixNano()))
}()
c.manager.SyncNodes()
}
Expand All @@ -342,7 +342,7 @@ func (c *Controller) processNode() {
func (c *Controller) processEndpoint(key string) {
defer func() {
now := c.syncTracker.Track()
metrics.LastSyncTimestamp.WithLabelValues().Set(float64(now.UTC().UnixNano()))
metrics.LastSyncTimestamp.Set(float64(now.UTC().UnixNano()))
}()

namespace, name, err := cache.SplitMetaNamespaceKey(key)
Expand Down Expand Up @@ -371,7 +371,7 @@ func (c *Controller) serviceWorker() {
func (c *Controller) processService(key string) error {
defer func() {
now := c.syncTracker.Track()
metrics.LastSyncTimestamp.WithLabelValues().Set(float64(now.UTC().UnixNano()))
metrics.LastSyncTimestamp.Set(float64(now.UTC().UnixNano()))
}()

namespace, name, err := cache.SplitMetaNamespaceKey(key)
Expand Down Expand Up @@ -403,7 +403,7 @@ func (c *Controller) processService(key string) error {
return err
}
negUsage.IngressNeg = len(svcPortInfoMap)
if err := c.mergeStandaloneNEGsPortInfo(service, types.NamespacedName{Namespace: namespace, Name: name}, svcPortInfoMap); err != nil {
if err := c.mergeStandaloneNEGsPortInfo(service, types.NamespacedName{Namespace: namespace, Name: name}, svcPortInfoMap, &negUsage); err != nil {
return err
}
negUsage.StandaloneNeg = len(svcPortInfoMap) - negUsage.IngressNeg
Expand Down Expand Up @@ -470,7 +470,7 @@ func (c *Controller) mergeIngressPortInfo(service *apiv1.Service, name types.Nam
}

// mergeStandaloneNEGsPortInfo merge Standalone NEG PortInfo into portInfoMap
func (c *Controller) mergeStandaloneNEGsPortInfo(service *apiv1.Service, name types.NamespacedName, portInfoMap negtypes.PortInfoMap) error {
func (c *Controller) mergeStandaloneNEGsPortInfo(service *apiv1.Service, name types.NamespacedName, portInfoMap negtypes.PortInfoMap, negUsage *usage.NegServiceState) error {
negAnnotation, foundNEGAnnotation, err := annotations.FromService(service).NEGAnnotation()
if err != nil {
return err
Expand Down Expand Up @@ -502,6 +502,7 @@ func (c *Controller) mergeStandaloneNEGsPortInfo(service *apiv1.Service, name ty
if negAnnotation.NEGEnabledForIngress() && len(customNames) != 0 {
return fmt.Errorf("configuration for negs in service (%s) is invalid, custom neg name cannot be used with ingress enabled", name.String())
}
negUsage.CustomNamedNeg = len(customNames)

if err := portInfoMap.Merge(negtypes.NewPortInfoMap(name.Namespace, name.Name, exposedNegSvcPort, c.namer /*readinessGate*/, true, customNames)); err != nil {
return fmt.Errorf("failed to merge service ports exposed as standalone NEGs (%v) into ingress referenced service ports (%v): %v", exposedNegSvcPort, portInfoMap, err)
Expand Down
9 changes: 8 additions & 1 deletion pkg/neg/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"reflect"
"sync"
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
Expand All @@ -36,6 +37,7 @@ import (
"k8s.io/client-go/tools/record"
negv1beta1 "k8s.io/ingress-gce/pkg/apis/svcneg/v1beta1"
"k8s.io/ingress-gce/pkg/flags"
"k8s.io/ingress-gce/pkg/neg/metrics"
"k8s.io/ingress-gce/pkg/neg/readiness"
negsyncer "k8s.io/ingress-gce/pkg/neg/syncers"
negtypes "k8s.io/ingress-gce/pkg/neg/types"
Expand Down Expand Up @@ -108,6 +110,7 @@ func newSyncerManager(namer negtypes.NetworkEndpointGroupNamer, recorder record.
func (manager *syncerManager) EnsureSyncers(namespace, name string, newPorts negtypes.PortInfoMap) error {
manager.mu.Lock()
defer manager.mu.Unlock()
start := time.Now()
key := getServiceKey(namespace, name)
currentPorts, ok := manager.svcPortMap[key]
if !ok {
Expand Down Expand Up @@ -177,7 +180,9 @@ func (manager *syncerManager) EnsureSyncers(namespace, name string, newPorts neg
}
}
}
return utilerrors.NewAggregate(errList)
err := utilerrors.NewAggregate(errList)
metrics.PublishNegManagerProcessMetrics(metrics.SyncProcess, err, start)
return err
}

// StopSyncer stops all syncers for the input service.
Expand Down Expand Up @@ -236,6 +241,7 @@ func (manager *syncerManager) ShutDown() {
func (manager *syncerManager) GC() error {
klog.V(2).Infof("Start NEG garbage collection.")
defer klog.V(2).Infof("NEG garbage collection finished.")
start := time.Now()
// Garbage collect Syncers
manager.garbageCollectSyncer()

Expand All @@ -249,6 +255,7 @@ func (manager *syncerManager) GC() error {
if err != nil {
err = fmt.Errorf("failed to garbage collect negs: %v", err)
}
metrics.PublishNegManagerProcessMetrics(metrics.GCProcess, err, start)
return err
}

Expand Down
Loading