Skip to content

Commit

Permalink
kubebuilder init changes
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Dec 18, 2023
1 parent 57babba commit c0f9289
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 17 deletions.
8 changes: 4 additions & 4 deletions controller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ docker-push: ## push Docker image with the driver
##@ Codegen

.PHONY: manifests
manifests: controller-gen ## generate Kubernetes manifests
manifests: controller-gen ## generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations
go generate ./pkg/...
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths=./api/...
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: genmock
genmock: ## generates mocks before running tests
Expand All @@ -78,11 +78,11 @@ genmock: ## generates mocks before running tests
##@ Tests

.PHONY: fmt
fmt: ## Run go fmt against code.
fmt: ## run go fmt against code
@go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
vet: ## run go vet against code
@go vet ./...

.PHONY: test
Expand Down
91 changes: 91 additions & 0 deletions controller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# test
// TODO(user): Add simple overview of use/purpose

## Description
// TODO(user): An in-depth paragraph about your project and overview of use

## Getting Started

### Prerequisites
- go version v1.20.0+
- docker version 17.03+.
- kubectl version v1.11.3+.
- Access to a Kubernetes v1.11.3+ cluster.

### To Deploy on the cluster
**Build and push your image to the location specified by `IMG`:**

```sh
make docker-build docker-push IMG=<some-registry>/test:tag
```

**NOTE:** This image ought to be published in the personal registry you specified.
And it is required to have access to pull the image from the working environment.
Make sure you have the proper permission to the registry if the above commands don’t work.

**Install the CRDs into the cluster:**

```sh
make install
```

**Deploy the Manager to the cluster with the image specified by `IMG`:**

```sh
make deploy IMG=<some-registry>/test:tag
```

> **NOTE**: If you encounter RBAC errors, you may need to grant yourself cluster-admin
privileges or be logged in as admin.

**Create instances of your solution**
You can apply the samples (examples) from the config/sample:

```sh
kubectl apply -k config/samples/
```

>**NOTE**: Ensure that the samples has default values to test it out.
### To Uninstall
**Delete the instances (CRs) from the cluster:**

```sh
kubectl delete -k config/samples/
```

**Delete the APIs(CRDs) from the cluster:**

```sh
make uninstall
```

**UnDeploy the controller from the cluster:**

```sh
make undeploy
```

## Contributing
// TODO(user): Add detailed information on how you would like others to contribute to this project

**NOTE:** Run `make --help` for more information on all potential `make` targets

More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html)

## License

Copyright 2023.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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.

69 changes: 56 additions & 13 deletions controller/cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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.
*/

package main

import (
Expand All @@ -6,31 +22,39 @@ import (
"os"
"strings"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

deploymentsv1alpha "github.com/pluralsh/console/controller/api/v1alpha1"
"github.com/pluralsh/console/controller/internal/client"
"github.com/pluralsh/console/controller/internal/types"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
ctrlruntimezap "sigs.k8s.io/controller-runtime/pkg/log/zap"

deploymentsv1alpha "github.com/pluralsh/console/controller/api/v1alpha1"
"github.com/pluralsh/console/controller/internal/client"
"github.com/pluralsh/console/controller/internal/types"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
//+kubebuilder:scaffold:imports
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("Setup")
setupLog = ctrl.Log.WithName("setup")
// version is managed by GoReleaser, see: https://goreleaser.com/cookbooks/using-main.version/
version = "dev"
// commit is managed by GoReleaser, see: https://goreleaser.com/cookbooks/using-main.version/
commit = "none"
)

func init() {
utilruntime.Must(deploymentsv1alpha.AddToScheme(scheme))
utilruntime.Must(corev1.AddToScheme(scheme))

utilruntime.Must(deploymentsv1alpha.AddToScheme(scheme))

//+kubebuilder:scaffold:scheme
}

type controllerRunOptions struct {
Expand All @@ -53,7 +77,7 @@ func main() {
}
opts.BindFlags(flag.CommandLine)
flag.StringVar(&opt.metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&opt.probeAddr, "health-probe-bind-address", ":9001", "The address the probe endpoint binds to.")
flag.StringVar(&opt.probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&opt.enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
Expand All @@ -78,16 +102,35 @@ func main() {

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
LeaderElection: opt.enableLeaderElection,
LeaderElectionID: "dep344ab8.plural.sh",
Metrics: metricsserver.Options{BindAddress: opt.metricsAddr},
HealthProbeBindAddress: opt.probeAddr,
LeaderElection: opt.enableLeaderElection,
LeaderElectionID: "144e1fda.plural.sh",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
})
if err != nil {
setupLog.Error(err, "unable to create manager")
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}

//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err = mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create health check")
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}

Expand Down Expand Up @@ -131,7 +174,7 @@ func runOrDie(controllers []types.Controller, mgr ctrl.Manager) {
ctx := ctrl.SetupSignalHandler()
setupLog.Info("starting manager")
if err := mgr.Start(ctx); err != nil {
setupLog.Error(err, "error running manager")
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
Expand Down

0 comments on commit c0f9289

Please sign in to comment.