forked from gardener/gardener
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (135 loc) · 4.63 KB
/
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 main
import (
"context"
"fmt"
"os"
"path"
"strings"
"github.com/go-logr/logr"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
gardenerenvtest "github.com/gardener/gardener/pkg/envtest"
"github.com/gardener/gardener/pkg/logger"
)
const name = "start-envtest"
func main() {
opts := &options{}
cmd := &cobra.Command{
Use: name,
Short: "Launch an envtest environment",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.validate(); err != nil {
return err
}
log, err := logger.NewZapLogger(logger.DebugLevel, logger.FormatText)
if err != nil {
return fmt.Errorf("error instantiating zap logger: %w", err)
}
logf.SetLogger(log)
klog.SetLogger(log)
log = logf.Log.WithName(name)
// don't output usage on further errors raised during execution
cmd.SilenceUsage = true
// further errors will be logged properly, don't duplicate
cmd.SilenceErrors = true
return run(cmd.Context(), log, opts)
},
}
opts.addFlags(cmd.Flags())
if err := cmd.ExecuteContext(signals.SetupSignalHandler()); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
const (
typeKubernetes = "kubernetes"
typeGardener = "gardener"
)
var supportedTypes = sets.New(typeKubernetes, typeGardener)
type options struct {
environmentType string
kubeconfig string
}
func (o *options) addFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.environmentType, "environment-type", typeKubernetes, fmt.Sprintf("Type of environment to start. Supported values: %s", strings.Join(sets.List(supportedTypes), ", ")))
fs.StringVar(&o.kubeconfig, "kubeconfig", path.Join("..", "..", "dev", "envtest-kubeconfig.yaml"), "File to place the environment's admin kubeconfig in.")
}
func (o *options) validate() error {
if !supportedTypes.Has(o.environmentType) {
return fmt.Errorf("unsupported environment type %q, supported types are: %s", o.environmentType, strings.Join(sets.List(supportedTypes), ", "))
}
return nil
}
type testEnvironment interface {
Start() (*rest.Config, error)
Stop() error
}
func run(ctx context.Context, log logr.Logger, opts *options) error {
log.Info("Starting test environment", "type", opts.environmentType)
kubeEnvironment := &envtest.Environment{}
var testEnv testEnvironment = kubeEnvironment
if opts.environmentType == typeGardener {
testEnv = &gardenerenvtest.GardenerTestEnvironment{
Environment: kubeEnvironment,
GardenerAPIServer: &gardenerenvtest.GardenerAPIServer{
Args: []string{"--disable-admission-plugins="},
},
}
}
_, err := testEnv.Start()
if err != nil {
return fmt.Errorf("error starting test environment: %w", err)
}
defer func() {
log.Info("Stopping test environment")
if err := testEnv.Stop(); err != nil {
log.Error(err, "Error stopping test environment")
}
}()
adminUser, err := kubeEnvironment.ControlPlane.AddUser(envtest.User{
Name: "envtest-admin",
Groups: []string{"system:masters"},
}, nil)
if err != nil {
return fmt.Errorf("error adding admin user: %w", err)
}
kubeConfigBytes, err := adminUser.KubeConfig()
if err != nil {
return fmt.Errorf("error getting admin user kubeconfig: %w", err)
}
if err := os.WriteFile(opts.kubeconfig, kubeConfigBytes, 0600); err != nil {
return fmt.Errorf("error writing kubeconfig file: %w", err)
}
log.Info("Successfully written kubeconfig", "file", opts.kubeconfig)
defer func() {
log.Info("Cleaning up kubeconfig file", "file", opts.kubeconfig)
if err := os.Remove(opts.kubeconfig); err != nil {
log.Error(err, "Error cleaning up kubeconfig file", "file", opts.kubeconfig)
}
}()
log.Info("Test environment ready!")
// block until cancelled
<-ctx.Done()
log.Info("Stop procedure initiated")
return nil
}