Skip to content

Commit

Permalink
feat: support setting kubeconfig in a stack dimension (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
liu-hm19 committed Nov 9, 2023
1 parent 2a8b81c commit ebc034a
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/engine/operation/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (ao *ApplyOperation) Apply(request *ApplyRequest) (rsp *ApplyResponse, st s

resources := request.Spec.Resources
resources = append(resources, priorState.Resources...)
runtimesMap, s := runtimeinit.Runtimes(resources)
runtimesMap, s := runtimeinit.Runtimes(resources, o.Stack)
if status.IsErr(s) {
return nil, s
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestOperation_Apply(t *testing.T) {
o.ResultState = rs
return nil
}).Build()
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
return map[models.Type]runtime.Runtime{runtime.Kubernetes: &kubernetes.KubernetesRuntime{}}, nil
}).Build()

Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/destory.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (do *DestroyOperation) Destroy(request *DestroyRequest) (st status.Status)

// only destroy resources we have recorded
resources := priorState.Resources
runtimesMap, s := runtimeinit.Runtimes(resources)
runtimesMap, s := runtimeinit.Runtimes(resources, o.Stack)
if status.IsErr(s) {
return s
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (po *PreviewOperation) Preview(request *PreviewRequest) (rsp *PreviewRespon
// Kusion is a multi-runtime system. We initialize runtimes dynamically by resource types
resources := request.Spec.Resources
resources = append(resources, priorState.Resources...)
runtimesMap, s := runtimeinit.Runtimes(resources)
runtimesMap, s := runtimeinit.Runtimes(resources, o.Stack)
if status.IsErr(s) {
return nil, s
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestOperation_Preview(t *testing.T) {
},
}

mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
return map[models.Type]runtime.Runtime{runtime.Kubernetes: &fakePreviewRuntime{}}, nil
}).Build()
gotRsp, gotS := o.Preview(tt.args.request)
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (wo *WatchOperation) Watch(req *WatchRequest) error {

// init runtimes
resources := req.Spec.Resources
runtimes, s := runtimeinit.Runtimes(resources)
runtimes, s := runtimeinit.Runtimes(resources, wo.Stack)
if status.IsErr(s) {
return errors.New(s.Message())
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/engine/operation/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime"
runtimeinit "kusionstack.io/kusion/pkg/engine/runtime/init"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
)

Expand All @@ -31,7 +32,7 @@ func TestWatchOperation_Watch(t *testing.T) {
},
},
}
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
mockey.Mock(runtimeinit.Runtimes).To(func(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
return map[models.Type]runtime.Runtime{runtime.Kubernetes: fooRuntime}, nil
}).Build()
wo := &WatchOperation{opsmodels.Operation{RuntimeMap: map[models.Type]runtime.Runtime{runtime.Kubernetes: fooRuntime}}}
Expand Down
7 changes: 4 additions & 3 deletions pkg/engine/runtime/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime/kubernetes"
"kusionstack.io/kusion/pkg/engine/runtime/terraform"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
)

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

// InitFn runtime init func
type InitFn func() (runtime.Runtime, error)
type InitFn func(stack *projectstack.Stack) (runtime.Runtime, error)

func Runtimes(resources models.Resources) (map[models.Type]runtime.Runtime, status.Status) {
func Runtimes(resources models.Resources, stack *projectstack.Stack) (map[models.Type]runtime.Runtime, status.Status) {
runtimesMap := map[models.Type]runtime.Runtime{}
if resources == nil {
return runtimesMap, nil
Expand All @@ -35,7 +36,7 @@ func Runtimes(resources models.Resources) (map[models.Type]runtime.Runtime, stat
return nil, status.NewErrorStatusWithCode(status.IllegalManifest, fmt.Errorf("unknow resource type: %s. Currently supported resource types are: %v",
rt, reflect.ValueOf(SupportRuntimes).MapKeys()))
} else if runtimesMap[rt] == nil {
r, err := SupportRuntimes[rt]()
r, err := SupportRuntimes[rt](stack)
if err != nil {
return nil, status.NewErrorStatus(fmt.Errorf("init %s runtime failed. %w", rt, err))
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/engine/runtime/kubernetes/kubernetes_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
jsonutil "kusionstack.io/kusion/pkg/util/json"
"kusionstack.io/kusion/pkg/util/kube/config"
Expand All @@ -43,8 +44,8 @@ type KubernetesRuntime struct {
}

// NewKubernetesRuntime create a new KubernetesRuntime
func NewKubernetesRuntime() (runtime.Runtime, error) {
client, mapper, err := getKubernetesClient()
func NewKubernetesRuntime(stack *projectstack.Stack) (runtime.Runtime, error) {
client, mapper, err := getKubernetesClient(stack)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -375,9 +376,9 @@ func (k *KubernetesRuntime) Watch(ctx context.Context, request *runtime.WatchReq
}

// getKubernetesClient get kubernetes client
func getKubernetesClient() (dynamic.Interface, meta.RESTMapper, error) {
func getKubernetesClient(stack *projectstack.Stack) (dynamic.Interface, meta.RESTMapper, error) {
// build config
cfg, err := clientcmd.BuildConfigFromFlags("", config.GetKubeConfig())
cfg, err := clientcmd.BuildConfigFromFlags("", config.GetKubeConfig(stack))
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/engine/runtime/terraform/terraform_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"kusionstack.io/kusion/pkg/engine/runtime/terraform/tfops"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/status"
)

Expand All @@ -22,7 +23,7 @@ type TerraformRuntime struct {
mu *sync.Mutex
}

func NewTerraformRuntime() (runtime.Runtime, error) {
func NewTerraformRuntime(_ *projectstack.Stack) (runtime.Runtime, error) {
fs := afero.Afero{Fs: afero.NewOsFs()}
ws := tfops.NewWorkSpace(fs)
TFRuntime := &TerraformRuntime{
Expand Down
3 changes: 2 additions & 1 deletion pkg/projectstack/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ func (p *Project) TableReport() string {

// StackConfiguration is the stack configuration
type StackConfiguration struct {
Name string `json:"name" yaml:"name"` // Stack name
Name string `json:"name" yaml:"name"` // Stack name
KubeConfig string `json:"kubeConfig" yaml:"kubeConfig"` // KubeConfig file path for this stack
}

type Stack struct {
Expand Down
16 changes: 11 additions & 5 deletions pkg/util/kube/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"k8s.io/client-go/util/homedir"
"kusionstack.io/kusion/pkg/projectstack"
)

const (
Expand All @@ -18,12 +19,17 @@ var (
RecommendedKubeConfigFile = filepath.Join(RecommendedConfigDir, RecommendedKubeConfigFileName)
)

// 1. If $KUBECONFIG environment variable is set, then it is used it.
// 2. Otherwise, ${HOME}/.kube/config is used.
func GetKubeConfig() string {
// 1. If $KUBECONFIG environment variable is set, then it is used.
// 2. If not, and the `kubeConfig` in stack configuration is set, then it is used.
// 3. Otherwise, ${HOME}/.kube/config is used.
func GetKubeConfig(stack *projectstack.Stack) string {
if kubeConfigFile := os.Getenv(RecommendedConfigPathEnvVar); kubeConfigFile != "" {
return kubeConfigFile
} else {
return RecommendedKubeConfigFile
} else if stack.KubeConfig != "" {
kubeConfigFile, _ := filepath.Abs(stack.KubeConfig)
if kubeConfigFile != "" {
return kubeConfigFile
}
}
return RecommendedKubeConfigFile
}
16 changes: 14 additions & 2 deletions pkg/util/kube/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"
"kusionstack.io/kusion/pkg/projectstack"
)

func mockGetenv(result string) {
Expand All @@ -18,13 +19,24 @@ func mockGetenv(result string) {
}

func TestGetKubeConfig(t *testing.T) {
stack := &projectstack.Stack{
StackConfiguration: projectstack.StackConfiguration{
KubeConfig: "",
},
}

// Mock
mockey.PatchConvey("test null env config", t, func() {
mockGetenv("")
assert.Equal(t, RecommendedKubeConfigFile, GetKubeConfig())
assert.Equal(t, RecommendedKubeConfigFile, GetKubeConfig(stack))
})
mockey.PatchConvey("test env config", t, func() {
mockGetenv("test")
assert.Equal(t, "test", GetKubeConfig())
assert.Equal(t, "test", GetKubeConfig(stack))
})
mockey.PatchConvey("test stack config", t, func() {
mockGetenv("")
stack.KubeConfig = "/home/test/kubeconfig"
assert.Equal(t, "/home/test/kubeconfig", GetKubeConfig(stack))
})
}

0 comments on commit ebc034a

Please sign in to comment.