Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use embedded object in PVC to mantain metadata #353

Merged
merged 6 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/bin
.bash_history
.vscode
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Changelog
## [v1.1.0-rc.2] - X


Update notes:

Ensure you update the CRD definition since CRD is no longer managed by the operator:
```
kubectl create -f https://raw.githubusercontent.com/spotahome/redis-operator/master/example/redisfailover/basic.yaml
```

## [v1.1.0-rc.1] - 2022-01-12

### Major Changes
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := v1.1.0-rc.1
VERSION := v1.1.0-rc.2

# Name of this service/application
SERVICE_NAME := redis-operator
Expand Down
47 changes: 28 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,32 @@ Redis Operator creates/configures/manages redis-failovers atop Kubernetes.
Redis Operator is meant to be run on Kubernetes 1.19+.
All dependencies have been vendored, so there's no need to any additional download.

### Versions deployed
## Operator deployment on kubernetes

The image versions deployed by the operator can be found on the [defaults file](api/redisfailover/v1/defaults.go).
In order to create Redis failovers inside a Kubernetes cluster, the operator has to be deployed. It can be done with [deployment](example/operator) or with the provided [Helm chart](charts/redisoperator).

## Images
### Using the Helm chart

### Redis Operator
From the root folder of the project, execute the following:

[![Redis Operator Image](https://quay.io/repository/spotahome/redis-operator/status "Redis Operator Image")](https://quay.io/repository/spotahome/redis-operator)
```
helm repo add redis-operator https://spotahome.github.io/redis-operator
helm repo update
helm install redis-operator redis-operator/redis-operator
```

## Operator deployment on kubernetes
#### Update helm chart

In order to create Redis failovers inside a Kubernetes cluster, the operator has to be deployed. It can be done with [deployment](example/operator) or with the provided [Helm chart](charts/redisoperator).
Helm chart only manage the creation of CRD in the first install. In order to update the CRD you will need to apply directly.

### Using a Deployment
```
kubectl apply -f https://raw.githubusercontent.com/spotahome/redis-operator/master/manifests/databases.spotahome.com_redisfailovers.yaml
```

```
helm upgrade redis-operator redis-operator/redis-operator
```
### Using kubectl

To create the operator, you can directly create it with kubectl:

Expand All @@ -34,16 +45,6 @@ kubectl create -f https://raw.githubusercontent.com/spotahome/redis-operator/mas

This will create a deployment named `redisoperator`.

### Using the Helm chart

From the root folder of the project, execute the following:

```
helm repo add redis-operator https://spotahome.github.io/redis-operator
helm repo update
helm install redis-operator redis-operator/redis-operator
```

## Usage

Once the operator is deployed inside a Kubernetes cluster, a new API will be accesible, so you'll be able to create, update and delete redisfailovers.
Expand Down Expand Up @@ -211,13 +212,16 @@ This allows for ease of bootstrapping from an existing `RedisFailover` instance
When `allowSentinels` is provided, the Operator will also create the defined Sentinel resources. These sentinels will be configured to point to the provided
`bootstrapNode` as their monitored master.

### Default versions

The image versions deployed by the operator can be found on the [defaults file](api/redisfailover/v1/defaults.go).
## Cleanup

### Operator and CRD

If you want to delete the operator from your Kubernetes cluster, the operator deployment should be deleted.

Also, the CRD has to be deleted too:
Also, the CRD has to be deleted. Deleting CRD automatically wil delete all redis failover custom resources and their managed resources:

```
kubectl delete crd redisfailovers.databases.spotahome.com
Expand All @@ -231,6 +235,11 @@ Thanks to Kubernetes' `OwnerReference`, all the objects created from a redis-fai
kubectl delete redisfailover <NAME>
```

## Docker Images

### Redis Operator

[![Redis Operator Image](https://quay.io/repository/spotahome/redis-operator/status "Redis Operator Image")](https://quay.io/repository/spotahome/redis-operator)
## Documentation

For the code documentation, you can lookup on the [GoDoc](https://godoc.org/github.com/spotahome/redis-operator).
Expand Down
53 changes: 50 additions & 3 deletions api/redisfailover/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,56 @@ type SentinelExporter struct {

// RedisStorage defines the structure used to store the Redis Data
type RedisStorage struct {
KeepAfterDeletion bool `json:"keepAfterDeletion,omitempty"`
EmptyDir *corev1.EmptyDirVolumeSource `json:"emptyDir,omitempty"`
PersistentVolumeClaim *corev1.PersistentVolumeClaim `json:"persistentVolumeClaim,omitempty"`
KeepAfterDeletion bool `json:"keepAfterDeletion,omitempty"`
EmptyDir *corev1.EmptyDirVolumeSource `json:"emptyDir,omitempty"`
PersistentVolumeClaim *EmbeddedPersistentVolumeClaim `json:"persistentVolumeClaim,omitempty"`
}

// EmbeddedPersistentVolumeClaim is an embedded version of k8s.io/api/core/v1.PersistentVolumeClaim.
// It contains TypeMeta and a reduced ObjectMeta.
type EmbeddedPersistentVolumeClaim struct {
metav1.TypeMeta `json:",inline"`

// EmbeddedMetadata contains metadata relevant to an EmbeddedResource.
EmbeddedObjectMetadata `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

// Spec defines the desired characteristics of a volume requested by a pod author.
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
// +optional
Spec corev1.PersistentVolumeClaimSpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

// Status represents the current information/status of a persistent volume claim.
// Read-only.
// More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims
// +optional
Status corev1.PersistentVolumeClaimStatus `json:"status,omitempty" protobuf:"bytes,3,opt,name=status"`
}

// EmbeddedObjectMetadata contains a subset of the fields included in k8s.io/apimachinery/pkg/apis/meta/v1.ObjectMeta
// Only fields which are relevant to embedded resources are included.
type EmbeddedObjectMetadata struct {
// Name must be unique within a namespace. Is required when creating resources, although
// some resources may allow a client to request the generation of an appropriate name
// automatically. Name is primarily intended for creation idempotence and configuration
// definition.
// Cannot be updated.
// More info: http://kubernetes.io/docs/user-guide/identifiers#names
// +optional
Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"`

// Map of string keys and values that can be used to organize and categorize
// (scope and select) objects. May match selectors of replication controllers
// and services.
// More info: http://kubernetes.io/docs/user-guide/labels
// +optional
Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"`

// Annotations is an unstructured key value map stored with a resource that may be
// set by external tools to store and retrieve arbitrary metadata. They are not
// queryable and should be preserved when modifying objects.
// More info: http://kubernetes.io/docs/user-guide/annotations
// +optional
Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
52 changes: 51 additions & 1 deletion api/redisfailover/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion charts/redisoperator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ appVersion: 1.1.0-rc.1
apiVersion: v1
description: A Helm chart for the Spotahome Redis Operator
name: redis-operator
version: 3.1.3
version: 3.1.4
home: https://github.com/spotahome/redis-operator
keywords:
- "golang"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,9 @@ spec:
keepAfterDeletion:
type: boolean
persistentVolumeClaim:
description: PersistentVolumeClaim is a user's request for
and claim to a persistent volume
description: EmbeddedPersistentVolumeClaim is an embedded
version of k8s.io/api/core/v1.PersistentVolumeClaim. It
contains TypeMeta and a reduced ObjectMeta.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema
Expand All @@ -1370,8 +1371,34 @@ spec:
Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
description: 'Standard object''s metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata'
description: EmbeddedMetadata contains metadata relevant
to an EmbeddedResource.
properties:
annotations:
additionalProperties:
type: string
description: 'Annotations is an unstructured key value
map stored with a resource that may be set by external
tools to store and retrieve arbitrary metadata.
They are not queryable and should be preserved when
modifying objects. More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
labels:
additionalProperties:
type: string
description: 'Map of string keys and values that can
be used to organize and categorize (scope and select)
objects. May match selectors of replication controllers
and services. More info: http://kubernetes.io/docs/user-guide/labels'
type: object
name:
description: 'Name must be unique within a namespace.
Is required when creating resources, although some
resources may allow a client to request the generation
of an appropriate name automatically. Name is primarily
intended for creation idempotence and configuration
definition. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names'
type: string
type: object
spec:
description: 'Spec defines the desired characteristics
Expand Down
35 changes: 31 additions & 4 deletions manifests/databases.spotahome.com_redisfailovers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1354,8 +1354,9 @@ spec:
keepAfterDeletion:
type: boolean
persistentVolumeClaim:
description: PersistentVolumeClaim is a user's request for
and claim to a persistent volume
description: EmbeddedPersistentVolumeClaim is an embedded
version of k8s.io/api/core/v1.PersistentVolumeClaim. It
contains TypeMeta and a reduced ObjectMeta.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema
Expand All @@ -1370,8 +1371,34 @@ spec:
Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
description: 'Standard object''s metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata'
description: EmbeddedMetadata contains metadata relevant
to an EmbeddedResource.
properties:
annotations:
additionalProperties:
type: string
description: 'Annotations is an unstructured key value
map stored with a resource that may be set by external
tools to store and retrieve arbitrary metadata.
They are not queryable and should be preserved when
modifying objects. More info: http://kubernetes.io/docs/user-guide/annotations'
type: object
labels:
additionalProperties:
type: string
description: 'Map of string keys and values that can
be used to organize and categorize (scope and select)
objects. May match selectors of replication controllers
and services. More info: http://kubernetes.io/docs/user-guide/labels'
type: object
name:
description: 'Name must be unique within a namespace.
Is required when creating resources, although some
resources may allow a client to request the generation
of an appropriate name automatically. Name is primarily
intended for creation idempotence and configuration
definition. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names'
type: string
type: object
spec:
description: 'Spec defines the desired characteristics
Expand Down
23 changes: 18 additions & 5 deletions operator/redisfailover/service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"bytes"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/resource"
Expand Down Expand Up @@ -263,9 +262,9 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri
ServiceName: name,
Replicas: &rf.Spec.Redis.Replicas,
UpdateStrategy: appsv1.StatefulSetUpdateStrategy{
Type: v1.OnDeleteStatefulSetStrategyType,
Type: appsv1.OnDeleteStatefulSetStrategyType,
},
PodManagementPolicy: v1.ParallelPodManagement,
PodManagementPolicy: appsv1.ParallelPodManagement,
Selector: &metav1.LabelSelector{
MatchLabels: selectorLabels,
},
Expand Down Expand Up @@ -340,12 +339,26 @@ func generateRedisStatefulSet(rf *redisfailoverv1.RedisFailover, labels map[stri
}

if rf.Spec.Redis.Storage.PersistentVolumeClaim != nil {
pvc := corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "PersistentVolumeClaim",
},
ObjectMeta: metav1.ObjectMeta{
Name: rf.Spec.Redis.Storage.PersistentVolumeClaim.EmbeddedObjectMetadata.Name,
Labels: rf.Spec.Redis.Storage.PersistentVolumeClaim.EmbeddedObjectMetadata.Labels,
Annotations: rf.Spec.Redis.Storage.PersistentVolumeClaim.EmbeddedObjectMetadata.Annotations,
CreationTimestamp: metav1.Time{},
},
Spec: rf.Spec.Redis.Storage.PersistentVolumeClaim.Spec,
Status: rf.Spec.Redis.Storage.PersistentVolumeClaim.Status,
}
if !rf.Spec.Redis.Storage.KeepAfterDeletion {
// Set an owner reference so the persistent volumes are deleted when the RF is
rf.Spec.Redis.Storage.PersistentVolumeClaim.OwnerReferences = ownerRefs
pvc.OwnerReferences = ownerRefs
}
ss.Spec.VolumeClaimTemplates = []corev1.PersistentVolumeClaim{
*rf.Spec.Redis.Storage.PersistentVolumeClaim,
pvc,
}
}

Expand Down
Loading