Skip to content

Commit

Permalink
fix: kubectl
Browse files Browse the repository at this point in the history
  • Loading branch information
xmapst committed Dec 19, 2024
1 parent 6ddac0f commit 185036b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
51 changes: 33 additions & 18 deletions internal/worker/runner/k8s/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"errors"
"fmt"
"os"
"runtime/debug"
"sync"
"time"

"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
Expand All @@ -18,20 +18,22 @@ import (
"github.com/xmapst/AutoExecFlow/internal/storage"
"github.com/xmapst/AutoExecFlow/internal/worker/common"
"github.com/xmapst/AutoExecFlow/internal/worker/runner/k8s/types"
"github.com/xmapst/AutoExecFlow/pkg/logx"
)

type SKubectl struct {
kubeConf *rest.Config
client *kubernetes.Clientset
dynamicClient *dynamic.DynamicClient
storage storage.IStep
subCommand string
workspace string
Config string `json:"kube_config" yaml:"KubeConfig"`
Namespace string `json:"namespace" yaml:"Namespace"`
ImageTag string `json:"image_tag" yaml:"ImageTag"`
IgnoreInitContainer *bool `json:"ignore_init_container" yaml:"IgnoreInitContainer"`
Resources []*types.SResource `json:"resources" yaml:"Resources"`
kubeConf *rest.Config
client *kubernetes.Clientset
dynamicClient *dynamic.DynamicClient
storage storage.IStep
subCommand string
workspace string

Config string `json:"kube_config"`
Namespace string `json:"namespace"`
ImageTag string `json:"image_tag"`
IgnoreInitContainer *bool `json:"ignoreInitContainer"`
Resources []*types.SResource `json:"resources"`
}

func New(storage storage.IStep, command, workspace string) (*SKubectl, error) {
Expand All @@ -42,15 +44,20 @@ func New(storage storage.IStep, command, workspace string) (*SKubectl, error) {
}, nil
}

func (k *SKubectl) init() error {
func (k *SKubectl) init() (err error) {
defer func() {
r := recover()
if r != nil {
logx.Errorln(string(debug.Stack()), r)
err = fmt.Errorf("panic: %s", r)
}
}()
content, err := k.storage.Content()
if err != nil {
return err
}
if err = json.Unmarshal([]byte(content), k); err != nil {
if err = yaml.Unmarshal([]byte(content), k); err != nil {
return err
}
if err = yaml.Unmarshal([]byte(content), k); err != nil {
return err
}

if k.Config == "" {
Expand Down Expand Up @@ -101,10 +108,18 @@ func (k *SKubectl) init() error {
k.Resources[kk].IgnoreInitContainer = k.IgnoreInitContainer
}
}
return nil
return
}

func (k *SKubectl) Run(ctx context.Context) (code int64, err error) {
defer func() {
r := recover()
if r != nil {
logx.Errorln(string(debug.Stack()), r)
err = fmt.Errorf("panic: %s", r)
code = common.CodeSystemErr
}
}()
timeout, err := k.storage.Timeout()
if err != nil {
return common.CodeSystemErr, err
Expand Down
18 changes: 9 additions & 9 deletions internal/worker/runner/k8s/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import (
)

type SResource struct {
Kind string `yaml:"Kind" json:"kind"`
Namespace string `yaml:"Namespace" json:"namespace"`
Name string `yaml:"Name" json:"name"`
Replicas *int32 `yaml:"Replicas" json:"replicas"`
ImageTag string `yaml:"ImageTag" json:"image_tag"`
IgnoreInitContainer *bool `json:"ignore_init_container" yaml:"IgnoreInitContainer"`
Env []Env `yaml:"Env" json:"env"`
Kind string `json:"kind"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Replicas *int32 `json:"replicas"`
ImageTag string `json:"image_tag"`
IgnoreInitContainer *bool `json:"ignoreInitContainer"`
Env []Env `json:"env"`
}

type Env struct {
Containers []string `yaml:"Containers" json:"containers"`
Operator Operator `yaml:"Operator" json:"operator"`
Containers []string `json:"containers"`
Operator Operator `json:"operator"`
corev1.EnvVar
}

Expand Down

0 comments on commit 185036b

Please sign in to comment.