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

feat: add v1alpha3 api with RuntimeSpec configuration #922

Merged
merged 29 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2157091
initial code to add v1alpha3 api with RuntimeSpec configuration
ashnamehrotra Nov 30, 2023
295cd7c
cleanup conversion file
ashnamehrotra Nov 30, 2023
4fdf322
Merge branch 'main' of github.com:Azure/eraser into add-runtime-sock
ashnamehrotra Dec 1, 2023
97fa922
modify unversioned_RuntimeSpec to unversioned_Runtime for this PR
ashnamehrotra Dec 1, 2023
e1ba0a4
move custom conversions to another file to prevent them from being ov…
ashnamehrotra Dec 1, 2023
5d939a4
add //nolint:unparam,varcheck above custom conversion functions
ashnamehrotra Dec 1, 2023
0471238
try unsused
ashnamehrotra Dec 1, 2023
4430bbb
lint change unused scope variable to _
ashnamehrotra Dec 1, 2023
cd806c5
nolint:revive
ashnamehrotra Dec 1, 2023
0e7a8e7
change auto in func name to manual for relevant funcs
ashnamehrotra Dec 4, 2023
5ab3676
remove unnecessary Runtime cast
ashnamehrotra Dec 4, 2023
c625811
remove Read, Update, NewManager funcs from v1alpha configs
ashnamehrotra Dec 4, 2023
74fdb3a
remove unversioned TODOs
ashnamehrotra Dec 4, 2023
fa8f247
fix RuntimeSpec unmarshall func
ashnamehrotra Dec 5, 2023
5d67b3d
add unit tests
ashnamehrotra Dec 5, 2023
81b2e2e
switch r Runtime
ashnamehrotra Dec 5, 2023
ddeaecb
change unversioned to v1alpha3 changes, modify relevant conversions
ashnamehrotra Dec 6, 2023
755f06a
lint fixes
ashnamehrotra Dec 6, 2023
0409e51
add case for v1alpha3 apiversion
ashnamehrotra Dec 6, 2023
442a1b7
unmarshal into string values
ashnamehrotra Dec 7, 2023
101b634
debugging for CI errors
ashnamehrotra Dec 7, 2023
4b0de32
more debugging
ashnamehrotra Dec 8, 2023
b4ef1d7
update values files to v1alpha3
ashnamehrotra Dec 11, 2023
b0d8dce
update manifest_staging/deploy/eraser.yaml
ashnamehrotra Dec 11, 2023
c8e2dc7
update helm-empty-values.yaml
ashnamehrotra Dec 12, 2023
4c4ffed
runtime spec unit test changes
ashnamehrotra Dec 14, 2023
bb0d846
change trivy log errors
ashnamehrotra Dec 14, 2023
c34e194
remove RuntimeAddress type and use string
ashnamehrotra Dec 14, 2023
1f69c3f
Merge branch 'main' of github.com:Azure/eraser into add-runtime-sock
ashnamehrotra Dec 14, 2023
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ EOL_IMG ?= docker.io/library/alpine:3.1
BUSYBOX_BASE_IMG ?= busybox:1.36.0
NON_VULNERABLE_IMG ?= ghcr.io/eraser-dev/non-vulnerable:latest
E2E_TESTS ?= $(shell find ./test/e2e/tests/ -mindepth 1 -type d)
API_VERSIONS ?= ./api/v1alpha1,./api/v1,./api/v1alpha2
API_VERSIONS ?= ./api/v1alpha1,./api/v1,./api/v1alpha2,./api/v1alpha3

HELM_UPGRADE_TEST ?=
TEST_LOGDIR ?= $(PWD)/test_logs
Expand Down
5 changes: 4 additions & 1 deletion api/unversioned/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ const (
func Default() *unversioned.EraserConfig {
return &unversioned.EraserConfig{
Manager: unversioned.ManagerConfig{
Runtime: "containerd",
Runtime: unversioned.RuntimeSpec{
Name: unversioned.RuntimeContainerd,
Address: "unix:///run/containerd/containerd.sock",
},
OTLPEndpoint: "",
LogLevel: "info",
Scheduling: unversioned.ScheduleConfig{
Expand Down
82 changes: 70 additions & 12 deletions api/unversioned/eraserconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package unversioned
import (
"encoding/json"
"fmt"
"net/url"
"time"

"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -28,14 +29,40 @@ import (
type (
Duration time.Duration
Runtime string

RuntimeSpec struct {
Name Runtime `json:"name"`
Address string `json:"address"`
}
)

const (
RuntimeContainerd Runtime = "containerd"
RuntimeDockerShim Runtime = "dockershim"
RuntimeCrio Runtime = "crio"

ContainerdPath = "/run/containerd/containerd.sock"
DockerPath = "/run/dockershim.sock"
CrioPath = "/run/crio/crio.sock"
)

func ConvertRuntimeToRuntimeSpec(r Runtime) (RuntimeSpec, error) {
var rs RuntimeSpec

switch r {
case RuntimeContainerd:
rs = RuntimeSpec{Name: RuntimeContainerd, Address: fmt.Sprintf("unix://%s", ContainerdPath)}
case RuntimeDockerShim:
rs = RuntimeSpec{Name: RuntimeDockerShim, Address: fmt.Sprintf("unix://%s", DockerPath)}
case RuntimeCrio:
rs = RuntimeSpec{Name: RuntimeCrio, Address: fmt.Sprintf("unix://%s", CrioPath)}
default:
return rs, fmt.Errorf("invalid runtime: valid names are %s, %s, %s", RuntimeContainerd, RuntimeDockerShim, RuntimeCrio)
}

return rs, nil
}

func (td *Duration) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
Expand All @@ -52,27 +79,58 @@ func (td *Duration) UnmarshalJSON(b []byte) error {
return nil
}

func (r *Runtime) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
func (td *Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, time.Duration(*td).String())), nil
}

func (r *RuntimeSpec) UnmarshalJSON(b []byte) error {
// create temp RuntimeSpec to prevent recursive error into this function when using unmarshall to check validity of provided RuntimeSpec
type TempRuntimeSpec struct {
Name string `json:"name"`
Address string `json:"address"`
}
var rs TempRuntimeSpec
err := json.Unmarshal(b, &rs)
if err != nil {
return err
return fmt.Errorf("error unmarshalling into TempRuntimeSpec %v %s", err, string(b))
}

switch rt := Runtime(str); rt {
switch rt := Runtime(rs.Name); rt {
// make sure user provided Runtime is valid
case RuntimeContainerd, RuntimeDockerShim, RuntimeCrio:
*r = rt
if rs.Address != "" {
// check that provided RuntimeAddress is valid
u, err := url.Parse(rs.Address)
if err != nil {
return err
}

switch u.Scheme {
case "tcp", "unix":
default:
return fmt.Errorf("invalid RuntimeAddress scheme: valid schemes for runtime socket address are `tcp` and `unix`")
}

r.Name = Runtime(rs.Name)
r.Address = rs.Address

return nil
}

// if RuntimeAddress is not provided, get defaults
converted, err := ConvertRuntimeToRuntimeSpec(rt)
if err != nil {
return err
}

*r = converted
default:
return fmt.Errorf("cannot determine runtime type: %s. valid values are containerd, dockershim, or crio", str)
return fmt.Errorf("invalid runtime: valid names are %s, %s, %s", RuntimeContainerd, RuntimeDockerShim, RuntimeCrio)
}

return nil
}

func (td *Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, time.Duration(*td).String())), nil
}

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

Expand All @@ -89,7 +147,7 @@ type ContainerConfig struct {
}

type ManagerConfig struct {
Runtime Runtime `json:"runtime,omitempty"`
Runtime RuntimeSpec `json:"runtime,omitempty"`
OTLPEndpoint string `json:"otlpEndpoint,omitempty"`
LogLevel string `json:"logLevel,omitempty"`
Scheduling ScheduleConfig `json:"scheduling,omitempty"`
Expand Down
16 changes: 16 additions & 0 deletions api/unversioned/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 0 additions & 41 deletions api/v1alpha1/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config

import (
"fmt"
"sync"
"time"

v1alpha1 "github.com/eraser-dev/eraser/api/v1alpha1"
Expand All @@ -29,46 +28,6 @@ severities:
- LOW
`

type Manager struct {
mtx sync.Mutex
cfg *v1alpha1.EraserConfig
}

func (m *Manager) Read() (v1alpha1.EraserConfig, error) {
m.mtx.Lock()
defer m.mtx.Unlock()

if m.cfg == nil {
return v1alpha1.EraserConfig{}, fmt.Errorf("ConfigManager configuration is nil, aborting")
}

cfg := *m.cfg
return cfg, nil
}

func (m *Manager) Update(newC *v1alpha1.EraserConfig) error {
m.mtx.Lock()
defer m.mtx.Unlock()

if m.cfg == nil {
return fmt.Errorf("ConfigManager configuration is nil, aborting")
}

if newC == nil {
return fmt.Errorf("new configuration is nil, aborting")
}

*m.cfg = *newC
return nil
}

func NewManager(cfg *v1alpha1.EraserConfig) *Manager {
return &Manager{
mtx: sync.Mutex{},
cfg: cfg,
}
}

const (
noDelay = v1alpha1.Duration(0)
oneDay = v1alpha1.Duration(time.Hour * 24)
Expand Down
39 changes: 39 additions & 0 deletions api/v1alpha1/custom_conversions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package v1alpha1

import (
unversioned "github.com/eraser-dev/eraser/api/unversioned"
conversion "k8s.io/apimachinery/pkg/conversion"
)

//nolint:revive
func Convert_v1alpha1_ManagerConfig_To_unversioned_ManagerConfig(in *ManagerConfig, out *unversioned.ManagerConfig, s conversion.Scope) error {
return autoConvert_v1alpha1_ManagerConfig_To_unversioned_ManagerConfig(in, out, s)
}

//nolint:revive
func manualConvert_v1alpha1_Runtime_To_unversioned_RuntimeSpec(in *Runtime, out *unversioned.RuntimeSpec, _ conversion.Scope) error {
out.Name = unversioned.Runtime(string(*in))
out.Address = ""
return nil
}

//nolint:revive
func Convert_v1alpha1_Runtime_To_unversioned_RuntimeSpec(in *Runtime, out *unversioned.RuntimeSpec, s conversion.Scope) error {
return manualConvert_v1alpha1_Runtime_To_unversioned_RuntimeSpec(in, out, s)
}

//nolint:revive
func Convert_unversioned_ManagerConfig_To_v1alpha1_ManagerConfig(in *unversioned.ManagerConfig, out *ManagerConfig, s conversion.Scope) error {
return autoConvert_unversioned_ManagerConfig_To_v1alpha1_ManagerConfig(in, out, s)
}

//nolint:revive
func manualConvert_unversioned_RuntimeSpec_To_v1alpha1_Runtime(in *unversioned.RuntimeSpec, out *Runtime, _ conversion.Scope) error {
*out = Runtime(in.Name)
return nil
}

//nolint:revive
func Convert_unversioned_RuntimeSpec_To_v1alpha1_Runtime(in *unversioned.RuntimeSpec, out *Runtime, s conversion.Scope) error {
return manualConvert_unversioned_RuntimeSpec_To_v1alpha1_Runtime(in, out, s)
}
48 changes: 26 additions & 22 deletions api/v1alpha1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading