| 1 |
/* |
| 2 |
Copyright 2024 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 |
package agent |
| 17 |
|
| 18 |
import ( |
| 19 |
"context" |
| 20 |
"errors" |
| 21 |
"time" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/labels" |
| 24 |
"k8s.io/apimachinery/pkg/runtime" |
| 25 |
"k8s.io/apimachinery/pkg/watch" |
| 26 |
"k8s.io/client-go/kubernetes" |
| 27 |
"k8s.io/client-go/tools/cache" |
| 28 |
"k8s.io/klog/v2" |
| 29 |
"k8s.io/utils/clock" |
| 30 |
"sigs.k8s.io/apiserver-network-proxy/pkg/agent/metrics" |
| 31 |
"sigs.k8s.io/apiserver-network-proxy/pkg/util" |
| 32 |
|
| 33 |
coordinationv1api "k8s.io/api/coordination/v1" |
| 34 |
apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 35 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 36 |
coordinationv1lister "k8s.io/client-go/listers/coordination/v1" |
| 37 |
) |
| 38 |
|
| 39 |
// A ServerLeaseCounter counts leases in the k8s apiserver to determine the |
| 40 |
// current proxy server count. |
| 41 |
type ServerLeaseCounter struct { |
| 42 |
leaseLister coordinationv1lister.LeaseLister |
| 43 |
selector labels.Selector |
| 44 |
fallbackCount int |
| 45 |
pc clock.PassiveClock |
| 46 |
} |
| 47 |
|
| 48 |
// NewServerLeaseCounter creates a server counter that counts valid leases that match the label |
| 49 |
// selector and provides the fallback count (initially 0) if this fails. |
| 50 |
func NewServerLeaseCounter(pc clock.PassiveClock, leaseLister coordinationv1lister.LeaseLister, labelSelector labels.Selector) *ServerLeaseCounter { |
| 51 |
return &ServerLeaseCounter{ |
| 52 |
leaseLister: leaseLister, |
| 53 |
selector: labelSelector, |
| 54 |
fallbackCount: 1, |
| 55 |
pc: pc, |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Count counts the number of leases in the apiserver matching the provided |
| 60 |
// label selector. |
| 61 |
// |
| 62 |
// In the event that no valid leases are found or lease listing fails, the |
| 63 |
// fallback count is returned. This fallback count is updated upon successful |
| 64 |
// discovery of valid leases. |
| 65 |
func (lc *ServerLeaseCounter) Count() int { |
| 66 |
leases, err := lc.leaseLister.List(lc.selector) |
| 67 |
if err != nil { |
| 68 |
klog.Errorf("Could not list leases to update server count, using fallback count (%v): %v", lc.fallbackCount, err) |
| 69 |
|
| 70 |
return lc.fallbackCount |
| 71 |
} |
| 72 |
|
| 73 |
count := 0 |
| 74 |
for _, lease := range leases { |
| 75 |
if util.IsLeaseValid(lc.pc, *lease) { |
| 76 |
count++ |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
// Ensure returned count is always at least 1. |
| 81 |
if count == 0 { |
| 82 |
klog.Warningf("No valid leases found, assuming server count of 1") |
| 83 |
count = 1 |
| 84 |
} |
| 85 |
|
| 86 |
if count != lc.fallbackCount { |
| 87 |
lc.fallbackCount = count |
| 88 |
} |
| 89 |
|
| 90 |
return count |
| 91 |
} |
| 92 |
|
| 93 |
func NewLeaseInformerWithMetrics(client kubernetes.Interface, namespace string, resyncPeriod time.Duration) cache.SharedIndexInformer { |
| 94 |
lw := &cache.ListWatch{ |
| 95 |
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { |
| 96 |
start := time.Now() |
| 97 |
httpCode := 200 |
| 98 |
defer func() { |
| 99 |
latency := time.Now().Sub(start) |
| 100 |
metrics.Metrics.ObserveLeaseListLatency(latency, httpCode) |
| 101 |
}() |
| 102 |
obj, err := client.CoordinationV1().Leases(namespace).List(context.TODO(), options) |
| 103 |
if err != nil { |
| 104 |
klog.Errorf("Could not list leases: %v", err) |
| 105 |
|
| 106 |
var apiStatus apierrors.APIStatus |
| 107 |
if errors.As(err, &apiStatus) { |
| 108 |
status := apiStatus.Status() |
| 109 |
httpCode = int(status.Code) |
| 110 |
metrics.Metrics.ObserveLeaseList(int(status.Code), string(status.Reason)) |
| 111 |
} else { |
| 112 |
klog.Errorf("Lease list error could not be logged to metrics as it is not an APIStatus: %v", err) |
| 113 |
} |
| 114 |
return nil, err |
| 115 |
} |
| 116 |
|
| 117 |
metrics.Metrics.ObserveLeaseList(200, "") |
| 118 |
return obj, nil |
| 119 |
}, |
| 120 |
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { |
| 121 |
obj, err := client.CoordinationV1().Leases(namespace).Watch(context.TODO(), options) |
| 122 |
if err != nil { |
| 123 |
klog.Errorf("Could not watch leases: %v", err) |
| 124 |
|
| 125 |
var apiStatus apierrors.APIStatus |
| 126 |
if errors.As(err, &apiStatus) { |
| 127 |
status := apiStatus.Status() |
| 128 |
metrics.Metrics.ObserveLeaseWatch(int(status.Code), string(status.Reason)) |
| 129 |
} else { |
| 130 |
klog.Errorf("Lease watch error could not be logged to metrics as it is not an APIStatus: %v", err) |
| 131 |
} |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
|
| 135 |
metrics.Metrics.ObserveLeaseWatch(200, "") |
| 136 |
return obj, nil |
| 137 |
}, |
| 138 |
} |
| 139 |
return cache.NewSharedIndexInformer( |
| 140 |
cache.ToListWatcherWithWatchListSemantics(lw, client), |
| 141 |
&coordinationv1api.Lease{}, |
| 142 |
resyncPeriod, |
| 143 |
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}, |
| 144 |
) |
| 145 |
} |