Skip to content

Commit e39ae0f

Browse files
authored
Merge pull request #1188 from gianlucam76/optional-referenced-resources
(feat) referenced resources can be marked optional
2 parents 30325c8 + 7c26f65 commit e39ae0f

12 files changed

Lines changed: 417 additions & 20 deletions

api/v1beta1/spec.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ type ValueFrom struct {
138138
// - ConfigMap/Secret
139139
// +kubebuilder:validation:Enum=ConfigMap;Secret
140140
Kind string `json:"kind"`
141+
142+
// Optional indicates that the referenced resource is not mandatory.
143+
// If set to true and the resource is not found, the error will be ignored,
144+
// and Sveltos will continue processing other ValueFroms.
145+
// +kubebuilder:default:=false
146+
// +optional
147+
Optional bool `json:"optional,omitempty"`
141148
}
142149

143150
type RegistryCredentialsConfig struct {
@@ -485,6 +492,13 @@ type KustomizationRef struct {
485492
// +optional
486493
Path string `json:"path,omitempty"`
487494

495+
// Optional indicates that the referenced resource is not mandatory.
496+
// If set to true and the resource is not found, the error will be ignored,
497+
// and Sveltos will continue processing other ValueFroms.
498+
// +kubebuilder:default:=false
499+
// +optional
500+
Optional bool `json:"optional,omitempty"`
501+
488502
// TargetNamespace sets or overrides the namespace in the
489503
// kustomization.yaml file.
490504
// +kubebuilder:validation:MinLength=1
@@ -559,6 +573,13 @@ type TemplateResourceRef struct {
559573
// Identifier is how the resource will be referred to in the
560574
// template
561575
Identifier string `json:"identifier"`
576+
577+
// Optional indicates that the referenced resource is not mandatory.
578+
// If set to true and the resource is not found, the error will be ignored,
579+
// and Sveltos will continue processing other TemplateResourceRefs.
580+
// +kubebuilder:default:=false
581+
// +optional
582+
Optional bool `json:"optional,omitempty"`
562583
}
563584

564585
type PolicyRef struct {
@@ -592,6 +613,13 @@ type PolicyRef struct {
592613
// +kubebuilder:default:=Remote
593614
// +optional
594615
DeploymentType DeploymentType `json:"deploymentType,omitempty"`
616+
617+
// Optional indicates that the referenced resource is not mandatory.
618+
// If set to true and the resource is not found, the error will be ignored,
619+
// and Sveltos will continue processing other PolicyRefs.
620+
// +kubebuilder:default:=false
621+
// +optional
622+
Optional bool `json:"optional,omitempty"`
595623
}
596624

597625
type DriftExclusion struct {

config/crd/bases/config.projectsveltos.io_clusterprofiles.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,13 @@ spec:
584584
For Profile namespace must be left empty. The Profile namespace will be used.
585585
Namespace can be expressed as a template and instantiate using any cluster field.
586586
type: string
587+
optional:
588+
default: false
589+
description: |-
590+
Optional indicates that the referenced resource is not mandatory.
591+
If set to true and the resource is not found, the error will be ignored,
592+
and Sveltos will continue processing other ValueFroms.
593+
type: boolean
587594
required:
588595
- kind
589596
- name
@@ -656,6 +663,13 @@ spec:
656663
For Profile namespace must be left empty. The Profile namespace will be used.
657664
Namespace can be expressed as a template and instantiate using any cluster field.
658665
type: string
666+
optional:
667+
default: false
668+
description: |-
669+
Optional indicates that the referenced resource is not mandatory.
670+
If set to true and the resource is not found, the error will be ignored,
671+
and Sveltos will continue processing other ValueFroms.
672+
type: boolean
659673
path:
660674
description: |-
661675
Path to the directory containing the kustomization.yaml file, or the
@@ -732,6 +746,13 @@ spec:
732746
For Profile namespace must be left empty. The Profile namespace will be used.
733747
Namespace can be expressed as a template and instantiate using any cluster field.
734748
type: string
749+
optional:
750+
default: false
751+
description: |-
752+
Optional indicates that the referenced resource is not mandatory.
753+
If set to true and the resource is not found, the error will be ignored,
754+
and Sveltos will continue processing other ValueFroms.
755+
type: boolean
735756
required:
736757
- kind
737758
- name
@@ -872,6 +893,13 @@ spec:
872893
For Profile namespace must be left empty. Profile namespace will be used.
873894
Namespace can be expressed as a template and instantiate using any cluster field.
874895
type: string
896+
optional:
897+
default: false
898+
description: |-
899+
Optional indicates that the referenced resource is not mandatory.
900+
If set to true and the resource is not found, the error will be ignored,
901+
and Sveltos will continue processing other PolicyRefs.
902+
type: boolean
875903
path:
876904
description: |-
877905
Path to the directory containing the YAML files.
@@ -945,6 +973,13 @@ spec:
945973
Identifier is how the resource will be referred to in the
946974
template
947975
type: string
976+
optional:
977+
default: false
978+
description: |-
979+
Optional indicates that the referenced resource is not mandatory.
980+
If set to true and the resource is not found, the error will be ignored,
981+
and Sveltos will continue processing other TemplateResourceRefs.
982+
type: boolean
948983
resource:
949984
description: |-
950985
Resource references a Kubernetes instance in the management

config/crd/bases/config.projectsveltos.io_clustersummaries.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,13 @@ spec:
622622
For Profile namespace must be left empty. The Profile namespace will be used.
623623
Namespace can be expressed as a template and instantiate using any cluster field.
624624
type: string
625+
optional:
626+
default: false
627+
description: |-
628+
Optional indicates that the referenced resource is not mandatory.
629+
If set to true and the resource is not found, the error will be ignored,
630+
and Sveltos will continue processing other ValueFroms.
631+
type: boolean
625632
required:
626633
- kind
627634
- name
@@ -694,6 +701,13 @@ spec:
694701
For Profile namespace must be left empty. The Profile namespace will be used.
695702
Namespace can be expressed as a template and instantiate using any cluster field.
696703
type: string
704+
optional:
705+
default: false
706+
description: |-
707+
Optional indicates that the referenced resource is not mandatory.
708+
If set to true and the resource is not found, the error will be ignored,
709+
and Sveltos will continue processing other ValueFroms.
710+
type: boolean
697711
path:
698712
description: |-
699713
Path to the directory containing the kustomization.yaml file, or the
@@ -770,6 +784,13 @@ spec:
770784
For Profile namespace must be left empty. The Profile namespace will be used.
771785
Namespace can be expressed as a template and instantiate using any cluster field.
772786
type: string
787+
optional:
788+
default: false
789+
description: |-
790+
Optional indicates that the referenced resource is not mandatory.
791+
If set to true and the resource is not found, the error will be ignored,
792+
and Sveltos will continue processing other ValueFroms.
793+
type: boolean
773794
required:
774795
- kind
775796
- name
@@ -910,6 +931,13 @@ spec:
910931
For Profile namespace must be left empty. Profile namespace will be used.
911932
Namespace can be expressed as a template and instantiate using any cluster field.
912933
type: string
934+
optional:
935+
default: false
936+
description: |-
937+
Optional indicates that the referenced resource is not mandatory.
938+
If set to true and the resource is not found, the error will be ignored,
939+
and Sveltos will continue processing other PolicyRefs.
940+
type: boolean
913941
path:
914942
description: |-
915943
Path to the directory containing the YAML files.
@@ -983,6 +1011,13 @@ spec:
9831011
Identifier is how the resource will be referred to in the
9841012
template
9851013
type: string
1014+
optional:
1015+
default: false
1016+
description: |-
1017+
Optional indicates that the referenced resource is not mandatory.
1018+
If set to true and the resource is not found, the error will be ignored,
1019+
and Sveltos will continue processing other TemplateResourceRefs.
1020+
type: boolean
9861021
resource:
9871022
description: |-
9881023
Resource references a Kubernetes instance in the management

config/crd/bases/config.projectsveltos.io_profiles.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,13 @@ spec:
584584
For Profile namespace must be left empty. The Profile namespace will be used.
585585
Namespace can be expressed as a template and instantiate using any cluster field.
586586
type: string
587+
optional:
588+
default: false
589+
description: |-
590+
Optional indicates that the referenced resource is not mandatory.
591+
If set to true and the resource is not found, the error will be ignored,
592+
and Sveltos will continue processing other ValueFroms.
593+
type: boolean
587594
required:
588595
- kind
589596
- name
@@ -656,6 +663,13 @@ spec:
656663
For Profile namespace must be left empty. The Profile namespace will be used.
657664
Namespace can be expressed as a template and instantiate using any cluster field.
658665
type: string
666+
optional:
667+
default: false
668+
description: |-
669+
Optional indicates that the referenced resource is not mandatory.
670+
If set to true and the resource is not found, the error will be ignored,
671+
and Sveltos will continue processing other ValueFroms.
672+
type: boolean
659673
path:
660674
description: |-
661675
Path to the directory containing the kustomization.yaml file, or the
@@ -732,6 +746,13 @@ spec:
732746
For Profile namespace must be left empty. The Profile namespace will be used.
733747
Namespace can be expressed as a template and instantiate using any cluster field.
734748
type: string
749+
optional:
750+
default: false
751+
description: |-
752+
Optional indicates that the referenced resource is not mandatory.
753+
If set to true and the resource is not found, the error will be ignored,
754+
and Sveltos will continue processing other ValueFroms.
755+
type: boolean
735756
required:
736757
- kind
737758
- name
@@ -872,6 +893,13 @@ spec:
872893
For Profile namespace must be left empty. Profile namespace will be used.
873894
Namespace can be expressed as a template and instantiate using any cluster field.
874895
type: string
896+
optional:
897+
default: false
898+
description: |-
899+
Optional indicates that the referenced resource is not mandatory.
900+
If set to true and the resource is not found, the error will be ignored,
901+
and Sveltos will continue processing other PolicyRefs.
902+
type: boolean
875903
path:
876904
description: |-
877905
Path to the directory containing the YAML files.
@@ -945,6 +973,13 @@ spec:
945973
Identifier is how the resource will be referred to in the
946974
template
947975
type: string
976+
optional:
977+
default: false
978+
description: |-
979+
Optional indicates that the referenced resource is not mandatory.
980+
If set to true and the resource is not found, the error will be ignored,
981+
and Sveltos will continue processing other TemplateResourceRefs.
982+
type: boolean
948983
resource:
949984
description: |-
950985
Resource references a Kubernetes instance in the management

controllers/clustersummary_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,9 @@ func (r *ClusterSummaryReconciler) getKustomizationRefReferences(ctx context.Con
933933

934934
valuesFromReferences, err := getKustomizationValueFrom(ctx, clusterSummaryScope, kr)
935935
if err != nil {
936+
if kr.Optional {
937+
continue
938+
}
936939
return nil, err
937940
}
938941
currentReferences.Append(valuesFromReferences)

controllers/handlers_utils.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,9 @@ func collectReferencedObjects(ctx context.Context, controlClusterClient client.C
11741174
msg := fmt.Sprintf("Referenced resource: %s %s/%s does not exist",
11751175
reference.Kind, reference.Namespace, name)
11761176
logger.V(logs.LogInfo).Info(msg)
1177+
if reference.Optional {
1178+
continue
1179+
}
11771180
return nil, nil, &NonRetriableError{Message: msg}
11781181
}
11791182
return nil, nil, err
@@ -1917,15 +1920,11 @@ func getValuesFrom(ctx context.Context, c client.Client, clusterSummary *configv
19171920
if valuesFrom[i].Kind == string(libsveltosv1beta1.ConfigMapReferencedResourceKind) {
19181921
configMap, err := getConfigMap(ctx, c, types.NamespacedName{Namespace: namespace, Name: name})
19191922
if err != nil {
1920-
msg := fmt.Sprintf("failed to get ConfigMap %s/%s", namespace, name)
1921-
logger.V(logs.LogInfo).Info(fmt.Sprintf("%s: %v", msg, err))
1922-
if apierrors.IsNotFound(err) {
1923-
msg := fmt.Sprintf("Referenced resource: %s %s/%s does not exist",
1924-
libsveltosv1beta1.ConfigMapReferencedResourceKind, namespace, name)
1925-
logger.V(logs.LogInfo).Info(msg)
1926-
return nil, nil, &NonRetriableError{Message: msg}
1923+
err = handleReferenceError(err, valuesFrom[i].Kind, namespace, name, valuesFrom[i].Optional, logger)
1924+
if err == nil {
1925+
continue
19271926
}
1928-
return nil, nil, fmt.Errorf("%s: %w", msg, err)
1927+
return nil, nil, err
19291928
}
19301929

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

1979+
func handleReferenceError(err error, kind, namespace, name string, optional bool,
1980+
logger logr.Logger) error {
1981+
1982+
msg := fmt.Sprintf("Referenced resource: %s %s/%s", kind, namespace, name)
1983+
1984+
if apierrors.IsNotFound(err) {
1985+
msg += " does not exist"
1986+
logger.V(logs.LogInfo).Info(msg)
1987+
if optional {
1988+
return nil
1989+
}
1990+
return &NonRetriableError{Message: msg}
1991+
}
1992+
1993+
logger.V(logs.LogInfo).Info(fmt.Sprintf("%s: %v", msg, err))
1994+
return fmt.Errorf("%s: %w", msg, err)
1995+
}
1996+
19841997
func addToMap(m map[string]string, key, value string) {
19851998
// Check if the key exists in the map
19861999
if existingValue, ok := m[key]; ok {

controllers/templateresourcedef_utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func collectTemplateResourceRefs(ctx context.Context, clusterSummary *configv1be
7979
var u *unstructured.Unstructured
8080
u, err = dr.Get(ctx, ref.Resource.Name, metav1.GetOptions{})
8181
if err != nil {
82-
if apierrors.IsNotFound(err) {
82+
if apierrors.IsNotFound(err) && ref.Optional {
8383
continue
8484
}
8585
return nil, err

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/onsi/ginkgo/v2 v2.23.4
1717
github.com/onsi/gomega v1.37.0
1818
github.com/pkg/errors v0.9.1
19-
github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1
19+
github.com/projectsveltos/libsveltos v0.53.1-0.20250508204842-3ec81919e31d
2020
github.com/prometheus/client_golang v1.22.0
2121
github.com/spf13/pflag v1.0.6
2222
github.com/yuin/gopher-lua v1.1.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ github.com/poy/onpar v1.1.2 h1:QaNrNiZx0+Nar5dLgTVp5mXkyoVFIbepjyEoGSnhbAY=
329329
github.com/poy/onpar v1.1.2/go.mod h1:6X8FLNoxyr9kkmnlqpK6LSoiOtrO6MICtWwEuWkLjzg=
330330
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
331331
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
332-
github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1 h1:62mLgYOrZiqhMD6vc8JcndMs8obZpq+tfvNePsi774Y=
333-
github.com/projectsveltos/libsveltos v0.53.1-0.20250506070210-ef75a78e70a1/go.mod h1:sqZMoHeBgXY6cMIRrByGsdZxpOmwdK+p/H3G81yZ6q0=
332+
github.com/projectsveltos/libsveltos v0.53.1-0.20250508204842-3ec81919e31d h1:JcGfM5vYF+mWaR1xz7qDn0O0UNPqdzb/bBWJanwSamU=
333+
github.com/projectsveltos/libsveltos v0.53.1-0.20250508204842-3ec81919e31d/go.mod h1:sqZMoHeBgXY6cMIRrByGsdZxpOmwdK+p/H3G81yZ6q0=
334334
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20250301182851-e4fbb9fd7ff7 h1:KdDtBEJPgavOHlut1gq2i6bFm5dgoNHNsOUC8oe2hK4=
335335
github.com/projectsveltos/lua-utils/glua-json v0.0.0-20250301182851-e4fbb9fd7ff7/go.mod h1:AIzg+JWbfrFWazyM5Ka2fX69r9aFr3+o2Mvn9SfKDYU=
336336
github.com/projectsveltos/lua-utils/glua-runes v0.0.0-20250301182851-e4fbb9fd7ff7 h1:kZzOx+XTEfCRjxw1yACuGhFSyS7ybP/NNJFAZYNARCk=

0 commit comments

Comments
 (0)