-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Batuhan Apaydın <[email protected]>
- Loading branch information
1 parent
c3962df
commit 89bd203
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,4 +37,5 @@ const ( | |
Orange string = "#ff5400" | ||
|
||
Kubeless string = "Kubeless" | ||
Openfaas string = "Openfaas" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package outputs | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"strconv" | ||
|
||
"github.com/DataDog/datadog-go/statsd" | ||
"github.com/google/uuid" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/falcosecurity/falcosidekick/types" | ||
) | ||
|
||
// NewOpenfaasClient returns a new output.Client for accessing Kubernetes. | ||
func NewOpenfaasClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) { | ||
if config.Openfaas.Kubeconfig != "" { | ||
restConfig, err := clientcmd.BuildConfigFromFlags("", config.Openfaas.Kubeconfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clientset, err := kubernetes.NewForConfig(restConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Client{ | ||
OutputType: "OpenFaaS", | ||
Config: config, | ||
Stats: stats, | ||
PromStats: promStats, | ||
StatsdClient: statsdClient, | ||
DogstatsdClient: dogstatsdClient, | ||
KubernetesClient: clientset, | ||
}, nil | ||
} | ||
return NewClient( | ||
"OpenFaaS", | ||
"http://"+config.Openfaas.GatewayAddress+"."+config.Openfaas.GatewayNamespace+":"+strconv.Itoa(config.Openfaas.Port)+"/function/"+config.Openfaas.FunctionName+"."+config.Openfaas.FunctionNamespace, | ||
config, | ||
stats, | ||
promStats, | ||
statsdClient, | ||
dogstatsdClient, | ||
) | ||
} | ||
|
||
// OpenfaasCall . | ||
func (c *Client) OpenfaasCall(falcopayload types.FalcoPayload) { | ||
c.Stats.Openfaas.Add(Total, 1) | ||
|
||
if c.Config.Openfaas.Kubeconfig != "" { | ||
str, _ := json.Marshal(falcopayload) | ||
req := c.KubernetesClient.CoreV1().RESTClient().Post().AbsPath("/api/v1/namespaces/" + c.Config.Openfaas.GatewayNamespace + "/services/" + c.Config.Openfaas.GatewayAddress + ":" + strconv.Itoa(c.Config.Openfaas.Port) + "/proxy" + "/function/" + c.Config.Openfaas.FunctionName + "." + c.Config.Openfaas.FunctionNamespace).Body(str) | ||
req.SetHeader("event-id", uuid.New().String()) | ||
req.SetHeader("Content-Type", "application/json") | ||
req.SetHeader("User-Agent", "Falcosidekick") | ||
|
||
res := req.Do(context.TODO()) | ||
rawbody, err := res.Raw() | ||
if err != nil { | ||
go c.CountMetric(Outputs, 1, []string{"output:OpenFaaS", "status:error"}) | ||
c.Stats.Openfaas.Add(Error, 1) | ||
c.PromStats.Outputs.With(map[string]string{"destination": "OpenFaaS", "status": Error}).Inc() | ||
log.Printf("[ERROR] : Openfaas - %v\n", err) | ||
return | ||
} | ||
log.Printf("[INFO] : OpenFaaS - Function Response : %v\n", string(rawbody)) | ||
} else { | ||
err := c.Post(falcopayload) | ||
if err != nil { | ||
go c.CountMetric(Outputs, 1, []string{"output:OpenFaaS", "status:error"}) | ||
c.Stats.Openfaas.Add(Error, 1) | ||
c.PromStats.Outputs.With(map[string]string{"destination": "OpenFaaS", "status": Error}).Inc() | ||
log.Printf("[ERROR] : OpenFaaS - %v\n", err) | ||
return | ||
} | ||
} | ||
log.Printf("[INFO] : OpenFaaS - Call Function \"%v\" OK\n", c.Config.Openfaas.FunctionName+"."+c.Config.Openfaas.FunctionNamespace) | ||
go c.CountMetric(Outputs, 1, []string{"output:OpenFaaS", "status:ok"}) | ||
c.Stats.Openfaas.Add(OK, 1) | ||
c.PromStats.Outputs.With(map[string]string{"destination": "OpenFaaS", "status": OK}).Inc() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters