Skip to content

Commit

Permalink
fix samples
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed May 8, 2023
1 parent ed2d282 commit 91bbdf9
Show file tree
Hide file tree
Showing 17 changed files with 9,828 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
*/

package v1

/*
*/

Expand Down Expand Up @@ -61,7 +62,6 @@ import (
the fields.
*/


// CronJobSpec defines the desired state of CronJob
type CronJobSpec struct {
//+kubebuilder:validation:MinLength=0
Expand Down Expand Up @@ -188,4 +188,5 @@ type CronJobList struct {
func init() {
SchemeBuilder.Register(&CronJob{}, &CronJobList{})
}

//+kubebuilder:docs-gen:collapse=Root Object Definitions
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Next, we'll setup a logger for the webhooks.
*/

var cronjoblog = logf.Log.WithName("cronjob-resource")

/*
Then, we set up the webhook with the manager.
*/
Expand All @@ -63,14 +64,11 @@ A webhook will automatically be served that calls this defaulting.
The `Default` method is expected to mutate the receiver, setting the defaults.
*/




var _ webhook.Defaulter = &CronJob{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *CronJob) Default() {
cronjoblog.Info("default", "name", r.Name)
cronjoblog.Info("default", "name", r.Name)

if r.Spec.ConcurrencyPolicy == "" {
r.Spec.ConcurrencyPolicy = AllowConcurrent
Expand Down Expand Up @@ -116,24 +114,19 @@ Here, however, we just use the same shared validation for `ValidateCreate` and
validate anything on deletion.
*/




var _ webhook.Validator = &CronJob{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *CronJob) ValidateCreate() (admission.Warnings, error) {
cronjoblog.Info("validate create", "name", r.Name)


return nil, r.validateCronJob()
}

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


return nil, r.validateCronJob()
}

Expand All @@ -143,6 +136,8 @@ func (r *CronJob) ValidateDelete() (admission.Warnings, error) {

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

/*
We validate the name and the spec of the CronJob.
*/
Expand Down Expand Up @@ -217,4 +212,3 @@ func (r *CronJob) validateCronJobName() *field.Error {
}

// +kubebuilder:docs-gen:collapse=Validate object name
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +kubebuilder:docs-gen:collapse=Apache License

/*
First, we have some *package-level* markers that denote that there are
Kubernetes objects in this package, and that this package represents the group
`batch.tutorial.kubebuilder.io`. The `object` generator makes use of the
former, while the latter is used by the CRD generator to generate the right
metadata for the CRDs it creates from this package.
*/

// Package v1 contains API Schema definitions for the batch v1 API group
// +kubebuilder:object:generate=true
Expand All @@ -24,6 +33,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

/*
Then, we have the commonly useful variables that help us set up our Scheme.
Since we need to use all the types in this package in our controller, it's
helpful (and the convention) to have a convenient method to add all the types to
some other `Scheme`. SchemeBuilder makes this easy for us.
*/

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "batch.tutorial.kubebuilder.io", Version: "v1"}
Expand Down

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

39 changes: 36 additions & 3 deletions docs/book/src/cronjob-tutorial/testdata/project/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +kubebuilder:docs-gen:collapse=Apache License

package main

Expand All @@ -36,6 +37,17 @@ import (
//+kubebuilder:scaffold:imports
)

// +kubebuilder:docs-gen:collapse=Imports

/*
The first difference to notice is that kubebuilder has added the new API
group's package (`batchv1`) to our scheme. This means that we can use those
objects in our controller.
If we would be using any other CRD we would have to add their scheme the same way.
Builtin types such as Job have their scheme added by `clientgoscheme`.
*/

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
Expand All @@ -48,7 +60,14 @@ func init() {
//+kubebuilder:scaffold:scheme
}

/*
The other thing that's changed is that kubebuilder has added a block calling our
CronJob controller's `SetupWithManager` method.
*/

func main() {
/*
*/
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
Expand Down Expand Up @@ -89,16 +108,29 @@ func main() {
os.Exit(1)
}

// +kubebuilder:docs-gen:collapse=old stuff

if err = (&controller.CronJobReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CronJob")
os.Exit(1)
}
if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
os.Exit(1)

/*
We'll also set up webhooks for our type, which we'll talk about next.
We just need to add them to the manager. Since we might want to run
the webhooks separately, or not run them when testing our controller
locally, we'll put them behind an environment variable.
We'll just make sure to set `ENABLE_WEBHOOKS=false` when we run locally.
*/
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CronJob")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder

Expand All @@ -116,4 +148,5 @@ func main() {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
// +kubebuilder:docs-gen:collapse=old stuff
}
Loading

0 comments on commit 91bbdf9

Please sign in to comment.