Skip to content

Commit

Permalink
Merge pull request #132 from gianlucam76/referenced-resource-namespace
Browse files Browse the repository at this point in the history
Referenced resources
  • Loading branch information
gianlucam76 committed Feb 7, 2023
2 parents 491b336 + 8441c9f commit 21c2062
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ spec:
minLength: 1
type: string
namespace:
description: Namespace of the referenced resource.
minLength: 1
description: Namespace of the referenced resource. Namespace
can be left empty. In such a case, namespace will be implicit
set to cluster's namespace.
type: string
required:
- kind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,9 @@ spec:
minLength: 1
type: string
namespace:
description: Namespace of the referenced resource.
minLength: 1
description: Namespace of the referenced resource. Namespace
can be left empty. In such a case, namespace will be implicit
set to cluster's namespace.
type: string
required:
- kind
Expand Down
5 changes: 4 additions & 1 deletion controllers/clustersummary_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,13 @@ func (r *ClusterSummaryReconciler) getCurrentReferences(clusterSummaryScope *sco
for i := range clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs {
referencedNamespace := clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Namespace
referencedName := clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Name

namespace := getReferenceResourceNamespace(clusterSummaryScope.Namespace(), referencedNamespace)

currentReferences.Insert(&corev1.ObjectReference{
APIVersion: corev1.SchemeGroupVersion.String(), // the only resources that can be referenced are Secret and ConfigMap
Kind: clusterSummaryScope.ClusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i].Kind,
Namespace: referencedNamespace,
Namespace: namespace,
Name: referencedName,
})
}
Expand Down
35 changes: 35 additions & 0 deletions controllers/clustersummary_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,41 @@ var _ = Describe("ClustersummaryController", func() {
// Because CAPI cluster does not exist and ClusterSummary is marked for deletion, finalizer can be removed
Expect(controllers.CanRemoveFinalizer(reconciler, context.TODO(), clusterSummaryScope, klogr.New())).To(BeTrue())
})

It("getCurrentReferences collects all ClusterSummary referenced objects", func() {
referencedResourceNamespace := randomString()
clusterSummary.Spec.ClusterProfileSpec.PolicyRefs = []libsveltosv1alpha1.PolicyRef{
{
Namespace: referencedResourceNamespace,
Name: randomString(),
Kind: string(libsveltosv1alpha1.ConfigMapReferencedResourceKind),
},
}

c := fake.NewClientBuilder().WithScheme(scheme).Build()

clusterSummaryScope := getClusterSummaryScope(c, klogr.New(), clusterProfile, clusterSummary)
reconciler := getClusterSummaryReconciler(nil, nil)
set := controllers.GetCurrentReferences(reconciler, clusterSummaryScope)
Expect(set.Len()).To(Equal(1))
items := set.Items()
Expect(items[0].Namespace).To(Equal(referencedResourceNamespace))
})

It("getCurrentReferences collects all ClusterSummary referenced objects using cluster namespace when not set", func() {
clusterSummary.Spec.ClusterProfileSpec.PolicyRefs = []libsveltosv1alpha1.PolicyRef{
{Namespace: "", Name: randomString(), Kind: string(libsveltosv1alpha1.ConfigMapReferencedResourceKind)},
}

c := fake.NewClientBuilder().WithScheme(scheme).Build()

clusterSummaryScope := getClusterSummaryScope(c, klogr.New(), clusterProfile, clusterSummary)
reconciler := getClusterSummaryReconciler(nil, nil)
set := controllers.GetCurrentReferences(reconciler, clusterSummaryScope)
Expect(set.Len()).To(Equal(1))
items := set.Items()
Expect(items[0].Namespace).To(Equal(clusterSummary.Namespace))
})
})

var _ = Describe("ClusterSummaryReconciler: requeue methods", func() {
Expand Down
3 changes: 2 additions & 1 deletion controllers/clustersummary_deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,8 @@ func (r *ClusterSummaryReconciler) updateDeployedGroupVersionKind(ctx context.Co

logger.V(logs.LogDebug).Info("update status with deployed GroupVersionKinds")
// Collect all referenced configMaps/secrets.
referencedObjects, err := collectReferencedObjects(ctx, r.Client, references, logger)
referencedObjects, err := collectReferencedObjects(ctx, r.Client, clusterSummaryScope.Namespace(),
references, logger)
if err != nil {
logger.V(logs.LogDebug).Info(fmt.Sprintf("failed to collect referenced configMaps/secrets. Err: %v", err))
return err
Expand Down
14 changes: 0 additions & 14 deletions controllers/clustersummary_deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,6 @@ var _ = Describe("ClustersummaryDeployer", func() {
Expect(cs.Status.FeatureSummaries[0].FeatureID).To(Equal(configv1alpha1.FeatureResources))
Expect(cs.Status.FeatureSummaries[0].DeployedGroupVersionKind).To(ContainElement("ClusterRole.v1.rbac.authorization.k8s.io"))
})

It("getCurrentReferences collects all ClusterSummary referenced objects", func() {
clusterSummary.Spec.ClusterProfileSpec.PolicyRefs = []libsveltosv1alpha1.PolicyRef{
{Namespace: randomString(), Name: randomString(), Kind: string(libsveltosv1alpha1.ConfigMapReferencedResourceKind)},
}

c := fake.NewClientBuilder().WithScheme(scheme).Build()

clusterSummaryScope := getClusterSummaryScope(c, logger, clusterProfile, clusterSummary)
reconciler := getClusterSummaryReconciler(nil, nil)
set := controllers.GetCurrentReferences(reconciler, clusterSummaryScope)
expectedLength := len(clusterSummary.Spec.ClusterProfileSpec.PolicyRefs)
Expect(set.Len()).To(Equal(expectedLength))
})
})

var _ = Describe("Convert result", func() {
Expand Down
33 changes: 17 additions & 16 deletions controllers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,23 @@ var (
GenericDeploy = genericDeploy
GenericUndeploy = genericUndeploy

GetClusterSummary = getClusterSummary
AddLabel = addLabel
CreateNamespace = createNamespace
GetEntryKey = getEntryKey
DeployContentOfConfigMap = deployContentOfConfigMap
DeployContentOfSecret = deployContentOfSecret
DeployContent = deployContent
GetClusterSummaryAdmin = getClusterSummaryAdmin
AddAnnotation = addAnnotation
ComputePolicyHash = computePolicyHash
GetPolicyInfo = getPolicyInfo
UndeployStaleResources = undeployStaleResources
GetDeployedGroupVersionKinds = getDeployedGroupVersionKinds
CanDelete = canDelete
HandleResourceDelete = handleResourceDelete
GetSecret = getSecret
GetClusterSummary = getClusterSummary
AddLabel = addLabel
CreateNamespace = createNamespace
GetEntryKey = getEntryKey
DeployContentOfConfigMap = deployContentOfConfigMap
DeployContentOfSecret = deployContentOfSecret
DeployContent = deployContent
GetClusterSummaryAdmin = getClusterSummaryAdmin
AddAnnotation = addAnnotation
ComputePolicyHash = computePolicyHash
GetPolicyInfo = getPolicyInfo
UndeployStaleResources = undeployStaleResources
GetDeployedGroupVersionKinds = getDeployedGroupVersionKinds
CanDelete = canDelete
HandleResourceDelete = handleResourceDelete
GetSecret = getSecret
GetReferenceResourceNamespace = getReferenceResourceNamespace

ResourcesHash = resourcesHash
GetResourceRefs = getResourceRefs
Expand Down
5 changes: 3 additions & 2 deletions controllers/handlers_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,17 @@ func resourcesHash(ctx context.Context, c client.Client, clusterSummaryScope *sc
clusterSummary := clusterSummaryScope.ClusterSummary
for i := range clusterSummary.Spec.ClusterProfileSpec.PolicyRefs {
reference := &clusterSummary.Spec.ClusterProfileSpec.PolicyRefs[i]
namespace := getReferenceResourceNamespace(clusterSummaryScope.Namespace(), reference.Namespace)
var err error
if reference.Kind == string(libsveltosv1alpha1.ConfigMapReferencedResourceKind) {
configmap := &corev1.ConfigMap{}
err = c.Get(ctx, types.NamespacedName{Namespace: reference.Namespace, Name: reference.Name}, configmap)
err = c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: reference.Name}, configmap)
if err == nil {
config += render.AsCode(configmap.Data)
}
} else {
secret := &corev1.Secret{}
err = c.Get(ctx, types.NamespacedName{Namespace: reference.Namespace, Name: reference.Name}, secret)
err = c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: reference.Name}, secret)
if err == nil {
config += render.AsCode(secret.Data)
}
Expand Down
22 changes: 18 additions & 4 deletions controllers/handlers_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,21 +358,35 @@ func getClusterSummaryAndClusterClient(ctx context.Context, clusterNamespace, cl
return clusterSummary, clusterClient, nil
}

// getReferenceResourceNamespace returns the namespace to use for a referenced resource.
// If namespace is set on referencedResource, that namespace will be used.
// If namespace is not set, cluster namespace will be used
func getReferenceResourceNamespace(clusterNamespace, referencedResourceNamespace string) string {
if referencedResourceNamespace != "" {
return referencedResourceNamespace
}

return clusterNamespace
}

// collectReferencedObjects collects all referenced configMaps/secrets in control cluster
func collectReferencedObjects(ctx context.Context, controlClusterClient client.Client,
func collectReferencedObjects(ctx context.Context, controlClusterClient client.Client, clusterNamespace string,
references []libsveltosv1alpha1.PolicyRef, logger logr.Logger) ([]client.Object, error) {

objects := make([]client.Object, 0)
for i := range references {
var err error
var object client.Object
reference := &references[i]

namespace := getReferenceResourceNamespace(clusterNamespace, references[i].Namespace)

if reference.Kind == string(libsveltosv1alpha1.ConfigMapReferencedResourceKind) {
object, err = getConfigMap(ctx, controlClusterClient,
types.NamespacedName{Namespace: reference.Namespace, Name: reference.Name})
types.NamespacedName{Namespace: namespace, Name: reference.Name})
} else {
object, err = getSecret(ctx, controlClusterClient,
types.NamespacedName{Namespace: reference.Namespace, Name: reference.Name})
types.NamespacedName{Namespace: namespace, Name: reference.Name})
}
if err != nil {
if apierrors.IsNotFound(err) {
Expand All @@ -396,7 +410,7 @@ func deployReferencedObjects(ctx context.Context, c client.Client, remoteConfig
refs := featureHandler.getRefs(clusterSummary)

var referencedObjects []client.Object
referencedObjects, err = collectReferencedObjects(ctx, c, refs, logger)
referencedObjects, err = collectReferencedObjects(ctx, c, clusterSummary.Namespace, refs, logger)
if err != nil {
return nil, err
}
Expand Down
16 changes: 16 additions & 0 deletions controllers/handlers_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ var _ = Describe("HandlersUtils", func() {
}
})

It("getReferenceResourceNamespace returns the referenced resource namespace when set. cluster namespace otherwise.", func() {
referecedResource := libsveltosv1alpha1.PolicyRef{
Namespace: "",
Name: randomString(),
Kind: string(libsveltosv1alpha1.ConfigMapReferencedResourceKind),
}

clusterNamespace := randomString()
Expect(controllers.GetReferenceResourceNamespace(clusterNamespace, referecedResource.Namespace)).To(
Equal(clusterNamespace))

referecedResource.Namespace = randomString()
Expect(controllers.GetReferenceResourceNamespace(clusterNamespace, referecedResource.Namespace)).To(
Equal(referecedResource.Namespace))
})

It("deployContentOfSecret deploys all policies contained in a ConfigMap", func() {
services := fmt.Sprintf(serviceTemplate, namespace, namespace)
depl := fmt.Sprintf(deplTemplate, namespace)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/onsi/ginkgo/v2 v2.6.0
github.com/onsi/gomega v1.24.1
github.com/pkg/errors v0.9.1
github.com/projectsveltos/libsveltos v0.4.1-0.20230207194120-47188ddd590f
github.com/projectsveltos/libsveltos v0.4.1-0.20230207202124-d0a8595fab5e
github.com/prometheus/client_golang v1.13.0
github.com/spf13/pflag v1.0.5
golang.org/x/text v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,8 @@ github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndr
github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s=
github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1 h1:oL4IBbcqwhhNWh31bjOX8C/OCy0zs9906d/VUru+bqg=
github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1/go.mod h1:nSbFQvMj97ZyhFRSJYtut+msi4sOY6zJDGCdSc+/rZU=
github.com/projectsveltos/libsveltos v0.4.1-0.20230207194120-47188ddd590f h1:nBYz2Zs5CdpGF6vwnXzpwbbsO+Cg1vhMBIzgM1nf2ok=
github.com/projectsveltos/libsveltos v0.4.1-0.20230207194120-47188ddd590f/go.mod h1:smYCt3DQSZpQqsaoM2mJAIP6RAMXcxw5Af0mzkncCs4=
github.com/projectsveltos/libsveltos v0.4.1-0.20230207202124-d0a8595fab5e h1:LrgQHZkRta852MySb/u1lchve/AQcrF76n/Iq9SPuKg=
github.com/projectsveltos/libsveltos v0.4.1-0.20230207202124-d0a8595fab5e/go.mod h1:smYCt3DQSZpQqsaoM2mJAIP6RAMXcxw5Af0mzkncCs4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g=
Expand Down
10 changes: 6 additions & 4 deletions manifest/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,9 @@ spec:
minLength: 1
type: string
namespace:
description: Namespace of the referenced resource.
minLength: 1
description: Namespace of the referenced resource. Namespace
can be left empty. In such a case, namespace will be implicit
set to cluster's namespace.
type: string
required:
- kind
Expand Down Expand Up @@ -851,8 +852,9 @@ spec:
minLength: 1
type: string
namespace:
description: Namespace of the referenced resource.
minLength: 1
description: Namespace of the referenced resource. Namespace
can be left empty. In such a case, namespace will be implicit
set to cluster's namespace.
type: string
required:
- kind
Expand Down
Loading

0 comments on commit 21c2062

Please sign in to comment.