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

Adding support for podman play volumes #8266

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 12 additions & 1 deletion pkg/domain/entities/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,19 @@ type PlayKubePod struct {
Logs []string
}

// PlayKubeVolume represents a single named volume
type PlayKubeVolume struct {
// Name - Name of the volume created as a result of play kube.
Name string
// Labels - Volume Labels
Labels map[string]string
// MountPoint - Volume MountPoint
MountPoint string
}

// PlayKubeReport contains the results of running play kube.
type PlayKubeReport struct {
// Pods - pods created by play kube.
Pods []PlayKubePod
Pods []PlayKubePod
Volumes []PlayKubeVolume
}
37 changes: 37 additions & 0 deletions pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/containers/buildah/pkg/parse"
Expand Down Expand Up @@ -71,12 +72,48 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, path string, options en
return nil, errors.Wrapf(err, "unable to read YAML %q as Kube Deployment", path)
}
return ic.playKubeDeployment(ctx, &deploymentYAML, options)
case "PersistentVolume":
var volYAML v1.PersistentVolume
if err := yaml.Unmarshal(content, &volYAML); err != nil {
return nil, errors.Wrapf(err, "unable to read YAML %q as Kube PersistentVolume", path)
}
return ic.playKubePersistentVolume(ctx, &volYAML, options)
default:
return nil, errors.Errorf("invalid YAML kind: %q. [Pod|Deployment] are the only supported Kubernetes Kinds", kubeObject.Kind)
}

}

func (ic *ContainerEngine) playKubePersistentVolume(ctx context.Context, deploymentYAML *v1.PersistentVolume, options entities.PlayKubeOptions) (*entities.PlayKubeReport, error) {
var (
report entities.PlayKubeReport
)

volOptions := []libpod.VolumeCreateOption{
libpod.WithVolumeLabels(deploymentYAML.GetLabels()),
libpod.WithVolumeName(deploymentYAML.GetName()),
}

uid, err := strconv.Atoi(string(deploymentYAML.GetUID()))
if err != nil {
return nil, errors.Errorf("Failed to parse UID for volume %s", err)
}
volOptions = append(volOptions, libpod.WithVolumeUID(uid))

vol, err := ic.Libpod.NewVolume(ctx, volOptions...)
if err != nil {
return nil, err
}

report.Volumes = append(report.Volumes, entities.PlayKubeVolume{
Name: vol.Name(),
Labels: vol.Labels(),
MountPoint: vol.MountPoint(),
})

return &report, nil
}

func (ic *ContainerEngine) playKubeDeployment(ctx context.Context, deploymentYAML *v1apps.Deployment, options entities.PlayKubeOptions) (*entities.PlayKubeReport, error) {
var (
deploymentName string
Expand Down
34 changes: 34 additions & 0 deletions test/e2e/play_kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,23 @@ spec:
status: {}
`

var persistentVolumeYamlTemplate = `
apiVersion: v1
kind: PersistentVolume
metadata:
name: {{ .Name }}
labels:
type: {{ .Type }}
spec:
storageClassName: {{ .StorageClass }}
capacity:
storage: {{ .StorageCapacity }}
accessModes:
- {{ .AccessMode }}
hostPath:
path: "{{ .Path }}
`

var deploymentYamlTemplate = `
apiVersion: v1
kind: Deployment
Expand Down Expand Up @@ -316,6 +333,8 @@ func generateKubeYaml(kind string, object interface{}, pathname string) error {
yamlTemplate = podYamlTemplate
case "deployment":
yamlTemplate = deploymentYamlTemplate
case "persistentvolume":
yamlTemplate = persistentVolumeYamlTemplate
default:
return fmt.Errorf("unsupported kubernetes kind")
}
Expand Down Expand Up @@ -844,6 +863,21 @@ var _ = Describe("Podman play kube", func() {
Expect(inspect.OutputToString()).To(ContainSubstring(`[echo hello world]`))
})

It("podman play kube test volume correct command", func() {
vol := getVolume("type", "path")
err := generateKubeYaml("persistentvolume", vol, kubeYaml)
Expect(err).To(BeNil())

kube := podmanTest.Podman([]string{"play", "kube", kubeYaml})
kube.WaitWithDefaultTimeout()
Expect(kube.ExitCode()).To(Equal(0))

inspect := podmanTest.Podman([]string{"volume", "inspect", vol.Name, "--format", "'{{ .Name }}'"})
inspect.WaitWithDefaultTimeout()
Expect(inspect.ExitCode()).To(Equal(0))
Expect(inspect.OutputToString()).To(ContainSubstring(vol.Name))
})

It("podman play kube test restartPolicy", func() {
// podName, set, expect
testSli := [][]string{
Expand Down