Skip to content

Commit

Permalink
ensure backup sidecar injection & deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
hossainemruz committed Feb 27, 2019
1 parent 905d876 commit 78c98f5
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 94 deletions.
37 changes: 33 additions & 4 deletions api/crds/v1beta1/backupconfiguration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,6 @@ spec:
items:
type: string
type: array
mountPath:
description: Path within the container at which the volume should
be mounted. Must not contain ':'.
type: string
ref:
properties:
apiVersion:
Expand All @@ -360,6 +356,39 @@ spec:
type: string
name:
type: string
volumeMounts:
description: VolumeMounts specifies the volumes to mount inside
stash sidecar/init container Specify the volumes that contains
the target directories
items:
description: VolumeMount describes a mounting of a Volume within
a container.
properties:
mountPath:
description: Path within the container at which the volume
should be mounted. Must not contain ':'.
type: string
mountPropagation:
description: mountPropagation determines how mounts are propagated
from the host to container and the other way around. When
not set, MountPropagationNone is used. This field is beta
in 1.10.
type: string
name:
description: This must match the Name of a Volume.
type: string
readOnly:
description: Mounted read-only if true, read-write otherwise
(false or unspecified). Defaults to false.
type: boolean
subPath:
description: Path within the volume from which the container's
volume should be mounted. Defaults to "" (volume's root).
type: string
required:
- name
- mountPath
type: array
task:
properties:
name:
Expand Down
37 changes: 33 additions & 4 deletions api/crds/v1beta1/restoresession.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,6 @@ spec:
items:
type: string
type: array
mountPath:
description: Path within the container at which the volume should
be mounted. Must not contain ':'.
type: string
ref:
properties:
apiVersion:
Expand All @@ -378,6 +374,39 @@ spec:
type: string
name:
type: string
volumeMounts:
description: VolumeMounts specifies the volumes to mount inside
stash sidecar/init container Specify the volumes that contains
the target directories
items:
description: VolumeMount describes a mounting of a Volume within
a container.
properties:
mountPath:
description: Path within the container at which the volume
should be mounted. Must not contain ':'.
type: string
mountPropagation:
description: mountPropagation determines how mounts are propagated
from the host to container and the other way around. When
not set, MountPropagationNone is used. This field is beta
in 1.10.
type: string
name:
description: This must match the Name of a Volume.
type: string
readOnly:
description: Mounted read-only if true, read-write otherwise
(false or unspecified). Defaults to false.
type: boolean
subPath:
description: Path within the volume from which the container's
volume should be mounted. Defaults to "" (volume's root).
type: string
required:
- name
- mountPath
type: array
task:
properties:
name:
Expand Down
4 changes: 4 additions & 0 deletions apis/stash/v1alpha1/restic_helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package v1alpha1

import (
"hash/fnv"
"strconv"

"github.com/appscode/stash/apis"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
hashutil "k8s.io/kubernetes/pkg/util/hash"
crdutils "kmodules.xyz/client-go/apiextensions/v1beta1"
)

Expand Down
2 changes: 1 addition & 1 deletion apis/stash/v1beta1/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const (
LastAppliedConfiguration = BackupKey + "/last-applied-configuration"
// ResourceVersion will be used to trigger restarts for ReplicaSet and RC pods

KeyBackupTemplate = KeyStash + "/backup-template"
KeyBackupConfigurationTemplate = KeyStash + "/backup-template"
KeyTargetDirectories = KeyStash + "/target-directories"

KeyLastAppliedRestoreSession = KeyStash + "/last-applied-restoresession"
Expand Down
16 changes: 11 additions & 5 deletions apis/stash/v1beta1/openapi_generated.go

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

10 changes: 7 additions & 3 deletions apis/stash/v1beta1/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package v1beta1

import (
core "k8s.io/api/core/v1"
)

// Param declares a value to use for the Param called Name.
type Param struct {
Name string `json:"name"`
Expand All @@ -18,10 +22,10 @@ type Target struct {
// Directories specify the directories to backup
// +optional
Directories []string `json:"directories,omitempty"`
// Path within the container at which the volume should be mounted. Must
// not contain ':'.
// VolumeMounts specifies the volumes to mount inside stash sidecar/init container
// Specify the volumes that contains the target directories
// +optional
MountPath string `json:"mountPath,omitempty"`
VolumeMounts []core.VolumeMount `json:"volumeMounts,omitempty"`
}

type TargetRef struct {
Expand Down
7 changes: 7 additions & 0 deletions apis/stash/v1beta1/zz_generated.deepcopy.go

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

23 changes: 12 additions & 11 deletions pkg/controller/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"strings"

"github.com/appscode/envsubst"
wapi "github.com/appscode/kubernetes-webhook-util/apis/workload/v1"
"github.com/appscode/stash/apis"
v1alpha1_api "github.com/appscode/stash/apis/stash/v1alpha1"
v1beta1_api "github.com/appscode/stash/apis/stash/v1beta1"
v1alpha1_util "github.com/appscode/stash/client/clientset/versioned/typed/stash/v1alpha1/util"
v1beta1_util "github.com/appscode/stash/client/clientset/versioned/typed/stash/v1beta1/util"
"github.com/appscode/stash/pkg/util"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
meta_util "kmodules.xyz/client-go/meta"
wapi "kmodules.xyz/webhook-runtime/apis/workload/v1"
)

const (
Expand All @@ -27,10 +28,10 @@ const (
func (c *StashController) applyBackupAnnotationLogic(w *wapi.Workload) error {

// if workload has backup annotations then ensure Repository and BackupConfiguration
if meta_util.HasKey(w.Annotations, v1beta1_api.KeyBackupTemplate) &&
if meta_util.HasKey(w.Annotations, v1beta1_api.KeyBackupConfigurationTemplate) &&
meta_util.HasKey(w.Annotations, v1beta1_api.KeyTargetDirectories) {
// backup annotations found. so, we have to ensure Repository and BackupConfiguration from BackupTemplate
backupTemplateName, err := meta_util.GetStringValue(w.Annotations, v1beta1_api.KeyBackupTemplate)
// backup annotations found. so, we have to ensure Repository and BackupConfiguration from BackupConfigurationTemplate
backupTemplateName, err := meta_util.GetStringValue(w.Annotations, v1beta1_api.KeyBackupConfigurationTemplate)
if err != nil {
return err
}
Expand All @@ -39,12 +40,12 @@ func (c *StashController) applyBackupAnnotationLogic(w *wapi.Workload) error {
return err
}

backupTemplate, err := c.stashClient.StashV1beta1().BackupTemplates().Get(backupTemplateName, metav1.GetOptions{})
backupTemplate, err := c.stashClient.StashV1beta1().BackupConfigurationTemplates().Get(backupTemplateName, metav1.GetOptions{})
if err != nil {
return err
}

// resolve BackupTemplate's variables
// resolve BackupConfigurationTemplate's variables
inputs := make(map[string]string, 0)
inputs[TargetAPIVersion] = w.APIVersion
inputs[TargetKind] = w.Kind
Expand Down Expand Up @@ -109,7 +110,7 @@ func (c *StashController) applyBackupConfigurationLogic(w *wapi.Workload) (*modi
// this means BackupConfiguration has been newly created/updated.
// in this case, we have to add/update sidecar container accordingly.
if newbc != nil && !util.BackupConfigurationEqual(oldbc, newbc) {
err := c.ensureBackupSidecar(w, oldbc, newbc)
err := c.ensureBackupSidecar(w, newbc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -157,7 +158,7 @@ func (c *StashController) applyResticLogic(w *wapi.Workload) (*modifier, error)
// this means Restic has been newly created/updated.
// in this case, we have to add/update the sidecar container accordingly.
if newRestic != nil && !util.ResticEqual(oldRestic, newRestic) {
modificationType, err := c.ensureWorkloadSidecar(w, oldRestic, newRestic)
modificationType, err := c.ensureWorkloadSidecar(w, newRestic)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -211,12 +212,12 @@ func applyReplacements(in string, values map[string]string) (string, error) {
})
}

func (c *StashController) ensureRepository(backupTemplate *v1beta1_api.BackupTemplate, w *wapi.Workload) error {
func (c *StashController) ensureRepository(backupTemplate *v1beta1_api.BackupConfigurationTemplate, w *wapi.Workload) error {
meta := metav1.ObjectMeta{
Name: getRepositoryName(w),
Namespace: w.Namespace,
}
_, _, err := v1beta1_util.CreateOrPatchRepository(c.stashClient.StashV1beta1(), meta, func(in *v1beta1_api.Repository) *v1beta1_api.Repository {
_, _, err := v1alpha1_util.CreateOrPatchRepository(c.stashClient.StashV1alpha1(), meta, func(in *v1alpha1_api.Repository) *v1alpha1_api.Repository {
in.Spec.Backend = backupTemplate.Spec.Backend
return in
})
Expand All @@ -226,7 +227,7 @@ func (c *StashController) ensureRepository(backupTemplate *v1beta1_api.BackupTem
return nil
}

func (c *StashController) ensureBackupConfiguration(backupTemplate *v1beta1_api.BackupTemplate, w *wapi.Workload, directories []string) error {
func (c *StashController) ensureBackupConfiguration(backupTemplate *v1beta1_api.BackupConfigurationTemplate, w *wapi.Workload, directories []string) error {
meta := metav1.ObjectMeta{
Name: getBackupConfigurationName(w),
Namespace: w.Namespace,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/init_container.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package controller

import (
wapi "github.com/appscode/kubernetes-webhook-util/apis/workload/v1"
wapi "kmodules.xyz/webhook-runtime/apis/workload/v1"
v1beta1_api "github.com/appscode/stash/apis/stash/v1beta1"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/appscode/go/log"
wapi "github.com/appscode/kubernetes-webhook-util/apis/workload/v1"
wapi "kmodules.xyz/webhook-runtime/apis/workload/v1"
api "github.com/appscode/stash/apis/stash/v1alpha1"
v1beta1_api "github.com/appscode/stash/apis/stash/v1beta1"
"github.com/appscode/stash/client/clientset/versioned/scheme"
Expand Down
Loading

0 comments on commit 78c98f5

Please sign in to comment.