Skip to content

Commit

Permalink
Merge pull request #187 from openshift-cherrypick-robot/cherry-pick-1…
Browse files Browse the repository at this point in the history
…86-to-18.0-fr1

[18.0-fr1] Ensure nodeSelector logic is consistent for all operators
  • Loading branch information
openshift-merge-bot[bot] authored Nov 22, 2024
2 parents 1b231eb + 2c834c3 commit 6de316c
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 17 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/barbican_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type BarbicanSpecBase struct {
// +kubebuilder:validation:Optional
// NodeSelector to target subset of worker nodes running this component. Setting here overrides
// any global NodeSelector settings within the Barbican CR.
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
NodeSelector *map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// CustomServiceConfig - customize the service config using this parameter to change service defaults,
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type BarbicanComponentTemplate struct {
// +kubebuilder:validation:Optional
// NodeSelector to target subset of worker nodes running this component. Setting here overrides
// any global NodeSelector settings within the Barbican CR.
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
NodeSelector *map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// +kubebuilder:default=1
Expand Down
20 changes: 14 additions & 6 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions controllers/barbican_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,8 @@ func (r *BarbicanReconciler) apiDeploymentCreateOrUpdate(ctx context.Context, in

// If NodeSelector is not specified in BarbicanAPITemplate, the current
// API instance inherits the value from the top-level CR.
if apiSpec.BarbicanAPITemplate.BarbicanAPITemplateCore.BarbicanComponentTemplate.NodeSelector == nil {
apiSpec.BarbicanAPITemplate.BarbicanAPITemplateCore.BarbicanComponentTemplate.NodeSelector = instance.Spec.BarbicanSpecBase.NodeSelector
if apiSpec.NodeSelector == nil {
apiSpec.NodeSelector = instance.Spec.NodeSelector
}

deployment := &barbicanv1beta1.BarbicanAPI{
Expand Down Expand Up @@ -725,8 +725,8 @@ func (r *BarbicanReconciler) workerDeploymentCreateOrUpdate(ctx context.Context,

// If NodeSelector is not specified in BarbicanWorkerTemplate, the current
// Worker instance inherits the value from the top-level CR.
if workerSpec.BarbicanWorkerTemplate.BarbicanWorkerTemplateCore.BarbicanComponentTemplate.NodeSelector == nil {
workerSpec.BarbicanWorkerTemplate.BarbicanWorkerTemplateCore.BarbicanComponentTemplate.NodeSelector = instance.Spec.BarbicanSpecBase.NodeSelector
if workerSpec.NodeSelector == nil {
workerSpec.NodeSelector = instance.Spec.NodeSelector
}

deployment := &barbicanv1beta1.BarbicanWorker{
Expand Down Expand Up @@ -768,8 +768,8 @@ func (r *BarbicanReconciler) keystoneListenerDeploymentCreateOrUpdate(ctx contex

// If NodeSelector is not specified in BarbicanKeystoneListenerTemplate, the current
// KeystoneListener instance inherits the value from the top-level CR.
if keystoneListenerSpec.BarbicanKeystoneListenerTemplate.BarbicanKeystoneListenerTemplateCore.BarbicanComponentTemplate.NodeSelector == nil {
keystoneListenerSpec.BarbicanKeystoneListenerTemplate.BarbicanKeystoneListenerTemplateCore.BarbicanComponentTemplate.NodeSelector = instance.Spec.BarbicanSpecBase.NodeSelector
if keystoneListenerSpec.NodeSelector == nil {
keystoneListenerSpec.NodeSelector = instance.Spec.NodeSelector
}

deployment := &barbicanv1beta1.BarbicanKeystoneListener{
Expand Down
4 changes: 4 additions & 0 deletions pkg/barbican/dbsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,9 @@ func DbSyncJob(instance *barbicanv1beta1.Barbican, labels map[string]string, ann
dbSyncVolume...,
)

if instance.Spec.NodeSelector != nil {
job.Spec.Template.Spec.NodeSelector = *instance.Spec.NodeSelector
}

return job
}
5 changes: 4 additions & 1 deletion pkg/barbicanapi/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func Deployment(
},
Spec: corev1.PodSpec{
ServiceAccountName: instance.Spec.ServiceAccount,
NodeSelector: instance.Spec.NodeSelector,
Containers: []corev1.Container{
{
Name: instance.Name + "-log",
Expand Down Expand Up @@ -183,5 +182,9 @@ func Deployment(
instance.Spec.CustomServiceConfigSecrets),
apiVolumes...)

if instance.Spec.NodeSelector != nil {
deployment.Spec.Template.Spec.NodeSelector = *instance.Spec.NodeSelector
}

return deployment, nil
}
6 changes: 5 additions & 1 deletion pkg/barbicankeystonelistener/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func Deployment(
},
Spec: corev1.PodSpec{
ServiceAccountName: instance.Spec.ServiceAccount,
NodeSelector: instance.Spec.NodeSelector,
Containers: []corev1.Container{
{
Name: instance.Name + "-log",
Expand Down Expand Up @@ -129,5 +128,10 @@ func Deployment(
instance.Name,
instance.Spec.CustomServiceConfigSecrets),
keystoneListenerVolumes...)

if instance.Spec.NodeSelector != nil {
deployment.Spec.Template.Spec.NodeSelector = *instance.Spec.NodeSelector
}

return deployment
}
6 changes: 5 additions & 1 deletion pkg/barbicanworker/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func Deployment(
},
Spec: corev1.PodSpec{
ServiceAccountName: instance.Spec.ServiceAccount,
NodeSelector: instance.Spec.NodeSelector,
Containers: []corev1.Container{
{
Name: instance.Name + "-log",
Expand Down Expand Up @@ -157,5 +156,10 @@ func Deployment(
instance.Name,
instance.Spec.CustomServiceConfigSecrets),
workerVolumes...)

if instance.Spec.NodeSelector != nil {
deployment.Spec.Template.Spec.NodeSelector = *instance.Spec.NodeSelector
}

return deployment
}
142 changes: 142 additions & 0 deletions tests/functional/barbican_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,148 @@ var _ = Describe("Barbican controller", func() {
})
})

When("A Barbican with nodeSelector is created", func() {
BeforeEach(func() {
spec := GetDefaultBarbicanSpec()
spec["nodeSelector"] = map[string]interface{}{
"foo": "bar",
}
spec["barbicanAPI"] = GetDefaultBarbicanAPISpec()
DeferCleanup(k8sClient.Delete, ctx, CreateBarbicanMessageBusSecret(barbicanTest.Instance.Namespace, "rabbitmq-secret"))
DeferCleanup(th.DeleteInstance, CreateBarbican(barbicanTest.Instance, spec))
DeferCleanup(k8sClient.Delete, ctx, CreateKeystoneAPISecret(barbicanTest.Instance.Namespace, SecretName))

DeferCleanup(
k8sClient.Delete, ctx, CreateBarbicanSecret(barbicanTest.Instance.Namespace, "test-osp-secret-barbican"))

DeferCleanup(
mariadb.DeleteDBService,
mariadb.CreateDBService(
barbicanTest.Instance.Namespace,
GetBarbican(barbicanTest.Instance).Spec.DatabaseInstance,
corev1.ServiceSpec{
Ports: []corev1.ServicePort{{Port: 3306}},
},
),
)
infra.SimulateTransportURLReady(barbicanTest.BarbicanTransportURL)
DeferCleanup(keystone.DeleteKeystoneAPI, keystone.CreateKeystoneAPI(barbicanTest.Instance.Namespace))
mariadb.SimulateMariaDBAccountCompleted(barbicanTest.BarbicanDatabaseAccount)
mariadb.SimulateMariaDBDatabaseCompleted(barbicanTest.BarbicanDatabaseName)
th.SimulateJobSuccess(barbicanTest.BarbicanDBSync)
})

It("sets nodeSelector in resource specs", func() {
Eventually(func(g Gomega) {
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())
})

It("updates nodeSelector in resource specs when changed", func() {
Eventually(func(g Gomega) {
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
barbican := GetBarbican(barbicanName)
newNodeSelector := map[string]string{
"foo2": "bar2",
}
barbican.Spec.NodeSelector = &newNodeSelector
g.Expect(k8sClient.Update(ctx, barbican)).Should(Succeed())
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
th.SimulateJobSuccess(barbicanTest.BarbicanDBSync)
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo2": "bar2"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo2": "bar2"}))
}, timeout, interval).Should(Succeed())
})

It("removes nodeSelector from resource specs when cleared", func() {
Eventually(func(g Gomega) {
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
barbican := GetBarbican(barbicanName)
emptyNodeSelector := map[string]string{}
barbican.Spec.NodeSelector = &emptyNodeSelector
g.Expect(k8sClient.Update(ctx, barbican)).Should(Succeed())
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
th.SimulateJobSuccess(barbicanTest.BarbicanDBSync)
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(BeNil())
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(BeNil())
}, timeout, interval).Should(Succeed())
})

It("removes nodeSelector from resource specs when nilled", func() {
Eventually(func(g Gomega) {
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
barbican := GetBarbican(barbicanName)
barbican.Spec.NodeSelector = nil
g.Expect(k8sClient.Update(ctx, barbican)).Should(Succeed())
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
th.SimulateJobSuccess(barbicanTest.BarbicanDBSync)
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(BeNil())
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(BeNil())
}, timeout, interval).Should(Succeed())
})

It("allows nodeSelector service override", func() {
Eventually(func(g Gomega) {
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
barbican := GetBarbican(barbicanName)
apiNodeSelector := map[string]string{
"foo": "api",
}
barbican.Spec.BarbicanAPI.NodeSelector = &apiNodeSelector
g.Expect(k8sClient.Update(ctx, barbican)).Should(Succeed())
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
th.SimulateJobSuccess(barbicanTest.BarbicanDBSync)
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "api"}))
}, timeout, interval).Should(Succeed())
})

It("allows nodeSelector service override to empty", func() {
Eventually(func(g Gomega) {
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
barbican := GetBarbican(barbicanName)
emptyNodeSelector := map[string]string{}
barbican.Spec.BarbicanAPI.NodeSelector = &emptyNodeSelector
g.Expect(k8sClient.Update(ctx, barbican)).Should(Succeed())
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
th.SimulateJobSuccess(barbicanTest.BarbicanDBSync)
g.Expect(th.GetJob(barbicanTest.BarbicanDBSync).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
g.Expect(th.GetDeployment(barbicanTest.BarbicanAPI).Spec.Template.Spec.NodeSelector).To(BeNil())
}, timeout, interval).Should(Succeed())
})
})

// Run MariaDBAccount suite tests. these are pre-packaged ginkgo tests
// that exercise standard account create / update patterns that should be
// common to all controllers that ensure MariaDBAccount CRs.
Expand Down

0 comments on commit 6de316c

Please sign in to comment.