Skip to content

Commit

Permalink
Merge pull request #1 from build-security/feature/ES-1811-kubeapi-fet…
Browse files Browse the repository at this point in the history
…cher-generated-cleanup

Feature/es 1811 kubeapi fetcher generated cleanup
  • Loading branch information
eyalkraft authored Oct 11, 2021
2 parents c3cb365 + 303344d commit 0b5cf86
Show file tree
Hide file tree
Showing 31 changed files with 17,109 additions and 0 deletions.
27 changes: 27 additions & 0 deletions kubebeat/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# See: http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.json]
indent_size = 4
indent_style = space

[*.py]
indent_style = space
indent_size = 4

[*.yml]
indent_style = space
indent_size = 2

[Makefile]
indent_style = tab

[Vagrantfile]
indent_size = 2
indent_style = space
7 changes: 7 additions & 0 deletions kubebeat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.idea
/build

.DS_Store
/kubebeat
/kubebeat.test
*.pyc
Empty file added kubebeat/CONTRIBUTING.md
Empty file.
13 changes: 13 additions & 0 deletions kubebeat/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) {year} Eyal Kraft

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.
20 changes: 20 additions & 0 deletions kubebeat/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
BEAT_NAME=kubebeat
BEAT_PATH=github.com/elastic/beats/v7/kubebeat
BEAT_GOPATH=$(firstword $(subst :, ,${GOPATH}))
SYSTEM_TESTS=false
TEST_ENVIRONMENT=false
ES_BEATS_IMPORT_PATH=github.com/elastic/beats/v7
ES_BEATS?=$(shell go list -m -f '{{.Dir}}' ${ES_BEATS_IMPORT_PATH})
LIBBEAT_MAKEFILE=$(ES_BEATS)/libbeat/scripts/Makefile
GOPACKAGES=$(shell go list ${BEAT_PATH}/... | grep -v /tools)
GOBUILD_FLAGS=-i -ldflags "-X ${ES_BEATS_IMPORT_PATH}/libbeat/version.buildTime=$(NOW) -X ${ES_BEATS_IMPORT_PATH}/libbeat/version.commit=$(COMMIT_ID)"
MAGE_IMPORT_PATH=github.com/magefile/mage
NO_COLLECT=true
CHECK_HEADERS_DISABLED=true

# Path to the libbeat Makefile
-include $(LIBBEAT_MAKEFILE)

.PHONY: copy-vendor
copy-vendor:
mage vendorUpdate
5 changes: 5 additions & 0 deletions kubebeat/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kubebeat
Copyright {year} Eyal Kraft

This product includes software developed by The Apache Software
Foundation (http://www.apache.org/).
116 changes: 116 additions & 0 deletions kubebeat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# {Beat}

Welcome to {Beat}.

Ensure that this folder is at the following location:
`${GOPATH}/src/github.com/elastic/beats/v7/kubebeat`

## Getting Started with {Beat}

### Requirements

* [Golang](https://golang.org/dl/) 1.7

### Init Project
To get running with {Beat} and also install the
dependencies, run the following command:

```
make update
```

It will create a clean git history for each major step. Note that you can always rewrite the history if you wish before pushing your changes.

To push {Beat} in the git repository, run the following commands:

```
git remote set-url origin https://github.com/elastic/beats/v7/kubebeat
git push origin master
```

For further development, check out the [beat developer guide](https://www.elastic.co/guide/en/beats/libbeat/current/new-beat.html).

### Build

To build the binary for {Beat} run the command below. This will generate a binary
in the same directory with the name kubebeat.

```
make
```


### Run

To run {Beat} with debugging output enabled, run:

```
./kubebeat -c kubebeat.yml -e -d "*"
```


### Test

To test {Beat}, run the following command:

```
make testsuite
```

alternatively:
```
make unit-tests
make system-tests
make integration-tests
make coverage-report
```

The test coverage is reported in the folder `./build/coverage/`

### Update

Each beat has a template for the mapping in elasticsearch and a documentation for the fields
which is automatically generated based on `fields.yml` by running the following command.

```
make update
```


### Cleanup

To clean {Beat} source code, run the following command:

```
make fmt
```

To clean up the build directory and generated artifacts, run:

```
make clean
```


### Clone

To clone {Beat} from the git repository, run the following commands:

```
mkdir -p ${GOPATH}/src/github.com/elastic/beats/v7/kubebeat
git clone https://github.com/elastic/beats/v7/kubebeat ${GOPATH}/src/github.com/elastic/beats/v7/kubebeat
```


For further development, check out the [beat developer guide](https://www.elastic.co/guide/en/beats/libbeat/current/new-beat.html).


## Packaging

The beat frameworks provides tools to crosscompile and package your beat for different platforms. This requires [docker](https://www.docker.com/) and vendoring as described above. To build packages of your beat, run the following command:

```
make release
```

This will fetch and create all images required for the build process. The whole process to finish can take several minutes.
2 changes: 2 additions & 0 deletions kubebeat/_meta/config/beat.docker.yml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
kubebeat:
period: 1s
7 changes: 7 additions & 0 deletions kubebeat/_meta/config/beat.reference.yml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
################### {Beat} Configuration Example #########################

############################# {Beat} ######################################

kubebeat:
# Defines how often an event is sent to the output
period: 1s
7 changes: 7 additions & 0 deletions kubebeat/_meta/config/beat.yml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
################### {Beat} Configuration Example #########################

############################# {Beat} ######################################

kubebeat:
# Defines how often an event is sent to the output
period: 1s
9 changes: 9 additions & 0 deletions kubebeat/_meta/fields.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- key: kubebeat
title: kubebeat
description:
fields:
- name: counter
type: long
required: true
description: >
PLEASE UPDATE DOCUMENTATION
71 changes: 71 additions & 0 deletions kubebeat/beater/kubebeat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package beater

import (
"fmt"
"time"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/common"
"github.com/elastic/beats/v7/libbeat/logp"

"github.com/elastic/beats/v7/kubebeat/config"
)

// kubebeat configuration.
type kubebeat struct {
done chan struct{}
config config.Config
client beat.Client
}

// New creates an instance of kubebeat.
func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
c := config.DefaultConfig
if err := cfg.Unpack(&c); err != nil {
return nil, fmt.Errorf("Error reading config file: %v", err)
}

bt := &kubebeat{
done: make(chan struct{}),
config: c,
}
return bt, nil
}

// Run starts kubebeat.
func (bt *kubebeat) Run(b *beat.Beat) error {
logp.Info("kubebeat is running! Hit CTRL-C to stop it.")

var err error
bt.client, err = b.Publisher.Connect()
if err != nil {
return err
}

ticker := time.NewTicker(bt.config.Period)
counter := 1
for {
select {
case <-bt.done:
return nil
case <-ticker.C:
}

event := beat.Event{
Timestamp: time.Now(),
Fields: common.MapStr{
"type": b.Info.Name,
"counter": counter,
},
}
bt.client.Publish(event)
logp.Info("Event sent")
counter++
}
}

// Stop stops kubebeat.
func (bt *kubebeat) Stop() {
bt.client.Close()
close(bt.done)
}
14 changes: 14 additions & 0 deletions kubebeat/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cmd

import (
"github.com/elastic/beats/v7/kubebeat/beater"

cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/cmd/instance"
)

// Name of this beat
var Name = "kubebeat"

// RootCmd to handle beats cli
var RootCmd = cmd.GenRootCmdWithSettings(beater.New, instance.Settings{Name: Name})
14 changes: 14 additions & 0 deletions kubebeat/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Config is put into a different package to prevent cyclic imports in case
// it is needed in several locations

package config

import "time"

type Config struct {
Period time.Duration `config:"period"`
}

var DefaultConfig = Config{
Period: 1 * time.Second,
}
3 changes: 3 additions & 0 deletions kubebeat/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// +build !integration

package config
Loading

0 comments on commit 0b5cf86

Please sign in to comment.