Skip to content

Commit 39ed325

Browse files
committed
Introduce webhook to restrict to one RSCT CR
1 parent 785aeea commit 39ed325

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ RUN go mod download
1212
# Copy the go source
1313
COPY cmd/main.go cmd/main.go
1414
COPY api/ api/
15-
COPY internal/controller/ internal/controller/
15+
COPY internal/ internal/
1616

1717
# Build
1818
# the GOARCH has not a default value to allow the binary be built according to the host where the command

cmd/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737

3838
rsctv1alpha1 "github.com/ocp-power-automation/rsct-operator/api/v1alpha1"
3939
"github.com/ocp-power-automation/rsct-operator/internal/controller"
40+
rsctwebhook "github.com/ocp-power-automation/rsct-operator/internal/webhook/rsct"
4041
//+kubebuilder:scaffold:imports
4142
)
4243

@@ -150,6 +151,11 @@ func main() {
150151
}
151152
//+kubebuilder:scaffold:builder
152153

154+
if err := rsctwebhook.RegisterWebhooks(mgr); err != nil {
155+
setupLog.Error(err, "unable to register webhooks")
156+
os.Exit(1)
157+
}
158+
153159
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
154160
setupLog.Error(err, "unable to set up health check")
155161
os.Exit(1)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package rsct
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
rsctv1alpha1 "github.com/ocp-power-automation/rsct-operator/api/v1alpha1"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
ctrl "sigs.k8s.io/controller-runtime"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
11+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
12+
)
13+
14+
type RSCTValidator struct {
15+
Client client.Client
16+
}
17+
18+
// ValidateCreate implements admission.CustomValidator.
19+
func (r *RSCTValidator) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
20+
cr, ok := obj.(*rsctv1alpha1.RSCT)
21+
if !ok {
22+
return nil, fmt.Errorf("expected RSCT, got %T", obj)
23+
}
24+
25+
var crList rsctv1alpha1.RSCTList
26+
if err := r.Client.List(ctx, &crList); err != nil {
27+
return nil, fmt.Errorf("cannot list RSCT: %w", err)
28+
}
29+
30+
if len(crList.Items) > 0 {
31+
return nil, fmt.Errorf("only one RSCT instance is allowed (found %d), rejecting creation of %s", len(crList.Items), cr.Name)
32+
}
33+
34+
return nil, nil
35+
}
36+
37+
// ValidateDelete implements admission.CustomValidator.
38+
func (r *RSCTValidator) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
39+
return nil, nil
40+
}
41+
42+
// ValidateUpdate implements admission.CustomValidator.
43+
func (r *RSCTValidator) ValidateUpdate(ctx context.Context, oldObj runtime.Object, newObj runtime.Object) (warnings admission.Warnings, err error) {
44+
return nil, nil
45+
}
46+
47+
var _ admission.CustomValidator = &RSCTValidator{}
48+
49+
// Register the webhook with the manager
50+
func RegisterWebhooks(mgr ctrl.Manager) error {
51+
return ctrl.NewWebhookManagedBy(mgr).
52+
For(&rsctv1alpha1.RSCT{}).
53+
WithValidator(&RSCTValidator{Client: mgr.GetClient()}).
54+
Complete()
55+
}

0 commit comments

Comments
 (0)