Skip to content

Commit

Permalink
Merge pull request #417 from olliewalsh/node_selectors
Browse files Browse the repository at this point in the history
Ensure nodeSelector logic is consistent for all operators
  • Loading branch information
openshift-merge-bot[bot] authored Nov 21, 2024
2 parents 08d72c3 + 3e1f03d commit bc64ab6
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 37 deletions.
2 changes: 1 addition & 1 deletion api/v1beta1/amphoracontroller_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type OctaviaAmphoraControllerSpecCore struct {

// +kubebuilder:validation:Optional
// NodeSelector to target subset of worker nodes running this service
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
NodeSelector *map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// +kubebuilder:default="# add your customization here"
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/octavia_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ type OctaviaSpecBase struct {

// +kubebuilder:validation:Optional
// NodeSelector to target subset of worker nodes running this service
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
NodeSelector *map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// +kubebuilder:default=false
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/octaviaapi_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type OctaviaAPISpecCore struct {

// +kubebuilder:validation:Optional
// NodeSelector to target subset of worker nodes running this service
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
NodeSelector *map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// +kubebuilder:default=false
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/octaviarsyslog_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type OctaviaRsyslogSpecCore struct {

// +kubebuilder:validation:Optional
// NodeSelector to target subset of worker nodes running this service
NodeSelector map[string]string `json:"nodeSelector,omitempty"`
NodeSelector *map[string]string `json:"nodeSelector,omitempty"`

// +kubebuilder:validation:Optional
// ConfigOverwrite - interface to overwrite default config files like e.g. logging.conf or policy.json.
Expand Down
40 changes: 28 additions & 12 deletions api/v1beta1/zz_generated.deepcopy.go

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

21 changes: 12 additions & 9 deletions controllers/octavia_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,10 @@ func (r *OctaviaReconciler) apiDeploymentCreateOrUpdate(instance *octaviav1.Octa
},
}

if instance.Spec.OctaviaAPI.NodeSelector == nil {
instance.Spec.OctaviaAPI.NodeSelector = instance.Spec.NodeSelector
}

op, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, deployment, func() error {
deployment.Spec = instance.Spec.OctaviaAPI
deployment.Spec.DatabaseInstance = instance.Spec.DatabaseInstance
Expand All @@ -1489,9 +1493,6 @@ func (r *OctaviaReconciler) apiDeploymentCreateOrUpdate(instance *octaviav1.Octa
deployment.Spec.TLS = instance.Spec.OctaviaAPI.TLS
deployment.Spec.APITimeout = instance.Spec.APITimeout

if len(deployment.Spec.NodeSelector) == 0 {
deployment.Spec.NodeSelector = instance.Spec.NodeSelector
}
err := controllerutil.SetControllerReference(instance, deployment, r.Scheme)
if err != nil {
return err
Expand Down Expand Up @@ -1537,6 +1538,10 @@ func (r *OctaviaReconciler) amphoraControllerDaemonSetCreateOrUpdate(
},
}

if controllerSpec.NodeSelector == nil {
controllerSpec.NodeSelector = instance.Spec.NodeSelector
}

op, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, daemonset, func() error {
daemonset.Spec = controllerSpec
daemonset.Spec.Role = role
Expand All @@ -1556,9 +1561,6 @@ func (r *OctaviaReconciler) amphoraControllerDaemonSetCreateOrUpdate(
daemonset.Spec.OctaviaProviderSubnetGateway = networkInfo.ManagementSubnetGateway
daemonset.Spec.OctaviaProviderSubnetCIDR = networkInfo.ManagementSubnetCIDR
daemonset.Spec.OctaviaProviderSubnetExtraCIDRs = networkInfo.ManagementSubnetExtraCIDRs
if len(daemonset.Spec.NodeSelector) == 0 {
daemonset.Spec.NodeSelector = instance.Spec.NodeSelector
}
err := controllerutil.SetControllerReference(instance, daemonset, r.Scheme)
if err != nil {
return err
Expand Down Expand Up @@ -1618,13 +1620,14 @@ func (r *OctaviaReconciler) octaviaRsyslogDaemonSetCreateOrUpdate(
},
}

if controllerSpec.NodeSelector == nil {
controllerSpec.NodeSelector = instance.Spec.NodeSelector
}

op, err := controllerutil.CreateOrUpdate(context.TODO(), r.Client, daemonset, func() error {
daemonset.Spec = controllerSpec
daemonset.Spec.ServiceUser = instance.Spec.ServiceUser
daemonset.Spec.ServiceAccount = instance.RbacResourceName()
if len(daemonset.Spec.NodeSelector) == 0 {
daemonset.Spec.NodeSelector = instance.Spec.NodeSelector
}
err := controllerutil.SetControllerReference(instance, daemonset, r.Scheme)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/amphoracontrollers/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ func DaemonSet(
},
corev1.LabelHostname,
)
if len(instance.Spec.NodeSelector) > 0 {
daemonset.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
if instance.Spec.NodeSelector != nil {
daemonset.Spec.Template.Spec.NodeSelector = *instance.Spec.NodeSelector
}

return daemonset
Expand Down
4 changes: 4 additions & 0 deletions pkg/octavia/dbsync.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,9 @@ func DbSyncJob(
},
}

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

return job
}
4 changes: 4 additions & 0 deletions pkg/octavia/image_upload_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func ImageUploadDeployment(
}
depl.Spec.Template.Spec.InitContainers = initContainer(initContainerDetails)

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

return depl
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/octaviaapi/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ func Deployment(
},
corev1.LabelHostname,
)
if len(instance.Spec.NodeSelector) > 0 {
deployment.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
if instance.Spec.NodeSelector != nil {
deployment.Spec.Template.Spec.NodeSelector = *instance.Spec.NodeSelector
}

return deployment, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/octaviarsyslog/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ func DaemonSet(
},
corev1.LabelHostname,
)
if len(instance.Spec.NodeSelector) > 0 {
daemonset.Spec.Template.Spec.NodeSelector = instance.Spec.NodeSelector
if instance.Spec.NodeSelector != nil {
daemonset.Spec.Template.Spec.NodeSelector = *instance.Spec.NodeSelector
}

return daemonset
Expand Down
107 changes: 101 additions & 6 deletions tests/functional/octavia_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ var _ = Describe("Octavia controller", func() {
var octaviaName types.NamespacedName
var transportURLName types.NamespacedName
var transportURLSecretName types.NamespacedName
var octaviaDBSyncName types.NamespacedName

BeforeEach(func() {
name = fmt.Sprintf("octavia-%s", uuid.New().String())
Expand All @@ -128,6 +129,11 @@ var _ = Describe("Octavia controller", func() {
Namespace: namespace,
Name: RabbitmqSecretName,
}

octaviaDBSyncName = types.NamespacedName{
Namespace: namespace,
Name: octaviaName.Name + "-db-sync",
}
})

When("an Octavia instance is created", func() {
Expand Down Expand Up @@ -337,7 +343,7 @@ var _ = Describe("Octavia controller", func() {
mariadb.SimulateMariaDBDatabaseCompleted(types.NamespacedName{Namespace: namespace, Name: octavia.DatabaseCRName})
mariadb.SimulateMariaDBAccountCompleted(types.NamespacedName{Namespace: namespace, Name: GetOctavia(octaviaName).Spec.PersistenceDatabaseAccount})
mariadb.SimulateMariaDBDatabaseCompleted(types.NamespacedName{Namespace: namespace, Name: octavia.PersistenceDatabaseCRName})
th.SimulateJobSuccess(types.NamespacedName{Namespace: namespace, Name: octaviaName.Name + "-db-sync"})
th.SimulateJobSuccess(octaviaDBSyncName)
octavia := GetOctavia(octaviaName)
hostname := "hostname-for-" + octavia.Spec.DatabaseInstance + "." + namespace + ".svc"
Expect(octavia.Status.DatabaseHostname).To(Equal(hostname))
Expand Down Expand Up @@ -369,7 +375,7 @@ var _ = Describe("Octavia controller", func() {

DeferCleanup(th.DeleteInstance, CreateOctavia(octaviaName, spec))

th.SimulateJobSuccess(types.NamespacedName{Namespace: namespace, Name: octaviaName.Name + "-db-sync"})
th.SimulateJobSuccess(octaviaDBSyncName)
})

It("should set Service Config Ready Condition", func() {
Expand Down Expand Up @@ -499,7 +505,7 @@ var _ = Describe("Octavia controller", func() {

DeferCleanup(th.DeleteInstance, CreateOctavia(octaviaName, spec))

th.SimulateJobSuccess(types.NamespacedName{Namespace: namespace, Name: octaviaName.Name + "-db-sync"})
th.SimulateJobSuccess(octaviaDBSyncName)
})

It("should create appropriate resources in Neutron", func() {
Expand Down Expand Up @@ -662,7 +668,7 @@ var _ = Describe("Octavia controller", func() {
spec["lbMgmtNetwork"].(map[string]interface{})["createDefaultLbMgmtNetwork"] = false
DeferCleanup(th.DeleteInstance, CreateOctavia(octaviaName, spec))

th.SimulateJobSuccess(types.NamespacedName{Namespace: namespace, Name: octaviaName.Name + "-db-sync"})
th.SimulateJobSuccess(octaviaDBSyncName)
})

It("should create appropriate resources in Neutron", func() {
Expand Down Expand Up @@ -820,6 +826,95 @@ var _ = Describe("Octavia controller", func() {
})
})

When("Ocatavia is created with nodeSelector", func() {
BeforeEach(func() {
spec["nodeSelector"] = map[string]interface{}{
"foo": "bar",
}

createAndSimulateKeystone(octaviaName)

createAndSimulateOctaviaSecrets(octaviaName)
createAndSimulateTransportURL(transportURLName, transportURLSecretName)

createAndSimulateDB(spec)

DeferCleanup(th.DeleteInstance, CreateOctavia(octaviaName, spec))

th.SimulateJobSuccess(octaviaDBSyncName)

// TODO: assert nodeSelector on more resources when supported
})

It("sets nodeSelector in resource specs", func() {
Eventually(func(g Gomega) {
g.Expect(th.GetJob(octaviaDBSyncName).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(octaviaDBSyncName).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

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

Eventually(func(g Gomega) {
th.SimulateJobSuccess(octaviaDBSyncName)
g.Expect(th.GetJob(octaviaDBSyncName).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(octaviaDBSyncName).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

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

Eventually(func(g Gomega) {
th.SimulateJobSuccess(octaviaDBSyncName)
g.Expect(th.GetJob(octaviaDBSyncName).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(octaviaDBSyncName).Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"foo": "bar"}))
}, timeout, interval).Should(Succeed())

Eventually(func(g Gomega) {
octavia := GetOctavia(octaviaName)
octavia.Spec.NodeSelector = nil
g.Expect(k8sClient.Update(ctx, octavia)).Should(Succeed())
}, timeout, interval).Should(Succeed())

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

// It("allows nodeSelector service override", func() {
// })

// It("allows nodeSelector service override to empty", func() {
// })

})

// Predictable IPs

// Amphora Controller Daemonsets
Expand Down Expand Up @@ -852,7 +947,7 @@ var _ = Describe("Octavia controller", func() {

DeferCleanup(th.DeleteInstance, CreateOctavia(octaviaName, spec))

th.SimulateJobSuccess(types.NamespacedName{Namespace: namespace, Name: octaviaName.Name + "-db-sync"})
th.SimulateJobSuccess(octaviaDBSyncName)
})

It("should set OctaviaAmphoraSSHReady condition", func() {
Expand Down Expand Up @@ -913,7 +1008,7 @@ var _ = Describe("Octavia controller", func() {

DeferCleanup(th.DeleteInstance, CreateOctavia(octaviaName, spec))

th.SimulateJobSuccess(types.NamespacedName{Namespace: namespace, Name: octaviaName.Name + "-db-sync"})
th.SimulateJobSuccess(octaviaDBSyncName)
})

// PENDING https://issues.redhat.com/browse/OSPRH-10543
Expand Down

0 comments on commit bc64ab6

Please sign in to comment.