Skip to content

Commit

Permalink
go, api: Create a structure API
Browse files Browse the repository at this point in the history
At some scenarios is useful to have a "structured" version of the
network_state, this change creates a golang api tested toward examples
and stated used at integration tests it also add the kubebuilder tags to
be k8s CRD friendly, the api is created using a code generator writen in
rust that use the crate "syn" to traverse the rust ast of specific files
that contains the nmstate rust interface.

Signed-off-by: Enrique Llorente <ellorent@redhat.com>
  • Loading branch information
qinqon committed Jan 22, 2024
1 parent b0010c0 commit ee8922d
Show file tree
Hide file tree
Showing 43 changed files with 12,097 additions and 339 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ jobs:
run: sudo .github/workflows/run_test.sh ${{ matrix.job_type }}

- uses: actions/upload-artifact@v3
if: success() || failure()
with:
name: nmstate-test-junit-artifact-${{ matrix.job_type }}
path: junit.*xml
Expand Down
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ doc/nmstate-autoconf.8
nmstate-*.tar.gz
nmstate-*.tar.gz.asc
/tags
.states
.k8s

api.test
crd.test
rust/src/go/nmstate/file.txt
23 changes: 11 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ CLI_EXEC_RELEASE=rust/target/release/$(CLI_EXEC)
PREFIX ?= /usr/local
SYSCONFDIR ?= $(PREFIX)/etc
SYSTEMD_UNIT_DIR ?= $(PREFIX)/lib/systemd/system
GO_MODULE_SRC ?= rust/src/go/nmstate
GO_MODULE_SRC ?= rust/src/go/nmstate/v2
CLI_MANPAGE=doc/nmstatectl.8
CLI_MANPAGE2=doc/nmstate-autoconf.8
SYSTEMD_UNIT_MANPAGE=doc/nmstate.service.8
Expand Down Expand Up @@ -219,18 +219,17 @@ clib_check: $(CLIB_SO_DEV_DEBUG) $(CLIB_HEADER)
$(TMPDIR)/nmpolicy_yaml_test 1>/dev/null
rm -rf $(TMPDIR)

.PHONY: go_generate
go_generate:
make -C rust/src/go/nmstate/v2 generate
make -C rust/src/go/api/v2 generate
make -C rust/src/go/crd/v2 generate

.PHONY: go_check
go_check: $(CLIB_SO_DEV_DEBUG) $(CLIB_HEADER)
$(eval TMPDIR := $(shell mktemp -d))
cp $(CLIB_SO_DEV_DEBUG) $(TMPDIR)/$(CLIB_SO_FULL)
ln -sfv $(CLIB_SO_FULL) $(TMPDIR)/$(CLIB_SO_MAN)
ln -sfv $(CLIB_SO_FULL) $(TMPDIR)/$(CLIB_SO_DEV)
cp $(CLIB_HEADER) $(TMPDIR)/$(shell basename $(CLIB_HEADER))
cd rust/src/go/nmstate; LD_LIBRARY_PATH=$(TMPDIR) \
CGO_CFLAGS="-I$(TMPDIR)" \
CGO_LDFLAGS="-L$(TMPDIR)" \
go test $(WHAT)
rm -rf $(TMPDIR)
go_check: $(CLIB_SO_DEV_DEBUG) $(CLIB_HEADER) go_generate
make CLIB_SO_DEV_DEBUG=$(ROOT_DIR)/$(CLIB_SO_DEV_DEBUG) CLIB_HEADER=$(ROOT_DIR)/$(CLIB_HEADER) CLIB_SO_FULL=$(CLIB_SO_FULL) CLIB_SO_MAN=$(CLIB_SO_MAN) CLIB_SO_DEV=$(CLIB_SO_DEV) -C rust/src/go/nmstate/v2 lint check
make -C rust/src/go/api/v2 lint build-test check-examples
make -C rust/src/go/crd/v2 build-test check-examples

rust_check:
cd rust; cargo test -- --test-threads=1 --show-output;
Expand Down
9 changes: 9 additions & 0 deletions automation/check-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -xe

if [[ -n "$(git status --porcelain)" ]]; then
echo "Git missing generated files"
git status --porcelain
exit 1
fi
5 changes: 5 additions & 0 deletions automation/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function run_tests {
"support yet"
else
exec_cmd "make go_check"
exec_cmd "automation/check-gen.sh"
fi
fi

Expand All @@ -96,6 +97,7 @@ function run_tests {
pytest \
$PYTEST_OPTIONS \
--junitxml=junit.integ.xml \
--go-checker \
tests/integration \
${nmstate_pytest_extra_args}"
fi
Expand All @@ -106,6 +108,7 @@ function run_tests {
pytest \
$PYTEST_OPTIONS \
--junitxml=junit.integ_tier1.xml \
--go-checker \
-m tier1 \
tests/integration \
${nmstate_pytest_extra_args}"
Expand All @@ -117,6 +120,7 @@ function run_tests {
pytest \
$PYTEST_OPTIONS \
--junitxml=junit.integ_tier2.xml \
--go-checker \
-m tier2 \
tests/integration \
${nmstate_pytest_extra_args}"
Expand All @@ -129,6 +133,7 @@ function run_tests {
pytest \
$PYTEST_OPTIONS \
--junitxml=junit.integ_slow.xml \
--go-checker \
-m slow --runslow \
tests/integration \
${nmstate_pytest_extra_args}"
Expand Down
1 change: 0 additions & 1 deletion examples/dns_remove.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
dns-resolver:
config: {}
interfaces: []
1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"src/cli",
"src/clib",
"src/lib",
"src/go/apigen",
]

[workspace.metadata.vendor-filter]
Expand Down
108 changes: 108 additions & 0 deletions rust/src/go/api/v2/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
linters-settings:
dupl:
threshold: 100
funlen:
lines: -1 # the number of lines (code + empty lines) is not a right metric and leads to code without empty line or one-liner.
statements: 50
goconst:
min-len: 2
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
gocyclo:
min-complexity: 15
gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
goheader:
template-path: boilerplate.go.txt
gomnd:
# don't include the "operation" and "assign"
checks:
- argument
- case
- condition
- return
ignored-numbers:
- '0'
- '1'
- '2'
- '3'
ignored-functions:
- strings.SplitN
errorlint:
asserts: false
nolintlint:
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
revive:
rules:
- name: unexported-return
disabled: true
- name: unused-parameter

linters:
disable-all: true
enable:
- bodyclose
- depguard
- dogsled
- dupl
- errcheck
- errorlint
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- nakedret
- noctx
- nolintlint
- revive
- staticcheck
- stylecheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

# don't enable:
# - asciicheck
# - scopelint
# - gochecknoglobals
# - gocognit
# - godot
# - godox
# - goerr113
# - interfacer
# - maligned
# - nestif
# - prealloc
# - testpackage
# - wsl

run:
timeout: 5m
16 changes: 16 additions & 0 deletions rust/src/go/api/v2/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
JUNIT_DIR ?= /tmp/
GO_JUNIT_REPORT=$(shell go env GOPATH)/bin/go-junit-report -set-exit-code

build-test:
go install github.com/jstemmer/go-junit-report/v2@latest
go test -c
check-examples:
go test -v -run TestAPI/examples 2>&1 | $(GO_JUNIT_REPORT) -set-exit-code -iocopy -out $(JUNIT_DIR)/junit.go-api.examples.xml
check-integration:
go test -v -run TestAPI/integration 2>&1 | $(GO_JUNIT_REPORT) -set-exit-code -iocopy -out $(JUNIT_DIR)/junit.go-api.integration.xml
check:
go test -v 2>&1 | $(GO_JUNIT_REPORT) -set-exit-code -iocopy -out $(JUNIT_DIR)/junit.go-api.xml
generate:
go generate
lint:
go run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2 run
16 changes: 16 additions & 0 deletions rust/src/go/api/v2/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright The NMState Authors.

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.
*/

3 changes: 3 additions & 0 deletions rust/src/go/api/v2/controller-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash -e

GOFLAGS=-mod=mod go run sigs.k8s.io/controller-tools/cmd/controller-gen@v0.14.0 $@

0 comments on commit ee8922d

Please sign in to comment.