diff --git a/Makefile b/Makefile index d12fb0d9..ac81d4f3 100644 --- a/Makefile +++ b/Makefile @@ -158,6 +158,10 @@ build: generate fmt vet ## Build manager binary. run: manifests generate fmt vet ## Run a controller from your host. go run ./cmd/main.go +.PHONY: debug +debug: manifests build + dlv --listen=:2345 --headless=true --api-version=2 exec ./bin/manager -- --config=./config/manager/controller_manager_config.yaml + .PHONY: docker-build docker-build: ## Build docker image with the manager. docker buildx build --build-arg VERSION=${VERSION} -t ${IMG} . --load diff --git a/PROJECT b/PROJECT index ffb82467..0eab5dc4 100644 --- a/PROJECT +++ b/PROJECT @@ -1,3 +1,7 @@ +# Code generated by tool. DO NOT EDIT. +# This file is used to track the info used to scaffold your project +# and allow the plugins properly work. +# More info: https://book.kubebuilder.io/reference/project-config.html domain: cassandra.datastax.com layout: - go.kubebuilder.io/v4-alpha @@ -29,4 +33,13 @@ resources: kind: CassandraTask path: github.com/k8ssandra/cass-operator/apis/control/v1alpha1 version: v1alpha1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: cassandra.datastax.com + group: cassandra.datastax.com + kind: CassandraDataCenterAutoscaler + path: github.com/k8ssandra/cass-operator/api/cassandra.datastax.com/v1beta1 + version: v1beta1 version: "3" diff --git a/apis/autoscaling/v1beta1/cassandradatacenterautoscaler_types.go b/apis/autoscaling/v1beta1/cassandradatacenterautoscaler_types.go new file mode 100644 index 00000000..b01611d7 --- /dev/null +++ b/apis/autoscaling/v1beta1/cassandradatacenterautoscaler_types.go @@ -0,0 +1,64 @@ +/* +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 v1beta1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! +// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. + +// CassandraDataCenterAutoscalerSpec defines the desired state of CassandraDataCenterAutoscaler +type CassandraDataCenterAutoscalerSpec struct { + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + // Important: Run "make" to regenerate code after modifying this file + CassandraDatacenterRef string `json:"cassandraDatacenterRef,omitempty"` + // Foo is an example field of CassandraDataCenterAutoscaler. Edit cassandradatacenterautoscaler_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// CassandraDataCenterAutoscalerStatus defines the observed state of CassandraDataCenterAutoscaler +type CassandraDataCenterAutoscalerStatus struct { + // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster + // Important: Run "make" to regenerate code after modifying this file +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status + +// CassandraDataCenterAutoscaler is the Schema for the cassandradatacenterautoscalers API +type CassandraDataCenterAutoscaler struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec CassandraDataCenterAutoscalerSpec `json:"spec,omitempty"` + Status CassandraDataCenterAutoscalerStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// CassandraDataCenterAutoscalerList contains a list of CassandraDataCenterAutoscaler +type CassandraDataCenterAutoscalerList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []CassandraDataCenterAutoscaler `json:"items"` +} + +func init() { + SchemeBuilder.Register(&CassandraDataCenterAutoscaler{}, &CassandraDataCenterAutoscalerList{}) +} diff --git a/apis/autoscaling/v1beta1/groupversion_info.go b/apis/autoscaling/v1beta1/groupversion_info.go new file mode 100644 index 00000000..9b6f8286 --- /dev/null +++ b/apis/autoscaling/v1beta1/groupversion_info.go @@ -0,0 +1,36 @@ +/* +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 v1beta1 contains API Schema definitions for the cassandra.datastax.com v1beta1 API group +// +kubebuilder:object:generate=true +// +groupName=cassandra.datastax.com.cassandra.datastax.com +package v1beta1 + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/scheme" +) + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: "cassandra.datastax.com.cassandra.datastax.com", Version: "v1beta1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme +) diff --git a/apis/autoscaling/v1beta1/zz_generated.deepcopy.go b/apis/autoscaling/v1beta1/zz_generated.deepcopy.go new file mode 100644 index 00000000..542c6bc2 --- /dev/null +++ b/apis/autoscaling/v1beta1/zz_generated.deepcopy.go @@ -0,0 +1,115 @@ +//go:build !ignore_autogenerated +// +build !ignore_autogenerated + +/* +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. +*/ + +// Code generated by controller-gen. DO NOT EDIT. + +package v1beta1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CassandraDataCenterAutoscaler) DeepCopyInto(out *CassandraDataCenterAutoscaler) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CassandraDataCenterAutoscaler. +func (in *CassandraDataCenterAutoscaler) DeepCopy() *CassandraDataCenterAutoscaler { + if in == nil { + return nil + } + out := new(CassandraDataCenterAutoscaler) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CassandraDataCenterAutoscaler) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CassandraDataCenterAutoscalerList) DeepCopyInto(out *CassandraDataCenterAutoscalerList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]CassandraDataCenterAutoscaler, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CassandraDataCenterAutoscalerList. +func (in *CassandraDataCenterAutoscalerList) DeepCopy() *CassandraDataCenterAutoscalerList { + if in == nil { + return nil + } + out := new(CassandraDataCenterAutoscalerList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CassandraDataCenterAutoscalerList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CassandraDataCenterAutoscalerSpec) DeepCopyInto(out *CassandraDataCenterAutoscalerSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CassandraDataCenterAutoscalerSpec. +func (in *CassandraDataCenterAutoscalerSpec) DeepCopy() *CassandraDataCenterAutoscalerSpec { + if in == nil { + return nil + } + out := new(CassandraDataCenterAutoscalerSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CassandraDataCenterAutoscalerStatus) DeepCopyInto(out *CassandraDataCenterAutoscalerStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CassandraDataCenterAutoscalerStatus. +func (in *CassandraDataCenterAutoscalerStatus) DeepCopy() *CassandraDataCenterAutoscalerStatus { + if in == nil { + return nil + } + out := new(CassandraDataCenterAutoscalerStatus) + in.DeepCopyInto(out) + return out +} diff --git a/apis/cassandra/v1beta1/zz_generated.deepcopy.go b/apis/cassandra/v1beta1/zz_generated.deepcopy.go index da773243..558f1dfc 100644 --- a/apis/cassandra/v1beta1/zz_generated.deepcopy.go +++ b/apis/cassandra/v1beta1/zz_generated.deepcopy.go @@ -2,7 +2,7 @@ // +build !ignore_autogenerated /* -Copyright 2021. +Copyright 2023. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/apis/config/v1beta1/zz_generated.deepcopy.go b/apis/config/v1beta1/zz_generated.deepcopy.go index 79386c32..8f0d56d8 100644 --- a/apis/config/v1beta1/zz_generated.deepcopy.go +++ b/apis/config/v1beta1/zz_generated.deepcopy.go @@ -2,7 +2,7 @@ // +build !ignore_autogenerated /* -Copyright 2021. +Copyright 2023. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/apis/control/v1alpha1/zz_generated.deepcopy.go b/apis/control/v1alpha1/zz_generated.deepcopy.go index 096ead4c..3a2fa124 100644 --- a/apis/control/v1alpha1/zz_generated.deepcopy.go +++ b/apis/control/v1alpha1/zz_generated.deepcopy.go @@ -2,7 +2,7 @@ // +build !ignore_autogenerated /* -Copyright 2021. +Copyright 2023. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/cmd/main.go b/cmd/main.go index 7bf7f2be..e579566b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,6 +18,8 @@ package main import ( "flag" + cassandradatastaxcomv1beta1 "github.com/k8ssandra/cass-operator/apis/autoscaling/v1beta1" + cassandradatastaxcomcontroller "github.com/k8ssandra/cass-operator/internal/controllers/autoscaling" "os" "strings" @@ -55,6 +57,7 @@ func init() { utilruntime.Must(api.AddToScheme(scheme)) utilruntime.Must(configv1beta1.AddToScheme(scheme)) utilruntime.Must(controlv1alpha1.AddToScheme(scheme)) + utilruntime.Must(cassandradatastaxcomv1beta1.AddToScheme(scheme)) //+kubebuilder:scaffold:scheme } @@ -124,12 +127,14 @@ func main() { os.Exit(1) } - if !operConfig.DisableWebhooks { - if err = (&api.CassandraDatacenter{}).SetupWebhookWithManager(mgr); err != nil { - setupLog.Error(err, "unable to create webhook", "webhook", "CassandraDatacenter") - os.Exit(1) + /* + if !operConfig.DisableWebhooks { + if err = (&api.CassandraDatacenter{}).SetupWebhookWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "CassandraDatacenter") + os.Exit(1) + } } - } + */ if err = (&controlcontrollers.CassandraTaskReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), @@ -137,6 +142,13 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "CassandraTask") os.Exit(1) } + if err = (&cassandradatastaxcomcontroller.CassandraDataCenterAutoscalerReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "CassandraDataCenterAutoscaler") + os.Exit(1) + } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/config/crd/bases/cassandra.datastax.com.cassandra.datastax.com_cassandradatacenterautoscalers.yaml b/config/crd/bases/cassandra.datastax.com.cassandra.datastax.com_cassandradatacenterautoscalers.yaml new file mode 100644 index 00000000..bff28879 --- /dev/null +++ b/config/crd/bases/cassandra.datastax.com.cassandra.datastax.com_cassandradatacenterautoscalers.yaml @@ -0,0 +1,56 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.12.0 + name: cassandradatacenterautoscalers.cassandra.datastax.com.cassandra.datastax.com +spec: + group: cassandra.datastax.com.cassandra.datastax.com + names: + kind: CassandraDataCenterAutoscaler + listKind: CassandraDataCenterAutoscalerList + plural: cassandradatacenterautoscalers + singular: cassandradatacenterautoscaler + scope: Namespaced + versions: + - name: v1beta1 + schema: + openAPIV3Schema: + description: CassandraDataCenterAutoscaler is the Schema for the cassandradatacenterautoscalers + API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: CassandraDataCenterAutoscalerSpec defines the desired state + of CassandraDataCenterAutoscaler + properties: + cassandraDatacenterRef: + description: 'INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + Important: Run "make" to regenerate code after modifying this file' + type: string + foo: + description: Foo is an example field of CassandraDataCenterAutoscaler. + Edit cassandradatacenterautoscaler_types.go to remove/update + type: string + type: object + status: + description: CassandraDataCenterAutoscalerStatus defines the observed + state of CassandraDataCenterAutoscaler + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index 781b36dd..b2d4a430 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -4,6 +4,7 @@ resources: - bases/cassandra.datastax.com_cassandradatacenters.yaml - bases/control.k8ssandra.io_cassandratasks.yaml +- bases/cassandra.datastax.com.cassandra.datastax.com_cassandradatacenterautoscalers.yaml #+kubebuilder:scaffold:crdkustomizeresource patchesJson6902: @@ -33,12 +34,14 @@ patchesStrategicMerge: # patches here are for enabling the conversion webhook for each CRD #- patches/webhook_in_cassandradatacenters.yaml #- patches/webhook_in_cassandrajobs.yaml +#- patches/webhook_in_cassandradatacenterautoscalers.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix. # patches here are for enabling the CA injection for each CRD # - patches/cainjection_in_cassandradatacenters.yaml #- patches/cainjection_in_cassandrajobs.yaml +#- patches/cainjection_in_cassandradatacenterautoscalers.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/config/crd/patches/cainjection_in_cassandra.datastax.com_cassandradatacenterautoscalers.yaml b/config/crd/patches/cainjection_in_cassandra.datastax.com_cassandradatacenterautoscalers.yaml new file mode 100644 index 00000000..4f993607 --- /dev/null +++ b/config/crd/patches/cainjection_in_cassandra.datastax.com_cassandradatacenterautoscalers.yaml @@ -0,0 +1,7 @@ +# The following patch adds a directive for certmanager to inject CA into the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + cert-manager.io/inject-ca-from: CERTIFICATE_NAMESPACE/CERTIFICATE_NAME + name: cassandradatacenterautoscalers.cassandra.datastax.com.cassandra.datastax.com diff --git a/config/crd/patches/webhook_in_cassandra.datastax.com_cassandradatacenterautoscalers.yaml b/config/crd/patches/webhook_in_cassandra.datastax.com_cassandradatacenterautoscalers.yaml new file mode 100644 index 00000000..7940076b --- /dev/null +++ b/config/crd/patches/webhook_in_cassandra.datastax.com_cassandradatacenterautoscalers.yaml @@ -0,0 +1,16 @@ +# The following patch enables a conversion webhook for the CRD +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: cassandradatacenterautoscalers.cassandra.datastax.com.cassandra.datastax.com +spec: + conversion: + strategy: Webhook + webhook: + clientConfig: + service: + namespace: system + name: webhook-service + path: /convert + conversionReviewVersions: + - v1 diff --git a/config/manager/controller_manager_config.yaml b/config/manager/controller_manager_config.yaml index 095232db..59b53775 100644 --- a/config/manager/controller_manager_config.yaml +++ b/config/manager/controller_manager_config.yaml @@ -9,7 +9,7 @@ metrics: webhook: port: 9443 leaderElection: - leaderElect: true + leaderElect: false resourceName: b569adb7.cassandra.datastax.com disableWebhooks: true -imageConfigFile: /configs/image_config.yaml +imageConfigFile: ./config/manager/image_config.yaml diff --git a/config/rbac/cassandra.datastax.com_cassandradatacenterautoscaler_editor_role.yaml b/config/rbac/cassandra.datastax.com_cassandradatacenterautoscaler_editor_role.yaml new file mode 100644 index 00000000..8031d6c3 --- /dev/null +++ b/config/rbac/cassandra.datastax.com_cassandradatacenterautoscaler_editor_role.yaml @@ -0,0 +1,31 @@ +# permissions for end users to edit cassandradatacenterautoscalers. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: cassandradatacenterautoscaler-editor-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: cass-operator + app.kubernetes.io/part-of: cass-operator + app.kubernetes.io/managed-by: kustomize + name: cassandradatacenterautoscaler-editor-role +rules: +- apiGroups: + - cassandra.datastax.com.cassandra.datastax.com + resources: + - cassandradatacenterautoscalers + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - cassandra.datastax.com.cassandra.datastax.com + resources: + - cassandradatacenterautoscalers/status + verbs: + - get diff --git a/config/rbac/cassandra.datastax.com_cassandradatacenterautoscaler_viewer_role.yaml b/config/rbac/cassandra.datastax.com_cassandradatacenterautoscaler_viewer_role.yaml new file mode 100644 index 00000000..f9dd3485 --- /dev/null +++ b/config/rbac/cassandra.datastax.com_cassandradatacenterautoscaler_viewer_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to view cassandradatacenterautoscalers. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: cassandradatacenterautoscaler-viewer-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: cass-operator + app.kubernetes.io/part-of: cass-operator + app.kubernetes.io/managed-by: kustomize + name: cassandradatacenterautoscaler-viewer-role +rules: +- apiGroups: + - cassandra.datastax.com.cassandra.datastax.com + resources: + - cassandradatacenterautoscalers + verbs: + - get + - list + - watch +- apiGroups: + - cassandra.datastax.com.cassandra.datastax.com + resources: + - cassandradatacenterautoscalers/status + verbs: + - get diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 7706c3a5..0f945b46 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -4,6 +4,32 @@ kind: ClusterRole metadata: name: manager-role rules: +- apiGroups: + - cassandra.datastax.com.cassandra.datastax.com + resources: + - cassandradatacenterautoscalers + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - cassandra.datastax.com.cassandra.datastax.com + resources: + - cassandradatacenterautoscalers/finalizers + verbs: + - update +- apiGroups: + - cassandra.datastax.com.cassandra.datastax.com + resources: + - cassandradatacenterautoscalers/status + verbs: + - get + - patch + - update - apiGroups: - "" resources: diff --git a/config/samples/cassandra.datastax.com_v1beta1_cassandradatacenterautoscaler.yaml b/config/samples/cassandra.datastax.com_v1beta1_cassandradatacenterautoscaler.yaml new file mode 100644 index 00000000..b88a4c6a --- /dev/null +++ b/config/samples/cassandra.datastax.com_v1beta1_cassandradatacenterautoscaler.yaml @@ -0,0 +1,14 @@ +apiVersion: cassandra.datastax.com.cassandra.datastax.com/v1beta1 +kind: CassandraDataCenterAutoscaler +metadata: + labels: + app.kubernetes.io/name: cassandradatacenterautoscaler + app.kubernetes.io/instance: cassandradatacenterautoscaler-sample + app.kubernetes.io/part-of: cass-operator + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: cass-operator + name: cassandradatacenterautoscaler-sample +spec: + # TODO(user): Add fields here + foo: lol + cassandraDatacenterRef: cassandra diff --git a/config/samples/kustomization.yaml b/config/samples/kustomization.yaml index 29a69e67..ac8fe736 100644 --- a/config/samples/kustomization.yaml +++ b/config/samples/kustomization.yaml @@ -2,4 +2,5 @@ resources: - example-cassdc-three-nodes-single-rack.yaml - cassandratask-cleanup.yaml +- cassandra.datastax.com_v1beta1_cassandradatacenterautoscaler.yaml #+kubebuilder:scaffold:manifestskustomizesamples diff --git a/go.mod b/go.mod index 540cdad1..00d82763 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( require ( github.com/Jeffail/gabs/v2 v2.7.0 github.com/onsi/ginkgo/v2 v2.9.4 + github.com/prometheus/client_golang v1.15.1 go.uber.org/zap v1.24.0 golang.org/x/mod v0.12.0 k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 @@ -55,7 +56,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_golang v1.15.1 // indirect github.com/prometheus/client_model v0.4.0 // indirect github.com/prometheus/common v0.42.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect diff --git a/internal/controllers/autoscaling/cassandradatacenterautoscaler_controller.go b/internal/controllers/autoscaling/cassandradatacenterautoscaler_controller.go new file mode 100644 index 00000000..13bcdbf9 --- /dev/null +++ b/internal/controllers/autoscaling/cassandradatacenterautoscaler_controller.go @@ -0,0 +1,69 @@ +/* +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 autoscaling + +import ( + "context" + "fmt" + cassandradatastaxcomv1beta1 "github.com/k8ssandra/cass-operator/apis/autoscaling/v1beta1" + + "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" +) + +// CassandraDataCenterAutoscalerReconciler reconciles a CassandraDataCenterAutoscaler object +type CassandraDataCenterAutoscalerReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=cassandra.datastax.com.cassandra.datastax.com,resources=cassandradatacenterautoscalers,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=cassandra.datastax.com.cassandra.datastax.com,resources=cassandradatacenterautoscalers/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=cassandra.datastax.com.cassandra.datastax.com,resources=cassandradatacenterautoscalers/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 CassandraDataCenterAutoscaler 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.14.1/pkg/reconcile +func (r *CassandraDataCenterAutoscalerReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + logger := log.FromContext(ctx) + + logger.Info("Bem vindo ao maravilhoso mundo de autoscaling. Recebi o evento do recurso %s", "resource name", req.NamespacedName) + autoscaler := &cassandradatastaxcomv1beta1.CassandraDataCenterAutoscaler{} + err := r.Get(ctx, req.NamespacedName, autoscaler) + if err != nil { + logger.Error(err, "Could not find autoscaler crd in question", "namespaced name", req.NamespacedName) + return ctrl.Result{}, err + } + // TODO(user): your logic here + logger.Info(fmt.Sprintf("Datacenter Ref = %s", autoscaler.Spec.CassandraDatacenterRef)) + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *CassandraDataCenterAutoscalerReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&cassandradatastaxcomv1beta1.CassandraDataCenterAutoscaler{}). + Complete(r) +} diff --git a/internal/controllers/autoscaling/suite_test.go b/internal/controllers/autoscaling/suite_test.go new file mode 100644 index 00000000..50e46e8a --- /dev/null +++ b/internal/controllers/autoscaling/suite_test.go @@ -0,0 +1,79 @@ +/* +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 autoscaling + +import ( + cassandradatastaxcomv1beta1 "github.com/k8ssandra/cass-operator/apis/autoscaling/v1beta1" + "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: true, + } + + var err error + // cfg is defined in this file globally. + cfg, err = testEnv.Start() + Expect(err).NotTo(HaveOccurred()) + Expect(cfg).NotTo(BeNil()) + + err = cassandradatastaxcomv1beta1.AddToScheme(scheme.Scheme) + Expect(err).NotTo(HaveOccurred()) + + //+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()) +})