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: customize k8s resources namespace based on extensions #1060

Merged
merged 1 commit into from
Apr 22, 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
4 changes: 2 additions & 2 deletions pkg/apis/api.kusion.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ type Extension struct {
Kind ExtensionKind `yaml:"kind" json:"kind"`

// The KubeNamespaceExtension
KubeNamespace KubeNamespaceExtension `yaml:",inline,omitempty" json:",inline,omitempty"`
KubeNamespace KubeNamespaceExtension `yaml:"kubernetesNamespace,omitempty" json:"kubernetesNamespace,omitempty"`

// The KubeMetadataExtension
KubeMetadata KubeMetadataExtension `yaml:",inline,omitempty" json:",inline,omitempty"`
KubeMetadata KubeMetadataExtension `yaml:"kubernetesMetadata,omitempty" json:"kubernetesMetadata,omitempty"`
}

// KubeNamespaceExtension allows you to override kubernetes namespace.
Expand Down
49 changes: 49 additions & 0 deletions pkg/engine/api/builders/appconfig_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package builders
import (
"fmt"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"kcl-lang.io/kpm/pkg/api"

v1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
Expand Down Expand Up @@ -51,5 +52,53 @@ func (acg *AppsConfigBuilder) Build(kclPackage *api.KclPackage, project *v1.Proj
return nil, err
}

// updates generated spec resources based on project and stack extensions.
patchResourcesWithExtensions(project, stack, i)

return i, nil
}

// patchResourcesWithExtensions updates generated spec resources based on project and stack extensions.
func patchResourcesWithExtensions(project *v1.Project, stack *v1.Stack, spec *v1.Spec) {
extensions := mergeExtensions(project, stack)
if len(extensions) == 0 {
return
}

for _, extension := range extensions {
switch extension.Kind {
case v1.KubernetesNamespace:
patchResourcesKubeNamespace(spec, extension.KubeNamespace.Namespace)
default:
// do nothing
}
}
}

func patchResourcesKubeNamespace(spec *v1.Spec, namespace string) {
for _, resource := range spec.Resources {
if resource.Type == v1.Kubernetes {
u := &unstructured.Unstructured{Object: resource.Attributes}
u.SetNamespace(namespace)
}
}
}

func mergeExtensions(project *v1.Project, stack *v1.Stack) []*v1.Extension {
var extensions []*v1.Extension
extensionKindMap := make(map[string]struct{})
if stack.Extensions != nil && len(stack.Extensions) != 0 {
for _, extension := range stack.Extensions {
extensions = append(extensions, extension)
extensionKindMap[string(extension.Kind)] = struct{}{}
}
}
if project.Extensions != nil && len(project.Extensions) != 0 {
for _, extension := range project.Extensions {
if _, exist := extensionKindMap[string(extension.Kind)]; !exist {
extensions = append(extensions, extension)
}
}
}
return extensions
}
Loading