Skip to content
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
6 changes: 6 additions & 0 deletions api/v1beta1/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,12 @@ type KustomizationRef struct {
// +optional
Path string `json:"path,omitempty"`

// Components is a list of paths to Kustomize components. These paths are relative to the
// `Path` field and are included in the Kustomize build to provide reusable configuration logic.
// The paths can be static or leverage Go templates for dynamic customization.
// +optional
Components []string `json:"components,omitempty"`

// Optional indicates that the referenced resource is not mandatory.
// If set to true and the resource is not found, the error will be ignored,
// and Sveltos will continue processing other ValueFroms.
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,14 @@ spec:
be run on those paths and the outcome will be deployed.
items:
properties:
components:
description: |-
Components is a list of paths to Kustomize components. These paths are relative to the
`Path` field and are included in the Kustomize build to provide reusable configuration logic.
The paths can be static or leverage Go templates for dynamic customization.
items:
type: string
type: array
deploymentType:
default: Remote
description: |-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,14 @@ spec:
be run on those paths and the outcome will be deployed.
items:
properties:
components:
description: |-
Components is a list of paths to Kustomize components. These paths are relative to the
`Path` field and are included in the Kustomize build to provide reusable configuration logic.
The paths can be static or leverage Go templates for dynamic customization.
items:
type: string
type: array
deploymentType:
default: Remote
description: |-
Expand Down
8 changes: 8 additions & 0 deletions config/crd/bases/config.projectsveltos.io_profiles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,14 @@ spec:
be run on those paths and the outcome will be deployed.
items:
properties:
components:
description: |-
Components is a list of paths to Kustomize components. These paths are relative to the
`Path` field and are included in the Kustomize build to provide reusable configuration logic.
The paths can be static or leverage Go templates for dynamic customization.
items:
type: string
type: array
deploymentType:
default: Remote
description: |-
Expand Down
66 changes: 62 additions & 4 deletions controllers/handlers_kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"sigs.k8s.io/kustomize/api/resmap"
kustomizetypes "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/yaml"

configv1beta1 "github.com/projectsveltos/addon-controller/api/v1beta1"
"github.com/projectsveltos/addon-controller/controllers/clustercache"
Expand Down Expand Up @@ -587,6 +588,19 @@ func deployKustomizeRef(ctx context.Context, c client.Client, remoteRestConfig *

logger.V(logs.LogDebug).Info(fmt.Sprintf("using path %s", instantiatedPath))

// New: Instantiate components paths
var instantiatedComponents []string
for _, comp := range kustomizationRef.Components {
instantiatedComp, err := instantiateTemplateValues(ctx, getManagementClusterConfig(), getManagementClusterClient(),
clusterSummary, clusterSummary.GetName(), comp, objects, nil, logger)
if err != nil {
return nil, nil, err
}
instantiatedComponents = append(instantiatedComponents, instantiatedComp)
}

logger.V(logs.LogDebug).Info(fmt.Sprintf("using path %s with components %v", instantiatedPath, instantiatedComponents))

// check build path exists
dirPath := filepath.Join(tmpDir, instantiatedPath)
_, err = os.Stat(dirPath)
Expand All @@ -595,6 +609,49 @@ func deployKustomizeRef(ctx context.Context, c client.Client, remoteRestConfig *
return nil, nil, err
}

// Read the existing kustomization.yaml file
kustFilePath := filepath.Join(dirPath, "kustomization.yaml")
kustData, err := os.ReadFile(kustFilePath)
if err != nil {
return nil, nil, fmt.Errorf("failed to read kustomization.yaml: %w", err)
}

// Unmarshal the YAML into a map
var existingKust map[string]interface{}
if err := yaml.Unmarshal(kustData, &existingKust); err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal kustomization.yaml: %w", err)
}

// Add the new components to the map
if len(instantiatedComponents) > 0 {
// The components field is a list, so we need to append to it
if components, ok := existingKust["components"].([]interface{}); ok {
for _, comp := range instantiatedComponents {
components = append(components, comp)
}
existingKust["components"] = components
} else {
// If the field doesn't exist or is not a list, create a new list
var newComponents []interface{}
for _, comp := range instantiatedComponents {
newComponents = append(newComponents, comp)
}
existingKust["components"] = newComponents
}
}

// Marshal the modified map back to YAML
modifiedKustData, err := yaml.Marshal(&existingKust)
if err != nil {
return nil, nil, fmt.Errorf("failed to marshal modified kustomization: %w", err)
}

// Write the modified content back to the file
err = os.WriteFile(kustFilePath, modifiedKustData, permission0600)
if err != nil {
return nil, nil, fmt.Errorf("failed to write modified kustomization.yaml: %w", err)
}

fs := filesys.MakeFsOnDisk()

var resMap resmap.ResMap
Expand Down Expand Up @@ -756,7 +813,7 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary
objectsToDeployRemotely = make([]*unstructured.Unstructured, 0, len(resources))
for i := range resources {
resource := resources[i]
yaml, err := resource.AsYAML()
resourceYAML, err := resource.AsYAML()
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get resource YAML %v", err))
return nil, nil, nil, err
Expand All @@ -767,8 +824,9 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary
// All objects coming from Kustomize output can be expressed as template. Those will be instantiated using
// substitute values first, and the resource in the management cluster later.
templateName := fmt.Sprintf("%s-substitutevalues", clusterSummary.Name)
yaml, err = instantiateResourceWithSubstituteValues(templateName, yaml, instantiatedSubstituteValues,
funcmap.HasTextTemplateAnnotation(clusterSummary.Annotations), logger)
resourceYAML, err = instantiateResourceWithSubstituteValues(templateName, resourceYAML,
instantiatedSubstituteValues, funcmap.HasTextTemplateAnnotation(clusterSummary.Annotations),
logger)
if err != nil {
msg := fmt.Sprintf("failed to instantiate resource with substitute values: %v", err)
logger.V(logs.LogInfo).Info(msg)
Expand All @@ -777,7 +835,7 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary
}

var u *unstructured.Unstructured
u, err = k8s_utils.GetUnstructured(yaml)
u, err = k8s_utils.GetUnstructured(resourceYAML)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to get unstructured %v", err))
return nil, nil, nil, err
Expand Down
24 changes: 24 additions & 0 deletions manifest/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,14 @@ spec:
be run on those paths and the outcome will be deployed.
items:
properties:
components:
description: |-
Components is a list of paths to Kustomize components. These paths are relative to the
`Path` field and are included in the Kustomize build to provide reusable configuration logic.
The paths can be static or leverage Go templates for dynamic customization.
items:
type: string
type: array
deploymentType:
default: Remote
description: |-
Expand Down Expand Up @@ -2641,6 +2649,14 @@ spec:
be run on those paths and the outcome will be deployed.
items:
properties:
components:
description: |-
Components is a list of paths to Kustomize components. These paths are relative to the
`Path` field and are included in the Kustomize build to provide reusable configuration logic.
The paths can be static or leverage Go templates for dynamic customization.
items:
type: string
type: array
deploymentType:
default: Remote
description: |-
Expand Down Expand Up @@ -3979,6 +3995,14 @@ spec:
be run on those paths and the outcome will be deployed.
items:
properties:
components:
description: |-
Components is a list of paths to Kustomize components. These paths are relative to the
`Path` field and are included in the Kustomize build to provide reusable configuration logic.
The paths can be static or leverage Go templates for dynamic customization.
items:
type: string
type: array
deploymentType:
default: Remote
description: |-
Expand Down