/
interfaces.go
244 lines (205 loc) · 7.45 KB
/
interfaces.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright 2018 Heptio Inc.
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 client
import (
"io"
"time"
"github.com/heptio/sonobuoy/pkg/config"
"github.com/heptio/sonobuoy/pkg/plugin/aggregation"
"github.com/heptio/sonobuoy/pkg/plugin/manifest"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// ConfigValidator allows the command configurations to be validated.
type validator interface {
Validate() error
}
// LogConfig are the input options for viewing a Sonobuoy run's logs.
type LogConfig struct {
// Follow determines if the logs should be followed or not (tail -f).
Follow bool
// Namespace is the namespace the sonobuoy aggregator is running in.
Namespace string
// Plugin is the name of the plugin to show the logs of.
Plugin string
// Out is the writer to write to.
Out io.Writer
}
// Validate checks the config to determine if it is valid.
func (lc *LogConfig) Validate() error {
if lc.Namespace == "" {
return errors.New("namespace cannot be empty")
}
return nil
}
// GenConfig are the input options for generating a Sonobuoy manifest.
type GenConfig struct {
E2EConfig *E2EConfig
Config *config.Config
EnableRBAC bool
ImagePullPolicy string
KubeConformanceImage string
SSHKeyPath string
SSHUser string
// DynamicPlugins are plugins which we know by name and whose manifest
// YAML are generated dynamically using the GenConfig settings.
DynamicPlugins []string
// StaticPlugins are plugins whose manifest YAML has been provided
// explicitly and will be written without further consideration of other
// GenConfig settings.
StaticPlugins []*manifest.Manifest
// PluginEnvOverrides are mappings between plugin name and k-v pairs to be
// set as env vars on the given plugin. If a plugin has overrides set, it
// will completely override all other env vars set on the plugin. Provided
// out of band from the plugins because of how the dynamic plugins are not
// yet able to be manipulated in this way.
PluginEnvOverrides map[string]map[string]string
// ShowDefaultPodSpec determines whether or not the default pod spec for
// the plugin should be incuded in the output.
ShowDefaultPodSpec bool
}
// Validate checks the config to determine if it is valid.
func (gc *GenConfig) Validate() error {
if gc.E2EConfig == nil {
return errors.New("nil E2EConfig provided")
}
return nil
}
// E2EConfig is the configuration of the E2E tests.
type E2EConfig struct {
Focus string
Skip string
Parallel string
// CustomRegistries is the contents of a yaml file which will be
// used as KUBE_TEST_REPO_LIST which overrides which registries
// e2e tests use.
CustomRegistries string
}
// RunConfig are the input options for running Sonobuoy.
type RunConfig struct {
GenConfig
Wait time.Duration
WaitOutput string
}
// Validate checks the config to determine if it is valid.
func (rc *RunConfig) Validate() error {
err := rc.GenConfig.Validate()
return errors.Wrap(err, "GenConfig validation failed")
}
// DeleteConfig are the input options for cleaning up a Sonobuoy run.
type DeleteConfig struct {
Namespace string
EnableRBAC bool
DeleteAll bool
Wait time.Duration
WaitOutput string
}
// Validate checks the config to determine if it is valid.
func (dc *DeleteConfig) Validate() error {
if dc.Namespace == "" {
return errors.New("namespace cannot be empty")
}
return nil
}
// RetrieveConfig are the input options for retrieving a Sonobuoy run's results.
type RetrieveConfig struct {
// Namespace is the namespace the sonobuoy aggregator is running in.
Namespace string
}
// Validate checks the config to determine if it is valid.
func (rc *RetrieveConfig) Validate() error {
if rc.Namespace == "" {
return errors.New("namespace cannot be empty")
}
return nil
}
// StatusConfig is the input options for retrieving a Sonobuoy run's results.
type StatusConfig struct {
// Namespace is the namespace the sonobuoy aggregator is running in.
Namespace string
}
// Validate checks the config to determine if it is valid.
func (sc *StatusConfig) Validate() error {
if sc.Namespace == "" {
return errors.New("namespace cannot be empty")
}
return nil
}
// PreflightConfig are the options passed to PreflightChecks.
type PreflightConfig struct {
Namespace string
}
// Validate checks the config to determine if it is valid.
func (pfc *PreflightConfig) Validate() error {
if pfc.Namespace == "" {
return errors.New("namespace cannot be empty")
}
return nil
}
// SonobuoyKubeAPIClient is the interface Sonobuoy uses to communicate with a kube-apiserver.
type SonobuoyKubeAPIClient interface {
CreateObject(*unstructured.Unstructured) (*unstructured.Unstructured, error)
Name(*unstructured.Unstructured) (string, error)
Namespace(*unstructured.Unstructured) (string, error)
ResourceVersion(*unstructured.Unstructured) (string, error)
}
// SonobuoyClient is a high-level interface to Sonobuoy operations.
type SonobuoyClient struct {
RestConfig *rest.Config
client kubernetes.Interface
dynamicClient SonobuoyKubeAPIClient
}
// NewSonobuoyClient creates a new SonobuoyClient
func NewSonobuoyClient(restConfig *rest.Config, skc SonobuoyKubeAPIClient) (*SonobuoyClient, error) {
sc := &SonobuoyClient{
RestConfig: restConfig,
client: nil,
dynamicClient: skc,
}
return sc, nil
}
// Client creates or retrieves an existing kubernetes client from the SonobuoyClient's RESTConfig.
func (s *SonobuoyClient) Client() (kubernetes.Interface, error) {
if s.client == nil {
client, err := kubernetes.NewForConfig(s.RestConfig)
if err != nil {
return nil, errors.Wrap(err, "couldn't create kubernetes client")
}
s.client = client
}
return s.client, nil
}
// Make sure SonobuoyClient implements the interface
var _ Interface = &SonobuoyClient{}
// Interface is the main contract that we will give to external consumers of this library
// This will provide a consistent look/feel to upstream and allow us to expose sonobuoy behavior
// to other automation systems.
type Interface interface {
// Run generates the manifest, then tries to apply it to the cluster.
// returns created resources or an error
Run(cfg *RunConfig) error
// GenerateManifest fills in a template with a Sonobuoy config
GenerateManifest(cfg *GenConfig) ([]byte, error)
// RetrieveResults copies results from a sonobuoy run into a Reader in tar format.
RetrieveResults(cfg *RetrieveConfig) (io.Reader, <-chan error, error)
// GetStatus determines the status of the sonobuoy run in order to assist the user.
GetStatus(cfg *StatusConfig) (*aggregation.Status, error)
// LogReader returns a reader that contains a merged stream of sonobuoy logs.
LogReader(cfg *LogConfig) (*Reader, error)
// Delete removes a sonobuoy run, namespace, and all associated resources.
Delete(cfg *DeleteConfig) error
// PreflightChecks runs a number of preflight checks to confirm the environment is good for Sonobuoy
PreflightChecks(cfg *PreflightConfig) []error
}