Skip to content

Commit ebc034a

Browse files
authored
feat: support setting kubeconfig in a stack dimension (#593)
1 parent 2a8b81c commit ebc034a

File tree

13 files changed

+46
-23
lines changed

13 files changed

+46
-23
lines changed

pkg/engine/operation/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (ao *ApplyOperation) Apply(request *ApplyRequest) (rsp *ApplyResponse, st s
8888

8989
resources := request.Spec.Resources
9090
resources = append(resources, priorState.Resources...)
91-
runtimesMap, s := runtimeinit.Runtimes(resources)
91+
runtimesMap, s := runtimeinit.Runtimes(resources, o.Stack)
9292
if status.IsErr(s) {
9393
return nil, s
9494
}

pkg/engine/operation/apply_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestOperation_Apply(t *testing.T) {
176176
o.ResultState = rs
177177
return nil
178178
}).Build()
179-
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
179+
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
180180
return map[models.Type]runtime.Runtime{runtime.Kubernetes: &kubernetes.KubernetesRuntime{}}, nil
181181
}).Build()
182182

pkg/engine/operation/destory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func (do *DestroyOperation) Destroy(request *DestroyRequest) (st status.Status)
5454

5555
// only destroy resources we have recorded
5656
resources := priorState.Resources
57-
runtimesMap, s := runtimeinit.Runtimes(resources)
57+
runtimesMap, s := runtimeinit.Runtimes(resources, o.Stack)
5858
if status.IsErr(s) {
5959
return s
6060
}

pkg/engine/operation/preview.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (po *PreviewOperation) Preview(request *PreviewRequest) (rsp *PreviewRespon
6464
// Kusion is a multi-runtime system. We initialize runtimes dynamically by resource types
6565
resources := request.Spec.Resources
6666
resources = append(resources, priorState.Resources...)
67-
runtimesMap, s := runtimeinit.Runtimes(resources)
67+
runtimesMap, s := runtimeinit.Runtimes(resources, o.Stack)
6868
if status.IsErr(s) {
6969
return nil, s
7070
}

pkg/engine/operation/preview_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func TestOperation_Preview(t *testing.T) {
264264
},
265265
}
266266

267-
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
267+
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
268268
return map[models.Type]runtime.Runtime{runtime.Kubernetes: &fakePreviewRuntime{}}, nil
269269
}).Build()
270270
gotRsp, gotS := o.Preview(tt.args.request)

pkg/engine/operation/watch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {
3636

3737
// init runtimes
3838
resources := req.Spec.Resources
39-
runtimes, s := runtimeinit.Runtimes(resources)
39+
runtimes, s := runtimeinit.Runtimes(resources, wo.Stack)
4040
if status.IsErr(s) {
4141
return errors.New(s.Message())
4242
}

pkg/engine/operation/watch_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"kusionstack.io/kusion/pkg/engine/runtime"
1414
runtimeinit "kusionstack.io/kusion/pkg/engine/runtime/init"
1515
"kusionstack.io/kusion/pkg/models"
16+
"kusionstack.io/kusion/pkg/projectstack"
1617
"kusionstack.io/kusion/pkg/status"
1718
)
1819

@@ -31,7 +32,7 @@ func TestWatchOperation_Watch(t *testing.T) {
3132
},
3233
},
3334
}
34-
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
35+
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
3536
return map[models.Type]runtime.Runtime{runtime.Kubernetes: fooRuntime}, nil
3637
}).Build()
3738
wo := &WatchOperation{opsmodels.Operation{RuntimeMap: map[models.Type]runtime.Runtime{runtime.Kubernetes: fooRuntime}}}

pkg/engine/runtime/init/init.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"kusionstack.io/kusion/pkg/engine/runtime/kubernetes"
99
"kusionstack.io/kusion/pkg/engine/runtime/terraform"
1010
"kusionstack.io/kusion/pkg/models"
11+
"kusionstack.io/kusion/pkg/projectstack"
1112
"kusionstack.io/kusion/pkg/status"
1213
)
1314

@@ -17,9 +18,9 @@ var SupportRuntimes = map[models.Type]InitFn{
1718
}
1819

1920
// InitFn runtime init func
20-
type InitFn func() (runtime.Runtime, error)
21+
type InitFn func(stack *projectstack.Stack) (runtime.Runtime, error)
2122

22-
func Runtimes(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
23+
func Runtimes(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
2324
runtimesMap := map[models.Type]runtime.Runtime{}
2425
if resources == nil {
2526
return runtimesMap, nil
@@ -35,7 +36,7 @@ func Runtimes(resources models.Resources) (map[models.Type]runtime.Runtime, stat
3536
return nil, status.NewErrorStatusWithCode(status.IllegalManifest, fmt.Errorf("unknow resource type: %s. Currently supported resource types are: %v",
3637
rt, reflect.ValueOf(SupportRuntimes).MapKeys()))
3738
} else if runtimesMap[rt] == nil {
38-
r, err := SupportRuntimes[rt]()
39+
r, err := SupportRuntimes[rt](stack)
3940
if err != nil {
4041
return nil, status.NewErrorStatus(fmt.Errorf("init %s runtime failed. %w", rt, err))
4142
}

pkg/engine/runtime/kubernetes/kubernetes_runtime.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"kusionstack.io/kusion/pkg/engine/runtime"
3131
"kusionstack.io/kusion/pkg/log"
3232
"kusionstack.io/kusion/pkg/models"
33+
"kusionstack.io/kusion/pkg/projectstack"
3334
"kusionstack.io/kusion/pkg/status"
3435
jsonutil "kusionstack.io/kusion/pkg/util/json"
3536
"kusionstack.io/kusion/pkg/util/kube/config"
@@ -43,8 +44,8 @@ type KubernetesRuntime struct {
4344
}
4445

4546
// NewKubernetesRuntime create a new KubernetesRuntime
46-
func NewKubernetesRuntime() (runtime.Runtime, error) {
47-
client, mapper, err := getKubernetesClient()
47+
func NewKubernetesRuntime(stack *projectstack.Stack) (runtime.Runtime, error) {
48+
client, mapper, err := getKubernetesClient(stack)
4849
if err != nil {
4950
return nil, err
5051
}
@@ -375,9 +376,9 @@ func (k *KubernetesRuntime) Watch(ctx context.Context, request *runtime.WatchReq
375376
}
376377

377378
// getKubernetesClient get kubernetes client
378-
func getKubernetesClient() (dynamic.Interface, meta.RESTMapper, error) {
379+
func getKubernetesClient(stack *projectstack.Stack) (dynamic.Interface, meta.RESTMapper, error) {
379380
// build config
380-
cfg, err := clientcmd.BuildConfigFromFlags("", config.GetKubeConfig())
381+
cfg, err := clientcmd.BuildConfigFromFlags("", config.GetKubeConfig(stack))
381382
if err != nil {
382383
return nil, nil, err
383384
}

pkg/engine/runtime/terraform/terraform_runtime.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"kusionstack.io/kusion/pkg/engine/runtime/terraform/tfops"
1313
"kusionstack.io/kusion/pkg/log"
1414
"kusionstack.io/kusion/pkg/models"
15+
"kusionstack.io/kusion/pkg/projectstack"
1516
"kusionstack.io/kusion/pkg/status"
1617
)
1718

@@ -22,7 +23,7 @@ type TerraformRuntime struct {
2223
mu *sync.Mutex
2324
}
2425

25-
func NewTerraformRuntime() (runtime.Runtime, error) {
26+
func NewTerraformRuntime(_ *projectstack.Stack) (runtime.Runtime, error) {
2627
fs := afero.Afero{Fs: afero.NewOsFs()}
2728
ws := tfops.NewWorkSpace(fs)
2829
TFRuntime := &TerraformRuntime{

0 commit comments

Comments
 (0)