-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cassandra Autoscaler Api (#1308)
Signed-off-by: Sabbir <sabbir@appscode.com>
- Loading branch information
1 parent
5a256e6
commit f622d2d
Showing
16 changed files
with
1,559 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
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 v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"kubedb.dev/apimachinery/apis" | ||
"kubedb.dev/apimachinery/apis/autoscaling" | ||
"kubedb.dev/apimachinery/crds" | ||
|
||
"kmodules.xyz/client-go/apiextensions" | ||
) | ||
|
||
func (r CassandraAutoscaler) CustomResourceDefinition() *apiextensions.CustomResourceDefinition { | ||
return crds.MustCustomResourceDefinition(SchemeGroupVersion.WithResource(ResourcePluralCassandraAutoscaler)) | ||
} | ||
|
||
var _ apis.ResourceInfo = &CassandraAutoscaler{} | ||
|
||
func (r CassandraAutoscaler) ResourceFQN() string { | ||
return fmt.Sprintf("%s.%s", ResourcePluralCassandraAutoscaler, autoscaling.GroupName) | ||
} | ||
|
||
func (r CassandraAutoscaler) ResourceShortCode() string { | ||
return ResourceCodeCassandraAutoscaler | ||
} | ||
|
||
func (r CassandraAutoscaler) ResourceKind() string { | ||
return ResourceKindCassandraAutoscaler | ||
} | ||
|
||
func (r CassandraAutoscaler) ResourceSingular() string { | ||
return ResourceSingularCassandraAutoscaler | ||
} | ||
|
||
func (r CassandraAutoscaler) ResourcePlural() string { | ||
return ResourcePluralCassandraAutoscaler | ||
} | ||
|
||
func (r CassandraAutoscaler) ValidateSpecs() error { | ||
return nil | ||
} | ||
|
||
var _ StatusAccessor = &CassandraAutoscaler{} | ||
|
||
func (r *CassandraAutoscaler) GetStatus() AutoscalerStatus { | ||
return r.Status | ||
} | ||
|
||
func (r *CassandraAutoscaler) SetStatus(s AutoscalerStatus) { | ||
r.Status = s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
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 v1alpha1 | ||
|
||
import ( | ||
opsapi "kubedb.dev/apimachinery/apis/ops/v1alpha1" | ||
|
||
core "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
ResourceCodeCassandraAutoscaler = "casscaler" | ||
ResourceKindCassandraAutoscaler = "CassandraAutoscaler" | ||
ResourceSingularCassandraAutoscaler = "cassandraautoscaler" | ||
ResourcePluralCassandraAutoscaler = "cassandraautoscalers" | ||
) | ||
|
||
// CassandraAutoscaler is the configuration for a cassandra database | ||
// autoscaler, which automatically manages pod resources based on historical and | ||
// real time resource utilization. | ||
|
||
// +genclient | ||
// +k8s:openapi-gen=true | ||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// +kubebuilder:object:root=true | ||
// +kubebuilder:resource:path=cassandraautoscalers,singular=cassandraautoscaler,shortName=casscaler,categories={autoscaler,kubedb,appscode} | ||
// +kubebuilder:subresource:status | ||
type CassandraAutoscaler struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// Standard object metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata | ||
// +optional | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
// Specification of the behavior of the autoscaler. | ||
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status. | ||
Spec CassandraAutoscalerSpec `json:"spec"` | ||
|
||
// Current information about the autoscaler. | ||
// +optional | ||
Status AutoscalerStatus `json:"status,omitempty"` | ||
} | ||
|
||
// CassandraAutoscalerSpec is the specification of the behavior of the autoscaler. | ||
type CassandraAutoscalerSpec struct { | ||
DatabaseRef *core.LocalObjectReference `json:"databaseRef"` | ||
|
||
// This field will be used to control the behaviour of ops-manager | ||
OpsRequestOptions *CassandraOpsRequestOptions `json:"opsRequestOptions,omitempty"` | ||
|
||
Compute *CassandraComputeAutoscalerSpec `json:"compute,omitempty"` | ||
Storage *CassandraStorageAutoscalerSpec `json:"storage,omitempty"` | ||
} | ||
|
||
type CassandraComputeAutoscalerSpec struct { | ||
// +optional | ||
NodeTopology *NodeTopology `json:"nodeTopology,omitempty"` | ||
|
||
Cassandra *ComputeAutoscalerSpec `json:"cassandra,omitempty"` | ||
} | ||
|
||
type CassandraStorageAutoscalerSpec struct { | ||
Cassandra *StorageAutoscalerSpec `json:"cassandra,omitempty"` | ||
} | ||
|
||
type CassandraOpsRequestOptions struct { | ||
// Timeout for each step of the ops request in second. If a step doesn't finish within the specified timeout, the ops request will result in failure. | ||
Timeout *metav1.Duration `json:"timeout,omitempty"` | ||
|
||
// ApplyOption is to control the execution of OpsRequest depending on the database state. | ||
// +kubebuilder:default="IfReady" | ||
Apply opsapi.ApplyOption `json:"apply,omitempty"` | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
// CassandraAutoscalerList is a list of CassandraAutoscaler objects. | ||
type CassandraAutoscalerList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
// metadata is the standard list metadata. | ||
// +optional | ||
metav1.ListMeta `json:"metadata"` | ||
|
||
// items is the list of cassandra database autoscaler objects. | ||
Items []CassandraAutoscaler `json:"items"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Copyright AppsCode Inc. and Contributors | ||
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 v1alpha1 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
olddbapi "kubedb.dev/apimachinery/apis/kubedb/v1alpha2" | ||
opsapi "kubedb.dev/apimachinery/apis/ops/v1alpha1" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// log is for logging in this package. | ||
var casLog = logf.Log.WithName("cassandra-autoscaler") | ||
|
||
var _ webhook.Defaulter = &CassandraAutoscaler{} | ||
|
||
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type | ||
func (r *CassandraAutoscaler) Default() { | ||
casLog.Info("defaulting", "name", r.Name) | ||
r.setDefaults() | ||
} | ||
|
||
func (r *CassandraAutoscaler) setDefaults() { | ||
var db olddbapi.Cassandra | ||
err := DefaultClient.Get(context.TODO(), types.NamespacedName{ | ||
Name: r.Spec.DatabaseRef.Name, | ||
Namespace: r.Namespace, | ||
}, &db) | ||
if err != nil { | ||
_ = fmt.Errorf("can't get Cassandra %s/%s \n", r.Namespace, r.Spec.DatabaseRef.Name) | ||
return | ||
} | ||
|
||
r.setOpsReqOptsDefaults() | ||
|
||
if r.Spec.Storage != nil { | ||
setDefaultStorageValues(r.Spec.Storage.Cassandra) | ||
} | ||
|
||
if r.Spec.Compute != nil { | ||
setDefaultComputeValues(r.Spec.Compute.Cassandra) | ||
} | ||
} | ||
|
||
func (r *CassandraAutoscaler) setOpsReqOptsDefaults() { | ||
if r.Spec.OpsRequestOptions == nil { | ||
r.Spec.OpsRequestOptions = &CassandraOpsRequestOptions{} | ||
} | ||
// Timeout is defaulted to 600s in ops-manager retries.go (to retry 120 times with 5sec pause between each) | ||
// OplogMaxLagSeconds & ObjectsCountDiffPercentage are defaults to 0 | ||
if r.Spec.OpsRequestOptions.Apply == "" { | ||
r.Spec.OpsRequestOptions.Apply = opsapi.ApplyOptionIfReady | ||
} | ||
} | ||
|
||
var _ webhook.Validator = &CassandraAutoscaler{} | ||
|
||
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *CassandraAutoscaler) ValidateCreate() (admission.Warnings, error) { | ||
casLog.Info("validate create", "name", r.Name) | ||
return nil, r.validate() | ||
} | ||
|
||
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type | ||
func (r *CassandraAutoscaler) ValidateUpdate(oldObj runtime.Object) (admission.Warnings, error) { | ||
casLog.Info("validate create", "name", r.Name) | ||
return nil, r.validate() | ||
} | ||
|
||
func (r *CassandraAutoscaler) ValidateDelete() (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func (r *CassandraAutoscaler) validate() error { | ||
if r.Spec.DatabaseRef == nil { | ||
return errors.New("databaseRef can't be empty") | ||
} | ||
var kf olddbapi.Cassandra | ||
err := DefaultClient.Get(context.TODO(), types.NamespacedName{ | ||
Name: r.Spec.DatabaseRef.Name, | ||
Namespace: r.Namespace, | ||
}, &kf) | ||
if err != nil { | ||
_ = fmt.Errorf("can't get Cassandra %s/%s \n", r.Namespace, r.Spec.DatabaseRef.Name) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.