Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: PrimalPimmy <[email protected]>

using k3s action

Signed-off-by: PrimalPimmy <[email protected]>

reverting

Signed-off-by: PrimalPimmy <[email protected]>

reverting

Signed-off-by: PrimalPimmy <[email protected]>

Matching address

Signed-off-by: PrimalPimmy <[email protected]>

Updated install script

Signed-off-by: PrimalPimmy <[email protected]>

Updated install script

Signed-off-by: PrimalPimmy <[email protected]>

Updated install script

Signed-off-by: PrimalPimmy <[email protected]>
  • Loading branch information
PrimalPimmy committed Aug 22, 2023
1 parent f92c422 commit 5ad7afe
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 103 deletions.
10 changes: 1 addition & 9 deletions .github/workflows/ci-test-ginkgo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,15 @@ jobs:
- name: Building k8tls
run: make build

- name: check for docker
run: docker images

- name: Deploy k8tls
run: |
docker save kubearmor/k8tls:latest | sudo k3s ctr images import -
kubectl apply -f ./tests/res/test-k8tls.yaml
sleep 10
- name: Checking if /tmp can be read
run: ls /tmp

- name: Check k8tls is completed
run: kubectl get pods -A
- name: Checking if /tmp can be read
run: ls /tmp

- name: Test k8tls using Ginkgo
run: |
# mkdir /tmp/k8tls
Expand Down
71 changes: 7 additions & 64 deletions .github/workflows/scripts/install_k3s.sh
Original file line number Diff line number Diff line change
@@ -1,66 +1,9 @@
#!/bin/bash
# SPDX-License-Identifier: Apache-2.0
# Copyright 2021 Authors of KubeArmor
curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_EXEC="--disable=traefik" sh -
[[ $? != 0 ]] && echo "Failed to install k3s" && exit 1
mkdir -p ~/.kube && cp /etc/rancher/k3s/k3s.yaml ~/.kube/config

if [ "$RUNTIME" == "" ]; then
if [ -S /var/run/docker.sock ]; then
RUNTIME="docker"
elif [ -S /var/run/crio/crio.sock ]; then
RUNTIME="crio"
else # default
RUNTIME="containerd"
fi
fi

# create a single-node K3s cluster
if [ "$RUNTIME" == "docker" ]; then # docker
CGROUP_SYSTEMD=$(docker info 2> /dev/null | grep -i cgroup | grep systemd | wc -l)
if [ $CGROUP_SYSTEMD == 1 ]; then
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="v1.23.9+k3s1" K3S_KUBECONFIG_MODE="644" INSTALL_K3S_EXEC="--disable=traefik --docker --kubelet-arg cgroup-driver=systemd" sh -
[[ $? != 0 ]] && echo "Failed to install k3s" && exit 1
else # cgroupfs
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="v1.23.9+k3s1" K3S_KUBECONFIG_MODE="644" INSTALL_K3S_EXEC="--disable=traefik --docker" sh -
[[ $? != 0 ]] && echo "Failed to install k3s" && exit 1
fi
elif [ "$RUNTIME" == "crio" ]; then # cri-o
curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_EXEC="--disable=traefik --container-runtime-endpoint unix:///var/run/crio/crio.sock --kubelet-arg cgroup-driver=systemd" sh -
[[ $? != 0 ]] && echo "Failed to install k3s" && exit 1
else # use containerd by default
curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_EXEC="--disable=traefik" sh -
[[ $? != 0 ]] && echo "Failed to install k3s" && exit 1
fi

if [[ $(hostname) = kubearmor-dev* ]]; then
mkdir -p /home/vagrant/.kube
sudo cp /etc/rancher/k3s/k3s.yaml /home/vagrant/.kube/config
sudo chown -R vagrant:vagrant /home/vagrant/.kube
echo "export KUBECONFIG=/home/vagrant/.kube/config" | tee -a /home/vagrant/.bashrc
PATH=$PATH:/bin:/usr/bin:/usr/local/bin
else
KUBEDIR=$HOME/.kube
KUBECONFIG=$KUBEDIR/config
[[ ! -d $KUBEDIR ]] && mkdir $KUBEDIR
if [ -f $KUBECONFIG ]; then
echo "Found $KUBECONFIG already in place ... backing it up to $KUBECONFIG.backup"
cp $KUBECONFIG $KUBECONFIG.backup
fi
sudo cp /etc/rancher/k3s/k3s.yaml $KUBECONFIG
sudo chown $USER:$USER $KUBECONFIG
echo "export KUBECONFIG=$KUBECONFIG" | tee -a ~/.bashrc
fi

echo "wait for initialization"
sleep 15

runtime="15 minute"
endtime=$(date -ud "$runtime" +%s)

while [[ $(date -u +%s) -le $endtime ]]
until kubectl wait --for=condition=ready --timeout=15m -n kube-system pod -l k8s-app=metrics-server
do
status=$(kubectl get pods -A -o jsonpath={.items[*].status.phase})
[[ $(echo $status | grep -v Running | wc -l) -eq 0 ]] && break
echo "wait for initialization"
sleep 1
done

kubectl get pods -A
[[ $? != 0 ]] && echo "Checking for metrics-server"
sleep 10
done
43 changes: 15 additions & 28 deletions tests/kubetls-test.go → tests/k8tls-test.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
package test
package k8tlstest

import (
"encoding/csv"
"fmt"
"os"

// . "github.com/kubearmor/KubeArmor/tests/util"
"regexp"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = BeforeSuite(func() {
// err := K8sApply([]string{"res/test-k8tls.yaml"})
// Expect(err).To(BeNil())
// install wordpress-mysql app
// err := K8sApply([]string{"res/test-k8tls.yaml"})
// Expect(err).To(BeNil())
// time.Sleep(5 * time.Second)

})

var _ = AfterSuite(func() {

// err := K8sDelete([]string{"res/test-k8tls.yaml"})
// Expect(err).To(BeNil())

})

func matchCSV(file1 string, file2 string) {
// Read the first CSV file
file1Data, err := os.Open(file1)
Expand All @@ -37,7 +20,6 @@ func matchCSV(file1 string, file2 string) {
file1Records, err := reader1.ReadAll()
Expect(err).NotTo(HaveOccurred())

// Read the second CSV file
file2Data, err := os.Open(file2)
Expect(err).NotTo(HaveOccurred())
defer file2Data.Close()
Expand All @@ -46,7 +28,6 @@ func matchCSV(file1 string, file2 string) {
file2Records, err := reader2.ReadAll()
Expect(err).NotTo(HaveOccurred())

// Determine the index of the "Address" column
addressColumnIndex := -1
for i, header := range file1Records[0] {
if header == "Address" {
Expand All @@ -59,21 +40,27 @@ func matchCSV(file1 string, file2 string) {
Expect(len(file1Records[0])).To(Equal(len(file2Records[0])))

for i := 0; i < len(file1Records[0]); i++ {
if i == addressColumnIndex {
continue // Skip comparing the "Address" column
}
// if i == addressColumnIndex {
// continue // Skip comparing the "Address" column
// }
Expect(file1Records[0][i]).To(Equal(file2Records[0][i]))
}

// Compare rows
Expect(len(file1Records)).To(Equal(len(file2Records)))

for i := 0; i < len(file1Records); i++ {
for j := 0; j < len(file1Records[i]); j++ {
if j == addressColumnIndex {
continue // Skip comparing the "Address" column
ipPortPattern := `^\d+\.\d+\.\d+\.\d+:\d+$`

// Check if both values match the IP:Port pattern
isMatch := regexp.MustCompile(ipPortPattern).MatchString(file1Records[i][j]) &&
regexp.MustCompile(ipPortPattern).MatchString(file2Records[i][j])
Expect(isMatch).To(BeTrue(), fmt.Sprintf("Address mismatch at row %d", i+1))
} else {
Expect(file1Records[i][j]).To(Equal(file2Records[i][j]))
}
Expect(file1Records[i][j]).To(Equal(file2Records[i][j]))

}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package k8tlstest

import (
"testing"
Expand All @@ -9,5 +9,5 @@ import (

func TestTests(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Tests Suite")
RunSpecs(t, "K8tls Tests Suite")
}

0 comments on commit 5ad7afe

Please sign in to comment.