summaryrefslogtreecommitdiff
path: root/cmd/podman/common
diff options
context:
space:
mode:
authorcdoern <cdoern@redhat.com>2021-09-15 15:08:42 -0400
committercdoern <cdoern@redhat.com>2021-09-20 08:33:15 -0400
commitcb077c968de08c98eda023f5eeefc7d9d8e79231 (patch)
tree52da19dff15418a0ea5c1781962bc3daba5f4622 /cmd/podman/common
parent5f41ffdd194a828625b3bb6ec55ed87d2830fe58 (diff)
downloadpodman-cb077c968de08c98eda023f5eeefc7d9d8e79231.tar.gz
podman-cb077c968de08c98eda023f5eeefc7d9d8e79231.tar.bz2
podman-cb077c968de08c98eda023f5eeefc7d9d8e79231.zip
Created MapOptions for PodCreate
MapOptions take the pod and container create options, assigning matching values from infra back to the pod for the Libpod API. This function, unlike the previous one, does not require any manual additions when new options are added since it uses the structs JSON tags, this is a more modular approach. Signed-off-by: cdoern <cdoern@redhat.com>
Diffstat (limited to 'cmd/podman/common')
-rw-r--r--cmd/podman/common/create.go12
-rw-r--r--cmd/podman/common/create_test.go53
2 files changed, 65 insertions, 0 deletions
diff --git a/cmd/podman/common/create.go b/cmd/podman/common/create.go
index 6200592b4..a65e90fab 100644
--- a/cmd/podman/common/create.go
+++ b/cmd/podman/common/create.go
@@ -15,6 +15,18 @@ const sizeWithUnitFormat = "(format: `<number>[<unit>]`, where unit = b (bytes),
var containerConfig = registry.PodmanConfig()
+// ContainerToPodOptions takes the Container and Pod Create options, assigning the matching values back to podCreate for the purpose of the libpod API
+// For this function to succeed, the JSON tags in PodCreateOptions and ContainerCreateOptions need to match due to the Marshaling and Unmarshaling done.
+// The types of the options also need to match or else the unmarshaling will fail even if the tags match
+func ContainerToPodOptions(containerCreate *entities.ContainerCreateOptions, podCreate *entities.PodCreateOptions) error {
+ contMarshal, err := json.Marshal(containerCreate)
+ if err != nil {
+ return err
+ }
+ return json.Unmarshal(contMarshal, podCreate)
+}
+
+// DefineCreateFlags declares and instantiates the container create flags
func DefineCreateFlags(cmd *cobra.Command, cf *entities.ContainerCreateOptions, isInfra bool) {
createFlags := cmd.Flags()
diff --git a/cmd/podman/common/create_test.go b/cmd/podman/common/create_test.go
new file mode 100644
index 000000000..17b47dd16
--- /dev/null
+++ b/cmd/podman/common/create_test.go
@@ -0,0 +1,53 @@
+package common_test
+
+import (
+ "reflect"
+ "strings"
+ "testing"
+
+ "github.com/containers/podman/v3/cmd/podman/common"
+ "github.com/containers/podman/v3/pkg/domain/entities"
+ "github.com/stretchr/testify/assert"
+)
+
+func TestPodOptions(t *testing.T) {
+ entry := "/test1"
+ exampleOptions := entities.ContainerCreateOptions{CPUS: 5.5, CPUSetCPUs: "0-4", Entrypoint: &entry, Hostname: "foo", Name: "testing123", Volume: []string{"/fakeVol1", "/fakeVol2"}, Net: &entities.NetOptions{CNINetworks: []string{"FakeNetwork"}}, PID: "ns:/proc/self/ns"}
+
+ podOptions := entities.PodCreateOptions{}
+ err := common.ContainerToPodOptions(&exampleOptions, &podOptions)
+ assert.Nil(t, err)
+
+ cc := reflect.ValueOf(&exampleOptions).Elem()
+ pc := reflect.ValueOf(&podOptions).Elem()
+
+ pcType := reflect.TypeOf(podOptions)
+ for i := 0; i < pc.NumField(); i++ {
+ podField := pc.FieldByIndex([]int{i})
+ podType := pcType.Field(i)
+ for j := 0; j < cc.NumField(); j++ {
+ containerField := cc.FieldByIndex([]int{j})
+ containerType := reflect.TypeOf(exampleOptions).Field(j)
+ tagPod := strings.Split(string(podType.Tag.Get("json")), ",")[0]
+ tagContainer := strings.Split(string(containerType.Tag.Get("json")), ",")[0]
+ if tagPod == tagContainer && (tagPod != "" && tagContainer != "") {
+ areEqual := true
+ if containerField.Kind() == podField.Kind() {
+ switch containerField.Kind() {
+ case reflect.Slice:
+ for i, w := range containerField.Interface().([]string) {
+ areEqual = podField.Interface().([]string)[i] == w
+ }
+ case reflect.String:
+ areEqual = (podField.String() == containerField.String())
+ case reflect.Bool:
+ areEqual = (podField.Bool() == containerField.Bool())
+ case reflect.Ptr:
+ areEqual = (reflect.DeepEqual(podField.Elem().Interface(), containerField.Elem().Interface()))
+ }
+ }
+ assert.True(t, areEqual)
+ }
+ }
+ }
+}