Created
January 24, 2021 13:51
-
-
Save mountkin/854ffb0030ddce68629d87c946fb594c to your computer and use it in GitHub Desktop.
kubebuilder example
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
/* | |
Copyright 2021. | |
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" | |
"fmt" | |
"github.com/go-logr/logr" | |
appv1 "k8s.io/api/apps/v1" | |
corev1 "k8s.io/api/core/v1" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/apimachinery/pkg/runtime" | |
ctrl "sigs.k8s.io/controller-runtime" | |
"sigs.k8s.io/controller-runtime/pkg/client" | |
redisv1 "github.com/mountkin/redis-operator/api/v1" | |
) | |
const ( | |
ownerKey = ".metadata.controller" | |
deploymentNameTpl = "%s-redis-%d" | |
) | |
// ClusterReconciler reconciles a Cluster object | |
type ClusterReconciler struct { | |
client.Client | |
Log logr.Logger | |
Scheme *runtime.Scheme | |
} | |
//+kubebuilder:rbac:groups=redis.example.com,resources=clusters,verbs=get;list;watch;create;update;patch;delete | |
//+kubebuilder:rbac:groups=redis.example.com,resources=clusters/status,verbs=get;update;patch | |
//+kubebuilder:rbac:groups=redis.example.com,resources=clusters/finalizers,verbs=update | |
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete | |
//+kubebuilder:rbac:groups=apps,resources=deployments/status,verbs=get | |
//+kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch | |
//+kubebuilder:rbac:groups="",resources=pods/status,verbs=get | |
// 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 Cluster 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.7.0/pkg/reconcile | |
func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | |
_ = r.Log.WithValues("cluster", req.NamespacedName) | |
cluster := &redisv1.Cluster{} | |
if err := r.Get(ctx, req.NamespacedName, cluster); err != nil { | |
if client.IgnoreNotFound(err) != nil { | |
r.Log.Error(err, "failed to get Cluster resource", "cluster", req.NamespacedName) | |
return ctrl.Result{}, err | |
} | |
return ctrl.Result{}, nil | |
} | |
deployments := &appv1.DeploymentList{} | |
if err := r.List(ctx, deployments, client.InNamespace(req.Namespace), client.MatchingFields{ownerKey: req.Name}); err != nil { | |
r.Log.Error(err, "failed to list deployments", "cluster", req.NamespacedName) | |
return ctrl.Result{Requeue: true}, err | |
} | |
if len(deployments.Items) < cluster.Spec.Replicas { | |
// add missing deployments | |
if err := r.createDeployments(ctx, cluster, deployments); err != nil { | |
return ctrl.Result{Requeue: true}, err | |
} | |
} else if len(deployments.Items) > cluster.Spec.Replicas { | |
// delete the extra deployments | |
} | |
// Update cluster status | |
pods := &corev1.PodList{} | |
err := r.List(ctx, pods, client.InNamespace(req.Namespace), client.MatchingLabels{ | |
"redis-cluster-name": cluster.Name, | |
}) | |
if err != nil || len(pods.Items) == 0 { | |
return ctrl.Result{}, nil | |
} | |
var instanceStatus []redisv1.InstanceStatus | |
for _, pod := range pods.Items { | |
status := redisv1.InstanceStatus{ | |
Role: "master", | |
DeploymentName: pod.GetOwnerReferences()[0].Name, | |
IP: pod.Status.PodIP, | |
} | |
instanceStatus = append(instanceStatus, status) | |
} | |
cluster.Status.Instances = instanceStatus | |
if err := r.Status().Update(ctx, cluster); err != nil { | |
r.Log.Error(err, "failed to update cluster status", "cluster", req.NamespacedName) | |
} | |
r.Log.Info("xxxxxxxx Reconcile called", "cluster", cluster) | |
return ctrl.Result{}, nil | |
} | |
func (r *ClusterReconciler) createDeployments(ctx context.Context, cluster *redisv1.Cluster, deployments *appv1.DeploymentList) error { | |
var replicas int32 = 1 | |
names := r.genDeploymentName(cluster, deployments) | |
ns := cluster.GetNamespace() | |
if ns == "" { | |
ns = metav1.NamespaceDefault | |
} | |
for _, name := range names { | |
deployment := &appv1.Deployment{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: name, | |
Labels: map[string]string{ | |
"operator-name": "redis", | |
"redis-cluster-name": cluster.Name, | |
}, | |
Annotations: map[string]string{}, | |
Namespace: ns, | |
}, | |
Spec: appv1.DeploymentSpec{ | |
Replicas: &replicas, | |
Selector: &metav1.LabelSelector{ | |
MatchLabels: map[string]string{ | |
"redis-instance-name": name, | |
"redis-cluster-name": cluster.Name, | |
}, | |
}, | |
Template: corev1.PodTemplateSpec{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Labels: map[string]string{ | |
"redis-instance-name": name, | |
"redis-cluster-name": cluster.Name, | |
}, | |
}, | |
Spec: corev1.PodSpec{ | |
Containers: []corev1.Container{ | |
{ | |
Name: "redis", | |
Image: cluster.Spec.Image, | |
Ports: []corev1.ContainerPort{ | |
{ | |
Name: "redis", | |
Protocol: corev1.ProtocolTCP, | |
ContainerPort: int32(cluster.Spec.Port), | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
} | |
if err := ctrl.SetControllerReference(cluster, deployment, r.Scheme); err != nil { | |
return err | |
} | |
if err := r.Create(ctx, deployment); err != nil { | |
r.Log.Error(err, "failed to create deployment ", "deployment name", name) | |
return err | |
} | |
} | |
return nil | |
} | |
func (r *ClusterReconciler) genDeploymentName(cluster *redisv1.Cluster, deployments *appv1.DeploymentList) []string { | |
existedNames := make(map[string]bool) | |
newNames := []string{} | |
diff := cluster.Spec.Replicas - len(deployments.Items) | |
for _, d := range deployments.Items { | |
existedNames[d.Name] = true | |
} | |
for i := 1; i <= cluster.Spec.Replicas; i++ { | |
name := fmt.Sprintf(deploymentNameTpl, cluster.Name, i) | |
if !existedNames[name] { | |
newNames = append(newNames, name) | |
if len(newNames) == diff { | |
return newNames | |
} | |
} | |
} | |
return newNames | |
} | |
// SetupWithManager sets up the controller with the Manager. | |
func (r *ClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { | |
err := mgr.GetFieldIndexer().IndexField(context.Background(), &appv1.Deployment{}, ownerKey, func(o client.Object) []string { | |
deploy := o.(*appv1.Deployment) | |
owner := metav1.GetControllerOf(deploy) | |
if owner == nil { | |
return nil | |
} | |
if owner.APIVersion != redisv1.GroupVersion.String() || owner.Kind != "Cluster" { | |
return nil | |
} | |
return []string{owner.Name} | |
}) | |
if err != nil { | |
return err | |
} | |
return ctrl.NewControllerManagedBy(mgr). | |
For(&redisv1.Cluster{}). | |
Owns(&appv1.Deployment{}). | |
Complete(r) | |
} |
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
/* | |
Copyright 2021. | |
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 v1 | |
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. | |
// ClusterSpec defines the desired state of Cluster | |
type ClusterSpec struct { | |
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster | |
// Important: Run "make" to regenerate code after modifying this file | |
// Foo is an example field of Cluster. Edit cluster_types.go to remove/update | |
Image string `json:"image"` | |
Replicas int `json:"replicas"` | |
Port int `json:"port"` | |
} | |
// InstanceStatus holds the status of the redis instance. | |
type InstanceStatus struct { | |
Role string `json:"role,omitempty"` | |
IP string `json:"ip,omitempty"` | |
DeploymentName string `json:"deployment_name,omitempty"` | |
} | |
// ClusterStatus defines the observed state of Cluster | |
type ClusterStatus struct { | |
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster | |
// Important: Run "make" to regenerate code after modifying this file | |
Instances []InstanceStatus `json:"instances,omitempty"` | |
Phase string `json:"phase,omitempty"` | |
} | |
//+kubebuilder:object:root=true | |
//+kubebuilder:subresource:status | |
// +kubebuilder:resource:scope=Cluster | |
// Cluster is the Schema for the clusters API | |
type Cluster struct { | |
metav1.TypeMeta `json:",inline"` | |
metav1.ObjectMeta `json:"metadata,omitempty"` | |
Spec ClusterSpec `json:"spec,omitempty"` | |
Status ClusterStatus `json:"status,omitempty"` | |
} | |
//+kubebuilder:object:root=true | |
// ClusterList contains a list of Cluster | |
type ClusterList struct { | |
metav1.TypeMeta `json:",inline"` | |
metav1.ListMeta `json:"metadata,omitempty"` | |
Items []Cluster `json:"items"` | |
} | |
func init() { | |
SchemeBuilder.Register(&Cluster{}, &ClusterList{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment