Skip to content

Commit

Permalink
Merge pull request #104 from Xieql/add-controller-scaffold
Browse files Browse the repository at this point in the history
add controller scaffold
  • Loading branch information
kurator-bot authored Nov 21, 2022
2 parents e0ae397 + 79d55ad commit 5390bf3
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
47 changes: 47 additions & 0 deletions cmd/cluster-operator/customcluster/customcluster.go
Original file line number Diff line number Diff line change
@@ -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 customcluster

import (
"context"

ctrl "sigs.k8s.io/controller-runtime"

"kurator.dev/kurator/pkg/controllers"
)

var log = ctrl.Log.WithName("custom_cluster")

func InitControllers(ctx context.Context, mgr ctrl.Manager) error {
if err := (&controllers.CustomClusterController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create controller", "controller", "CustomCluster")
return err
}

if err := (&controllers.CustomMachineController{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
log.Error(err, "unable to create controller", "controller", "CustomMachine")
return err
}

return nil
}
5 changes: 5 additions & 0 deletions cmd/cluster-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/customcluster"
"kurator.dev/kurator/cmd/cluster-operator/scheme"
"kurator.dev/kurator/pkg/version"
)
Expand Down Expand Up @@ -128,6 +129,10 @@ func run(ctx context.Context, opts *config.Options) error {
return err
}

if err = customcluster.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
Expand Down
25 changes: 25 additions & 0 deletions cmd/cluster-operator/scheme/customcluster.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/cluster/v1alpha1/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
62 changes: 62 additions & 0 deletions pkg/controllers/customcluster_controller.go
Original file line number Diff line number Diff line change
@@ -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 CustomClusterController 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 *CustomClusterController) 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 *CustomClusterController) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&clusterv1alpha1.CustomCluster{}).
Complete(r)
}
62 changes: 62 additions & 0 deletions pkg/controllers/custommachine_controller.go
Original file line number Diff line number Diff line change
@@ -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 CustomMachineController 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 *CustomMachineController) 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 *CustomMachineController) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&clusterv1alpha1.CustomMachine{}).
Complete(r)
}
74 changes: 74 additions & 0 deletions pkg/controllers/suite_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
4 changes: 2 additions & 2 deletions pkg/moreos/proc_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build windows

/*
Copyright Kurator Authors.
Expand All @@ -14,8 +16,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

//go:build windows

package moreos

import (
Expand Down

0 comments on commit 5390bf3

Please sign in to comment.