Skip to content

Commit

Permalink
Merge pull request openstack-k8s-operators#824 from dprince/single_op…
Browse files Browse the repository at this point in the history
…enstack_version

OpenStackVersion: ensure there is only one
  • Loading branch information
openshift-merge-bot[bot] authored Jun 6, 2024
2 parents f1690a3 + 803ac51 commit 3b8e72b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
50 changes: 48 additions & 2 deletions apis/core/v1beta1/openstackversion_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,24 @@ limitations under the License.
package v1beta1

import (
"context"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openstack-k8s-operators/lib-common/modules/common/util"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
goClient "sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

var versionWebhookClient goClient.Client

// OpenStackVersionDefaults -
type OpenStackVersionDefaults struct {
availableVersion string
Expand All @@ -47,13 +53,16 @@ func SetupOpenStackVersionDefaults(defaults OpenStackVersionDefaults) {

// SetupWebhookWithManager - register OpenStackVersion with the controller manager
func (r *OpenStackVersion) SetupWebhookWithManager(mgr ctrl.Manager) error {

if versionWebhookClient == nil {
versionWebhookClient = mgr.GetClient()
}

return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}

// TODO(user): EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!

//+kubebuilder:webhook:path=/mutate-core-openstack-org-v1beta1-openstackversion,mutating=true,failurePolicy=fail,sideEffects=None,groups=core.openstack.org,resources=openstackversions,verbs=create;update,versions=v1beta1,name=mopenstackversion.kb.io,admissionReviewVersions=v1

var _ webhook.Defaulter = &OpenStackVersion{}
Expand Down Expand Up @@ -91,6 +100,43 @@ func (r *OpenStackVersion) ValidateCreate() (admission.Warnings, error) {
)
}

versionList := &OpenStackVersionList{}

listOpts := []client.ListOption{
client.InNamespace(r.Namespace),
}

if err := versionWebhookClient.List(context.TODO(), versionList, listOpts...); err != nil {

return nil, apierrors.NewForbidden(
schema.GroupResource{
Group: GroupVersion.WithKind("OpenStackVersion").Group,
Resource: GroupVersion.WithKind("OpenStackVersion").Kind,
}, r.GetName(), &field.Error{
Type: field.ErrorTypeForbidden,
Field: "",
BadValue: r.Spec.TargetVersion,
Detail: err.Error(),
},
)

}

if len(versionList.Items) >= 1 {

return nil, apierrors.NewForbidden(
schema.GroupResource{
Group: GroupVersion.WithKind("OpenStackVersion").Group,
Resource: GroupVersion.WithKind("OpenStackVersion").Kind,
}, r.GetName(), &field.Error{
Type: field.ErrorTypeForbidden,
Field: "",
BadValue: r.Spec.TargetVersion,
Detail: "Only one OpenStackVersion instance is supported at this time.",
},
)
}

return nil, nil
}

Expand Down
12 changes: 12 additions & 0 deletions tests/functional/openstackversion_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ var _ = Describe("OpenStackOperator controller", func() {
names.OpenStackVersionName,
)
})

It("should fail to create more than one OpenStackVersion", func() {

instance := &corev1.OpenStackVersion{}
instance.ObjectMeta.Namespace = names.Namespace
instance.Name = "foo"
Eventually(func(g Gomega) {
g.Expect(k8sClient.Create(ctx, instance)).Should(Not(Succeed()))
}, timeout, interval).Should(Succeed())

})

It("should initialize container images", func() {
Eventually(func(g Gomega) {

Expand Down

0 comments on commit 3b8e72b

Please sign in to comment.