Browse Source

add pdb resource

tags/0.0.5
Vladimir Smagin 4 months ago
parent
commit
2083a723fd
8 changed files with 103 additions and 9 deletions
  1. +5
    -6
      Dockerfile
  2. +1
    -1
      VERSION
  3. +1
    -1
      deploy/operator.yaml
  4. +8
    -0
      pkg/apis/blindage/v1alpha1/wordpress_types.go
  5. +16
    -0
      pkg/controller/wordpress/manifests.go
  6. +18
    -0
      pkg/controller/wordpress/reconcile.go
  7. +53
    -0
      pkg/controller/wordpress/wordpress_controller.go
  8. +1
    -1
      version/version.go

+ 5
- 6
Dockerfile View File

@@ -1,7 +1,7 @@
FROM golang:1.13-alpine
FROM golang:1.13

RUN apk update
RUN apk add git mercurial
RUN apt-get update
RUN apt-get install -y git mercurial

WORKDIR /app/operator
COPY . .
@@ -11,8 +11,7 @@ WORKDIR /app/operator/cmd/manager
RUN CGO_ENABLED=0 go build -mod=vendor
RUN go install -mod=vendor

FROM alpine:3.11
FROM ubuntu:18.04

COPY --from=0 /go/bin/manager /usr/local/bin/operator
COPY --from=0 /go/bin/manager /usr/bin/wordpress-operator

ENTRYPOINT ["/usr/local/bin/entrypoint"]

+ 1
- 1
VERSION View File

@@ -1 +1 @@
0.0.4.1
0.0.5

+ 1
- 1
deploy/operator.yaml View File

@@ -15,7 +15,7 @@ spec:
serviceAccountName: wordpress-operator
containers:
- name: wordpress-operator
image: iam21h/wordpress-operator:0.0.4.1
image: iam21h/wordpress-operator:0.0.5
command:
- wordpress-operator
imagePullPolicy: Always


+ 8
- 0
pkg/apis/blindage/v1alpha1/wordpress_types.go View File

@@ -12,6 +12,11 @@ import (
// WordpressSpec defines the desired state of Wordpress
// +k8s:openapi-gen=true

type PDB struct {
MinAvailable *int32 `json:"minAvailable,omitempty"`
MaxUnavailable *int32 `json:"maxUnavailable,omitempty"`
}

type ExternalMysql struct {
Hostname string `json:"hostname"`
Database string `json:"database"`
@@ -64,6 +69,9 @@ type WordpressSpec struct {
// set annotations to service and pod, default is empty
WordpressServiceAnnotations *map[string]string `json:"wordpressServiceAnnotations,omitempty"`
WordpressPodAnnotations *map[string]string `json:"wordpressPodAnnotations,omitempty"`

// Create PodDisruptionBudget
PodDisruptionBudget *PDB `json:"podDisruptionBudget,omitempty"`
}

// WordpressStatus defines the observed state of Wordpress


+ 16
- 0
pkg/controller/wordpress/manifests.go View File

@@ -4,6 +4,7 @@ import (
blindagev1alpha1 "git.blindage.org/21h/wordpress-operator/pkg/apis/blindage/v1alpha1"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/policy/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
@@ -343,3 +344,18 @@ func generateDatabaseStatefulSet(cr *blindagev1alpha1.Wordpress) v1.StatefulSet

return sts
}

// GeneratePdbResource used to create PodDisruptionBudget resources
func GeneratePdbResource(cr *blindagev1alpha1.Wordpress, name string, pdbSpec v1beta1.PodDisruptionBudgetSpec) v1beta1.PodDisruptionBudget {

pdb := v1beta1.PodDisruptionBudget{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: cr.Namespace,
Labels: baseLabels(cr),
},
Spec: pdbSpec,
}
return pdb

}

+ 18
- 0
pkg/controller/wordpress/reconcile.go View File

@@ -5,12 +5,30 @@ import (

v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/api/policy/v1beta1"
)

// check if reconcile required, returns:
// 1) reconcile this resource or not
// 2) result resource to apply

func reconcilePodDisruptionBudget(foundPodDisruptionBudget v1beta1.PodDisruptionBudget, newPodDisruptionBudget v1beta1.PodDisruptionBudget) (bool, v1beta1.PodDisruptionBudget) {

reconcileRequired := false

if !reflect.DeepEqual(foundPodDisruptionBudget.Spec.MaxUnavailable, newPodDisruptionBudget.Spec.MaxUnavailable) {
foundPodDisruptionBudget.Spec.MaxUnavailable = newPodDisruptionBudget.Spec.MaxUnavailable
reconcileRequired = true
}

if !reflect.DeepEqual(foundPodDisruptionBudget.Spec.MinAvailable, newPodDisruptionBudget.Spec.MinAvailable) {
foundPodDisruptionBudget.Spec.MinAvailable = newPodDisruptionBudget.Spec.MinAvailable
reconcileRequired = true
}

return reconcileRequired, foundPodDisruptionBudget
}

func reconcilePersistentVolumeClaim(foundVolume corev1.PersistentVolumeClaim, newVolume corev1.PersistentVolumeClaim) (bool, corev1.PersistentVolumeClaim) {

reconcileRequired := false


+ 53
- 0
pkg/controller/wordpress/wordpress_controller.go View File

@@ -6,12 +6,15 @@ import (

raven "github.com/getsentry/raven-go"
v1 "k8s.io/api/apps/v1"
"k8s.io/api/policy/v1beta1"

blindagev1alpha1 "git.blindage.org/21h/wordpress-operator/pkg/apis/blindage/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -331,5 +334,55 @@ func (r *ReconcileWordpress) Reconcile(request reconcile.Request) (reconcile.Res
}
}

// PodDisruptionBudget
if instance.Spec.PodDisruptionBudget != nil {
pdbSpec := v1beta1.PodDisruptionBudgetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: mergeLabels(baseLabels(instance), map[string]string{"component": "wordpress"}),
},
}

if instance.Spec.PodDisruptionBudget.MaxUnavailable != nil {
pdbSpec.MaxUnavailable = &intstr.IntOrString{Type: intstr.Int, IntVal: *instance.Spec.PodDisruptionBudget.MaxUnavailable}
}
if instance.Spec.PodDisruptionBudget.MinAvailable != nil {
pdbSpec.MinAvailable = &intstr.IntOrString{Type: intstr.Int, IntVal: *instance.Spec.PodDisruptionBudget.MinAvailable}
}

newPodDisruptionBudget := GeneratePdbResource(instance, instance.Name, pdbSpec)

if err := controllerutil.SetControllerReference(instance, &newPodDisruptionBudget, r.scheme); err != nil {
raven.CaptureErrorAndWait(err, nil)
return reconcile.Result{}, err
}

foundPodDisruptionBudget := v1beta1.PodDisruptionBudget{}
err := r.client.Get(context.TODO(), types.NamespacedName{Name: newPodDisruptionBudget.Name, Namespace: newPodDisruptionBudget.Namespace}, &foundPodDisruptionBudget)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating a PodDisruptionBudget", "Namespace", newPodDisruptionBudget.Namespace, "Name", newPodDisruptionBudget.Name)
err = r.client.Create(context.TODO(), &newPodDisruptionBudget)
if err != nil {
reqLogger.Info("Creating PodDisruptionBudget error", "Namespace", newPodDisruptionBudget.Namespace, "Name", newPodDisruptionBudget.Name, "Error", err)
raven.CaptureErrorAndWait(err, nil)
return reconcile.Result{}, err
}
} else if err != nil {
raven.CaptureErrorAndWait(err, nil)
return reconcile.Result{}, err
} else {
if reconcileRequired, reconResource := reconcilePodDisruptionBudget(foundPodDisruptionBudget, newPodDisruptionBudget); reconcileRequired {
reqLogger.Info("Updating PodDisruptionBudget", "Namespace", reconResource.Namespace, "Name", reconResource.Name)
if err = r.client.Update(context.TODO(), &reconResource); err != nil {
reqLogger.Info("Reconcile PodDisruptionBudget error", "Namespace", foundPodDisruptionBudget.Namespace, "Name", foundPodDisruptionBudget.Name, "Error", err)
raven.CaptureErrorAndWait(err, nil)
return reconcile.Result{}, err
}
}
}

reqLogger.Info("Reconcile PodDisruptionBudget complete", "PodDisruptionBudget Name", instance.Name, "Namespace", instance.Namespace)
return reconcile.Result{}, nil
}

return reconcile.Result{}, nil
}

+ 1
- 1
version/version.go View File

@@ -1,5 +1,5 @@
package version

var (
Version = "0.0.4.1"
Version = "0.0.5"
)

Loading…
Cancel
Save