From 77653ec93d2beef5594db6ee40326674933517f1 Mon Sep 17 00:00:00 2001 From: Xieql Date: Sat, 19 Nov 2022 11:38:12 +0800 Subject: [PATCH 1/3] add-controller-scaffold Signed-off-by: Xieql --- Makefile | 2 +- .../customresource/customresource.go | 47 ++++++++++++ cmd/cluster-operator/main.go | 5 ++ cmd/cluster-operator/scheme/customresource.go | 25 +++++++ go.mod | 3 +- go.sum | 1 + pkg/controllers/customcluster_controller.go | 62 ++++++++++++++++ pkg/controllers/custommachine_controller.go | 62 ++++++++++++++++ pkg/controllers/suite_test.go | 74 +++++++++++++++++++ pkg/moreos/proc_windows.go | 4 +- 10 files changed, 281 insertions(+), 4 deletions(-) create mode 100644 cmd/cluster-operator/customresource/customresource.go create mode 100644 cmd/cluster-operator/scheme/customresource.go create mode 100644 pkg/controllers/customcluster_controller.go create mode 100644 pkg/controllers/custommachine_controller.go create mode 100644 pkg/controllers/suite_test.go diff --git a/Makefile b/Makefile index f2200a4b3..07335dc22 100644 --- a/Makefile +++ b/Makefile @@ -103,7 +103,7 @@ gen-prom-thanos: init-gen gen-thanos: init-gen hack/gen-thanos.sh -.PHONY: gen-thanos +.PHONY: sync-crds sync-crds: hack/sync-crds.sh diff --git a/cmd/cluster-operator/customresource/customresource.go b/cmd/cluster-operator/customresource/customresource.go new file mode 100644 index 000000000..a7dbf0cdd --- /dev/null +++ b/cmd/cluster-operator/customresource/customresource.go @@ -0,0 +1,47 @@ +/* +Copyright Kurator Authors. + +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 customresource + +import ( + "context" + + ctrl "sigs.k8s.io/controller-runtime" + + "kurator.dev/kurator/pkg/controllers" +) + +var log = ctrl.Log.WithName("custom_resource") + +func InitControllers(ctx context.Context, mgr ctrl.Manager) error { + if err := (&controllers.CustomClusterReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + log.Error(err, "unable to create controller", "controller", "CustomCluster") + return err + } + + if err := (&controllers.CustomMachineReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + log.Error(err, "unable to create controller", "controller", "CustomMachine") + return err + } + + return nil +} diff --git a/cmd/cluster-operator/main.go b/cmd/cluster-operator/main.go index 0b63f1633..2946c44d0 100644 --- a/cmd/cluster-operator/main.go +++ b/cmd/cluster-operator/main.go @@ -38,6 +38,7 @@ import ( "kurator.dev/kurator/cmd/cluster-operator/aws" "kurator.dev/kurator/cmd/cluster-operator/config" + "kurator.dev/kurator/cmd/cluster-operator/customresource" "kurator.dev/kurator/cmd/cluster-operator/scheme" "kurator.dev/kurator/pkg/version" ) @@ -128,6 +129,10 @@ func run(ctx context.Context, opts *config.Options) error { return err } + if err = customresource.InitControllers(ctx, mgr); err != nil { + return err + } + if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil { log.Error(err, "unable to create ready check") return err diff --git a/cmd/cluster-operator/scheme/customresource.go b/cmd/cluster-operator/scheme/customresource.go new file mode 100644 index 000000000..ea6223b4a --- /dev/null +++ b/cmd/cluster-operator/scheme/customresource.go @@ -0,0 +1,25 @@ +/* +Copyright Kurator Authors. + +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 scheme + +import ( + clusterv1alpha1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1" +) + +func init() { + _ = clusterv1alpha1.AddToScheme(Scheme) +} diff --git a/go.mod b/go.mod index 119ad73cd..836a55704 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,8 @@ require ( github.com/hashicorp/go-multierror v1.1.1 github.com/karmada-io/karmada v1.1.1 github.com/mitchellh/cli v1.1.3 + github.com/onsi/ginkgo/v2 v2.4.0 + github.com/onsi/gomega v1.23.0 github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.56.3 github.com/prometheus-operator/prometheus-operator/pkg/client v0.56.3 github.com/prometheus/common v0.37.0 @@ -157,7 +159,6 @@ require ( github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect - github.com/onsi/gomega v1.23.0 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 4b0c61206..80d678926 100644 --- a/go.sum +++ b/go.sum @@ -1169,6 +1169,7 @@ github.com/onsi/ginkgo v1.16.2/go.mod h1:CObGmKUOKaSC0RjmoAK7tKyn4Azo5P2IWuoMnvw github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vvnwo0= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/ginkgo/v2 v2.4.0 h1:+Ig9nvqgS5OBSACXNk15PLdp0U9XPYROt9CFzVdFGIs= +github.com/onsi/ginkgo/v2 v2.4.0/go.mod h1:iHkDK1fKGcBoEHT5W7YBq4RFWaQulw+caOMkAt4OrFo= github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= diff --git a/pkg/controllers/customcluster_controller.go b/pkg/controllers/customcluster_controller.go new file mode 100644 index 000000000..6c6bec86f --- /dev/null +++ b/pkg/controllers/customcluster_controller.go @@ -0,0 +1,62 @@ +/* +Copyright Kurator Authors. + +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 controllers + +import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + clusterv1alpha1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1" +) + +// CustomClusterReconciler reconciles a CustomCluster object +type CustomClusterReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=kurator.dev.kurator.dev,resources=customclusters,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=kurator.dev.kurator.dev,resources=customclusters/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=kurator.dev.kurator.dev,resources=customclusters/finalizers,verbs=update + +// Reconcile is part of the main kubernetes reconciliation loop which aims to +// move the current state of the cluster closer to the desired state. +// TODO(user): Modify the Reconcile function to compare the state specified by +// the CustomCluster object against the actual cluster state, and then +// perform operations to make the cluster state reflect the state specified by +// the user. +// +// For more details, check Reconcile and its Result here: +// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile +func (r *CustomClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + // TODO(user): your logic here + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *CustomClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&clusterv1alpha1.CustomCluster{}). + Complete(r) +} diff --git a/pkg/controllers/custommachine_controller.go b/pkg/controllers/custommachine_controller.go new file mode 100644 index 000000000..28a53c48b --- /dev/null +++ b/pkg/controllers/custommachine_controller.go @@ -0,0 +1,62 @@ +/* +Copyright Kurator Authors. + +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 controllers + +import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + clusterv1alpha1 "kurator.dev/kurator/pkg/apis/cluster/v1alpha1" +) + +// CustomMachineReconciler reconciles a CustomMachine object +type CustomMachineReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=kurator.dev.kurator.dev,resources=custommachines,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=kurator.dev.kurator.dev,resources=custommachines/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=kurator.dev.kurator.dev,resources=custommachines/finalizers,verbs=update + +// Reconcile is part of the main kubernetes reconciliation loop which aims to +// move the current state of the cluster closer to the desired state. +// TODO(user): Modify the Reconcile function to compare the state specified by +// the CustomMachine object against the actual cluster state, and then +// perform operations to make the cluster state reflect the state specified by +// the user. +// +// For more details, check Reconcile and its Result here: +// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile +func (r *CustomMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + // TODO(user): your logic here + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *CustomMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&clusterv1alpha1.CustomMachine{}). + Complete(r) +} diff --git a/pkg/controllers/suite_test.go b/pkg/controllers/suite_test.go new file mode 100644 index 000000000..e3e6e65f5 --- /dev/null +++ b/pkg/controllers/suite_test.go @@ -0,0 +1,74 @@ +/* +Copyright Kurator Authors. + +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 controllers + +import ( + "path/filepath" + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + logf "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + //+kubebuilder:scaffold:imports +) + +// These tests use Ginkgo (BDD-style Go testing framework). Refer to +// http://onsi.github.io/ginkgo/ to learn more about Ginkgo. + +var cfg *rest.Config +var k8sClient client.Client +var testEnv *envtest.Environment + +func TestAPIs(t *testing.T) { + RegisterFailHandler(Fail) + + RunSpecs(t, "Controller Suite") +} + +var _ = BeforeSuite(func() { + logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + + By("bootstrapping test environment") + testEnv = &envtest.Environment{ + CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, + ErrorIfCRDPathMissing: false, + } + + var err error + // cfg is defined in this file globally. + cfg, err = testEnv.Start() + Expect(err).NotTo(HaveOccurred()) + Expect(cfg).NotTo(BeNil()) + + //+kubebuilder:scaffold:scheme + + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sClient).NotTo(BeNil()) + +}) + +var _ = AfterSuite(func() { + By("tearing down the test environment") + err := testEnv.Stop() + Expect(err).NotTo(HaveOccurred()) +}) diff --git a/pkg/moreos/proc_windows.go b/pkg/moreos/proc_windows.go index cf47cbcf5..c8ee193f3 100644 --- a/pkg/moreos/proc_windows.go +++ b/pkg/moreos/proc_windows.go @@ -1,3 +1,5 @@ +//go:build windows + /* Copyright Kurator Authors. @@ -14,8 +16,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -//go:build windows - package moreos import ( From bddb755cf7945210f46afe7120fca4c1d2af0158 Mon Sep 17 00:00:00 2001 From: Xieql Date: Mon, 21 Nov 2022 10:24:08 +0800 Subject: [PATCH 2/3] fix machine.go struct Signed-off-by: Xieql --- .../base/templates/cluster.kurator.dev_custommachines.yaml | 6 ++++++ pkg/apis/cluster/v1alpha1/machine.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/manifests/charts/base/templates/cluster.kurator.dev_custommachines.yaml b/manifests/charts/base/templates/cluster.kurator.dev_custommachines.yaml index 0782ab771..d5178f92e 100644 --- a/manifests/charts/base/templates/cluster.kurator.dev_custommachines.yaml +++ b/manifests/charts/base/templates/cluster.kurator.dev_custommachines.yaml @@ -55,6 +55,9 @@ spec: description: AdditionalTags is an optional set of tags to add to an instance. type: object + privateIP: + description: 'PrivateIP is the private ip address of the machine:' + type: string providerID: description: ProviderID is the unique identifier as specified by the cloud provider. @@ -130,6 +133,9 @@ spec: description: AdditionalTags is an optional set of tags to add to an instance. type: object + privateIP: + description: 'PrivateIP is the private ip address of the machine:' + type: string providerID: description: ProviderID is the unique identifier as specified by the cloud provider. diff --git a/pkg/apis/cluster/v1alpha1/machine.go b/pkg/apis/cluster/v1alpha1/machine.go index e2feada9f..d1f955695 100644 --- a/pkg/apis/cluster/v1alpha1/machine.go +++ b/pkg/apis/cluster/v1alpha1/machine.go @@ -71,7 +71,7 @@ type Machine struct { HostName string `json:"hostName,omitempty"` // PrivateIP is the private ip address of the machine: // +optional - PrivateIP string `json:"publicIP,omitempty"` + PrivateIP string `json:"privateIP,omitempty"` // PublicIP specifies the public IP. // +optional PublicIP string `json:"publicIP,omitempty"` From 79d55ad341c7a6a30c82815b52291b24dc3b4c51 Mon Sep 17 00:00:00 2001 From: Xieql Date: Mon, 21 Nov 2022 11:47:47 +0800 Subject: [PATCH 3/3] modify some names Signed-off-by: Xieql --- .../customresource.go => customcluster/customcluster.go} | 8 ++++---- cmd/cluster-operator/main.go | 4 ++-- .../scheme/{customresource.go => customcluster.go} | 0 pkg/controllers/customcluster_controller.go | 6 +++--- pkg/controllers/custommachine_controller.go | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) rename cmd/cluster-operator/{customresource/customresource.go => customcluster/customcluster.go} (86%) rename cmd/cluster-operator/scheme/{customresource.go => customcluster.go} (100%) diff --git a/cmd/cluster-operator/customresource/customresource.go b/cmd/cluster-operator/customcluster/customcluster.go similarity index 86% rename from cmd/cluster-operator/customresource/customresource.go rename to cmd/cluster-operator/customcluster/customcluster.go index a7dbf0cdd..787cad50e 100644 --- a/cmd/cluster-operator/customresource/customresource.go +++ b/cmd/cluster-operator/customcluster/customcluster.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package customresource +package customcluster import ( "context" @@ -24,10 +24,10 @@ import ( "kurator.dev/kurator/pkg/controllers" ) -var log = ctrl.Log.WithName("custom_resource") +var log = ctrl.Log.WithName("custom_cluster") func InitControllers(ctx context.Context, mgr ctrl.Manager) error { - if err := (&controllers.CustomClusterReconciler{ + if err := (&controllers.CustomClusterController{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { @@ -35,7 +35,7 @@ func InitControllers(ctx context.Context, mgr ctrl.Manager) error { return err } - if err := (&controllers.CustomMachineReconciler{ + if err := (&controllers.CustomMachineController{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { diff --git a/cmd/cluster-operator/main.go b/cmd/cluster-operator/main.go index 2946c44d0..161eb8646 100644 --- a/cmd/cluster-operator/main.go +++ b/cmd/cluster-operator/main.go @@ -38,7 +38,7 @@ import ( "kurator.dev/kurator/cmd/cluster-operator/aws" "kurator.dev/kurator/cmd/cluster-operator/config" - "kurator.dev/kurator/cmd/cluster-operator/customresource" + "kurator.dev/kurator/cmd/cluster-operator/customcluster" "kurator.dev/kurator/cmd/cluster-operator/scheme" "kurator.dev/kurator/pkg/version" ) @@ -129,7 +129,7 @@ func run(ctx context.Context, opts *config.Options) error { return err } - if err = customresource.InitControllers(ctx, mgr); err != nil { + if err = customcluster.InitControllers(ctx, mgr); err != nil { return err } diff --git a/cmd/cluster-operator/scheme/customresource.go b/cmd/cluster-operator/scheme/customcluster.go similarity index 100% rename from cmd/cluster-operator/scheme/customresource.go rename to cmd/cluster-operator/scheme/customcluster.go diff --git a/pkg/controllers/customcluster_controller.go b/pkg/controllers/customcluster_controller.go index 6c6bec86f..546e4fd9d 100644 --- a/pkg/controllers/customcluster_controller.go +++ b/pkg/controllers/customcluster_controller.go @@ -28,7 +28,7 @@ import ( ) // CustomClusterReconciler reconciles a CustomCluster object -type CustomClusterReconciler struct { +type CustomClusterController struct { client.Client Scheme *runtime.Scheme } @@ -46,7 +46,7 @@ type CustomClusterReconciler struct { // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile -func (r *CustomClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *CustomClusterController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) // TODO(user): your logic here @@ -55,7 +55,7 @@ func (r *CustomClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques } // SetupWithManager sets up the controller with the Manager. -func (r *CustomClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *CustomClusterController) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&clusterv1alpha1.CustomCluster{}). Complete(r) diff --git a/pkg/controllers/custommachine_controller.go b/pkg/controllers/custommachine_controller.go index 28a53c48b..6aec6d498 100644 --- a/pkg/controllers/custommachine_controller.go +++ b/pkg/controllers/custommachine_controller.go @@ -28,7 +28,7 @@ import ( ) // CustomMachineReconciler reconciles a CustomMachine object -type CustomMachineReconciler struct { +type CustomMachineController struct { client.Client Scheme *runtime.Scheme } @@ -46,7 +46,7 @@ type CustomMachineReconciler struct { // // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile -func (r *CustomMachineReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { +func (r *CustomMachineController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { _ = log.FromContext(ctx) // TODO(user): your logic here @@ -55,7 +55,7 @@ func (r *CustomMachineReconciler) Reconcile(ctx context.Context, req ctrl.Reques } // SetupWithManager sets up the controller with the Manager. -func (r *CustomMachineReconciler) SetupWithManager(mgr ctrl.Manager) error { +func (r *CustomMachineController) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&clusterv1alpha1.CustomMachine{}). Complete(r)