Repository: datreeio/datree
Pull request #12 (2021-04-19 09:00:04)

main


Patch:
diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md
index 23f8f41..2096b2b 100644
--- a/.github/ISSUE_TEMPLATE/bug_report.md
+++ b/.github/ISSUE_TEMPLATE/bug_report.md
@@ -1,3 +1,11 @@
+---
+name: Bug report
+about: Create a report to help us improve
+title: ''
+labels: bug
+assignees: ''
+
+---
 **Describe the bug**
 A clear and concise description of what the bug is.
 
diff --git a/.travis.yml b/.travis.yml
index 2dc5db9..5f8312f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,6 +5,7 @@ go:
 
 script:
   - go build -tags $DATREE_DEPLOYMENT
+  - go test ./...
   - curl -sfL https://git.io/goreleaser | VERSION=v$GORELEASER_VERSION sh -s -- check
 
 before_deploy:
diff --git a/CODEOWNERS b/CODEOWNERS
index 2b98383..84f9ecb 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -1,1 +1,11 @@
+# by deafult
 @datreeio/developers
+
+# specific files
+README.md  @eyarz
+LICENSE.md  @eyarz
+CONTRIBUTING.md  @eyarz
+
+# specific directories
+images/  @eyarz
+.github/ISSUE_TEMPLATE/  @eyarz
diff --git a/README.md b/README.md
index e384a2a..51e38c1 100644
--- a/README.md
+++ b/README.md
@@ -2,8 +2,8 @@
 
 ![Travis (.com) branch](https://img.shields.io/travis/com/datreeio/datree/staging?label=build-staging)
 ![Travis (.com) branch](https://img.shields.io/travis/com/datreeio/datree/main?label=build-main)
+[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fdatreeio%2Fdatree&count_bg=%2379C83D&title_bg=%23555555&icon=github.svg&icon_color=%23E7E7E7&title=views+%28today+%2F+total%29&edge_flat=false)](https://hits.seeyoufarm.com)
 ![open issues](https://img.shields.io/github/issues-raw/datreeio/datree)
-[![HitCount](http://hits.dwyl.com/datreeio/datree.svg)](http://hits.dwyl.com/datreeio/datree)
 
 ## What is Datree?
 [Datree](https://datree.io/?utm_source=github&utm_medium=organic_oss) helps to prevent Kubernetes misconfigurations from ever making it to production.  
@@ -40,4 +40,4 @@ The rules are covering different Kubernetes resources/use-cases:
 
 [Datree](https://datree.io/?utm_source=github&utm_medium=organic_oss) builds and maintains this project to make Kubernetes policies simple and accessible.  
 Start with our [documentation](https://hub.datree.io/?utm_source=github&utm_medium=organic_oss) for quick tutorials and examples.  
-If you need direct support you can contact us at support@datree.io.
\ No newline at end of file
+If you need direct support you can contact us at support@datree.io.
diff --git a/bl/evaluator.go b/bl/evaluator.go
index f394e98..26babd6 100644
--- a/bl/evaluator.go
+++ b/bl/evaluator.go
@@ -55,10 +55,10 @@ func (e *Evaluator) Evaluate(pattern string, cliId string, evaluationConc int) (
 
 	res, err := e.cliClient.RequestEvaluation(pattern, files, cliId)
 	if err != nil {
-		return nil, fileErrors, fmt.Errorf("RequestEvaluation has failed")
+		return nil, fileErrors, err
 	}
 
-	results := e.aggregateEvaluationResults(res.Results)
+	results := e.aggregateEvaluationResults(res.Results, len(files))
 
 	return results, fileErrors, nil
 }
@@ -92,7 +92,7 @@ func (e *Evaluator) PrintFileParsingErrors(errors []propertiesExtractor.FileErro
 	}
 }
 
-func (e *Evaluator) aggregateEvaluationResults(evaluationResults []cliClient.EvaluationResult) *EvaluationResults {
+func (e *Evaluator) aggregateEvaluationResults(evaluationResults []cliClient.EvaluationResult, filesCount int) *EvaluationResults {
 	mapper := make(map[string]map[int]*Rule)
 
 	totalRulesCount := len(evaluationResults)
@@ -133,7 +133,7 @@ func (e *Evaluator) aggregateEvaluationResults(evaluationResults []cliClient.Eva
 		}{
 			RulesCount:       totalRulesCount,
 			TotalFailedRules: totalFailedCount,
-			FilesCount:       len(filenames),
+			FilesCount:       filesCount,
 		},
 	}
 
diff --git a/install-linux.sh b/install-linux.sh
deleted file mode 100755
index e9f15d3..0000000
--- a/install-linux.sh
+++ /dev/null
@@ -1,14 +0,0 @@
-#!/bin/sh
-
-DOWNLOAD_URL=$(curl --silent "https://api.github.com/repos/datreeio/datree/releases/latest" | grep -o "browser_download_url.*\_Linux_x86_64.zip")
-DOWNLOAD_URL=${DOWNLOAD_URL//\"}
-DOWNLOAD_URL=${DOWNLOAD_URL/browser_download_url: /}
-
-OUTPUT_BASENAME=datree-latest
-OUTPUT_BASENAME_WITH_POSTFIX=$OUTPUT_BASENAME.zip
-
-curl -L $DOWNLOAD_URL -o $OUTPUT_BASENAME_WITH_POSTFIX
-unzip $OUTPUT_BASENAME_WITH_POSTFIX -d $OUTPUT_BASENAME
-
-cp $OUTPUT_BASENAME/datree /usr/local/bin
-chmod +x /usr/local/bin/datree
diff --git a/install.sh b/install.sh
index 5a27223..8cbb3a3 100755
--- a/install.sh
+++ b/install.sh
@@ -1,6 +1,8 @@
-#!/bin/sh
+#!/bin/bash
+
+osName=$(uname -s)
+DOWNLOAD_URL=$(curl --silent "https://api.github.com/repos/datreeio/datree/releases/latest" | grep -o "browser_download_url.*\_${osName}_x86_64.zip")
 
-DOWNLOAD_URL=$(curl --silent "https://api.github.com/repos/datreeio/datree/releases/latest" | grep -o "browser_download_url.*\_Darwin_x86_64.zip")
 DOWNLOAD_URL=${DOWNLOAD_URL//\"}
 DOWNLOAD_URL=${DOWNLOAD_URL/browser_download_url: /}
 
@@ -10,4 +12,15 @@ OUTPUT_BASENAME_WITH_POSTFIX=$OUTPUT_BASENAME.zip
 curl -L $DOWNLOAD_URL -o $OUTPUT_BASENAME_WITH_POSTFIX
 unzip $OUTPUT_BASENAME_WITH_POSTFIX -d $OUTPUT_BASENAME
 
-cp $OUTPUT_BASENAME/datree /usr/local/bin
+if [[ $osName == "Linux" ]]
+then
+    sudo cp $OUTPUT_BASENAME/datree /usr/local/bin
+    mkdir ~/.datree
+else
+    cp $OUTPUT_BASENAME/datree /usr/local/bin
+fi
+
+rm $OUTPUT_BASENAME_WITH_POSTFIX
+rm -rf $OUTPUT_BASENAME
+
+curl https://get.datree.io/k8s-demo.yaml > ~/.datree/k8s-demo.yaml
diff --git a/internal/fixtures/kube/k8s-demo.yaml b/internal/fixtures/kube/k8s-demo.yaml
new file mode 100644
index 0000000..8e0fbad
--- /dev/null
+++ b/internal/fixtures/kube/k8s-demo.yaml
@@ -0,0 +1,196 @@
+apiVersion: extensions/v1beta1
+kind: Pod
+metadata:
+  name: static-web
+  namespace: test
+  labels:
+    role: .myrole
+spec:
+  serviceAccount: my-service-account
+  hostPID: true
+  hostIPC: true
+  hostNetwork: true
+  containers:
+    - name: web
+      image: nginx:latest
+      securityContext:
+        privileged: true
+        runAsUser: 2000
+      livenessProbe:
+        httpGet:
+          path: /health
+          port: 8080
+          httpHeaders:
+            - name: Custom-Header
+              value: Awesome
+      readinessProbe:
+        tcpSocket:
+          port: 8080
+        initialDelaySeconds: 5
+        periodSeconds: 10
+      resources:
+        requests:
+          cpu: "64Mi"
+        limits:
+          memory: "128Mi"
+      ports:
+        - name: web
+          containerPort: 80
+          protocol: TCP
+---
+apiVersion: kubeadm.k8s.io/v1beta1
+kind: Deployment
+metadata:
+  name: rss-site
+  namespace: default
+  labels:
+    owner: test@datree.io
+    app: web
+spec:
+  replicas: 1
+  selector:
+    matchLabels:
+      app: web
+  template:
+    metadata:
+      namespace: test
+      labels:
+        app: web
+    spec:
+      restartPolicy: Never
+      volumes:
+        - name: docker-sock
+          hostPath:
+            path: /var/run/docker.sock
+      containers:
+        - name: front-end
+          image: nginx@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
+          livenessProbe:
+            httpGet:
+              path: /healthz
+              port: 8080
+              httpHeaders:
+                - name: Custom-Header
+                  value: Awesome
+          readinessProbe:
+            tcpSocket:
+              port: 8080
+            initialDelaySeconds: 5
+            periodSeconds: 10
+          resources:
+            requests:
+              cpu: "64Mi"
+            limits:
+              memory: "128Mi"
+          ports:
+            - containerPort: 80
+        - name: rss-reader
+          image: nickchase/nginx@sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
+          securityContext:
+            runAsUser: 100
+          livenessProbe:
+            httpGet:
+              path: /healthz
+              port: 8080
+              httpHeaders:
+                - name: Custom-Header
+                  value: Awesome
+          readinessProbe:
+            tcpSocket:
+              port: 8080
+            initialDelaySeconds: 5
+            periodSeconds: 10
+          resources:
+            requests:
+              memory: "64Mi"
+              cpu: "64Mi"
+            limits:
+              memory: "128Mi"
+              cpu: "500m"
+          ports:
+            - containerPort: 88
+---
+apiVersion: networking.k8s.io/v1
+kind: Ingress
+metadata:
+  name: ingress-wildcard-host
+  labels:
+    owner: test@datree.io
+    environment: prod
+spec:
+  rules:
+    - host: "*"
+      http:
+        paths:
+          - pathType: Prefix
+            path: "/bar"
+            backend:
+              service:
+                name: service1
+                port:
+                  number: 80
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: my-service
+  labels:
+    owner: test@datree.io
+spec:
+  type: NodePort
+  selector:
+    app: MyApp
+  ports:
+    - protocol: TCP
+      port: 80
+      targetPort: 9376
+---
+apiVersion: batch/v1beta1
+kind: CronJob
+metadata:
+  name: hello
+  namespace: test
+  labels:
+    owner: test@datree.io
+spec:
+  schedule: "1 * * *"
+  jobTemplate:
+    spec:
+      template:
+        spec:
+          volumes:
+            - name: docker-sock
+              hostPath:
+                path: /var/run/docker
+          containers:
+            - name: hello
+              image: busybox
+              imagePullPolicy: IfNotPresent
+              command:
+                - /bin/sh
+                - -c
+                - date; echo Hello from the Kubernetes cluster
+          restartPolicy: OnFailure
+---
+apiVersion: autoscaling/v2beta2
+kind: HorizontalPodAutoscaler
+metadata:
+  name: php-apache
+spec:
+  scaleTargetRef:
+    apiVersion: apps/v1
+    kind: Deployment
+    name: php-apache
+  metrics:
+    - type: Resource
+      resource:
+        name: cpu
+        target:
+          type: Utilization
+          averageUtilization: 50
+---
+apiVersion: rbac.authorization.k8s.io/v1alpha1
+kind: InitConfiguration
+---
+apiVersion: apps/v1beta2
+kind: Deployment
diff --git a/pkg/httpClient/client.go b/pkg/httpClient/client.go
index 577696e..6a0468b 100644
--- a/pkg/httpClient/client.go
+++ b/pkg/httpClient/client.go
@@ -3,6 +3,7 @@ package httpClient
 import (
 	"bytes"
 	"encoding/json"
+	"fmt"
 	"io"
 	"io/ioutil"
 	"net/http"
@@ -57,6 +58,11 @@ func (c *Client) Request(method string, resourceURI string, body interface{}, he
 	if err != nil {
 		return responseBody, err
 	}
+
+	if response.StatusCode > 399 {
+		return responseBody, fmt.Errorf("Error: %v", string(b))
+	}
+
 	responseBody = Response{
 		StatusCode: response.StatusCode,
 		Body:       b,


Test patch:
diff --git a/bl/evaluator_test.go b/bl/evaluator_test.go
index 5f8a889..5bd6d35 100644
--- a/bl/evaluator_test.go
+++ b/bl/evaluator_test.go
@@ -171,7 +171,7 @@ func test_evaluate_success() *evaluateTestCase {
 					RulesCount       int
 					TotalFailedRules int
 					FilesCount       int
-				}{RulesCount: 1, TotalFailedRules: 0, FilesCount: 0},
+				}{RulesCount: 1, TotalFailedRules: 0, FilesCount: 1},
 			},
 			fileErrors: []propertiesExtractor.FileError{},
 			err:        nil,
@@ -182,7 +182,7 @@ func test_evaluate_success() *evaluateTestCase {
 func test_evaluate_failedRequest() *evaluateTestCase {
 	evaluationResponse := cliClient.EvaluationResponse{}
 	return &evaluateTestCase{
-		name: "success",
+		name: "fail",
 		args: struct {
 			pattern        string
 			cliId          string
@@ -226,7 +226,7 @@ func test_evaluate_failedRequest() *evaluateTestCase {
 		}{
 			response:   nil,
 			fileErrors: []propertiesExtractor.FileError{},
-			err:        fmt.Errorf("RequestEvaluation has failed"),
+			err:        fmt.Errorf("error"),
 		},
 	}
 }