Skip to content

Commit 19b59f1

Browse files
committed
Remove K8sClient
1 parent 4dd5a17 commit 19b59f1

21 files changed

+233
-410
lines changed

tests/framework/crossplane.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ func fieldExistsInUpstream(
102102
directive Directive,
103103
opts ...Option,
104104
) bool {
105-
options := &Options{logEnabled: true}
106-
for _, opt := range opts {
107-
opt(options)
108-
}
105+
options := LogOptions(opts...)
109106
if options.logEnabled {
110107
GinkgoWriter.Printf(
111108
"Checking upstream for directive %q with value %q\n",
@@ -134,10 +131,7 @@ func getServerName(serverBlock Directives) string {
134131
}
135132

136133
func (e ExpectedNginxField) fieldFound(directive *Directive, opts ...Option) bool {
137-
options := &Options{logEnabled: true}
138-
for _, opt := range opts {
139-
opt(options)
140-
}
134+
options := LogOptions(opts...)
141135
arg := strings.Join(directive.Args, " ")
142136

143137
valueMatch := arg == e.Value

tests/framework/info.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ import (
1111

1212
// GetLogs returns the logs for all containers in all pods for a release.
1313
func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
14-
GinkgoWriter.Printf(
15-
"Getting logs for all containers in all pods for release %q in namespace %q\n",
16-
releaseName,
17-
namespace,
18-
)
1914
var returnLogs string
2015
pods, err := rm.GetPods(namespace, client.MatchingLabels{
2116
"app.kubernetes.io/instance": releaseName,
@@ -31,13 +26,6 @@ func GetLogs(rm ResourceManager, namespace string, releaseName string) string {
3126
Container: container.Name,
3227
})
3328
if err != nil {
34-
GinkgoWriter.Printf(
35-
"ERROR occurred during getting logs for container %q in pod %q in namespace %q: %v\n",
36-
container.Name,
37-
pod.Name,
38-
pod.Namespace,
39-
err,
40-
)
4129
returnLogs += fmt.Sprintf(" failed to get logs: %v\n", err)
4230
continue
4331
}
@@ -52,8 +40,6 @@ func GetEvents(rm ResourceManager, namespace string) string {
5240
var returnEvents string
5341
events, err := rm.GetEvents(namespace)
5442
if err != nil {
55-
GinkgoWriter.Printf("ERROR occurred during getting events in namespace %q: %v\n", namespace, err)
56-
5743
return fmt.Sprintf("failed to get events: %v", err)
5844
}
5945

@@ -74,7 +60,6 @@ func GetEvents(rm ResourceManager, namespace string) string {
7460

7561
// GetBuildInfo returns the build information.
7662
func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
77-
GinkgoWriter.Printf("Getting build info\n")
7863
commitHash = "unknown"
7964
commitTime = "unknown"
8065
dirtyBuild = "unknown"
@@ -101,10 +86,9 @@ func GetBuildInfo() (commitHash string, commitTime string, dirtyBuild string) {
10186
// AddNginxLogsAndEventsToReport adds nginx logs and events from the namespace to the report if the spec failed.
10287
func AddNginxLogsAndEventsToReport(rm ResourceManager, namespace string, opts ...Option) {
10388
if CurrentSpecReport().Failed() {
104-
GinkgoWriter.Printf("Current spec failed. Adding Nginx logs and events to report for namespace %q\n", namespace)
10589
var returnLogs string
10690

107-
nginxPodNames, _ := GetReadyNginxPodNames(rm.K8sClient, namespace, rm.TimeoutConfig.GetStatusTimeout, opts...)
91+
nginxPodNames, _ := rm.GetReadyNginxPodNames(namespace, rm.TimeoutConfig.GetStatusTimeout, opts...)
10892

10993
for _, nginxPodName := range nginxPodNames {
11094
returnLogs += fmt.Sprintf("Logs for Nginx Pod %s:\n", nginxPodName)

tests/framework/k8s_client.go

Lines changed: 0 additions & 193 deletions
This file was deleted.

tests/framework/logging.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ func WithLoggingDisabled() Option {
1111
opts.logEnabled = false
1212
}
1313
}
14+
15+
func LogOptions(opts ...Option) *Options {
16+
options := &Options{logEnabled: true}
17+
for _, opt := range opts {
18+
opt(options)
19+
}
20+
21+
return options
22+
}

tests/framework/ngf.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func InstallNGF(cfg InstallationConfig, extraArgs ...string) ([]byte, error) {
100100
}
101101

102102
// CreateLicenseSecret creates the NGINX Plus JWT secret.
103-
func CreateLicenseSecret(k8sClient K8sClient, namespace, filename string) error {
103+
func CreateLicenseSecret(rm ResourceManager, namespace, filename string) error {
104104
GinkgoWriter.Printf("Creating NGINX Plus license secret in namespace %q from file %q\n", namespace, filename)
105105

106106
conf, err := os.ReadFile(filename)
@@ -120,7 +120,7 @@ func CreateLicenseSecret(k8sClient K8sClient, namespace, filename string) error
120120
},
121121
}
122122

123-
if err := k8sClient.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) {
123+
if err := rm.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) {
124124
return fmt.Errorf("error creating namespace: %w", err)
125125
}
126126

@@ -134,7 +134,7 @@ func CreateLicenseSecret(k8sClient K8sClient, namespace, filename string) error
134134
},
135135
}
136136

137-
if err := k8sClient.Create(ctx, secret); err != nil && !apierrors.IsAlreadyExists(err) {
137+
if err := rm.Create(ctx, secret); err != nil && !apierrors.IsAlreadyExists(err) {
138138
createSecretErr := fmt.Errorf("error creating secret: %w", err)
139139
GinkgoWriter.Printf("%v\n", createSecretErr)
140140

@@ -181,7 +181,7 @@ func UpgradeNGF(cfg InstallationConfig, extraArgs ...string) ([]byte, error) {
181181
}
182182

183183
// UninstallNGF uninstalls NGF.
184-
func UninstallNGF(cfg InstallationConfig, k8sClient K8sClient) ([]byte, error) {
184+
func UninstallNGF(cfg InstallationConfig, rm ResourceManager) ([]byte, error) {
185185
args := []string{
186186
"uninstall", cfg.ReleaseName, "--namespace", cfg.Namespace,
187187
}
@@ -195,20 +195,20 @@ func UninstallNGF(cfg InstallationConfig, k8sClient K8sClient) ([]byte, error) {
195195
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
196196
defer cancel()
197197

198-
err = k8sClient.Delete(ctx, &core.Namespace{ObjectMeta: metav1.ObjectMeta{Name: cfg.Namespace}}, nil)
198+
err = rm.Delete(ctx, &core.Namespace{ObjectMeta: metav1.ObjectMeta{Name: cfg.Namespace}}, nil)
199199
if err != nil && !apierrors.IsNotFound(err) {
200200
return nil, err
201201
}
202202

203203
var crList apiext.CustomResourceDefinitionList
204-
if err := k8sClient.List(ctx, &crList, nil); err != nil {
204+
if err := rm.List(ctx, &crList); err != nil {
205205
return nil, err
206206
}
207207

208208
for _, cr := range crList.Items {
209209
if strings.Contains(cr.Spec.Group, "gateway.nginx.org") {
210210
cr := cr
211-
if err := k8sClient.Delete(ctx, &cr, nil); err != nil && !apierrors.IsNotFound(err) {
211+
if err := rm.Delete(ctx, &cr, nil); err != nil && !apierrors.IsNotFound(err) {
212212
return nil, err
213213
}
214214
}

tests/framework/prometheus.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,7 @@ func CreateMetricExistChecker(
475475
) func() error {
476476
return func() error {
477477
queryWithTimestamp := fmt.Sprintf("%s @ %d", query, getTime().Unix())
478-
options := &Options{logEnabled: true}
479-
for _, opt := range opts {
480-
opt(options)
481-
}
478+
options := LogOptions(opts...)
482479

483480
result, err := promInstance.Query(queryWithTimestamp)
484481
if err != nil {
@@ -541,10 +538,7 @@ func CreateEndTimeFinder(
541538

542539
// CreateResponseChecker returns a function that checks if there is a successful response from a url.
543540
func CreateResponseChecker(url, address string, requestTimeout time.Duration, opts ...Option) func() error {
544-
options := &Options{logEnabled: true}
545-
for _, opt := range opts {
546-
opt(options)
547-
}
541+
options := LogOptions(opts...)
548542
if options.logEnabled {
549543
GinkgoWriter.Printf("Starting checking response for url %q and address %q\n", url, address)
550544
}

tests/framework/request.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ func Get(
2424
headers, queryParams map[string]string,
2525
opts ...Option,
2626
) (int, string, error) {
27-
options := &Options{logEnabled: true}
28-
for _, opt := range opts {
29-
opt(options)
30-
}
27+
options := LogOptions(opts...)
3128

3229
resp, err := makeRequest(http.MethodGet, url, address, nil, timeout, headers, queryParams, opts...)
3330
if err != nil {
@@ -99,11 +96,7 @@ func makeRequest(
9996
ctx, cancel := context.WithTimeout(context.Background(), timeout)
10097
defer cancel()
10198

102-
options := &Options{logEnabled: true}
103-
104-
for _, opt := range opts {
105-
opt(options)
106-
}
99+
options := LogOptions(opts...)
107100
if options.logEnabled {
108101
requestDetails := fmt.Sprintf(
109102
"Method: %s, URL: %s, Address: %s, Headers: %v, QueryParams: %v\n",

0 commit comments

Comments
 (0)