Skip to content

Commit ab4357c

Browse files
authored
[K8s testing] Support use of Kustomize (#1125)
* Add support for kustomize.
1 parent 2412010 commit ab4357c

File tree

38 files changed

+1509
-106
lines changed

38 files changed

+1509
-106
lines changed

docs/howto/system_testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ There are some specific environment variables that should be leveraged to overco
303303
### Kubernetes service deployer
304304

305305
The Kubernetes service deployer requires the `_dev/deploy/k8s` directory to be present. It can include additional `*.yaml` files to deploy
306-
custom applications in the Kubernetes cluster (e.g. Nginx deployment). If no resource definitions (`*.yaml` files ) are needed,
306+
custom applications in the Kubernetes cluster (e.g. Nginx deployment). It is possible to use a `kustomization.yaml` file.
307+
If no resource definitions (`*.yaml` files ) are needed,
307308
the `_dev/deploy/k8s` directory must contain an `.empty` file (to preserve the `k8s` directory under version control).
308309

309310
The Kubernetes service deployer needs [kind](https://kind.sigs.k8s.io/) to be installed and the cluster to be up and running:
310-
311311
```bash
312312
wget -qO- https://raw.githubusercontent.com/elastic/elastic-package/main/scripts/kind-config.yaml | kind create cluster --config -
313313
```

internal/kubectl/kubectl.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ package kubectl
77
import (
88
"bytes"
99
"os/exec"
10+
"path/filepath"
1011

1112
"github.com/pkg/errors"
1213

1314
"github.com/elastic/elastic-package/internal/logger"
1415
)
1516

17+
const kustomizationFile = "kustomization.yaml"
18+
1619
// CurrentContext function returns the selected Kubernetes context.
1720
func CurrentContext() (string, error) {
1821
cmd := exec.Command("kubectl", "config", "current-context")
@@ -27,9 +30,13 @@ func CurrentContext() (string, error) {
2730
return string(bytes.TrimSpace(output)), nil
2831
}
2932

30-
func modifyKubernetesResources(action string, definitionPaths ...string) ([]byte, error) {
33+
func modifyKubernetesResources(action string, definitionPaths []string) ([]byte, error) {
3134
args := []string{action}
3235
for _, definitionPath := range definitionPaths {
36+
if filepath.Base(definitionPath) == kustomizationFile {
37+
args = []string{action, "-k", filepath.Dir(definitionPath)}
38+
break
39+
}
3340
args = append(args, "-f")
3441
args = append(args, definitionPath)
3542
}

internal/kubectl/kubectl_apply.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ func (c condition) String() string {
7070
}
7171

7272
// Apply function adds resources to the Kubernetes cluster based on provided definitions.
73-
func Apply(definitionPaths ...string) error {
74-
logger.Debugf("Apply Kubernetes definitions")
75-
out, err := modifyKubernetesResources("apply", definitionPaths...)
73+
func Apply(definitionsPath []string) error {
74+
logger.Debugf("Apply Kubernetes custom definitions")
75+
out, err := modifyKubernetesResources("apply", definitionsPath)
7676
if err != nil {
7777
return errors.Wrap(err, "can't modify Kubernetes resources (apply)")
7878
}

internal/kubectl/kubectl_delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package kubectl
66

77
// Delete function removes resources from the Kubernetes cluster based on provided definitions.
8-
func Delete(definitionPaths ...string) error {
9-
_, err := modifyKubernetesResources("delete", definitionPaths...)
8+
func Delete(definitionsPath []string) error {
9+
_, err := modifyKubernetesResources("delete", definitionsPath)
1010
return err
1111
}

internal/testrunner/runners/system/servicedeployer/kubernetes.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (s kubernetesDeployedService) TearDown() error {
4747
return nil
4848
}
4949

50-
err = kubectl.Delete(definitionPaths...)
50+
err = kubectl.Delete(definitionPaths)
5151
if err != nil {
5252
return errors.Wrapf(err, "can't uninstall Kubernetes resources (path: %s)", s.definitionsDir)
5353
}
@@ -70,9 +70,9 @@ func (s *kubernetesDeployedService) SetContext(sc ServiceContext) error {
7070
var _ DeployedService = new(kubernetesDeployedService)
7171

7272
// NewKubernetesServiceDeployer function creates a new instance of KubernetesServiceDeployer.
73-
func NewKubernetesServiceDeployer(definitionsDir string) (*KubernetesServiceDeployer, error) {
73+
func NewKubernetesServiceDeployer(definitionsPath string) (*KubernetesServiceDeployer, error) {
7474
return &KubernetesServiceDeployer{
75-
definitionsDir: definitionsDir,
75+
definitionsDir: definitionsPath,
7676
}, nil
7777
}
7878

@@ -115,15 +115,15 @@ func (ksd KubernetesServiceDeployer) installCustomDefinitions() error {
115115

116116
definitionPaths, err := findKubernetesDefinitions(ksd.definitionsDir)
117117
if err != nil {
118-
return errors.Wrapf(err, "can't find Kubernetes definitions in given directory (path: %s)", ksd.definitionsDir)
118+
return errors.Wrapf(err, "can't find Kubernetes definitions in given path: %s", ksd.definitionsDir)
119119
}
120120

121121
if len(definitionPaths) == 0 {
122-
logger.Debugf("no custom definitions found (directory: %s). Nothing else will be installed.", ksd.definitionsDir)
122+
logger.Debugf("no custom definitions found (path: %s). Nothing else will be installed.", ksd.definitionsDir)
123123
return nil
124124
}
125125

126-
err = kubectl.Apply(definitionPaths...)
126+
err = kubectl.Apply(definitionPaths)
127127
if err != nil {
128128
return errors.Wrap(err, "can't install custom definitions")
129129
}

test/packages/with-kind/kubernetes/_dev/deploy/k8s/example-redis-config.yaml renamed to test/packages/with-kind/kubernetes/_dev/deploy/k8s/configmap.yaml

File renamed without changes.

test/packages/with-kind/kubernetes/_dev/deploy/k8s/conrjob.yaml renamed to test/packages/with-kind/kubernetes/_dev/deploy/k8s/cronjob.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
apiVersion: batch/v1beta1
1+
apiVersion: batch/v1
22
kind: CronJob
33
metadata:
44
name: hello
5+
labels:
6+
k8s-app: myjob
57
spec:
68
schedule: "*/1 * * * *"
79
jobTemplate:
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: fluentd-elasticsearch
5+
namespace: kube-system
6+
labels:
7+
k8s-app: fluentd-logging
8+
spec:
9+
selector:
10+
matchLabels:
11+
name: fluentd-elasticsearch
12+
template:
13+
metadata:
14+
labels:
15+
name: fluentd-elasticsearch
16+
spec:
17+
tolerations:
18+
# these tolerations are to have the daemonset runnable on control plane nodes
19+
# remove them if your control plane nodes should not run pods
20+
- key: node-role.kubernetes.io/control-plane
21+
operator: Exists
22+
effect: NoSchedule
23+
- key: node-role.kubernetes.io/master
24+
operator: Exists
25+
effect: NoSchedule
26+
containers:
27+
- name: fluentd-elasticsearch
28+
image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
29+
resources:
30+
limits:
31+
memory: 200Mi
32+
requests:
33+
cpu: 100m
34+
memory: 200Mi
35+
volumeMounts:
36+
- name: varlog
37+
mountPath: /var/log
38+
- name: varlibdockercontainers
39+
mountPath: /var/lib/docker/containers
40+
readOnly: true
41+
terminationGracePeriodSeconds: 30
42+
volumes:
43+
- name: varlog
44+
hostPath:
45+
path: /var/log
46+
- name: varlibdockercontainers
47+
hostPath:
48+
path: /var/lib/docker/containers
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resources:
2+
- pv.yaml
3+
- pvc.yaml
4+
- cronjob.yaml
5+
- job.yaml
6+
- daemonset.yaml
7+
- configmap.yaml
8+
- rq.yaml
9+
- ss.yaml
10+
- github.com/kubernetes/kube-state-metrics?ref=abe3fd3184e16893b5a47196f90a94ed13e1b04d

test/packages/with-kind/kubernetes/data_stream/pod/agent/stream/stream.yml.hbs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
metricsets: ["pod"]
22
add_metadata: {{add_metadata}}
3+
{{#if add_resource_metadata_config}}
4+
{{add_resource_metadata_config}}
5+
{{/if}}
36
hosts:
47
{{#each hosts}}
58
- {{this}}
@@ -9,3 +12,14 @@ period: {{period}}
912
bearer_token_file: {{bearer_token_file}}
1013
ssl.verification_mode: {{ssl.verification_mode}}
1114
{{/if}}
15+
{{#if ssl.certificate_authorities}}
16+
ssl.certificate_authorities:
17+
{{#each ssl.certificate_authorities}}
18+
- {{this}}
19+
{{/each}}
20+
{{/if}}
21+
22+
{{#if processors}}
23+
processors:
24+
{{processors}}
25+
{{/if}}

0 commit comments

Comments
 (0)