2018-04-09 16:35:14 +00:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-11 16:17:59 +00:00
|
|
|
"github.com/go-kit/log"
|
2021-08-15 23:34:36 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-04-09 16:35:14 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2021-08-15 23:34:36 +00:00
|
|
|
"k8s.io/apimachinery/pkg/version"
|
|
|
|
fakediscovery "k8s.io/client-go/discovery/fake"
|
2018-04-09 16:35:14 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/kubernetes/fake"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2020-03-22 23:23:44 +00:00
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2020-03-22 23:23:44 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2018-04-09 16:35:14 +00:00
|
|
|
)
|
|
|
|
|
2020-07-27 08:38:08 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
testutil.TolerantVerifyLeak(m)
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:35:14 +00:00
|
|
|
// makeDiscovery creates a kubernetes.Discovery instance for testing.
|
2019-01-28 15:42:25 +00:00
|
|
|
func makeDiscovery(role Role, nsDiscovery NamespaceDiscovery, objects ...runtime.Object) (*Discovery, kubernetes.Interface) {
|
2021-08-15 23:34:36 +00:00
|
|
|
return makeDiscoveryWithVersion(role, nsDiscovery, "v1.22.0", objects...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeDiscoveryWithVersion creates a kubernetes.Discovery instance with the specified kubernetes version for testing.
|
|
|
|
func makeDiscoveryWithVersion(role Role, nsDiscovery NamespaceDiscovery, k8sVer string, objects ...runtime.Object) (*Discovery, kubernetes.Interface) {
|
2018-04-09 16:35:14 +00:00
|
|
|
clientset := fake.NewSimpleClientset(objects...)
|
2021-08-15 23:34:36 +00:00
|
|
|
fakeDiscovery, _ := clientset.Discovery().(*fakediscovery.FakeDiscovery)
|
|
|
|
fakeDiscovery.FakedServerVersion = &version.Info{GitVersion: k8sVer}
|
2019-01-28 15:42:25 +00:00
|
|
|
|
2018-04-09 16:35:14 +00:00
|
|
|
return &Discovery{
|
|
|
|
client: clientset,
|
|
|
|
logger: log.NewNopLogger(),
|
|
|
|
role: role,
|
|
|
|
namespaceDiscovery: &nsDiscovery,
|
2019-01-28 15:42:25 +00:00
|
|
|
}, clientset
|
2018-04-09 16:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type k8sDiscoveryTest struct {
|
|
|
|
// discovery is instance of discovery.Discoverer
|
2020-08-20 12:48:26 +00:00
|
|
|
discovery discovery.Discoverer
|
2018-04-09 16:35:14 +00:00
|
|
|
// beforeRun runs before discoverer run
|
|
|
|
beforeRun func()
|
|
|
|
// afterStart runs after discoverer has synced
|
|
|
|
afterStart func()
|
|
|
|
// expectedMaxItems is expected max items we may get from channel
|
|
|
|
expectedMaxItems int
|
|
|
|
// expectedRes is expected final result
|
|
|
|
expectedRes map[string]*targetgroup.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d k8sDiscoveryTest) Run(t *testing.T) {
|
2019-04-10 12:21:42 +00:00
|
|
|
t.Helper()
|
2018-04-09 16:35:14 +00:00
|
|
|
ch := make(chan []*targetgroup.Group)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if d.beforeRun != nil {
|
|
|
|
d.beforeRun()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run discoverer and start a goroutine to read results.
|
|
|
|
go d.discovery.Run(ctx, ch)
|
2020-02-18 08:24:48 +00:00
|
|
|
|
|
|
|
// Ensure that discovery has a discoverer set. This prevents a race
|
|
|
|
// condition where the above go routine may or may not have set a
|
|
|
|
// discoverer yet.
|
|
|
|
for {
|
|
|
|
dis := d.discovery.(*Discovery)
|
|
|
|
dis.RLock()
|
|
|
|
l := len(dis.discoverers)
|
|
|
|
dis.RUnlock()
|
|
|
|
if l > 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2018-04-09 16:35:14 +00:00
|
|
|
resChan := make(chan map[string]*targetgroup.Group)
|
2018-04-25 16:36:22 +00:00
|
|
|
go readResultWithTimeout(t, ch, d.expectedMaxItems, time.Second, resChan)
|
2018-04-09 16:35:14 +00:00
|
|
|
|
2018-04-09 19:23:04 +00:00
|
|
|
dd, ok := d.discovery.(hasSynced)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("discoverer does not implement hasSynced interface")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !cache.WaitForCacheSync(ctx.Done(), dd.hasSynced) {
|
|
|
|
t.Errorf("discoverer failed to sync: %v", dd)
|
|
|
|
return
|
2018-04-09 16:35:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.afterStart != nil {
|
|
|
|
d.afterStart()
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.expectedRes != nil {
|
|
|
|
res := <-resChan
|
|
|
|
requireTargetGroups(t, d.expectedRes, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 22:49:29 +00:00
|
|
|
// readResultWithTimeout reads all targetgroups from channel with timeout.
|
|
|
|
// It merges targetgroups by source and sends the result to result channel.
|
2018-04-25 16:36:22 +00:00
|
|
|
func readResultWithTimeout(t *testing.T, ch <-chan []*targetgroup.Group, max int, timeout time.Duration, resChan chan<- map[string]*targetgroup.Group) {
|
2020-06-11 14:02:09 +00:00
|
|
|
res := make(map[string]*targetgroup.Group)
|
2018-04-09 16:35:14 +00:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
2020-06-11 14:02:09 +00:00
|
|
|
for _, tg := range tgs {
|
|
|
|
if tg == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res[tg.Source] = tg
|
|
|
|
}
|
|
|
|
if len(res) == max {
|
2018-04-09 16:35:14 +00:00
|
|
|
// Reached max target groups we may get, break fast.
|
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
case <-time.After(timeout):
|
|
|
|
// Because we use queue, an object that is created then
|
|
|
|
// deleted or updated may be processed only once.
|
2018-12-18 10:52:40 +00:00
|
|
|
// So possibly we may skip events, timed out here.
|
2020-06-11 14:02:09 +00:00
|
|
|
t.Logf("timed out, got %d (max: %d) items, some events are skipped", len(res), max)
|
2018-04-09 16:35:14 +00:00
|
|
|
break Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resChan <- res
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireTargetGroups(t *testing.T, expected, res map[string]*targetgroup.Group) {
|
2019-04-10 12:21:42 +00:00
|
|
|
t.Helper()
|
2018-04-09 16:35:14 +00:00
|
|
|
b1, err := json.Marshal(expected)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
b2, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Equal(t, string(b1), string(b2))
|
2018-04-09 16:35:14 +00:00
|
|
|
}
|
2018-04-09 19:23:04 +00:00
|
|
|
|
|
|
|
type hasSynced interface {
|
2018-04-10 08:44:24 +00:00
|
|
|
// hasSynced returns true if all informers synced.
|
|
|
|
// This is only used in testing to determine when discoverer synced to
|
|
|
|
// kubernetes apiserver.
|
2018-04-09 19:23:04 +00:00
|
|
|
hasSynced() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ hasSynced = &Discovery{}
|
|
|
|
var _ hasSynced = &Node{}
|
|
|
|
var _ hasSynced = &Endpoints{}
|
2020-02-16 14:59:29 +00:00
|
|
|
var _ hasSynced = &EndpointSlice{}
|
2018-04-09 19:23:04 +00:00
|
|
|
var _ hasSynced = &Ingress{}
|
|
|
|
var _ hasSynced = &Pod{}
|
|
|
|
var _ hasSynced = &Service{}
|
|
|
|
|
|
|
|
func (d *Discovery) hasSynced() bool {
|
|
|
|
d.RLock()
|
|
|
|
defer d.RUnlock()
|
|
|
|
for _, discoverer := range d.discoverers {
|
|
|
|
if hasSynceddiscoverer, ok := discoverer.(hasSynced); ok {
|
|
|
|
if !hasSynceddiscoverer.hasSynced() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) hasSynced() bool {
|
|
|
|
return n.informer.HasSynced()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Endpoints) hasSynced() bool {
|
|
|
|
return e.endpointsInf.HasSynced() && e.serviceInf.HasSynced() && e.podInf.HasSynced()
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:59:29 +00:00
|
|
|
func (e *EndpointSlice) hasSynced() bool {
|
|
|
|
return e.endpointSliceInf.HasSynced() && e.serviceInf.HasSynced() && e.podInf.HasSynced()
|
|
|
|
}
|
|
|
|
|
2018-04-09 19:23:04 +00:00
|
|
|
func (i *Ingress) hasSynced() bool {
|
|
|
|
return i.informer.HasSynced()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Pod) hasSynced() bool {
|
|
|
|
return p.informer.HasSynced()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) hasSynced() bool {
|
|
|
|
return s.informer.HasSynced()
|
|
|
|
}
|
2021-08-15 23:34:36 +00:00
|
|
|
|
|
|
|
func TestRetryOnError(t *testing.T) {
|
|
|
|
for _, successAt := range []int{1, 2, 3} {
|
|
|
|
var called int
|
|
|
|
f := func() error {
|
|
|
|
called++
|
|
|
|
if called >= successAt {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New("dummy")
|
|
|
|
}
|
|
|
|
retryOnError(context.TODO(), 0, f)
|
|
|
|
require.Equal(t, successAt, called)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckNetworkingV1Supported(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
version string
|
|
|
|
wantSupported bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{version: "v1.18.0", wantSupported: false, wantErr: false},
|
|
|
|
{version: "v1.18.1", wantSupported: false, wantErr: false},
|
|
|
|
// networking v1 is supported since Kubernetes v1.19
|
|
|
|
{version: "v1.19.0", wantSupported: true, wantErr: false},
|
|
|
|
{version: "v1.20.0-beta.2", wantSupported: true, wantErr: false},
|
|
|
|
// error patterns
|
|
|
|
{version: "", wantSupported: false, wantErr: true},
|
|
|
|
{version: "<>", wantSupported: false, wantErr: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.version, func(t *testing.T) {
|
|
|
|
clientset := fake.NewSimpleClientset()
|
|
|
|
fakeDiscovery, _ := clientset.Discovery().(*fakediscovery.FakeDiscovery)
|
|
|
|
fakeDiscovery.FakedServerVersion = &version.Info{GitVersion: tc.version}
|
|
|
|
supported, err := checkNetworkingV1Supported(clientset)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
require.Equal(t, tc.wantSupported, supported)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|