Skip to content

Commit 0027449

Browse files
authored
Merge pull request #464 from stefanprodan/fix-cluster-wide-res
Fix cluster-wide resource readiness check
2 parents 9119dc1 + e9c95e5 commit 0027449

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

cmd/timoni/apply_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,15 @@ func TestApply_GlobalResources(t *testing.T) {
405405
namespace,
406406
name,
407407
modPath,
408-
), strings.NewReader("values: ns: enabled: true"))
408+
), strings.NewReader("values: globals: enabled: true"))
409409
g.Expect(err).ToNot(HaveOccurred())
410410
t.Log("\n", output)
411411

412412
ns := nsObj.DeepCopy()
413413
err = envTestClient.Get(context.Background(), client.ObjectKeyFromObject(ns), ns)
414414
g.Expect(err).ToNot(HaveOccurred())
415+
416+
g.Expect(output).To(ContainSubstring(fmt.Sprintf("ClusterRole/%s-readonly", name)))
415417
})
416418

417419
t.Run("uninstalls instance", func(t *testing.T) {

cmd/timoni/testdata/module/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ timoni -n module delete module
5050
| `client: image: pullPolicy:` | `string` | `"IfNotPresent"` | PullPolicy defines the pull policy for the image. By default, it is set to IfNotPresent. |
5151
| `server: enabled:` | `bool` | `true` | |
5252
| `domain:` | `string` | `"example.internal"` | |
53-
| `ns: enabled:` | `bool` | `false` | |
53+
| `globals: enabled:` | `bool` | `false` | |
5454
| `team:` | `string` | `"test"` | |
5555

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package templates
2+
3+
#ClusterRole: {
4+
#config: #Config
5+
apiVersion: "rbac.authorization.k8s.io/v1"
6+
kind: "ClusterRole"
7+
metadata: {
8+
name: "\(#config.metadata.name)-readonly"
9+
// This is for testing invalid namspace reference
10+
namespace: "default"
11+
}
12+
rules: [{
13+
apiGroups: [""]
14+
resources: ["*"]
15+
verbs: [
16+
"get",
17+
"list",
18+
"watch",
19+
]
20+
}]
21+
}

cmd/timoni/testdata/module/templates/config.cue

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import (
4242
domain: *"example.internal" | string
4343

4444
// +nodoc
45-
ns: {
45+
globals: {
4646
enabled: *false | bool
4747
}
4848

@@ -61,8 +61,10 @@ import (
6161
"\(config.metadata.name)-server": #ServerConfig & {#config: config}
6262
}
6363

64-
if config.ns.enabled {
64+
if config.globals.enabled {
6565
"\(config.metadata.name)-ns": #Namespace & {#config: config}
66+
"\(config.metadata.name)-cr": #ClusterRole & {#config: config}
67+
6668
}
6769
}
6870
}

internal/reconciler/reconciler.go

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2929
kerrors "k8s.io/apimachinery/pkg/util/errors"
3030
"k8s.io/cli-runtime/pkg/genericclioptions"
31+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3132

3233
apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
3334
"github.com/stefanprodan/timoni/internal/engine"
@@ -103,6 +104,17 @@ func (r *Reconciler) Init(ctx context.Context, builder *engine.ModuleBuilder, bu
103104
r.instanceManager.Instance.Labels[apiv1.BundleNameLabelKey] = instance.Bundle
104105
}
105106

107+
for _, obj := range r.currentObjects {
108+
// If the object is not namespaced, we need to remove the metadata.namespace field.
109+
if obj.GetNamespace() != "" {
110+
if namespaced, err := apiutil.IsObjectNamespaced(obj,
111+
r.resourceManager.Client().Scheme(),
112+
r.resourceManager.Client().RESTMapper()); err == nil && !namespaced {
113+
obj.SetNamespace("")
114+
}
115+
}
116+
}
117+
106118
if err := r.instanceManager.AddObjects(r.currentObjects); err != nil {
107119
return fmt.Errorf("adding objects to instance failed: %w", err)
108120
}

0 commit comments

Comments
 (0)