-
Notifications
You must be signed in to change notification settings - Fork 44
/
environment.go
135 lines (115 loc) · 3.75 KB
/
environment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
Portions Copyright (c) Microsoft Corporation.
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 common
import (
"context"
"fmt"
"log"
"os"
"strconv"
"testing"
"time"
"github.com/onsi/gomega"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
loggingtesting "knative.dev/pkg/logging/testing"
"knative.dev/pkg/system"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/Azure/karpenter-provider-azure/pkg/apis"
coreapis "sigs.k8s.io/karpenter/pkg/apis"
corev1beta1 "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
"sigs.k8s.io/karpenter/pkg/operator"
)
type ContextKey string
const (
GitRefContextKey = ContextKey("gitRef")
)
type Environment struct {
context.Context
cancel context.CancelFunc
Client client.Client
Config *rest.Config
KubeClient kubernetes.Interface
Monitor *Monitor
StartingNodeCount int
}
func NewEnvironment(t *testing.T) *Environment {
ctx := loggingtesting.TestContextWithLogger(t)
ctx, cancel := context.WithCancel(ctx)
config := NewConfig()
client := NewClient(ctx, config)
lo.Must0(os.Setenv(system.NamespaceEnvKey, "karpenter"))
if val, ok := os.LookupEnv("GIT_REF"); ok {
ctx = context.WithValue(ctx, GitRefContextKey, val)
}
gomega.SetDefaultEventuallyTimeout(5 * time.Minute)
gomega.SetDefaultEventuallyPollingInterval(1 * time.Second)
return &Environment{
Context: ctx,
cancel: cancel,
Config: config,
Client: client,
KubeClient: kubernetes.NewForConfigOrDie(config),
Monitor: NewMonitor(ctx, client),
}
}
func (env *Environment) Stop() {
env.cancel()
}
func NewConfig() *rest.Config {
config := controllerruntime.GetConfigOrDie()
config.UserAgent = fmt.Sprintf("testing-%s", operator.Version)
config.QPS = 1e6
config.Burst = 1e6
return config
}
func NewClient(ctx context.Context, config *rest.Config) client.Client {
scheme := runtime.NewScheme()
lo.Must0(clientgoscheme.AddToScheme(scheme))
lo.Must0(apis.AddToScheme(scheme))
lo.Must0(coreapis.AddToScheme(scheme))
cache := lo.Must(cache.New(config, cache.Options{Scheme: scheme}))
lo.Must0(cache.IndexField(ctx, &v1.Pod{}, "spec.nodeName", func(o client.Object) []string {
pod := o.(*v1.Pod)
return []string{pod.Spec.NodeName}
}))
lo.Must0(cache.IndexField(ctx, &v1.Event{}, "involvedObject.kind", func(o client.Object) []string {
evt := o.(*v1.Event)
return []string{evt.InvolvedObject.Kind}
}))
lo.Must0(cache.IndexField(ctx, &v1.Node{}, "spec.unschedulable", func(o client.Object) []string {
node := o.(*v1.Node)
return []string{strconv.FormatBool(node.Spec.Unschedulable)}
}))
lo.Must0(cache.IndexField(ctx, &v1.Node{}, "spec.taints[*].karpenter.sh/disruption", func(o client.Object) []string {
node := o.(*v1.Node)
t, _ := lo.Find(node.Spec.Taints, func(t v1.Taint) bool {
return t.Key == corev1beta1.DisruptionTaintKey
})
return []string{t.Value}
}))
c := lo.Must(client.New(config, client.Options{Scheme: scheme, Cache: &client.CacheOptions{Reader: cache}}))
go func() {
lo.Must0(cache.Start(ctx))
}()
if !cache.WaitForCacheSync(ctx) {
log.Fatalf("cache failed to sync")
}
return c
}