Skip to content

Commit

Permalink
Clusterctl support
Browse files Browse the repository at this point in the history
Signed-off-by: Alexey Makhov <[email protected]>
  • Loading branch information
makhov committed Oct 18, 2023
1 parent 05256c8 commit 992612e
Show file tree
Hide file tree
Showing 25 changed files with 3,797 additions and 59 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,45 @@ jobs:
ghcr.io/k0sproject/k0smotron:${{ needs.release.outputs.image_tag }}
quay.io/k0sproject/k0smotron:${{ needs.release.outputs.image_tag }}
push: true

- name: Create install files
if: github.repository == 'k0sproject/k0smotron'
run: |
make bootstrap-components.yaml IMG=quay.io/k0sproject/k0smotron:${{ needs.release.outputs.image_tag }}
make control-plane-components.yaml IMG=quay.io/k0sproject/k0smotron:${{ needs.release.outputs.image_tag }}
- name: Upload Release Assets - metadata.yaml
id: upload-release-asset-images
uses: shogo82148/[email protected]
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ./metadata.yaml
asset_name: metadata.yaml
asset_content_type: application/octet-stream

- name: Upload Release Assets - bootstrap-components.yaml
id: upload-release-asset-images
uses: shogo82148/[email protected]
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ./bootstrap-components.yaml
asset_name: bootstrap-components.yaml
asset_content_type: application/octet-stream

- name: Upload Release Assets - control-plane-components.yaml
id: upload-release-asset-images
uses: shogo82148/[email protected]
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ./control-plane-components.yaml
asset_name: control-plane-components.yaml
asset_content_type: application/octet-stream

- name: Upload Release Assets - infrastructure-components.yaml
id: upload-release-asset-images
uses: shogo82148/[email protected]
with:
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ./infrastructure-components.yaml
asset_name: infrastructure-components.yaml
asset_content_type: application/octet-stream
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Generated files
install.yaml
bootstrap-components.yaml
control-plane-components.yaml

# Binaries for programs and plugins
*.exe
Expand Down
27 changes: 26 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ config/crd/bases/bootstrap.cluster.x-k8s.io_k0sconfigs.yaml: $(CONTROLLER_GEN)

manifests_targets += config/crd/bases/k0smotron.io_clusters.yaml
manifests_targets += config/crd/bases/k0smotron.io_jointokenrequests.yaml
manifests_targets += config/crd/bases/bootstrap.cluster.x-k8s.io_k0sconfigs.yaml
manifests_targets += config/crd/bases/bootstrap.cluster.x-k8s.io_k0sworkerconfigs.yaml
manifests_targets += config/crd/bases/bootstrap.cluster.x-k8s.io_k0sworkerconfigtemplates.yaml
manifests_targets += config/crd/bases/controlplane.cluster.x-k8s.io_k0scontrollerconfigs.yaml
manifests_targets += config/crd/bases/controlplane.cluster.x-k8s.io_k0scontrolplanes.yaml
manifests_targets += config/crd/bases/controlplane.cluster.x-k8s.io_k0smotroncontrolplanes.yaml
manifests_targets += config/crd/bases/infrastructure.cluster.x-k8s.io_remoteclusters.yaml
manifests_targets += config/crd/bases/infrastructure.cluster.x-k8s.io_remotemachines.yaml
config/crd/bases/k0smotron.io_clusters.yaml: $(CONTROLLER_GEN) api/k0smotron.io/v1beta1/k0smotroncluster_types.go
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="./..." output:crd:artifacts:config=config/crd/bases

Expand All @@ -78,7 +83,7 @@ generate_targets += api/controlplane/v1beta1/zz_generated.deepcopy.go
$(generate_targets): $(CONTROLLER_GEN)
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

generate: $(generate_targets) ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
generate: $(generate_targets) clusterapi-manifests ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.


GO_PKGS=$(shell go list ./...)
Expand Down Expand Up @@ -166,6 +171,26 @@ release: manifests kustomize ## Deploy controller to the K8s cluster specified i
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default > install.yaml
git checkout config/manager/kustomization.yaml

clusterapi-manifests:
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="./api/bootstrap/..." output:crd:artifacts:config=config/clusterapi/bootstrap/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="./api/controlplane/..." output:crd:artifacts:config=config/clusterapi/controlplane/bases
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="./api/infrastructure/..." output:crd:artifacts:config=config/clusterapi/infrastructure/bases

bootstrap-components.yaml: clusterapi-manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/clusterapi/bootstrap/ > bootstrap-components.yaml
git checkout config/manager/kustomization.yaml

control-plane-components.yaml: clusterapi-manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/clusterapi/controlplane/ > control-plane-components.yaml
git checkout config/manager/kustomization.yaml

infrastructure-components.yaml: clusterapi-manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/clusterapi/infrastructure/ > infrastructure-components.yaml
git checkout config/manager/kustomization.yaml
##@ Build Dependencies

kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
Expand Down
145 changes: 87 additions & 58 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,20 @@ import (
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
enabledControllers = map[string]bool{
bootstrapController: true,
controlPlaneController: true,
infrastructureController: true,
}
)

const (
allControllers = "all"
bootstrapController = "bootstrap"
controlPlaneController = "control-plane"
infrastructureController = "infrastructure"
)

func init() {
Expand All @@ -70,17 +82,25 @@ func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var enabledController string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&enabledController, "enable-controller", "", "The controller to enable. Default: all")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()

if enabledController != "" && enabledController != allControllers {
enabledControllers = map[string]bool{
os.Args[1]: true,
}
}

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Expand Down Expand Up @@ -139,62 +159,67 @@ func main() {
}
//+kubebuilder:scaffold:builder

if err = (&bootstrap.Controller{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Bootstrap")
os.Exit(1)
}
if err = (&bootstrap.ControlPlaneController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Bootstrap")
os.Exit(1)
}

if err = (&controlplane.K0smotronController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "K0smotronControlPlane")
os.Exit(1)
}

if err = (&controlplane.K0sController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "K0sController")
os.Exit(1)
}

if err = (&infrastructure.RemoteMachineController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "K0sController")
setupLog.Error(err, "unable to create controller", "controller", "RemoteMachine")
os.Exit(1)
}

if err = (&infrastructure.ClusterController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RemoteCluster")
os.Exit(1)
if isControllerEnabled(bootstrapController) {
if err = (&bootstrap.Controller{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Bootstrap")
os.Exit(1)
}
if err = (&bootstrap.ControlPlaneController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Bootstrap")
os.Exit(1)
}
}

if isControllerEnabled(controlPlaneController) {
if err = (&controlplane.K0smotronController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "K0smotronControlPlane")
os.Exit(1)
}

if err = (&controlplane.K0sController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "K0sController")
os.Exit(1)
}
}

if isControllerEnabled(infrastructureController) {
if err = (&infrastructure.RemoteMachineController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
ClientSet: clientSet,
RESTConfig: restConfig,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RemoteMachine")
os.Exit(1)
}

if err = (&infrastructure.ClusterController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "RemoteCluster")
os.Exit(1)
}
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand All @@ -220,3 +245,7 @@ func loadRestConfig() (*rest.Config, error) {
}
return rest.InClusterConfig()
}

func isControllerEnabled(controllerName string) bool {
return enabledControllers[controllerName]
}
Loading

0 comments on commit 992612e

Please sign in to comment.