Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/howto/system_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,11 @@ There are some specific environment variables that should be leveraged to overco
### Kubernetes service deployer

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

The Kubernetes service deployer needs [kind](https://kind.sigs.k8s.io/) to be installed and the cluster to be up and running:

```bash
wget -qO- https://raw.githubusercontent.com/elastic/elastic-package/main/scripts/kind-config.yaml | kind create cluster --config -
```
Expand Down
9 changes: 8 additions & 1 deletion internal/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ package kubectl
import (
"bytes"
"os/exec"
"path/filepath"

"github.com/pkg/errors"

"github.com/elastic/elastic-package/internal/logger"
)

const kustomizationFile = "kustomization.yaml"

// CurrentContext function returns the selected Kubernetes context.
func CurrentContext() (string, error) {
cmd := exec.Command("kubectl", "config", "current-context")
Expand All @@ -27,9 +30,13 @@ func CurrentContext() (string, error) {
return string(bytes.TrimSpace(output)), nil
}

func modifyKubernetesResources(action string, definitionPaths ...string) ([]byte, error) {
func modifyKubernetesResources(action string, definitionPaths []string) ([]byte, error) {
args := []string{action}
for _, definitionPath := range definitionPaths {
if filepath.Base(definitionPath) == kustomizationFile {
args = []string{action, "-k", filepath.Dir(definitionPath)}
break
}
args = append(args, "-f")
args = append(args, definitionPath)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/kubectl/kubectl_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func (c condition) String() string {
}

// Apply function adds resources to the Kubernetes cluster based on provided definitions.
func Apply(definitionPaths ...string) error {
logger.Debugf("Apply Kubernetes definitions")
out, err := modifyKubernetesResources("apply", definitionPaths...)
func Apply(definitionsPath []string) error {
logger.Debugf("Apply Kubernetes custom definitions")
out, err := modifyKubernetesResources("apply", definitionsPath)
if err != nil {
return errors.Wrap(err, "can't modify Kubernetes resources (apply)")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/kubectl/kubectl_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package kubectl

// Delete function removes resources from the Kubernetes cluster based on provided definitions.
func Delete(definitionPaths ...string) error {
_, err := modifyKubernetesResources("delete", definitionPaths...)
func Delete(definitionsPath []string) error {
_, err := modifyKubernetesResources("delete", definitionsPath)
return err
}
12 changes: 6 additions & 6 deletions internal/testrunner/runners/system/servicedeployer/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (s kubernetesDeployedService) TearDown() error {
return nil
}

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

// NewKubernetesServiceDeployer function creates a new instance of KubernetesServiceDeployer.
func NewKubernetesServiceDeployer(definitionsDir string) (*KubernetesServiceDeployer, error) {
func NewKubernetesServiceDeployer(definitionsPath string) (*KubernetesServiceDeployer, error) {
return &KubernetesServiceDeployer{
definitionsDir: definitionsDir,
definitionsDir: definitionsPath,
}, nil
}

Expand Down Expand Up @@ -115,15 +115,15 @@ func (ksd KubernetesServiceDeployer) installCustomDefinitions() error {

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

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

err = kubectl.Apply(definitionPaths...)
err = kubectl.Apply(definitionPaths)
if err != nil {
return errors.Wrap(err, "can't install custom definitions")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
apiVersion: batch/v1beta1
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
labels:
k8s-app: myjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
Expand Down
48 changes: 48 additions & 0 deletions test/packages/with-kind/kubernetes/_dev/deploy/k8s/daemonset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: apps/v1
kind: DaemonSet
metadata:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this daemonset.yaml coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need these resources to check the state_* datastreams, but since I only included one for the pod, maybe I should delete all the others? I was trying to follow the same logic as the one that was here before.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this daemonset.yaml coming from?

@gsantoro it is the same as used in integrations repo - https://github.com/elastic/integrations/blob/main/packages/kubernetes/_dev/deploy/k8s/daemonset.yaml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am torn between deleting some of the resources because they are useless, or keeping them so they stay the same as in the integrations repo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me see if I understand.

you are adding a state_pod datastream here since pod didn't even need the kube-state-metrics resource that was already defined in those manifests resources (before the kustomization was introduced). those resources in deploy/k8s are in theory used to setup the environment for testing but since we don't check for output against an expected result, they are kind of useless. we only check that the setup didn't have any issues.

I would say, keep them in sync with what we are doing in the integrations repo. it's definitively another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they were useless before because the only data stream defined had the .empty file. They are no longer useless.

name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
# these tolerations are to have the daemonset runnable on control plane nodes
# remove them if your control plane nodes should not run pods
- key: node-role.kubernetes.io/control-plane
operator: Exists
effect: NoSchedule
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resources:
- pv.yaml
- pvc.yaml
- cronjob.yaml
- job.yaml
- daemonset.yaml
- configmap.yaml
- rq.yaml
- ss.yaml
- github.com/kubernetes/kube-state-metrics?ref=abe3fd3184e16893b5a47196f90a94ed13e1b04d
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
metricsets: ["pod"]
add_metadata: {{add_metadata}}
{{#if add_resource_metadata_config}}
{{add_resource_metadata_config}}
{{/if}}
hosts:
{{#each hosts}}
- {{this}}
Expand All @@ -9,3 +12,14 @@ period: {{period}}
bearer_token_file: {{bearer_token_file}}
ssl.verification_mode: {{ssl.verification_mode}}
{{/if}}
{{#if ssl.certificate_authorities}}
ssl.certificate_authorities:
{{#each ssl.certificate_authorities}}
- {{this}}
{{/each}}
{{/if}}

{{#if processors}}
processors:
{{processors}}
{{/if}}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
description: >
Kubernetes pod UID

- name: pod.ip
type: ip
description: >
Kubernetes pod IP

- name: namespace
dimension: true
type: keyword
description: >
Kubernetes namespace
Expand Down Expand Up @@ -74,6 +80,7 @@
Kubernetes node UID

- name: namespace_uid
dimension: true
type: keyword
description: >
Kubernetes namespace UID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
type: long
format: bytes
metric_type: counter
description: |-
description: |
Total number of incoming bytes.
28 changes: 28 additions & 0 deletions test/packages/with-kind/kubernetes/data_stream/pod/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,33 @@ streams:
required: true
show_user: true
default: none
- name: ssl.certificate_authorities
type: text
title: SSL Certificate Authorities
multi: true
required: false
show_user: false
- name: add_resource_metadata_config
type: yaml
title: Add node and namespace metadata
required: false
show_user: false
default: |
# add_resource_metadata:
# namespace:
# include_labels: ["namespacelabel1"]
# node:
# include_labels: ["nodelabel2"]
# include_annotations: ["nodeannotation1"]
# deployment: false
- name: processors
type: yaml
title: Processors
multi: false
required: false
show_user: false
description: >
Processors are used to reduce the number of fields in the exported event or to enhance the event with metadata. This executes in the agent before the events are shipped. See [Processors](https://www.elastic.co/guide/en/beats/filebeat/current/filtering-and-enhancing-data.html) for details.

title: Kubernetes Pod metrics
description: Collect Kubernetes Pod metrics
Loading