Skip to content

Commit

Permalink
add guestbook webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
fuscoyu committed Jan 17, 2024
1 parent 7afdaa6 commit 221883e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion project/guestbook/api/v1/guestbook_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ type GuestbookSpec struct {
// Important: Run "make" to regenerate code after modifying this file

// Foo is an example field of Guestbook. Edit guestbook_types.go to remove/update
Foo string `json:"foo,omitempty"`
Foo string `json:"foo,omitempty"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}

// GuestbookStatus defines the observed state of Guestbook
type GuestbookStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
Status string `json:"Status"`
}

//+kubebuilder:object:root=true
Expand Down
13 changes: 11 additions & 2 deletions project/guestbook/api/v1/guestbook_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1

import (
"errors"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -57,15 +59,15 @@ func (r *Guestbook) ValidateCreate() (admission.Warnings, error) {
guestbooklog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil, nil
return r.validataGuestbook()
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Guestbook) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
guestbooklog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil, nil
return r.validataGuestbook()
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
Expand All @@ -75,3 +77,10 @@ func (r *Guestbook) ValidateDelete() (admission.Warnings, error) {
// TODO(user): fill in your validation logic upon object deletion.
return nil, nil
}

func (r *Guestbook) validataGuestbook() (admission.Warnings, error) {
if len(r.Spec.Firstname) == 0 || len(r.Spec.Lastname) == 0 {
return nil, errors.New("firstname and Lastname cannot be set at the same time")
}
return nil, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,28 @@ spec:
spec:
description: GuestbookSpec defines the desired state of Guestbook
properties:
firstname:
type: string
foo:
description: Foo is an example field of Guestbook. Edit guestbook_types.go
to remove/update
type: string
lastname:
type: string
required:
- firstname
- lastname
type: object
status:
description: GuestbookStatus defines the observed state of Guestbook
properties:
Status:
description: 'INSERT ADDITIONAL STATUS FIELD - define observed state
of cluster Important: Run "make" to regenerate code after modifying
this file'
type: string
required:
- Status
type: object
type: object
served: true
Expand Down
2 changes: 1 addition & 1 deletion project/guestbook/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kind: Kustomization
images:
- name: controller
newName: 127.0.0.1:5000/guestbook
newTag: v5
newTag: v6
13 changes: 13 additions & 0 deletions project/guestbook/internal/controller/guestbook_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

LOG "log"

webappv1 "my.domain/guestbook/api/v1"
)

Expand All @@ -50,6 +52,17 @@ func (r *GuestbookReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
_ = log.FromContext(ctx)

// TODO(user): your logic here
obj := &webappv1.Guestbook{}
if err := r.Get(ctx, req.NamespacedName, obj); err != nil {
LOG.Println(err, "Unable to fetch object")
} else {
LOG.Println("Geeting from kubebuilder to", obj.Spec.Firstname, obj.Spec.Lastname)
}

obj.Status.Status = "Running"
if err := r.Status().Update(ctx, obj); err != nil {
LOG.Println(err, "Unable to update status")
}

return ctrl.Result{}, nil
}
Expand Down

0 comments on commit 221883e

Please sign in to comment.