-
-
Notifications
You must be signed in to change notification settings - Fork 0
Implemented the APIs based on template #15
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import ( | |
"github.com/docker/docker/api/types/network" | ||
"github.com/docker/docker/client" | ||
|
||
"errors" | ||
contract "sandbox/api/gen" | ||
"sandbox/internal" | ||
) | ||
|
@@ -58,10 +59,17 @@ type ContainerOrchestrator interface { | |
Boot() error | ||
GetTemplates() []BuiltImage | ||
GetContainer(ctx context.Context, id string) (*StartedContainer, error) | ||
AddTemplate(cfg contract.ImageConfig) error | ||
KillAll() | ||
KillContainer(StartedContainer) error | ||
} | ||
|
||
type Storage interface { | ||
SaveTemplates([]BuiltImage) error | ||
LoadTemplates() ([]BuiltImage, error) | ||
DeleteTemplate(id string) error | ||
} | ||
|
||
type CodenireOrchestrator struct { | ||
sync.Mutex | ||
numSysWorkers int | ||
|
@@ -75,9 +83,10 @@ type CodenireOrchestrator struct { | |
isolated bool | ||
|
||
dockerFilesPath string | ||
storage Storage | ||
} | ||
|
||
func NewCodenireOrchestrator() *CodenireOrchestrator { | ||
func NewCodenireOrchestrator(storage Storage) *CodenireOrchestrator { | ||
c, err := client.NewClientWithOpts(client.WithVersion("1.41")) | ||
if err != nil { | ||
panic("fail on createDB docker client") | ||
|
@@ -92,11 +101,17 @@ func NewCodenireOrchestrator() *CodenireOrchestrator { | |
idleContainersCount: *replicaContainerCnt, | ||
dockerFilesPath: *dockerFilesPath, | ||
isolated: *isolated, | ||
storage: storage, | ||
} | ||
} | ||
|
||
func (m *CodenireOrchestrator) Prepare() error { | ||
templates := parseConfigFiles(m.dockerFilesPath) | ||
loadedTemplates, err := m.storage.LoadTemplates() //loads the template from storage | ||
if err == nil && len(loadedTemplates) > 0 { | ||
m.imgs = loadedTemplates | ||
return nil | ||
} | ||
templates := parseConfigFiles(m.dockerFilesPath) //if no templates are loaded from storage, parse the config files | ||
|
||
for _, t := range templates { | ||
err := m.prebuildImage(t, m.dockerFilesPath) | ||
|
@@ -105,7 +120,7 @@ func (m *CodenireOrchestrator) Prepare() error { | |
continue | ||
} | ||
} | ||
|
||
m.storage.SaveTemplates(m.imgs) | ||
return nil | ||
} | ||
|
||
|
@@ -129,7 +144,29 @@ func (m *CodenireOrchestrator) Boot() (err error) { | |
} | ||
|
||
func (m *CodenireOrchestrator) GetTemplates() []BuiltImage { | ||
return m.imgs | ||
templates, err := m.storage.LoadTemplates() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currentrly, GetTemplates() need for work Playground and should be connect to running images. So we can do it lately, when all logic with storage wiil be done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that makes sense. We'll update it once the storage logic is complete. |
||
if err != nil { | ||
log.Println("Failed to load templates:", err) | ||
return nil | ||
} | ||
return templates //returns empty list on error. hence returns maximum one value ie []BuildImages | ||
} | ||
|
||
func (m *CodenireOrchestrator) GetTemplateByID(id string) (*BuiltImage, error) { | ||
templates, err := m.storage.LoadTemplates() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, t := range templates { | ||
if t.imageID != nil && *t.imageID == id { // Convert pointer to string for comparison | ||
return &t, nil | ||
} | ||
} | ||
return nil, errors.New("template not found") | ||
} | ||
func (m *CodenireOrchestrator) DeleteTemplate(id string) error { | ||
return m.storage.DeleteTemplate(id) | ||
} | ||
|
||
func (m *CodenireOrchestrator) GetContainer(ctx context.Context, id string) (*StartedContainer, error) { | ||
|
@@ -141,6 +178,54 @@ func (m *CodenireOrchestrator) GetContainer(ctx context.Context, id string) (*St | |
} | ||
} | ||
|
||
func (m *CodenireOrchestrator) AddTemplate(cfg contract.ImageConfig) error { | ||
templates, err := m.storage.LoadTemplates() | ||
if err != nil { | ||
log.Println("Failed to load templates:", err) | ||
return err | ||
} | ||
for _, t := range templates { | ||
if t.Template == cfg.Template { | ||
return errors.New("template already exists") | ||
} | ||
} | ||
newImage := BuiltImage{ | ||
ImageConfig: cfg, | ||
} | ||
templates = append(templates, newImage) | ||
return m.storage.SaveTemplates(templates) | ||
|
||
} | ||
|
||
func (m *CodenireOrchestrator) runTemplate(id string) (*StartedContainer, error) { | ||
template, err := m.GetTemplateByID(id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return m.runSndContainer(*template) | ||
} | ||
|
||
func (m *CodenireOrchestrator) updateTemplate(id string, newConfig contract.ImageConfig) error { | ||
template, err := m.storage.LoadTemplates() | ||
if err != nil { | ||
log.Println("Failed to load templates:", err) | ||
return err | ||
} | ||
|
||
found := false | ||
for i, t := range template { | ||
if t.Template == id { | ||
template[i].ImageConfig = newConfig | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
return errors.New("template not found") | ||
} | ||
return m.storage.SaveTemplates(template) | ||
} | ||
|
||
func (m *CodenireOrchestrator) KillAll() { | ||
m.Lock() | ||
defer m.Unlock() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this and other API code to separate file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By other API code, do you mean moving the handlers to a separate file too, or just the logic related functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean move code with API and storage from manager which control Docker images, networks and containers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. I'll move the API handlers and storage logic into separate files