Skip to content
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

feat: add context in the workspace #1113

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 16 additions & 49 deletions pkg/apis/api.kusion.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ type Workspace struct {
// Modules are the configs of a set of modules.
Modules ModuleConfigs `yaml:"modules,omitempty" json:"modules,omitempty"`

// Runtimes are the configs of a set of runtimes.
Runtimes *RuntimeConfigs `yaml:"runtimes,omitempty" json:"runtimes,omitempty"`

// SecretStore represents a secure external location for storing secrets.
SecretStore *SecretStoreSpec `yaml:"secretStore,omitempty" json:"secretStore,omitempty"`

// Context contains workspace-level configurations, such as topologies, server endpoints, metadata, etc.
Context GenericConfig `yaml:"context,omitempty" json:"context,omitempty"`
}

// ModuleConfigs is a set of multiple ModuleConfig, whose key is the module name.
Expand All @@ -114,17 +114,19 @@ type GenericConfig map[string]any
//
// Take the ModuleConfig of "mysql" for an example, which is shown as below:
//
// config := ModuleConfig {
// "path": "ghcr.io/kusionstack/mysql"
// config := ModuleConfig{
// "path": "ghcr.io/kusionstack/mysql"
// "version": "0.1.0"
// "default": {
// "type": "aws",
// "version": "5.7",
// "instanceType": "db.t3.micro",
// },
// "smallClass": {
// "instanceType": "db.t3.small",
// "projectSelector": []string{"foo", "bar"},
// "configs": {
// "default": {
// "type": "aws",
// "version": "5.7",
// "instanceType": "db.t3.micro",
// },
// "smallClass": {
// "instanceType": "db.t3.small",
// "projectSelector": []string{"foo", "bar"},
// },
// },
// }
type ModuleConfig struct {
Expand All @@ -150,45 +152,10 @@ type ModulePatcherConfigs map[string]*ModulePatcherConfig
type ModulePatcherConfig struct {
// GenericConfig contains the module configs.
GenericConfig `yaml:",inline" json:",inline"`

// ProjectSelector contains the selected projects.
ProjectSelector []string `yaml:"projectSelector" json:"projectSelector"`
}

// RuntimeConfigs contains a set of runtime config.
type RuntimeConfigs struct {
// Kubernetes contains the config to access a kubernetes cluster.
Kubernetes *KubernetesConfig `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty"`

// Terraform contains the config of multiple terraform providers.
Terraform TerraformConfig `yaml:"terraform,omitempty" json:"terraform,omitempty"`
}

// KubernetesConfig contains config to access a kubernetes cluster.
type KubernetesConfig struct {
// KubeConfig is the path of the kubeconfig file.
KubeConfig string `yaml:"kubeConfig" json:"kubeConfig"`
}

// TerraformConfig contains the config of multiple terraform provider config, whose key is
// the provider name.
type TerraformConfig map[string]*ProviderConfig

// ProviderConfig contains the full configurations of a specified provider. It is the combination
// of the specified provider's config in blocks "terraform/required_providers" and "providers" in
// terraform hcl file, where the former is described by fields Source and Version, and the latter
// is described by GenericConfig cause different provider has different config.
type ProviderConfig struct {
// Source of the provider.
Source string `yaml:"source" json:"source"`

// Version of the provider.
Version string `yaml:"version" json:"version"`

// GenericConfig is used to describe the config of a specified terraform provider.
GenericConfig `yaml:",inline,omitempty" json:",inline,omitempty"`
}

type ExtensionKind string

const (
Expand Down Expand Up @@ -358,7 +325,7 @@ type Resource struct {
// providerNamespace:providerName:resourceType:resourceName for Terraform resources
ID string `yaml:"id" json:"id"`

// Type represents all Runtimes we supported like Kubernetes and Terraform
// Type represents all Context we supported like Kubernetes and Terraform
Type Type `yaml:"type" json:"type"`

// Attributes represents all specified attributes of this resource
Expand Down
6 changes: 3 additions & 3 deletions pkg/engine/api/builders/appconfig_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func buildMockApp() (string, *internalv1.AppConfiguration) {
func buildMockWorkspace() *v1.Workspace {
return &v1.Workspace{
Name: "test",
Runtimes: &v1.RuntimeConfigs{
Kubernetes: &v1.KubernetesConfig{
KubeConfig: "/etc/kubeconfig.yaml",
Context: map[string]any{
"Kubernetes": map[string]string{
"Config": "/etc/kubeconfig.yaml",
},
},
}
Expand Down
4 changes: 0 additions & 4 deletions pkg/modules/generators/app_configurations_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,6 @@ func (g *appConfigurationGenerator) Generate(spec *v1.Spec) error {
return err
}

// Add kubeConfig from workspace if exist
modules.AddKubeConfigIf(spec, g.ws)
return nil
}

Expand Down Expand Up @@ -316,7 +314,6 @@ func (g *appConfigurationGenerator) callModules(
}

// parse module result
// todo extract to a method
for _, res := range response.Resources {
temp := &v1.Resource{}
err = yaml.Unmarshal(res, temp)
Expand All @@ -327,7 +324,6 @@ func (g *appConfigurationGenerator) callModules(
}

// parse patcher
// todo extract to a method
for _, patcher := range response.Patchers {
temp := &internalv1.Patcher{}
err = yaml.Unmarshal(patcher, temp)
Expand Down
6 changes: 3 additions & 3 deletions pkg/modules/generators/app_configurations_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ func buildMockWorkspace(namespace string) *v1.Workspace {
},
},
},
Runtimes: &v1.RuntimeConfigs{
Kubernetes: &v1.KubernetesConfig{
KubeConfig: "/etc/kubeconfig.yaml",
Context: map[string]any{
"Kubernetes": map[string]string{
"Config": "/etc/kubeconfig.yaml",
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/modules/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (p *Plugin) initModule() error {
key := p.key
split := strings.Split(key, "@")
msg := "init module failed. Invalid plugin module key: %s. " +
"The correct format for a key should be as follows: namespace/moduleName@version. e.g. kusionstack/mysql@v0.1.0"
"The correct format for a key should be as follows: org/moduleName@version. e.g. kusionstack/mysql@v0.1.0"
if len(split) != 2 {
return fmt.Errorf(msg, key)
}
Expand Down
20 changes: 0 additions & 20 deletions pkg/modules/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"

v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
"kusionstack.io/kusion/pkg/workspace"
)

// CallGeneratorFuncs calls each NewGeneratorFunc in the given slice
Expand Down Expand Up @@ -167,22 +166,3 @@ func PatchResource[T any](resources map[string][]*v1.Resource, gvk string, patch
}
return nil
}

// AddKubeConfigIf adds kubeConfig from workspace to extensions of Kubernetes type resource in intent.
// If there is already has kubeConfig in extensions, use the kubeConfig in extensions.
func AddKubeConfigIf(i *v1.Spec, ws *v1.Workspace) {
config := workspace.GetKubernetesConfig(ws.Runtimes)
if config == nil || config.KubeConfig == "" {
return
}
for n, resource := range i.Resources {
if resource.Type == v1.Kubernetes {
if resource.Extensions == nil {
i.Resources[n].Extensions = make(map[string]any)
}
if extensionsKubeConfig, ok := resource.Extensions[v1.ResourceExtensionKubeConfig]; !ok || extensionsKubeConfig == "" {
i.Resources[n].Extensions[v1.ResourceExtensionKubeConfig] = config.KubeConfig
}
}
}
}
118 changes: 0 additions & 118 deletions pkg/modules/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@ func TestAddKubeConfigIf(t *testing.T) {
name: "empty kubeConfig in workspace",
ws: &v1.Workspace{
Name: "dev",
Runtimes: &v1.RuntimeConfigs{
Kubernetes: &v1.KubernetesConfig{},
},
},
i: &v1.Spec{
Resources: v1.Resources{
Expand All @@ -289,125 +286,10 @@ func TestAddKubeConfigIf(t *testing.T) {
},
},
},
{
name: "add kubeConfig",
ws: &v1.Workspace{
Name: "dev",
Runtimes: &v1.RuntimeConfigs{
Kubernetes: &v1.KubernetesConfig{
KubeConfig: "/etc/kubeConfig.yaml",
},
},
},
i: &v1.Spec{
Resources: v1.Resources{
{
ID: "mock-id-1",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: nil,
},
{
ID: "mock-id-2",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: map[string]any{
"mock-extensions-key": "mock-extensions-value",
},
},
{
ID: "mock-id-2",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: map[string]any{
"kubeConfig": "/etc/should-use-kubeConfig.yaml",
},
},
{
ID: "mock-id-3",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: map[string]any{
"kubeConfig": "",
},
},
{
ID: "mock-id-4",
Type: "Terraform",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: nil,
},
},
},
expectedSpec: &v1.Spec{
Resources: v1.Resources{
{
ID: "mock-id-1",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: map[string]any{
"kubeConfig": "/etc/kubeConfig.yaml",
},
},
{
ID: "mock-id-2",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: map[string]any{
"mock-extensions-key": "mock-extensions-value",
"kubeConfig": "/etc/kubeConfig.yaml",
},
},
{
ID: "mock-id-2",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: map[string]any{
"kubeConfig": "/etc/should-use-kubeConfig.yaml",
},
},
{
ID: "mock-id-3",
Type: "Kubernetes",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: map[string]any{
"kubeConfig": "/etc/kubeConfig.yaml",
},
},
{
ID: "mock-id-4",
Type: "Terraform",
Attributes: map[string]any{
"mock-key": "mock-value",
},
Extensions: nil,
},
},
},
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
AddKubeConfigIf(tc.i, tc.ws)
assert.Equal(t, *tc.expectedSpec, *tc.i)
})
}
Expand Down
26 changes: 6 additions & 20 deletions pkg/workspace/storages/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,9 @@ func mockWorkspace(name string) *v1.Workspace {
},
},
},
Runtimes: &v1.RuntimeConfigs{
Kubernetes: &v1.KubernetesConfig{
KubeConfig: "/etc/kubeconfig.yaml",
},
Terraform: v1.TerraformConfig{
"aws": {
Source: "hashicorp/aws",
Version: "1.0.4",
GenericConfig: v1.GenericConfig{
"region": "us-east-1",
},
},
Context: map[string]any{
"kubernetes": v1.GenericConfig{
"config": "/etc/kubeconfig.yaml",
},
},
}
Expand All @@ -83,14 +74,9 @@ modules:
configs:
default:
type: aws
runtimes:
kubernetes:
kubeConfig: /etc/kubeconfig.yaml
terraform:
aws:
source: hashicorp/aws
version: 1.0.4
region: us-east-1
context:
kubernetes:
config: /etc/kubeconfig.yaml
`
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ modules:
configs:
default:
type: aws
runtimes:
context:
kubernetes:
kubeConfig: /etc/kubeconfig.yaml
terraform:
aws:
source: hashicorp/aws
version: 1.0.4
region: us-east-1
config: /etc/kubeconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ modules:
configs:
default:
type: aws
runtimes:
kubernetes:
kubeConfig: /etc/kubeconfig.yaml
terraform:
aws:
source: hashicorp/aws
version: 1.0.4
region: us-east-1
context:
kubernetes:
config: /etc/kubeconfig.yaml
Loading
Loading