Skip to content

Commit

Permalink
Merge pull request #464 from stefanprodan/fix-cluster-wide-res
Browse files Browse the repository at this point in the history
Fix cluster-wide resource readiness check
  • Loading branch information
stefanprodan authored Dec 16, 2024
2 parents 9119dc1 + e9c95e5 commit 0027449
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/timoni/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,15 @@ func TestApply_GlobalResources(t *testing.T) {
namespace,
name,
modPath,
), strings.NewReader("values: ns: enabled: true"))
), strings.NewReader("values: globals: enabled: true"))
g.Expect(err).ToNot(HaveOccurred())
t.Log("\n", output)

ns := nsObj.DeepCopy()
err = envTestClient.Get(context.Background(), client.ObjectKeyFromObject(ns), ns)
g.Expect(err).ToNot(HaveOccurred())

g.Expect(output).To(ContainSubstring(fmt.Sprintf("ClusterRole/%s-readonly", name)))
})

t.Run("uninstalls instance", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/testdata/module/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ timoni -n module delete module
| `client: image: pullPolicy:` | `string` | `"IfNotPresent"` | PullPolicy defines the pull policy for the image. By default, it is set to IfNotPresent. |
| `server: enabled:` | `bool` | `true` | |
| `domain:` | `string` | `"example.internal"` | |
| `ns: enabled:` | `bool` | `false` | |
| `globals: enabled:` | `bool` | `false` | |
| `team:` | `string` | `"test"` | |

21 changes: 21 additions & 0 deletions cmd/timoni/testdata/module/templates/clusterrole.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package templates

#ClusterRole: {
#config: #Config
apiVersion: "rbac.authorization.k8s.io/v1"
kind: "ClusterRole"
metadata: {
name: "\(#config.metadata.name)-readonly"
// This is for testing invalid namspace reference
namespace: "default"
}
rules: [{
apiGroups: [""]
resources: ["*"]
verbs: [
"get",
"list",
"watch",
]
}]
}
6 changes: 4 additions & 2 deletions cmd/timoni/testdata/module/templates/config.cue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
domain: *"example.internal" | string

// +nodoc
ns: {
globals: {
enabled: *false | bool
}

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

if config.ns.enabled {
if config.globals.enabled {
"\(config.metadata.name)-ns": #Namespace & {#config: config}
"\(config.metadata.name)-cr": #ClusterRole & {#config: config}

}
}
}
12 changes: 12 additions & 0 deletions internal/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

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

for _, obj := range r.currentObjects {
// If the object is not namespaced, we need to remove the metadata.namespace field.
if obj.GetNamespace() != "" {
if namespaced, err := apiutil.IsObjectNamespaced(obj,
r.resourceManager.Client().Scheme(),
r.resourceManager.Client().RESTMapper()); err == nil && !namespaced {
obj.SetNamespace("")
}
}
}

if err := r.instanceManager.AddObjects(r.currentObjects); err != nil {
return fmt.Errorf("adding objects to instance failed: %w", err)
}
Expand Down

0 comments on commit 0027449

Please sign in to comment.