Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 94f0a6e

Browse files
refactor Apply-type functions
1 parent b6a9ca6 commit 94f0a6e

File tree

3 files changed

+101
-111
lines changed

3 files changed

+101
-111
lines changed

tests/e2e/install_cluster.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ func requireCreatingKafkaCluster(kubectlOptions k8s.KubectlOptions, manifestPath
3737
By(fmt.Sprintf("KafkaCluster %s already exists\n", kafkaClusterName))
3838
} else {
3939
By("Deploying a KafkaCluster")
40-
applyK8sResourceManifest(kubectlOptions, manifestPath)
40+
err := applyK8sResourceManifest(kubectlOptions, manifestPath)
41+
Expect(err).NotTo(HaveOccurred())
4142
}
4243

4344
By("Verifying the KafkaCluster state")

tests/e2e/k8s.go

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,58 @@ const (
4242

4343
// crdNamePrefix is the prefix of the CRD names when listed through kubectl.
4444
crdNamePrefix = "customresourcedefinition.apiextensions.k8s.io/"
45+
46+
// Per the kubectl spec, "dry-run" strategy must be "none", "server", or "client".
47+
// With current use cases in e2e tests, we only expect to use "server" so the other options are commented out.
48+
dryRunStrategyArgServer string = "--dry-run=server" // submit server-side request without persisting the resource
49+
//dryRunStrategyArgClient string = "--dry-run=client" // the resource is only validated locally but not sent to the Api-server.
50+
//dryRunStrategyArgNone string = "--dry-run=none" // default value; submit server-side request and persist the resource
51+
4552
)
4653

4754
// applyK8sResourceManifests applies the specified manifest to the provided
4855
// kubectl context and namespace.
49-
func applyK8sResourceManifest(kubectlOptions k8s.KubectlOptions, manifestPath string) { //nolint:unused // Note: this might come in handy for manual K8s resource operations.
50-
By(fmt.Sprintf("Applying k8s manifest %s", manifestPath))
51-
k8s.KubectlApply(GinkgoT(), &kubectlOptions, manifestPath)
56+
func applyK8sResourceManifest(kubectlOptions k8s.KubectlOptions, manifestPath string, extraArgs ...string) error { //nolint:unused // Note: this might come in handy for manual K8s resource operations.
57+
args := []string{"apply", "-f", manifestPath}
58+
logMsg := fmt.Sprintf("Applying k8s manifest from path %s", manifestPath)
59+
logMsg, args = kubectlArgExtender(args, logMsg, "", "", kubectlOptions.Namespace, extraArgs)
60+
By(logMsg)
61+
62+
return k8s.RunKubectlE(GinkgoT(), &kubectlOptions, args...)
63+
}
64+
65+
// applyK8sResourceManifestFromString applies the specified manifest in string format to the provided
66+
// kubectl context and namespace.
67+
func applyK8sResourceManifestFromString(kubectlOptions k8s.KubectlOptions, manifest string, extraArgs ...string) error {
68+
// Replicating terratest's k8s.KubectlApplyFromStringE but with the possibility of a variadic argument that allows options like --dry-run
69+
//
70+
// TODO: look for a different implementation for temp files because terratest's version uses the composite test name to generate
71+
// the temp file name which, in our case, includes all the descriptive Ginkgo statements (which are by design quite verbose).
72+
// That can lead to erroring out on temp file creation based on the file name being too long.
73+
tmpfile, err := k8s.StoreConfigToTempFileE(GinkgoT(), manifest)
74+
if err != nil {
75+
return err
76+
}
77+
defer os.Remove(tmpfile)
78+
79+
return applyK8sResourceManifest(kubectlOptions, tmpfile, extraArgs...)
80+
}
81+
82+
// applyK8sResourceFromTemplate generates manifest from the specified go-template based on values
83+
// and applies the specified manifest to the provided kubectl context and namespace.
84+
func applyK8sResourceFromTemplate(kubectlOptions k8s.KubectlOptions, templateFile string, values map[string]interface{}, extraArgs ...string) error {
85+
By(fmt.Sprintf("Generating k8s manifest from template %s", templateFile))
86+
var manifest bytes.Buffer
87+
rawTemplate, err := os.ReadFile(templateFile)
88+
if err != nil {
89+
return err
90+
}
91+
t := template.Must(template.New("template").Funcs(sprig.TxtFuncMap()).Parse(string(rawTemplate)))
92+
err = t.Execute(&manifest, values)
93+
if err != nil {
94+
return err
95+
}
96+
return applyK8sResourceManifestFromString(kubectlOptions, manifest.String(), extraArgs...)
5297
}
5398

5499
// isExistingK8SResource queries a Resource by it's kind, namespace and name and
@@ -191,7 +236,10 @@ func installK8sCRD(kubectlOptions k8s.KubectlOptions, crd []byte, shouldBeValida
191236

192237
createOrReplaceK8sResourcesFromManifest(kubectlOptions, "crd", object.GetName(), tempPath, shouldBeValidated)
193238
default: // Note: regular CRD.
194-
applyK8sResourceManifest(kubectlOptions, tempPath)
239+
err = applyK8sResourceManifest(kubectlOptions, tempPath)
240+
if err != nil {
241+
return errors.WrapIfWithDetails(err, "applying CRD failed", "crd", string(crd))
242+
}
195243
}
196244

197245
return nil
@@ -453,30 +501,6 @@ func deleteK8sResourceNoErrNotFound(kubectlOptions k8s.KubectlOptions, timeout t
453501
return err
454502
}
455503

456-
// applyK8sResourceManifestFromString applies the specified manifest in string format to the provided
457-
// kubectl context and namespace.
458-
func applyK8sResourceManifestFromString(kubectlOptions k8s.KubectlOptions, manifest string) error {
459-
By(fmt.Sprintf("Applying k8s manifest\n%s", manifest))
460-
return k8s.KubectlApplyFromStringE(GinkgoT(), &kubectlOptions, manifest)
461-
}
462-
463-
// applyK8sResourceFromTemplate generates manifest from the specified go-template based on values
464-
// and applies the specified manifest to the provided kubectl context and namespace.
465-
func applyK8sResourceFromTemplate(kubectlOptions k8s.KubectlOptions, templateFile string, values map[string]interface{}) error {
466-
By(fmt.Sprintf("Generating K8s manifest from template %s", templateFile))
467-
var manifest bytes.Buffer
468-
rawTemplate, err := os.ReadFile(templateFile)
469-
if err != nil {
470-
return err
471-
}
472-
t := template.Must(template.New("template").Funcs(sprig.TxtFuncMap()).Parse(string(rawTemplate)))
473-
err = t.Execute(&manifest, values)
474-
if err != nil {
475-
return err
476-
}
477-
return applyK8sResourceManifestFromString(kubectlOptions, manifest.String())
478-
}
479-
480504
// listK8sResourceKinds lists all of the available resource kinds on the K8s cluster
481505
// with the apiGroupSelector parameter the result can be narrowed by the resource group.
482506
// extraArgs can be any kubectl api-resources parameter.

0 commit comments

Comments
 (0)