mirror of
https://github.com/stakater/Reloader.git
synced 2026-08-23 14:06:27 +00:00
Add a third RBAC posture between watch-globally (ClusterRole) and single namespace: give Reloader an explicit list of namespaces to watch. The chart creates a namespace-scoped Role + RoleBinding in each listed namespace (no ClusterRole), and one install covers them all. Go: - new --namespaces flag / options.Namespaces - resolveWatchNamespaces() picks list -> KUBERNETES_NAMESPACE -> all - controller creation loops over the watched namespaces - namespaces-to-ignore is now only honored in global mode (watchGlobally=true); in single-namespace and scoped modes the watched set is already explicit Helm: - new reloader.namespaces value (active when watchGlobally=false); accepts either a YAML list or a comma-separated string for consistency with the sibling namespace options - reloader-watchNamespaces helper (release ns always auto-included, deduped) - shared reloader-namespaced-rules template reused per namespace - role.yaml/rolebinding.yaml range over the list; deployment passes --namespaces - --namespaces-to-ignore only rendered when watchGlobally=true - fail guard for watchGlobally=true + namespaces set Tests: unit test for resolveWatchNamespaces; scoped-namespaces e2e case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
278 lines
11 KiB
Go
278 lines
11 KiB
Go
package flags
|
|
|
|
import (
|
|
"time"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
|
|
"github.com/stakater/Reloader/test/e2e/utils"
|
|
)
|
|
|
|
var _ = Describe("Watch Globally Flag Tests", Serial, func() {
|
|
var (
|
|
deploymentName string
|
|
configMapName string
|
|
otherNS string
|
|
adapter *utils.DeploymentAdapter
|
|
)
|
|
|
|
BeforeEach(func() {
|
|
deploymentName = utils.RandName("deploy")
|
|
configMapName = utils.RandName("cm")
|
|
otherNS = "other-" + utils.RandName("ns")
|
|
adapter = utils.NewDeploymentAdapter(kubeClient)
|
|
})
|
|
|
|
AfterEach(func() {
|
|
_ = utils.DeleteDeployment(ctx, kubeClient, testNamespace, deploymentName)
|
|
_ = utils.DeleteConfigMap(ctx, kubeClient, testNamespace, configMapName)
|
|
_ = utils.DeleteDeployment(ctx, kubeClient, otherNS, deploymentName)
|
|
_ = utils.DeleteConfigMap(ctx, kubeClient, otherNS, configMapName)
|
|
})
|
|
|
|
Context("with watchGlobally=false flag", func() {
|
|
BeforeEach(func() {
|
|
err := utils.CreateNamespace(ctx, kubeClient, otherNS)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
err = deployReloaderWithFlags(map[string]string{
|
|
"reloader.watchGlobally": "false",
|
|
})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
err = waitForReloaderReady()
|
|
Expect(err).NotTo(HaveOccurred())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
_ = undeployReloader()
|
|
_ = utils.DeleteNamespace(ctx, kubeClient, otherNS)
|
|
})
|
|
|
|
It("should reload workloads in Reloader's namespace when watchGlobally=false", func() {
|
|
By("Creating a ConfigMap in Reloader's namespace")
|
|
_, err := utils.CreateConfigMap(ctx, kubeClient, testNamespace, configMapName,
|
|
map[string]string{"key": "initial"}, nil)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Creating a Deployment in Reloader's namespace with auto annotation")
|
|
_, err = utils.CreateDeployment(ctx, kubeClient, testNamespace, deploymentName,
|
|
utils.WithConfigMapEnvFrom(configMapName),
|
|
utils.WithAnnotations(utils.BuildAutoTrueAnnotation()),
|
|
)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be ready")
|
|
err = adapter.WaitReady(ctx, testNamespace, deploymentName, utils.WorkloadReadyTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Updating the ConfigMap")
|
|
err = utils.UpdateConfigMap(ctx, kubeClient, testNamespace, configMapName, map[string]string{"key": "updated"})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be reloaded (same namespace should work)")
|
|
reloaded, err := adapter.WaitReloaded(ctx, testNamespace, deploymentName,
|
|
utils.AnnotationLastReloadedFrom, utils.ReloadTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(reloaded).To(BeTrue(), "Deployment in Reloader's namespace should reload with watchGlobally=false")
|
|
})
|
|
|
|
It("should NOT reload workloads in other namespaces when watchGlobally=false", func() {
|
|
By("Creating a ConfigMap in another namespace")
|
|
_, err := utils.CreateConfigMap(ctx, kubeClient, otherNS, configMapName,
|
|
map[string]string{"key": "initial"}, nil)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Creating a Deployment in another namespace with auto annotation")
|
|
_, err = utils.CreateDeployment(ctx, kubeClient, otherNS, deploymentName,
|
|
utils.WithConfigMapEnvFrom(configMapName),
|
|
utils.WithAnnotations(utils.BuildAutoTrueAnnotation()),
|
|
)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be ready")
|
|
err = adapter.WaitReady(ctx, otherNS, deploymentName, utils.WorkloadReadyTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Updating the ConfigMap in the other namespace")
|
|
err = utils.UpdateConfigMap(ctx, kubeClient, otherNS, configMapName, map[string]string{"key": "updated"})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Verifying Deployment was NOT reloaded (different namespace with watchGlobally=false)")
|
|
time.Sleep(utils.NegativeTestWait)
|
|
reloaded, err := adapter.WaitReloaded(ctx, otherNS, deploymentName,
|
|
utils.AnnotationLastReloadedFrom, utils.ShortTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(reloaded).To(BeFalse(), "Deployment in other namespace should NOT reload with watchGlobally=false")
|
|
})
|
|
})
|
|
|
|
Context("with scoped namespaces list (watchGlobally=false + reloader.namespaces)", func() {
|
|
var scopedNS string
|
|
|
|
BeforeEach(func() {
|
|
scopedNS = "scoped-" + utils.RandName("ns")
|
|
Expect(utils.CreateNamespace(ctx, kubeClient, scopedNS)).To(Succeed())
|
|
Expect(utils.CreateNamespace(ctx, kubeClient, otherNS)).To(Succeed())
|
|
|
|
// Watch only scopedNS explicitly; the release namespace (testNamespace)
|
|
// is auto-included by the chart. otherNS is intentionally left out.
|
|
err := deployReloaderWithFlags(map[string]string{
|
|
"reloader.watchGlobally": "false",
|
|
"reloader.namespaces": "{" + scopedNS + "}",
|
|
})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(waitForReloaderReady()).To(Succeed())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
_ = utils.DeleteDeployment(ctx, kubeClient, scopedNS, deploymentName)
|
|
_ = utils.DeleteConfigMap(ctx, kubeClient, scopedNS, configMapName)
|
|
_ = undeployReloader()
|
|
_ = utils.DeleteNamespace(ctx, kubeClient, scopedNS)
|
|
_ = utils.DeleteNamespace(ctx, kubeClient, otherNS)
|
|
})
|
|
|
|
It("should reload workloads in a listed namespace", func() {
|
|
By("Creating a ConfigMap in the listed namespace")
|
|
_, err := utils.CreateConfigMap(ctx, kubeClient, scopedNS, configMapName,
|
|
map[string]string{"key": "initial"}, nil)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Creating a Deployment in the listed namespace with auto annotation")
|
|
_, err = utils.CreateDeployment(ctx, kubeClient, scopedNS, deploymentName,
|
|
utils.WithConfigMapEnvFrom(configMapName),
|
|
utils.WithAnnotations(utils.BuildAutoTrueAnnotation()),
|
|
)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be ready")
|
|
err = adapter.WaitReady(ctx, scopedNS, deploymentName, utils.WorkloadReadyTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Updating the ConfigMap")
|
|
err = utils.UpdateConfigMap(ctx, kubeClient, scopedNS, configMapName, map[string]string{"key": "updated"})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be reloaded")
|
|
reloaded, err := adapter.WaitReloaded(ctx, scopedNS, deploymentName,
|
|
utils.AnnotationLastReloadedFrom, utils.ReloadTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(reloaded).To(BeTrue(), "Deployment in a listed namespace should reload")
|
|
})
|
|
|
|
It("should reload workloads in Reloader's auto-included release namespace", func() {
|
|
By("Creating a ConfigMap in Reloader's namespace")
|
|
_, err := utils.CreateConfigMap(ctx, kubeClient, testNamespace, configMapName,
|
|
map[string]string{"key": "initial"}, nil)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Creating a Deployment in Reloader's namespace with auto annotation")
|
|
_, err = utils.CreateDeployment(ctx, kubeClient, testNamespace, deploymentName,
|
|
utils.WithConfigMapEnvFrom(configMapName),
|
|
utils.WithAnnotations(utils.BuildAutoTrueAnnotation()),
|
|
)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be ready")
|
|
err = adapter.WaitReady(ctx, testNamespace, deploymentName, utils.WorkloadReadyTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Updating the ConfigMap")
|
|
err = utils.UpdateConfigMap(ctx, kubeClient, testNamespace, configMapName, map[string]string{"key": "updated"})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be reloaded (release namespace is auto-included)")
|
|
reloaded, err := adapter.WaitReloaded(ctx, testNamespace, deploymentName,
|
|
utils.AnnotationLastReloadedFrom, utils.ReloadTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(reloaded).To(BeTrue(), "Deployment in Reloader's auto-included namespace should reload")
|
|
})
|
|
|
|
It("should NOT reload workloads in an unlisted namespace", func() {
|
|
By("Creating a ConfigMap in an unlisted namespace")
|
|
_, err := utils.CreateConfigMap(ctx, kubeClient, otherNS, configMapName,
|
|
map[string]string{"key": "initial"}, nil)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Creating a Deployment in an unlisted namespace with auto annotation")
|
|
_, err = utils.CreateDeployment(ctx, kubeClient, otherNS, deploymentName,
|
|
utils.WithConfigMapEnvFrom(configMapName),
|
|
utils.WithAnnotations(utils.BuildAutoTrueAnnotation()),
|
|
)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be ready")
|
|
err = adapter.WaitReady(ctx, otherNS, deploymentName, utils.WorkloadReadyTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Updating the ConfigMap in the unlisted namespace")
|
|
err = utils.UpdateConfigMap(ctx, kubeClient, otherNS, configMapName, map[string]string{"key": "updated"})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Verifying Deployment was NOT reloaded (namespace not in the list)")
|
|
time.Sleep(utils.NegativeTestWait)
|
|
reloaded, err := adapter.WaitReloaded(ctx, otherNS, deploymentName,
|
|
utils.AnnotationLastReloadedFrom, utils.ShortTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(reloaded).To(BeFalse(), "Deployment in an unlisted namespace should NOT reload")
|
|
})
|
|
})
|
|
|
|
Context("with watchGlobally=true flag (default)", func() {
|
|
var globalNS string
|
|
|
|
BeforeEach(func() {
|
|
globalNS = "global-" + utils.RandName("ns")
|
|
|
|
err := utils.CreateNamespace(ctx, kubeClient, globalNS)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
err = deployReloaderWithFlags(map[string]string{
|
|
"reloader.watchGlobally": "true",
|
|
})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
err = waitForReloaderReady()
|
|
Expect(err).NotTo(HaveOccurred())
|
|
})
|
|
|
|
AfterEach(func() {
|
|
_ = utils.DeleteDeployment(ctx, kubeClient, globalNS, deploymentName)
|
|
_ = utils.DeleteConfigMap(ctx, kubeClient, globalNS, configMapName)
|
|
_ = undeployReloader()
|
|
_ = utils.DeleteNamespace(ctx, kubeClient, globalNS)
|
|
})
|
|
|
|
It("should reload workloads in any namespace when watchGlobally=true", func() {
|
|
By("Creating a ConfigMap in a different namespace")
|
|
_, err := utils.CreateConfigMap(ctx, kubeClient, globalNS, configMapName,
|
|
map[string]string{"key": "initial"}, nil)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Creating a Deployment in a different namespace with auto annotation")
|
|
_, err = utils.CreateDeployment(ctx, kubeClient, globalNS, deploymentName,
|
|
utils.WithConfigMapEnvFrom(configMapName),
|
|
utils.WithAnnotations(utils.BuildAutoTrueAnnotation()),
|
|
)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be ready")
|
|
err = adapter.WaitReady(ctx, globalNS, deploymentName, utils.WorkloadReadyTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Updating the ConfigMap")
|
|
err = utils.UpdateConfigMap(ctx, kubeClient, globalNS, configMapName, map[string]string{"key": "updated"})
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
By("Waiting for Deployment to be reloaded (watchGlobally=true)")
|
|
reloaded, err := adapter.WaitReloaded(ctx, globalNS, deploymentName,
|
|
utils.AnnotationLastReloadedFrom, utils.ReloadTimeout)
|
|
Expect(err).NotTo(HaveOccurred())
|
|
Expect(reloaded).To(BeTrue(), "Deployment in any namespace should reload with watchGlobally=true")
|
|
})
|
|
})
|
|
})
|