Skip to content

Commit

Permalink
*: add pod disruption budget
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyass committed Jul 27, 2021
1 parent 38f3969 commit 377e1ba
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 2 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ type ClusterSpec struct {
// +kubebuilder:default:=3
Replicas *int32 `json:"replicas,omitempty"`

// The number of pods from that set that must still be available after the
// eviction, even in the absence of the evicted pod
// +optional
// +kubebuilder:default:="50%"
MinAvailable string `json:"minAvailable,omitempty"`

// MysqlOpts is the options of MySQL container.
// +optional
// +kubebuilder:default:={rootPassword: "", rootHost: "127.0.0.1", user: "qc_usr", password: "Qing@123", database: "qingcloud", initTokuDB: true, resources: {limits: {cpu: "500m", memory: "1Gi"}, requests: {cpu: "100m", memory: "256Mi"}}}
Expand Down
5 changes: 5 additions & 0 deletions charts/mysql-operator/crds/mysql.radondb.com_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ spec:
type: object
type: object
type: object
minAvailable:
default: 50%
description: The number of pods from that set that must still be available
after the eviction, even in the absence of the evicted pod
type: string
mysqlOpts:
default:
database: qingcloud
Expand Down
12 changes: 12 additions & 0 deletions charts/mysql-operator/templates/cluster_rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ rules:
- get
- patch
- update
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
2 changes: 1 addition & 1 deletion cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func (c *Cluster) EnsureVolumeClaimTemplates(schema *runtime.Scheme) ([]corev1.P
// GetNameForResource returns the name of a resource from above
func (c *Cluster) GetNameForResource(name utils.ResourceName) string {
switch name {
case utils.StatefulSet, utils.ConfigMap, utils.HeadlessSVC:
case utils.StatefulSet, utils.ConfigMap, utils.HeadlessSVC, utils.PodDisruptionBudget:
return fmt.Sprintf("%s-mysql", c.Name)
case utils.LeaderService:
return fmt.Sprintf("%s-leader", c.Name)
Expand Down
49 changes: 49 additions & 0 deletions cluster/syncer/pdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2021 RadonDB.
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 syncer

import (
"github.com/presslabs/controller-util/syncer"
policyv1beta1 "k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/radondb/radondb-mysql-kubernetes/cluster"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)

// NewPDBSyncer returns podDisruptionBudget syncer.
func NewPDBSyncer(cli client.Client, c *cluster.Cluster) syncer.Interface {
pdb := &policyv1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: c.GetNameForResource(utils.PodDisruptionBudget),
Namespace: c.Namespace,
},
}

return syncer.NewObjectSyncer("PDB", c.Unwrap(), pdb, cli, func() error {
if pdb.Spec.MinAvailable != nil {
// this mean that pdb is created and should return because spec is imutable
return nil
}
ma := intstr.FromString(c.Spec.MinAvailable)
pdb.Spec.MinAvailable = &ma
pdb.Spec.Selector = metav1.SetAsLabelSelector(c.GetSelectorLabels())
return nil
})
}
2 changes: 1 addition & 1 deletion cluster/syncer/rolebinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewRoleBindingSyncer(cli client.Client, c *cluster.Cluster) syncer.Interfac
Labels: c.GetLabels(),
},
}
return syncer.NewObjectSyncer("Role", c.Unwrap(), roleBinding, cli, func() error {
return syncer.NewObjectSyncer("RoleBinding", c.Unwrap(), roleBinding, cli, func() error {
roleBinding.RoleRef = rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Expand Down
5 changes: 5 additions & 0 deletions config/crd/bases/mysql.radondb.com_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ spec:
type: object
type: object
type: object
minAvailable:
default: 50%
description: The number of pods from that set that must still be available
after the eviction, even in the absence of the evicted pod
type: string
mysqlOpts:
default:
database: qingcloud
Expand Down
12 changes: 12 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ rules:
- get
- patch
- update
- apiGroups:
- policy
resources:
- poddisruptionbudgets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- rbac.authorization.k8s.io
resources:
Expand Down
4 changes: 4 additions & 0 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/presslabs/controller-util/syncer"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1beta1 "k8s.io/api/policy/v1beta1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -52,6 +53,7 @@ type ClusterReconciler struct {
// +kubebuilder:rbac:groups=core,resources=serviceaccounts,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups=rbac.authorization.k8s.io,resources=roles;rolebindings,verbs=get;list;watch;create;update
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=policy,resources=poddisruptionbudgets,verbs=get;list;watch;create;update;patch;delete

// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
Expand Down Expand Up @@ -113,6 +115,7 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
clustersyncer.NewLeaderSVCSyncer(r.Client, instance),
clustersyncer.NewFollowerSVCSyncer(r.Client, instance),
clustersyncer.NewStatefulSetSyncer(r.Client, instance, cmRev, sctRev),
clustersyncer.NewPDBSyncer(r.Client, instance),
}

// run the syncers
Expand All @@ -136,5 +139,6 @@ func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
Owns(&rbacv1.RoleBinding{}).
Owns(&corev1.ServiceAccount{}).
Owns(&corev1.Secret{}).
Owns(&policyv1beta1.PodDisruptionBudget{}).
Complete(r)
}
2 changes: 2 additions & 0 deletions utils/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ const (
RoleBinding ResourceName = "rolebinding"
// ServiceAccount is the alias of the serviceaccount resource.
ServiceAccount ResourceName = "service-account"
// PodDisruptionBudget is the name of pod disruption budget for the statefulset.
PodDisruptionBudget ResourceName = "pdb"
)

0 comments on commit 377e1ba

Please sign in to comment.