Skip to content

Commit

Permalink
inkctl exec support box resource
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Feb 4, 2024
1 parent fffe0a4 commit 032ea9b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 12 deletions.
112 changes: 101 additions & 11 deletions core/command/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"context"
"errors"
"fmt"
"maps"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -472,25 +474,113 @@ func exec(cmd *cobra.Command, _ []string) error {

allSecrets := make([]*v1.Secret, 0)
for _, obj := range objSet[v1.KindSecret] {
var secret v1.Secret
if err := obj.ToObject(&secret); err != nil {
var item v1.Secret
if err := obj.ToObject(&item); err != nil {
return err
}
allSecrets = append(allSecrets, &secret)
allSecrets = append(allSecrets, &item)
}

allWorkflows := make([]*v1.Workflow, 0)
for _, obj := range objSet[v1.KindWorkflow] {
var item v1.Workflow
if err := obj.ToObject(&item); err != nil {
return err
}
allWorkflows = append(allWorkflows, &item)
}

allBoxes := make([]*v1.Box, 0)
for _, obj := range objSet[v1.KindBox] {
var box v1.Box
if err := obj.ToObject(&box); err != nil {
return err
}
allBoxes = append(allBoxes, &box)
}
if len(allBoxes) == 0 {
return execBuild(wc, allWorkflows, allSecrets, settings)
}

for _, box := range allBoxes {
currentSettings := make(map[string]string)
maps.Copy(currentSettings, box.Settings)
maps.Copy(currentSettings, settings)

var workflows []*v1.Workflow
workflowNames, workflowSelectors := box.GetSelectors(v1.KindWorkflow, currentSettings)
if len(workflowNames) > 0 {
containAll := slices.Contains(workflowNames, "")
for _, item := range allWorkflows {
if !containAll && !slices.Contains(workflowNames, item.GetName()) {
continue
}

matched := true
for _, ss := range workflowSelectors {
matched = ss.Match(item.Labels)
if matched {
break
}
}
if !matched {
continue
}
workflows = append(workflows, item)
}
}
if len(workflows) == 0 {
continue
}

var secrets []*v1.Secret
secretNames, secretSelector := box.GetSelectors(v1.KindSecret, currentSettings)
if len(secretNames) > 0 {
containAll := slices.Contains(secretNames, "")
for _, item := range allSecrets {
if !containAll && !slices.Contains(secretNames, item.GetName()) {
continue
}

matched := true
for _, ss := range secretSelector {
matched = ss.Match(item.Labels)
if matched {
break
}
}
if !matched {
continue
}
secrets = append(secrets, item)
}
}
if err := execBuild(wc, workflows, secrets, currentSettings); err != nil {
return err
}
}
return nil
}

func execBuild(
wc clients.WorkerV1,
allWorkflows []*v1.Workflow,
allSecrets []*v1.Secret,
settings map[string]string,
) error {
dataCh := make(chan *v1.Data)

build := &v1.Build{
Phase: v1.PhasePending,
Settings: settings,
}

// TODO 暂时仅支持 workflow 单独串行执行
for _, obj := range objSet[v1.KindWorkflow] {
var workflow v1.Workflow
if err := obj.ToObject(&workflow); err != nil {
return err
}

var hook worker.Hook
for _, workflow := range allWorkflows {
var (
hook worker.Hook
err error
)
workerObj := workflow.Worker()
switch workerObj.Kind {
case v1.WorkerKindHost:
Expand All @@ -517,7 +607,7 @@ func exec(cmd *cobra.Command, _ []string) error {

data := &v1.Data{
Build: build,
Workflow: &workflow,
Workflow: workflow,
Status: &v1.Stage{
Number: 1,
Phase: v1.PhasePending,
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/core/v1/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
type Box struct {
Metadata `yaml:",inline"`

Resources []BoxResource `json:"resources" yaml:"resources"`
Resources []BoxResource `json:"resources" yaml:"resources"`
Settings map[string]string `json:"settings,omitempty" yaml:"settings,omitempty"`
}

func (b *Box) GetSelectors(kind string, settings map[string]string) (names []string, selectors []*selector.Selector) {
Expand Down

0 comments on commit 032ea9b

Please sign in to comment.