Skip to content

Commit

Permalink
Fix download cmd for nfs backend (#187)
Browse files Browse the repository at this point in the history
Signed-off-by: hmsayem <[email protected]>
  • Loading branch information
hmsayem authored Sep 4, 2023
1 parent bb94106 commit 460773d
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 118 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ require (
kmodules.xyz/objectstore-api v0.25.1
kmodules.xyz/offshoot-api v0.25.4
kmodules.xyz/openshift v0.25.0
stash.appscode.dev/apimachinery v0.31.0
stash.appscode.dev/apimachinery v0.31.1
stash.appscode.dev/stash v0.31.0
)

require github.com/onsi/gomega v1.20.1 // indirect

require (
cloud.google.com/go v0.99.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand Down Expand Up @@ -85,6 +83,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/gomega v1.20.1 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=
stash.appscode.dev/apimachinery v0.31.0 h1:AXsuz/4K1ltW6oCVsqOS0EoYksVZTn60LXbK7V4IL90=
stash.appscode.dev/apimachinery v0.31.0/go.mod h1:IDbssRbYLSnMwZAQOGX4Vam+hl43FofP8BuXMLXVaPQ=
stash.appscode.dev/apimachinery v0.31.1 h1:5a4+kSnsNvg+iT+xTyoD7SjzbSdmTrmY+VuQuY/qJpw=
stash.appscode.dev/apimachinery v0.31.1/go.mod h1:IDbssRbYLSnMwZAQOGX4Vam+hl43FofP8BuXMLXVaPQ=
stash.appscode.dev/stash v0.31.0 h1:q5dLevm2mZECXEpyba04JbqyUeVtYTKtfS1QcpRU+II=
stash.appscode.dev/stash v0.31.0/go.mod h1:TfYcLyr+YsTqoKHNbyOgOCPqygjRpf3USMztNc2EfjU=
75 changes: 75 additions & 0 deletions pkg/backend_accessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pkg

import (
"context"
"fmt"

"stash.appscode.dev/apimachinery/apis/stash/v1alpha1"

filepathx "gomodules.xyz/x/filepath"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func getBackendMountingPod(kubeClient kubernetes.Interface, repo *v1alpha1.Repository) (*core.Pod, error) {
vol, mnt := repo.Spec.Backend.Local.ToVolumeAndMount(repo.Name)
var err error
if repo.LocalNetworkVolume() {
mnt.MountPath, err = filepathx.SecureJoin("/", repo.Name, mnt.MountPath, repo.LocalNetworkVolumePath())
if err != nil {
return nil, fmt.Errorf("failed to calculate filepath, reason: %s", err)
}
}
// list all the pods
podList, err := kubeClient.CoreV1().Pods(repo.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
// return the pod that has the vol and mnt
for i := range podList.Items {
if hasVolume(podList.Items[i].Spec.Volumes, vol) {
for _, c := range podList.Items[i].Spec.Containers {
if hasVolumeMount(c.VolumeMounts, mnt) {
return &podList.Items[i], nil
}
}
}
}

return nil, fmt.Errorf("no backend mounting pod found for Repository %v", repo.Name)
}

func hasVolume(volumes []core.Volume, vol core.Volume) bool {
for i := range volumes {
if volumes[i].Name == vol.Name {
return true
}
}
return false
}

func hasVolumeMount(mounts []core.VolumeMount, mnt core.VolumeMount) bool {
for i := range mounts {
if mounts[i].Name == mnt.Name && mounts[i].MountPath == mnt.MountPath {
return true
}
}
return false
}
Loading

0 comments on commit 460773d

Please sign in to comment.