Skip to content

Commit

Permalink
feat: add v1alpha3 api with RuntimeSpec configuration (#922)
Browse files Browse the repository at this point in the history
Signed-off-by: ashnamehrotra <[email protected]>
  • Loading branch information
ashnamehrotra authored Dec 14, 2023
1 parent f6e9204 commit afb831b
Show file tree
Hide file tree
Showing 24 changed files with 1,542 additions and 150 deletions.
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

0 comments on commit afb831b

Please sign in to comment.