diff options
author | Charlie Doern <cdoern@redhat.com> | 2022-07-12 15:23:45 -0400 |
---|---|---|
committer | Charlie Doern <cdoern@redhat.com> | 2022-08-04 13:59:58 -0400 |
commit | 842c6c7c6748f8705698d25a29945f8437f1bed2 (patch) | |
tree | 944a6f4e50f76a9f5a2d5e1eae0f698a69f39fd4 /pkg/specgen/generate | |
parent | 1cf6afb788a681fc379d58d7f1cb69eb1690dba9 (diff) | |
download | podman-842c6c7c6748f8705698d25a29945f8437f1bed2.tar.gz podman-842c6c7c6748f8705698d25a29945f8437f1bed2.tar.bz2 podman-842c6c7c6748f8705698d25a29945f8437f1bed2.zip |
podman generate spec
implement a new command `podman generate spec` which can formulate a json specgen to be consumed by both the pod
and container creation API.
supported flags are
--verbose (default true) print output to the terminal
--compact print the json output in a single line format to be piped to the API
--filename put the output in a file
--clone rename the pod/ctr in the spec so it won't conflict w/ an existing entity
Signed-off-by: Charlie Doern <cdoern@redhat.com>
Diffstat (limited to 'pkg/specgen/generate')
-rw-r--r-- | pkg/specgen/generate/container.go | 39 | ||||
-rw-r--r-- | pkg/specgen/generate/pod_create.go | 14 |
2 files changed, 53 insertions, 0 deletions
diff --git a/pkg/specgen/generate/container.go b/pkg/specgen/generate/container.go index 8cfac924b..b5d10df8c 100644 --- a/pkg/specgen/generate/container.go +++ b/pkg/specgen/generate/container.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "os" + "strconv" "strings" "time" @@ -555,3 +556,41 @@ func FinishThrottleDevices(s *specgen.SpecGenerator) error { } return nil } + +// Check name looks for existing containers/pods with the same name, and modifies the given string until a new name is found +func CheckName(rt *libpod.Runtime, n string, kind bool) string { + switch { + case strings.Contains(n, "-clone"): + ind := strings.Index(n, "-clone") + 6 + num, err := strconv.Atoi(n[ind:]) + if num == 0 && err != nil { // clone1 is hard to get with this logic, just check for it here. + if kind { + _, err = rt.LookupContainer(n + "1") + } else { + _, err = rt.LookupPod(n + "1") + } + + if err != nil { + n += "1" + break + } + } else { + n = n[0:ind] + } + err = nil + count := num + for err == nil { + count++ + tempN := n + strconv.Itoa(count) + if kind { + _, err = rt.LookupContainer(tempN) + } else { + _, err = rt.LookupPod(tempN) + } + } + n += strconv.Itoa(count) + default: + n += "-clone" + } + return n +} diff --git a/pkg/specgen/generate/pod_create.go b/pkg/specgen/generate/pod_create.go index 4e6362c9b..d6063b9a0 100644 --- a/pkg/specgen/generate/pod_create.go +++ b/pkg/specgen/generate/pod_create.go @@ -2,6 +2,7 @@ package generate import ( "context" + "encoding/json" "fmt" "net" "os" @@ -327,6 +328,19 @@ func PodConfigToSpec(rt *libpod.Runtime, spec *specgen.PodSpecGenerator, infraOp } spec.InfraContainerSpec = infraSpec + matching, err := json.Marshal(infraSpec) + if err != nil { + return nil, err + } + + // track name before unmarshal so we do not overwrite w/ infra + name := spec.Name + err = json.Unmarshal(matching, spec) + if err != nil { + return nil, err + } + + spec.Name = name } // need to reset hostname, name etc of both pod and infra |