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
28 changes: 28 additions & 0 deletions api/v1beta1/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ type ValueFrom struct {
// - ConfigMap/Secret
// +kubebuilder:validation:Enum=ConfigMap;Secret
Kind string `json:"kind"`

// 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.
// +kubebuilder:default:=false
// +optional
Optional bool `json:"optional,omitempty"`
}

type RegistryCredentialsConfig struct {
Expand Down Expand Up @@ -485,6 +492,13 @@ type KustomizationRef struct {
// +optional
Path string `json:"path,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.
// +kubebuilder:default:=false
// +optional
Optional bool `json:"optional,omitempty"`

// TargetNamespace sets or overrides the namespace in the
// kustomization.yaml file.
// +kubebuilder:validation:MinLength=1
Expand Down Expand Up @@ -559,6 +573,13 @@ type TemplateResourceRef struct {
// Identifier is how the resource will be referred to in the
// template
Identifier string `json:"identifier"`

// 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 TemplateResourceRefs.
// +kubebuilder:default:=false
// +optional
Optional bool `json:"optional,omitempty"`
}

type PolicyRef struct {
Expand Down Expand Up @@ -592,6 +613,13 @@ type PolicyRef struct {
// +kubebuilder:default:=Remote
// +optional
DeploymentType DeploymentType `json:"deploymentType,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 PolicyRefs.
// +kubebuilder:default:=false
// +optional
Optional bool `json:"optional,omitempty"`
}

type DriftExclusion struct {
Expand Down
35 changes: 35 additions & 0 deletions config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
required:
- kind
- name
Expand Down Expand Up @@ -656,6 +663,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
path:
description: |-
Path to the directory containing the kustomization.yaml file, or the
Expand Down Expand Up @@ -732,6 +746,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
required:
- kind
- name
Expand Down Expand Up @@ -872,6 +893,13 @@ spec:
For Profile namespace must be left empty. Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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 PolicyRefs.
type: boolean
path:
description: |-
Path to the directory containing the YAML files.
Expand Down Expand Up @@ -945,6 +973,13 @@ spec:
Identifier is how the resource will be referred to in the
template
type: string
optional:
default: false
description: |-
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 TemplateResourceRefs.
type: boolean
resource:
description: |-
Resource references a Kubernetes instance in the management
Expand Down
35 changes: 35 additions & 0 deletions config/crd/bases/config.projectsveltos.io_clustersummaries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
required:
- kind
- name
Expand Down Expand Up @@ -694,6 +701,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
path:
description: |-
Path to the directory containing the kustomization.yaml file, or the
Expand Down Expand Up @@ -770,6 +784,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
required:
- kind
- name
Expand Down Expand Up @@ -910,6 +931,13 @@ spec:
For Profile namespace must be left empty. Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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 PolicyRefs.
type: boolean
path:
description: |-
Path to the directory containing the YAML files.
Expand Down Expand Up @@ -983,6 +1011,13 @@ spec:
Identifier is how the resource will be referred to in the
template
type: string
optional:
default: false
description: |-
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 TemplateResourceRefs.
type: boolean
resource:
description: |-
Resource references a Kubernetes instance in the management
Expand Down
35 changes: 35 additions & 0 deletions config/crd/bases/config.projectsveltos.io_profiles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
required:
- kind
- name
Expand Down Expand Up @@ -656,6 +663,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
path:
description: |-
Path to the directory containing the kustomization.yaml file, or the
Expand Down Expand Up @@ -732,6 +746,13 @@ spec:
For Profile namespace must be left empty. The Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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.
type: boolean
required:
- kind
- name
Expand Down Expand Up @@ -872,6 +893,13 @@ spec:
For Profile namespace must be left empty. Profile namespace will be used.
Namespace can be expressed as a template and instantiate using any cluster field.
type: string
optional:
default: false
description: |-
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 PolicyRefs.
type: boolean
path:
description: |-
Path to the directory containing the YAML files.
Expand Down Expand Up @@ -945,6 +973,13 @@ spec:
Identifier is how the resource will be referred to in the
template
type: string
optional:
default: false
description: |-
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 TemplateResourceRefs.
type: boolean
resource:
description: |-
Resource references a Kubernetes instance in the management
Expand Down
3 changes: 3 additions & 0 deletions controllers/clustersummary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,9 @@ func (r *ClusterSummaryReconciler) getKustomizationRefReferences(ctx context.Con

valuesFromReferences, err := getKustomizationValueFrom(ctx, clusterSummaryScope, kr)
if err != nil {
if kr.Optional {
continue
}
return nil, err
}
currentReferences.Append(valuesFromReferences)
Expand Down
45 changes: 29 additions & 16 deletions controllers/handlers_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,9 @@ func collectReferencedObjects(ctx context.Context, controlClusterClient client.C
msg := fmt.Sprintf("Referenced resource: %s %s/%s does not exist",
reference.Kind, reference.Namespace, name)
logger.V(logs.LogInfo).Info(msg)
if reference.Optional {
continue
}
return nil, nil, &NonRetriableError{Message: msg}
}
return nil, nil, err
Expand Down Expand Up @@ -1917,15 +1920,11 @@ func getValuesFrom(ctx context.Context, c client.Client, clusterSummary *configv
if valuesFrom[i].Kind == string(libsveltosv1beta1.ConfigMapReferencedResourceKind) {
configMap, err := getConfigMap(ctx, c, types.NamespacedName{Namespace: namespace, Name: name})
if err != nil {
msg := fmt.Sprintf("failed to get ConfigMap %s/%s", namespace, name)
logger.V(logs.LogInfo).Info(fmt.Sprintf("%s: %v", msg, err))
if apierrors.IsNotFound(err) {
msg := fmt.Sprintf("Referenced resource: %s %s/%s does not exist",
libsveltosv1beta1.ConfigMapReferencedResourceKind, namespace, name)
logger.V(logs.LogInfo).Info(msg)
return nil, nil, &NonRetriableError{Message: msg}
err = handleReferenceError(err, valuesFrom[i].Kind, namespace, name, valuesFrom[i].Optional, logger)
if err == nil {
continue
}
return nil, nil, fmt.Errorf("%s: %w", msg, err)
return nil, nil, err
}

if instantiateTemplate(configMap, logger) {
Expand All @@ -1948,15 +1947,11 @@ func getValuesFrom(ctx context.Context, c client.Client, clusterSummary *configv
} else if valuesFrom[i].Kind == string(libsveltosv1beta1.SecretReferencedResourceKind) {
secret, err := getSecret(ctx, c, types.NamespacedName{Namespace: namespace, Name: name})
if err != nil {
msg := fmt.Sprintf("failed to get Secret %s/%s", namespace, name)
logger.V(logs.LogInfo).Info(fmt.Sprintf("%s: %v", msg, err))
if apierrors.IsNotFound(err) {
msg := fmt.Sprintf("Referenced resource: %s %s/%s does not exist",
libsveltosv1beta1.SecretReferencedResourceKind, namespace, name)
logger.V(logs.LogInfo).Info(msg)
return nil, nil, &NonRetriableError{Message: msg}
err = handleReferenceError(err, valuesFrom[i].Kind, namespace, name, valuesFrom[i].Optional, logger)
if err == nil {
continue
}
return nil, nil, fmt.Errorf("%s: %w", msg, err)
return nil, nil, err
}
if instantiateTemplate(secret, logger) {
for key, value := range secret.Data {
Expand All @@ -1981,6 +1976,24 @@ func getValuesFrom(ctx context.Context, c client.Client, clusterSummary *configv
return template, nonTemplate, nil
}

func handleReferenceError(err error, kind, namespace, name string, optional bool,
logger logr.Logger) error {

msg := fmt.Sprintf("Referenced resource: %s %s/%s", kind, namespace, name)

if apierrors.IsNotFound(err) {
msg += " does not exist"
logger.V(logs.LogInfo).Info(msg)
if optional {
return nil
}
return &NonRetriableError{Message: msg}
}

logger.V(logs.LogInfo).Info(fmt.Sprintf("%s: %v", msg, err))
return fmt.Errorf("%s: %w", msg, err)
}

func addToMap(m map[string]string, key, value string) {
// Check if the key exists in the map
if existingValue, ok := m[key]; ok {
Expand Down
2 changes: 1 addition & 1 deletion controllers/templateresourcedef_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func collectTemplateResourceRefs(ctx context.Context, clusterSummary *configv1be
var u *unstructured.Unstructured
u, err = dr.Get(ctx, ref.Resource.Name, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
if apierrors.IsNotFound(err) && ref.Optional {
continue
}
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/onsi/ginkgo/v2 v2.23.4
github.com/onsi/gomega v1.37.0
github.com/pkg/errors v0.9.1
github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1
github.com/projectsveltos/libsveltos v0.53.1-0.20250508204842-3ec81919e31d
github.com/prometheus/client_golang v1.22.0
github.com/spf13/pflag v1.0.6
github.com/yuin/gopher-lua v1.1.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1 h1:62mLgYOrZiqhMD6vc8JcndMs8obZpq+tfvNePsi774Y=
github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1/go.mod h1:sqZMoHeBgXY6cMIRrByGsdZxpOmwdK+p/H3G81yZ6q0=
github.com/projectsveltos/libsveltos v0.53.1-0.20250508204842-3ec81919e31d h1:JcGfM5vYF+mWaR1xz7qDn0O0UNPqdzb/bBWJanwSamU=
github.com/projectsveltos/libsveltos v0.53.1-0.20250508204842-3ec81919e31d/go.mod h1:sqZMoHeBgXY6cMIRrByGsdZxpOmwdK+p/H3G81yZ6q0=
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20250301182851-e4fbb9fd7ff7 h1:KdDtBEJPgavOHlut1gq2i6bFm5dgoNHNsOUC8oe2hK4=
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20250301182851-e4fbb9fd7ff7/go.mod h1:AIzg+JWbfrFWazyM5Ka2fX69r9aFr3+o2Mvn9SfKDYU=
github.com/projectsveltos/lua-utils/glua-runes v0.0.0-20250301182851-e4fbb9fd7ff7 h1:kZzOx+XTEfCRjxw1yACuGhFSyS7ybP/NNJFAZYNARCk=
Expand Down
Loading