Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github action setup #2

Merged
merged 6 commits into from
Oct 25, 2024
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
29 changes: 9 additions & 20 deletions .github/workflows/go.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go
name: Build Krknctl
on:
release:
types: [published]
pull_request:

jobs:

build:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4

- name: Set up Go
- name: set up go
uses: actions/setup-go@v4
with:
go-version: '1.23.1'

- name: Install Dependencies
if: github.event_name == 'release' && github.event.action == 'published'
- name: install dependencies
run: |
sudo apt-get update && sudo apt-get install libbtrfs-dev

- name: Advance Version
if: github.event_name == 'release' && github.event.action == 'published'
- name: advance version
run: |
cat <<< $(jq '.version="${{github.event.release.tag_name}}"' internal/config/config.json) > internal/config/config.json

- name: Build Linux AMD64
if: github.event_name == 'release' && github.event.action == 'published'
- name: build linux amd64
env:
GOOS: "linux"
GOARCH: "amd64"
Expand All @@ -45,8 +40,7 @@ jobs:
tar cfvz $FILENAME -C $BINARY krknctl
gh release upload ${{github.event.release.tag_name}} $FILENAME

- name: Build Linux ARM64
if: github.event_name == 'release' && github.event.action == 'published'
- name: build linux arm64
env:
GOOS: "linux"
GOARCH: "arm64"
Expand All @@ -59,8 +53,7 @@ jobs:
tar cfvz $FILENAME -C $BINARY krknctl
gh release upload ${{github.event.release.tag_name}} $FILENAME

- name: Build Darwin ARM64
if: github.event_name == 'release' && github.event.action == 'published'
- name: build darwin arm64
env:
GOOS: "darwin"
GOARCH: "arm64"
Expand All @@ -73,8 +66,7 @@ jobs:
tar cfvz $FILENAME -C $BINARY krknctl
gh release upload ${{github.event.release.tag_name}} $FILENAME

- name: Build Darwin AMD64
if: github.event_name == 'release' && github.event.action == 'published'
- name: build darwin amd64
env:
GOOS: "darwin"
GOARCH: "amd64"
Expand All @@ -87,7 +79,4 @@ jobs:
tar cfvz $FILENAME -C $BINARY krknctl
gh release upload ${{github.event.release.tag_name}} $FILENAME

- name: Test
if: github.event_name == 'pull_request'
run: |
go test -v ./...

65 changes: 65 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: krknctl tests
on:
pull_request:
jobs:
test:
name: krknctl tests
runs-on: ubuntu-24.04
steps:
- name: create multi-node kind cluster
uses: redhat-chaos/actions/kind@main
- name: code checkout
uses: actions/checkout@v4
- name: set up go
uses: actions/setup-go@v4
with:
go-version: '1.23.1'
- name: install podman
run: |
sudo apt-get update && sudo apt-get install podman libbtrfs-dev nodejs
- name: setup podman
run: |
echo "XDG_RUNTIME_DIR=/run/user/$UID" >> $GITHUB_ENV
echo "PODMAN_SOCKET=$XDG_RUNTIME_DIR/podman/podman.sock" >> $GITHUB_ENV
loginctl enable-linger $(whoami)
podman system service --time=0 $PODMAN_SOCKET &
- name: set up test formatters
run: |
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
go install github.com/ctrf-io/go-ctrf-json-reporter/cmd/go-ctrf-json-reporter@latest
- name: run test suite
run: |
export USERID=$(id -u)
go test -tags containers_image_openpgp -race -json -v -coverprofile=coverage.out ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
cat /tmp/gotest.log | go-ctrf-json-reporter -output ctrf-report.json
- name: generate test report
run: npx github-actions-ctrf ctrf-report.json
- name: generate coverage report
run: |
COVERAGE=$(go tool cover -func coverage.out | grep total | awk '{print $3}')
echo "" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "# TEST COVERAGE: $COVERAGE" >> $GITHUB_STEP_SUMMARY
echo "TOTAL_COVERAGE=$COVERAGE" >> $GITHUB_ENV

- name: check out doc repo
uses: actions/checkout@v4
with:
repository: krkn-chaos/krkn-lib-docs
path: krkn-lib-docs
ssh-key: ${{ secrets.KRKN_LIB_DOCS_PRIV_KEY }}

- name: push updated coverage badge
env:
COLOR: yellow
run: |
[[ $TOTAL_COVERAGE > 40 ]] && COLOR=green
curl "https://img.shields.io/badge/coverage-$TOTAL_COVERAGE%25-$COLOR" > ./krkn-lib-docs/coverage_badge_krknctl.svg
cd krkn-lib-docs
git add .
git config user.name "krkn-chaos"
git config user.email "<>"
git commit -m "[KRKN] Coverage Badge ${GITHUB_REF##*/}" || echo "no changes to commit"
git push


5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
![build](https://github.com/krkn-chaos/krknctl/actions/workflows/build.yml/badge.svg)
![test](https://github.com/krkn-chaos/krknctl/actions/workflows/test.yml/badge.svg)
![coverage](https://krkn-chaos.github.io/krkn-lib-docs/coverage_badge_krknctl.svg)


# krknctl
Krkn Terminal UI & CLI

Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func NewRunCommand(factory *factory.ProviderFactory, containerManager *container
return err
}
//WIP
socket, err := (*containerManager).GetContainerRuntimeSocket()
socket, err := (*containerManager).GetContainerRuntimeSocket(nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"container_prefix": "krknctl-containers",
"kubeconfig_prefix": ".krknctl-kubeconfig",
"darwin_socket_template": "unix://%s/.local/share/containers/podman/machine/podman.sock",
"linux_socket_template": "unix://run/user/%s/podman/podman.sock",
"linux_socket_template": "unix://run/user/%d/podman/podman.sock",
"linux_socket_root": "unix://run/podman/podman.sock"
}
2 changes: 1 addition & 1 deletion pkg/container_manager/container_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ type ContainerManager interface {

CleanContainers() (*int, error)

GetContainerRuntimeSocket() (*string, error)
GetContainerRuntimeSocket(userId *int) (*string, error)
}
15 changes: 7 additions & 8 deletions pkg/container_manager/podman/container_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/opencontainers/runtime-spec/specs-go"
"log"
"os"
"os/user"
"regexp"
"runtime"
"time"
Expand Down Expand Up @@ -111,7 +110,7 @@ func (c *ContainerManager) CleanContainers() (*int, error) {
if err != nil {
return nil, err
}
socket, err := c.GetContainerRuntimeSocket()
socket, err := c.GetContainerRuntimeSocket(nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -145,16 +144,16 @@ func (c *ContainerManager) CleanContainers() (*int, error) {
return &deletedContainers, nil
}

func (c *ContainerManager) GetContainerRuntimeSocket() (*string, error) {
func (c *ContainerManager) GetContainerRuntimeSocket(userId *int) (*string, error) {
if runtime.GOOS == "linux" {
currentUser, err := user.Current()
if err != nil {
return nil, err
uid := os.Getuid()
if userId != nil {
uid = *userId
}
if currentUser.Uid == "0" {
if uid == 0 {
return &c.Config.LinuxSocketRoot, nil
}
socket := fmt.Sprintf(c.Config.LinuxSocketTemplate, currentUser.Uid)
socket := fmt.Sprintf(c.Config.LinuxSocketTemplate, uid)
return &socket, nil
} else if runtime.GOOS == "darwin" {
home, _ := os.UserHomeDir()
Expand Down
33 changes: 29 additions & 4 deletions pkg/container_manager/podman/container_manager_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package podman

import (
"fmt"
krknctlconfig "github.com/krkn-chaos/krknctl/internal/config"
"github.com/krkn-chaos/krknctl/pkg/container_manager"
"github.com/krkn-chaos/krknctl/pkg/provider/quay"
"github.com/stretchr/testify/assert"
"os"
"os/user"
"strconv"
"testing"
)

Expand All @@ -13,13 +17,13 @@ func getTestConfig() krknctlconfig.Config {
Version: "0.0.1",
QuayProtocol: "https",
QuayHost: "quay.io",
QuayOrg: "redhat-chaos",
QuayOrg: "krkn-chaos",
QuayRegistry: "krkn-hub",
QuayRepositoryApi: "api/v1/repository",
ContainerPrefix: "krknctl-containers",
KubeconfigPrefix: "krknctl-kubeconfig",
DarwinSocketTemplate: "unix://%s/.local/share/containers/podman/machine/podman.sock",
LinuxSocketTemplate: "unix://run/user/%s/podman/podman.sock",
LinuxSocketTemplate: "unix://run/user/%d/podman/podman.sock",
LinuxSocketRoot: "unix://run/podman/podman.sock",
}
}
Expand All @@ -35,17 +39,38 @@ func TestConnect(t *testing.T) {
cm := ContainerManager{
Config: conf,
}
currentUser, err := user.Current()
fmt.Println("Current user: " + (*currentUser).Name)
fmt.Println("current user id" + (*currentUser).Uid)
quayProvider := quay.ScenarioProvider{}
scenario, err := quayProvider.GetScenarioDetail("cpu-hog", conf.GetQuayRepositoryApiUri())
scenario, err := quayProvider.GetScenarioDetail("node-cpu-hog", conf.GetQuayRepositoryApiUri())
assert.Nil(t, err)
assert.NotNil(t, scenario)
kubeconfig, err := container_manager.PrepareKubeconfig(nil, getTestConfig())
assert.Nil(t, err)
socket, err := cm.GetContainerRuntimeSocket()
assert.NotNil(t, kubeconfig)
fmt.Println("KUBECONFIG PARSED -> " + *kubeconfig)

envuid := os.Getenv("USERID")
var uid *int = nil
if envuid != "" {
_uid, err := strconv.Atoi(envuid)
assert.Nil(t, err)
uid = &_uid
fmt.Println("USERID -> ", *uid)
}
socket, err := cm.GetContainerRuntimeSocket(uid)
assert.Nil(t, err)
assert.NotNil(t, socket)

fmt.Println("CONTAINER SOCKET -> " + *socket)
containerId, err := cm.RunAndStream(conf.GetQuayImageUri()+":"+scenario.Name,
scenario.Name,
*socket,
env, false, map[string]string{}, *kubeconfig, scenario.KubeconfigPath)
if err != nil {
fmt.Println("ERROR -> " + err.Error())
}
assert.Nil(t, err)
assert.NotNil(t, containerId)

Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/quay/scenario_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (p *ScenarioProvider) parseKubeconfigPath(s string) (*string, error) {
}

func (p *ScenarioProvider) parseInputFields(s string) ([]typing.InputField, error) {
re, err := regexp.Compile("LABEL krknctl\\.input_fields='?(.*)'?")
re, err := regexp.Compile("LABEL krknctl\\.input_fields=\\'?(\\[.*\\])\\'?")
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/provider/quay/scenario_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ func getTestConfig() krknctlconfig.Config {
Version: "0.0.1",
QuayProtocol: "https",
QuayHost: "quay.io",
QuayOrg: "rh_ee_tsebasti",
QuayRegistry: "krknctl",
QuayOrg: "krkn-chaos",
QuayRegistry: "krknctl-test",
QuayRepositoryApi: "api/v1/repository",
}
}
Expand All @@ -24,7 +24,7 @@ func getWrongConfig() krknctlconfig.Config {
Version: "0.0.1",
QuayProtocol: "https",
QuayHost: "quay.io",
QuayOrg: "rh_ee_tsebasti",
QuayOrg: "krkn-chaos",
QuayRegistry: "do_not_exist",
QuayRepositoryApi: "api/v1/repository",
}
Expand Down
Loading