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

Fix test after moving Restic.Status part to Repository.Status #398

Closed
2 changes: 1 addition & 1 deletion apis/stash/v1alpha1/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c Repository) CustomResourceDefinition() *apiextensions.CustomResourceDefi
Singular: ResourceNameRepository,
Plural: ResourceTypeRepository,
Kind: ResourceKindRepository,
ShortNames: []string{"rec"},
ShortNames: []string{"repo"},
},
},
}
Expand Down
54 changes: 36 additions & 18 deletions apis/stash/v1alpha1/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,39 @@ spec:
singular: restic
scope: Namespaced
version: v1alpha1
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: recoveries.stash.appscode.com
labels:
app: stash
spec:
group: stash.appscode.com
names:
kind: Recovery
listKind: RecoveryList
plural: recoveries
shortNames:
- rec
singular: recovery
scope: Namespaced
version: v1alpha1
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: recoveries.stash.appscode.com
labels:
app: stash
spec:
group: stash.appscode.com
names:
kind: Recovery
listKind: RecoveryList
plural: recoveries
shortNames:
- rec
singular: recovery
scope: Namespaced
version: v1alpha1
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: repositories.stash.appscode.com
labels:
app: stash
spec:
group: stash.appscode.com
names:
kind: Repository
listKind: RepositoryList
plural: repositories
shortNames:
- repo
singular: repository
scope: Namespaced
version: v1alpha1
16 changes: 6 additions & 10 deletions apis/stash/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const (
type Restic struct {
metav1.TypeMeta `json:",inline,omitempty"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ResticSpec `json:"spec,omitempty"`
Status ResticStatus `json:"status,omitempty"`
Spec ResticSpec `json:"spec,omitempty"`
}

type ResticSpec struct {
Expand All @@ -49,14 +48,6 @@ type ResticSpec struct {
ImagePullSecrets []core.LocalObjectReference `json:"imagePullSecrets,omitempty"`
}

type ResticStatus struct {
FirstBackupTime *metav1.Time `json:"firstBackupTime,omitempty"`
LastBackupTime *metav1.Time `json:"lastBackupTime,omitempty"`
LastSuccessfulBackupTime *metav1.Time `json:"lastSuccessfulBackupTime,omitempty"`
LastBackupDuration string `json:"lastBackupDuration,omitempty"`
BackupCount int64 `json:"backupCount,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

type ResticList struct {
Expand Down Expand Up @@ -243,6 +234,11 @@ type RepositorySpec struct {
}

type RepositoryStatus struct {
FirstBackupTime *metav1.Time `json:"firstBackupTime,omitempty"`
LastBackupTime *metav1.Time `json:"lastBackupTime,omitempty"`
LastSuccessfulBackupTime *metav1.Time `json:"lastSuccessfulBackupTime,omitempty"`
LastBackupDuration string `json:"lastBackupDuration,omitempty"`
BackupCount int64 `json:"backupCount,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
73 changes: 28 additions & 45 deletions apis/stash/v1alpha1/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func (in *Repository) DeepCopyInto(out *Repository) {
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
out.Status = in.Status
in.Status.DeepCopyInto(&out.Status)
return
}

Expand Down Expand Up @@ -400,6 +400,33 @@ func (in *RepositorySpec) DeepCopy() *RepositorySpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *RepositoryStatus) DeepCopyInto(out *RepositoryStatus) {
*out = *in
if in.FirstBackupTime != nil {
in, out := &in.FirstBackupTime, &out.FirstBackupTime
if *in == nil {
*out = nil
} else {
*out = new(meta_v1.Time)
(*in).DeepCopyInto(*out)
}
}
if in.LastBackupTime != nil {
in, out := &in.LastBackupTime, &out.LastBackupTime
if *in == nil {
*out = nil
} else {
*out = new(meta_v1.Time)
(*in).DeepCopyInto(*out)
}
}
if in.LastSuccessfulBackupTime != nil {
in, out := &in.LastSuccessfulBackupTime, &out.LastSuccessfulBackupTime
if *in == nil {
*out = nil
} else {
*out = new(meta_v1.Time)
(*in).DeepCopyInto(*out)
}
}
return
}

Expand Down Expand Up @@ -435,7 +462,6 @@ func (in *Restic) DeepCopyInto(out *Restic) {
out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
in.Status.DeepCopyInto(&out.Status)
return
}

Expand Down Expand Up @@ -537,49 +563,6 @@ func (in *ResticSpec) DeepCopy() *ResticSpec {
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ResticStatus) DeepCopyInto(out *ResticStatus) {
*out = *in
if in.FirstBackupTime != nil {
in, out := &in.FirstBackupTime, &out.FirstBackupTime
if *in == nil {
*out = nil
} else {
*out = new(meta_v1.Time)
(*in).DeepCopyInto(*out)
}
}
if in.LastBackupTime != nil {
in, out := &in.LastBackupTime, &out.LastBackupTime
if *in == nil {
*out = nil
} else {
*out = new(meta_v1.Time)
(*in).DeepCopyInto(*out)
}
}
if in.LastSuccessfulBackupTime != nil {
in, out := &in.LastSuccessfulBackupTime, &out.LastSuccessfulBackupTime
if *in == nil {
*out = nil
} else {
*out = new(meta_v1.Time)
(*in).DeepCopyInto(*out)
}
}
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ResticStatus.
func (in *ResticStatus) DeepCopy() *ResticStatus {
if in == nil {
return nil
}
out := new(ResticStatus)
in.DeepCopyInto(out)
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *RestoreStats) DeepCopyInto(out *RestoreStats) {
*out = *in
Expand Down
2 changes: 1 addition & 1 deletion hack/deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ spec:
- --tls-cert-file=/var/serving-cert/tls.crt
- --tls-private-key-file=/var/serving-cert/tls.key
- --enable-analytics=${STASH_ENABLE_ANALYTICS}
image: ${STASH_DOCKER_REGISTRY}/stash:0.7.0-rc.2
image: ${STASH_DOCKER_REGISTRY}/stash:repository
ports:
- containerPort: 8443
- containerPort: 56790
Expand Down
10 changes: 8 additions & 2 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ func (c *Controller) setup() (*api.Restic, error) {
if err = c.resticCLI.InitRepositoryIfAbsent(); err != nil {
return resource, err
}

if err = c.createRepositoryCrdIfNotExist(resource); err != nil {
return resource, err
}
return resource, nil
}

Expand Down Expand Up @@ -282,7 +284,11 @@ func (c *Controller) runResticBackup(resource *api.Restic) (err error) {
restic_session_duration_seconds)
}
if err == nil {
stash_util.PatchRestic(c.stashClient.StashV1alpha1(), resource, func(in *api.Restic) *api.Restic {
repo, err := c.stashClient.StashV1alpha1().Repositories(resource.Namespace).Get(c.getRepositoryCrdName(resource), metav1.GetOptions{})
if err != nil {
return
}
stash_util.PatchRepository(c.stashClient.StashV1alpha1(), repo, func(in *api.Repository) *api.Repository {
in.Status.BackupCount++
in.Status.LastBackupTime = &startTime
if in.Status.FirstBackupTime == nil {
Expand Down
57 changes: 57 additions & 0 deletions pkg/backup/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package backup

import (
"strings"

"github.com/appscode/go/log"
api "github.com/appscode/stash/apis/stash/v1alpha1"
kerr "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (c *Controller) createRepositoryCrdIfNotExist(restic *api.Restic) error {
repository := &api.Repository{}
repository.Namespace = restic.Namespace
repository.Name = c.getRepositoryCrdName(restic)

_, err := c.stashClient.StashV1alpha1().Repositories(repository.Namespace).Get(repository.Name, metav1.GetOptions{})
if err != nil && kerr.IsNotFound(err) {
repository.Labels = map[string]string{
"restic": restic.Name,
"workload-kind": c.opt.Workload.Kind,
"workload-name": c.opt.Workload.Name,
}

switch c.opt.Workload.Kind {
case api.KindStatefulSet:
repository.Labels = map[string]string{
"pod-name": c.opt.PodName,
}
case api.KindDaemonSet:
repository.Labels = map[string]string{
"node-name": c.opt.NodeName,
}
}

repository.Spec.Backend = restic.Spec.Backend
_, err = c.stashClient.StashV1alpha1().Repositories(repository.Namespace).Create(repository)
if err == nil {
log.Infof("Repository %v created", repository.Name)
}
return err
}
return err
}

func (c *Controller) getRepositoryCrdName(restic *api.Restic) string {
name := ""
switch c.opt.Workload.Kind {
case api.KindDeployment, api.KindReplicaSet, api.KindReplicationController:
name = strings.ToLower(c.opt.Workload.Kind) + "." + c.opt.Workload.Name
case api.KindStatefulSet:
name = strings.ToLower(c.opt.Workload.Kind) + "." + c.opt.PodName
case api.KindDaemonSet:
name = strings.ToLower(c.opt.Workload.Kind) + "." + c.opt.Workload.Name + "." + c.opt.NodeName
}
return name
}
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (c *StashController) ensureCustomResourceDefinitions() error {
crds := []*crd_api.CustomResourceDefinition{
api.Restic{}.CustomResourceDefinition(),
api.Recovery{}.CustomResourceDefinition(),
api.Repository{}.CustomResourceDefinition(),
}
return apiext_util.RegisterCRDs(c.crdClient, crds)
}
Expand Down
Loading