Skip to content

Commit

Permalink
Pass context
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <tamal@appscode.com>
  • Loading branch information
tamalsaha committed May 22, 2020
1 parent 2105360 commit 11c9e42
Show file tree
Hide file tree
Showing 151 changed files with 1,651 additions and 1,873 deletions.
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ require (
k8s.io/client-go v12.0.0+incompatible
k8s.io/kube-aggregator v0.18.3
k8s.io/kubernetes v1.18.3
kmodules.xyz/client-go v0.0.0-20200521181834-92024b192ae7
kmodules.xyz/client-go v0.0.0-20200522120609-c6430d66212f
kmodules.xyz/constants v0.0.0-20200506032633-a21e58ceec72
kmodules.xyz/custom-resources v0.0.0-20200521070540-2221c4957ef6
kmodules.xyz/objectstore-api v0.0.0-20200521103120-92080446e04d
kmodules.xyz/offshoot-api v0.0.0-20200521035628-e135bf07b226
kmodules.xyz/openshift v0.0.0-20200521065736-da9980848f70
kmodules.xyz/openshift v0.0.0-20200522123204-ce4abf5433c8
kmodules.xyz/prober v0.0.0-20200521101241-adf06150535c
kmodules.xyz/webhook-runtime v0.0.0-20200521051441-6c051c487814
stash.appscode.dev/apimachinery v0.9.0-rc.6.0.20200522112633-5f8cf3a607d8
kmodules.xyz/webhook-runtime v0.0.0-20200522123600-ca70a7e28ed0
stash.appscode.dev/apimachinery v0.9.0-rc.6.0.20200522135619-e81205a3590e
)

replace (
Expand All @@ -78,6 +78,8 @@ replace (
github.com/Azure/go-autorest/logger => github.com/Azure/go-autorest/logger v0.1.0
github.com/Azure/go-autorest/tracing => github.com/Azure/go-autorest/tracing v0.5.0
github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.0.0
go.etcd.io/etcd => go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738
google.golang.org/grpc => google.golang.org/grpc v1.26.0
k8s.io/apimachinery => github.com/kmodules/apimachinery v0.19.0-alpha.0.0.20200520235721-10b58e57a423
k8s.io/apiserver => github.com/kmodules/apiserver v0.18.4-0.20200521000930-14c5f6df9625
k8s.io/client-go => k8s.io/client-go v0.18.3
Expand Down
84 changes: 8 additions & 76 deletions go.sum

Large diffs are not rendered by default.

98 changes: 58 additions & 40 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func (c *Controller) setup() (*api.Restic, *api.Repository, error) {
}

// check restic
restic, err := c.stashClient.StashV1alpha1().Restics(c.opt.Namespace).Get(c.opt.ResticName, metav1.GetOptions{})
restic, err := c.stashClient.StashV1alpha1().Restics(c.opt.Namespace).Get(context.TODO(), c.opt.ResticName, metav1.GetOptions{})
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -330,15 +330,21 @@ func (c *Controller) runResticBackup(restic *api.Restic, repository *api.Reposit
}
}
if err == nil {
_, err2 := stash_util.UpdateRepositoryStatus(c.stashClient.StashV1alpha1(), repository.ObjectMeta, func(in *api.RepositoryStatus) *api.RepositoryStatus {
in.BackupCount++
in.LastBackupTime = &startTime
if in.FirstBackupTime == nil {
in.FirstBackupTime = &startTime
}
in.LastBackupDuration = endTime.Sub(startTime.Time).String()
return in
})
_, err2 := stash_util.UpdateRepositoryStatus(
context.TODO(),
c.stashClient.StashV1alpha1(),
repository.ObjectMeta,
func(in *api.RepositoryStatus) *api.RepositoryStatus {
in.BackupCount++
in.LastBackupTime = &startTime
if in.FirstBackupTime == nil {
in.FirstBackupTime = &startTime
}
in.LastBackupDuration = endTime.Sub(startTime.Time).String()
return in
},
metav1.UpdateOptions{},
)
if err2 != nil {
log.Errorln(err2)
}
Expand Down Expand Up @@ -421,41 +427,53 @@ func (c *Controller) ensureCheckRBAC(namespace string, owner *metav1.OwnerRefere
Name: owner.Name,
Namespace: namespace,
}
_, _, err := core_util.CreateOrPatchServiceAccount(c.k8sClient, meta, func(in *core.ServiceAccount) *core.ServiceAccount {
core_util.EnsureOwnerReference(&in.ObjectMeta, owner)

if in.Labels == nil {
in.Labels = map[string]string{}
}
in.Labels[apis.LabelApp] = apis.AppLabelStash
return in
})
_, _, err := core_util.CreateOrPatchServiceAccount(
context.TODO(),
c.k8sClient,
meta,
func(in *core.ServiceAccount) *core.ServiceAccount {
core_util.EnsureOwnerReference(&in.ObjectMeta, owner)

if in.Labels == nil {
in.Labels = map[string]string{}
}
in.Labels[apis.LabelApp] = apis.AppLabelStash
return in
},
metav1.PatchOptions{},
)
if err != nil {
return err
}

// ensure role binding
_, _, err = rbac_util.CreateOrPatchRoleBinding(c.k8sClient, meta, func(in *rbac.RoleBinding) *rbac.RoleBinding {
core_util.EnsureOwnerReference(&in.ObjectMeta, owner)

if in.Labels == nil {
in.Labels = map[string]string{}
}
in.Labels[apis.LabelApp] = apis.AppLabelStash
_, _, err = rbac_util.CreateOrPatchRoleBinding(
context.TODO(),
c.k8sClient,
meta,
func(in *rbac.RoleBinding) *rbac.RoleBinding {
core_util.EnsureOwnerReference(&in.ObjectMeta, owner)

if in.Labels == nil {
in.Labels = map[string]string{}
}
in.Labels[apis.LabelApp] = apis.AppLabelStash

in.RoleRef = rbac.RoleRef{
APIGroup: rbac.GroupName,
Kind: apis.KindClusterRole,
Name: apis.StashSidecarClusterRole,
}
in.Subjects = []rbac.Subject{
{
Kind: rbac.ServiceAccountKind,
Name: meta.Name,
Namespace: meta.Namespace,
},
}
return in
})
in.RoleRef = rbac.RoleRef{
APIGroup: rbac.GroupName,
Kind: apis.KindClusterRole,
Name: apis.StashSidecarClusterRole,
}
in.Subjects = []rbac.Subject{
{
Kind: rbac.ServiceAccountKind,
Name: meta.Name,
Namespace: meta.Namespace,
},
}
return in
},
metav1.PatchOptions{},
)
return err
}
2 changes: 1 addition & 1 deletion pkg/backup/backupsession.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (c *BackupSessionController) backup(invoker apis.Invoker, targetInfo apis.T
}

// get repository
repository, err := c.StashClient.StashV1alpha1().Repositories(c.Namespace).Get(invoker.Repository, metav1.GetOptions{})
repository, err := c.StashClient.StashV1alpha1().Repositories(c.Namespace).Get(context.TODO(), invoker.Repository, metav1.GetOptions{})
if err != nil {
return nil, err
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/backup/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ limitations under the License.
package backup

import (
"context"

"stash.appscode.dev/apimachinery/apis"
api "stash.appscode.dev/apimachinery/apis/stash/v1alpha1"
"stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1alpha1/util"

"github.com/appscode/go/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (c *Controller) createRepositoryCrdIfNotExist(restic *api.Restic, prefix string) (*api.Repository, error) {
Expand Down Expand Up @@ -57,10 +60,16 @@ func (c *Controller) createRepositoryCrdIfNotExist(restic *api.Restic, prefix st
repository.Spec.Backend.Swift.Prefix = prefix
}

repo, _, err := util.CreateOrPatchRepository(c.stashClient.StashV1alpha1(), repository.ObjectMeta, func(in *api.Repository) *api.Repository {
in.Spec = repository.Spec
return in
})
repo, _, err := util.CreateOrPatchRepository(
context.TODO(),
c.stashClient.StashV1alpha1(),
repository.ObjectMeta,
func(in *api.Repository) *api.Repository {
in.Spec = repository.Spec
return in
},
metav1.PatchOptions{},
)
if err == nil {
log.Infof("Repository %v created", repository.Name)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/backup/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (c *Controller) runOnceForScheduler() error {
func (c *Controller) checkOnceForScheduler() (err error) {

var repository *api.Repository
repository, err = c.stashClient.StashV1alpha1().Repositories(c.opt.Namespace).Get(c.opt.Workload.GetRepositoryCRDName(c.opt.PodName, c.opt.NodeName), metav1.GetOptions{})
repository, err = c.stashClient.StashV1alpha1().Repositories(c.opt.Namespace).Get(context.TODO(), c.opt.Workload.GetRepositoryCRDName(c.opt.PodName, c.opt.NodeName), metav1.GetOptions{})
if kerr.IsNotFound(err) {
err = nil
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func New(k8sClient kubernetes.Interface, stashClient cs.StashV1alpha1Interface,
}

func (c *Controller) Run() (err error) {
restic, err := c.stashClient.Restics(c.opt.Namespace).Get(c.opt.ResticName, metav1.GetOptions{})
restic, err := c.stashClient.Restics(c.opt.Namespace).Get(context.TODO(), c.opt.ResticName, metav1.GetOptions{})
if err != nil {
return
}
Expand Down
35 changes: 21 additions & 14 deletions pkg/cmds/create_backupsession.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmds

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -103,21 +104,27 @@ func (opt *options) createBackupSession() error {
}

// create BackupSession
_, _, err = v1beta1_util.CreateOrPatchBackupSession(opt.stashClient.StashV1beta1(), bsMeta, func(in *api_v1beta1.BackupSession) *api_v1beta1.BackupSession {
// Set BackupConfiguration as BackupSession Owner
core_util.EnsureOwnerReference(&in.ObjectMeta, invoker.OwnerRef)
in.Spec.Invoker = api_v1beta1.BackupInvokerRef{
APIGroup: api_v1beta1.SchemeGroupVersion.Group,
Kind: opt.invokerType,
Name: opt.invokerName,
}
_, _, err = v1beta1_util.CreateOrPatchBackupSession(
context.TODO(),
opt.stashClient.StashV1beta1(),
bsMeta,
func(in *api_v1beta1.BackupSession) *api_v1beta1.BackupSession {
// Set BackupConfiguration as BackupSession Owner
core_util.EnsureOwnerReference(&in.ObjectMeta, invoker.OwnerRef)
in.Spec.Invoker = api_v1beta1.BackupInvokerRef{
APIGroup: api_v1beta1.SchemeGroupVersion.Group,
Kind: opt.invokerType,
Name: opt.invokerName,
}

in.Labels = invoker.Labels
// Add invoker name and kind as a labels so that BackupSession controller inside sidecar can discover this BackupSession
in.Labels[apis.LabelInvokerName] = opt.invokerName
in.Labels[apis.LabelInvokerType] = opt.invokerType
in.Labels = invoker.Labels
// Add invoker name and kind as a labels so that BackupSession controller inside sidecar can discover this BackupSession
in.Labels[apis.LabelInvokerName] = opt.invokerName
in.Labels[apis.LabelInvokerType] = opt.invokerType

return in
})
return in
},
metav1.PatchOptions{},
)
return err
}
4 changes: 2 additions & 2 deletions pkg/cmds/create_volumesnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewCmdCreateVolumeSnapshot() *cobra.Command {
opt.snapshotClient = vs_cs.NewForConfigOrDie(config)

// get backup session
backupSession, err := opt.stashClient.StashV1beta1().BackupSessions(opt.namespace).Get(opt.backupsession, metav1.GetOptions{})
backupSession, err := opt.stashClient.StashV1beta1().BackupSessions(opt.namespace).Get(context.TODO(), opt.backupsession, metav1.GetOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func (opt *VSoption) createVolumeSnapshot(bsMeta metav1.ObjectMeta, invoker apis
// use timestamp suffix of BackupSession name as suffix of the VolumeSnapshots name
parts := strings.Split(bsMeta.Name, "-")
volumeSnapshot := opt.getVolumeSnapshotDefinition(targetInfo.Target, invoker.ObjectMeta.Namespace, pvcName, parts[len(parts)-1])
snapshot, err := opt.snapshotClient.SnapshotV1beta1().VolumeSnapshots(opt.namespace).Create(&volumeSnapshot)
snapshot, err := opt.snapshotClient.SnapshotV1beta1().VolumeSnapshots(opt.namespace).Create(context.TODO(), &volumeSnapshot, metav1.CreateOptions{})
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmds/forget.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmds

import (
"context"
"fmt"

cs "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1alpha1"
Expand Down Expand Up @@ -50,7 +51,7 @@ func NewCmdForget() *cobra.Command {
if repositoryName == "" {
return fmt.Errorf("repository name not found")
}
repo, err := stashClient.Repositories(meta.Namespace()).Get(repositoryName, metav1.GetOptions{})
repo, err := stashClient.Repositories(meta.Namespace()).Get(context.TODO(), repositoryName, metav1.GetOptions{})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmds/restore_volumesnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (opt *VSoption) restoreVolumeSnapshot() (*restic.RestoreOutput, error) {
// start clock to measure the time takes to restore the volumes
startTime := time.Now()

restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(opt.restoresession, metav1.GetOptions{})
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoresession, metav1.GetOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (opt *VSoption) restoreVolumeSnapshot() (*restic.RestoreOutput, error) {
for i := range pvcList {
// verify that the respective VolumeSnapshot exist
if pvcList[i].Spec.DataSource != nil {
_, err = opt.snapshotClient.SnapshotV1beta1().VolumeSnapshots(opt.namespace).Get(pvcList[i].Spec.DataSource.Name, metav1.GetOptions{})
_, err = opt.snapshotClient.SnapshotV1beta1().VolumeSnapshots(opt.namespace).Get(context.TODO(), pvcList[i].Spec.DataSource.Name, metav1.GetOptions{})
if err != nil {
if kerr.IsNotFound(err) { // respective VolumeSnapshot does not exist
restoreOutput.HostRestoreStats = append(restoreOutput.HostRestoreStats, api_v1beta1.HostRestoreStats{
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmds/run_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (opt *hookOptions) executeHook() error {
}
} else if opt.restoreSessionName != "" {
// For restore hooks, RestoreSession name will be provided. We will read the hooks from the RestoreSession.
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(opt.restoreSessionName, metav1.GetOptions{})
restoreSession, err := opt.stashClient.StashV1beta1().RestoreSessions(opt.namespace).Get(context.TODO(), opt.restoreSessionName, metav1.GetOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (opt *hookOptions) getHookExecutorPodName(targetRef v1beta1.TargetRef) (str

func (opt *hookOptions) getAppPodName(appbindingName string) (string, error) {
// get the AppBinding
appbinding, err := opt.appClient.AppcatalogV1alpha1().AppBindings(opt.namespace).Get(appbindingName, metav1.GetOptions{})
appbinding, err := opt.appClient.AppcatalogV1alpha1().AppBindings(opt.namespace).Get(context.TODO(), appbindingName, metav1.GetOptions{})
if err != nil {
return "", err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmds/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmds

import (
"context"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -51,7 +52,7 @@ func NewCmdSnapshots() *cobra.Command {
if repositoryName == "" {
return fmt.Errorf("repository name not found")
}
repo, err := stashClient.Repositories(meta.Namespace()).Get(repositoryName, metav1.GetOptions{})
repo, err := stashClient.Repositories(meta.Namespace()).Get(context.TODO(), repositoryName, metav1.GetOptions{})
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 11c9e42

Please sign in to comment.