Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
7851f555
Commit
7851f555
authored
Oct 20, 2014
by
jhadvig
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Podex handling multiple images
parent
8261caed
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
33 deletions
+50
-33
podex.go
contrib/podex/podex.go
+50
-33
No files found.
contrib/podex/podex.go
View file @
7851f555
...
@@ -40,10 +40,11 @@ import (
...
@@ -40,10 +40,11 @@ import (
"gopkg.in/v1/yaml"
"gopkg.in/v1/yaml"
)
)
const
usage
=
"usage: podex [-json|-yaml]
<repo/dockerimage>
"
const
usage
=
"usage: podex [-json|-yaml]
-id=ID username/image1 ... username/imageN
"
var
generateJSON
=
flag
.
Bool
(
"json"
,
false
,
"generate json manifest"
)
var
generateJSON
=
flag
.
Bool
(
"json"
,
false
,
"generate json manifest"
)
var
generateYAML
=
flag
.
Bool
(
"yaml"
,
false
,
"generate yaml manifest"
)
var
generateYAML
=
flag
.
Bool
(
"yaml"
,
false
,
"generate yaml manifest"
)
var
podName
=
flag
.
String
(
"id"
,
""
,
"set pod name"
)
func
main
()
{
func
main
()
{
flag
.
Parse
()
flag
.
Parse
()
...
@@ -51,60 +52,64 @@ func main() {
...
@@ -51,60 +52,64 @@ func main() {
if
flag
.
NArg
()
<
1
{
if
flag
.
NArg
()
<
1
{
log
.
Fatal
(
usage
)
log
.
Fatal
(
usage
)
}
}
if
*
podName
==
""
{
imageName
:=
flag
.
Arg
(
0
)
if
flag
.
NArg
()
>
1
{
if
len
(
imageName
)
==
0
{
log
.
Fatal
(
usage
)
log
.
Fatal
(
usage
)
}
_
,
*
podName
=
parseDockerImage
(
flag
.
Arg
(
0
))
}
}
if
(
!*
generateJSON
&&
!*
generateYAML
)
||
(
*
generateJSON
&&
*
generateYAML
)
{
if
(
!*
generateJSON
&&
!*
generateYAML
)
||
(
*
generateJSON
&&
*
generateYAML
)
{
log
.
Fatal
(
usage
)
log
.
Fatal
(
usage
)
}
}
// Parse docker image name
// IMAGE: [REGISTRYHOST/][USERNAME/]NAME[:TAG]
// NAME: [a-z0-9-_.]
parts
:=
strings
.
Split
(
imageName
,
"/"
)
baseName
:=
parts
[
len
(
parts
)
-
1
]
dockerHost
:=
os
.
Getenv
(
"DOCKER_HOST"
)
dockerHost
:=
os
.
Getenv
(
"DOCKER_HOST"
)
if
dockerHost
==
""
{
if
dockerHost
==
""
{
log
.
Fatalf
(
"DOCKER_HOST is not set"
)
log
.
Fatalf
(
"DOCKER_HOST is not set"
)
}
}
docker
,
err
:=
dockerclient
.
NewClient
(
dockerHost
)
docker
,
err
:=
dockerclient
.
NewClient
(
dockerHost
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"failed to connect to %q: %v"
,
dockerHost
,
err
)
log
.
Fatalf
(
"failed to connect to %q: %v"
,
dockerHost
,
err
)
}
}
// TODO(proppy): use the regitry API instead of the remote API to get image metadata.
podContainers
:=
[]
v1beta1
.
Container
{}
img
,
err
:=
docker
.
InspectImage
(
imageName
)
if
err
!=
nil
{
for
_
,
imageName
:=
range
flag
.
Args
()
{
log
.
Fatalf
(
"failed to inspect image %q: %v"
,
imageName
,
err
)
parts
,
baseName
:=
parseDockerImage
(
imageName
)
container
:=
v1beta1
.
Container
{
Name
:
baseName
,
Image
:
imageName
,
}
// TODO(proppy): use the regitry API instead of the remote API to get image metadata.
img
,
err
:=
docker
.
InspectImage
(
imageName
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed to inspect image %q: %v"
,
imageName
,
err
)
}
for
p
:=
range
img
.
Config
.
ExposedPorts
{
port
,
err
:=
strconv
.
Atoi
(
p
.
Port
())
if
err
!=
nil
{
log
.
Fatalf
(
"failed to parse port %q: %v"
,
parts
[
0
],
err
)
}
container
.
Ports
=
append
(
container
.
Ports
,
v1beta1
.
Port
{
Name
:
strings
.
Join
([]
string
{
baseName
,
p
.
Proto
(),
p
.
Port
()},
"-"
),
ContainerPort
:
port
,
Protocol
:
v1beta1
.
Protocol
(
strings
.
ToUpper
(
p
.
Proto
())),
})
}
podContainers
=
append
(
podContainers
,
container
)
}
}
// TODO(proppy): add flag to handle multiple version
// TODO(proppy): add flag to handle multiple version
manifest
:=
v1beta1
.
ContainerManifest
{
manifest
:=
v1beta1
.
ContainerManifest
{
Version
:
"v1beta1"
,
Version
:
"v1beta1"
,
ID
:
baseName
+
"-pod"
,
ID
:
*
podName
+
"-pod"
,
Containers
:
[]
v1beta1
.
Container
{{
Containers
:
podContainers
,
Name
:
baseName
,
Image
:
imageName
,
}},
RestartPolicy
:
v1beta1
.
RestartPolicy
{
RestartPolicy
:
v1beta1
.
RestartPolicy
{
Always
:
&
v1beta1
.
RestartPolicyAlways
{},
Always
:
&
v1beta1
.
RestartPolicyAlways
{},
},
},
}
}
for
p
:=
range
img
.
Config
.
ExposedPorts
{
port
,
err
:=
strconv
.
Atoi
(
p
.
Port
())
if
err
!=
nil
{
log
.
Fatalf
(
"failed to parse port %q: %v"
,
parts
[
0
],
err
)
}
manifest
.
Containers
[
0
]
.
Ports
=
append
(
manifest
.
Containers
[
0
]
.
Ports
,
v1beta1
.
Port
{
Name
:
strings
.
Join
([]
string
{
baseName
,
p
.
Proto
(),
p
.
Port
()},
"-"
),
ContainerPort
:
port
,
Protocol
:
v1beta1
.
Protocol
(
strings
.
ToUpper
(
p
.
Proto
())),
})
}
if
*
generateJSON
{
if
*
generateJSON
{
bs
,
err
:=
json
.
MarshalIndent
(
manifest
,
""
,
" "
)
bs
,
err
:=
json
.
MarshalIndent
(
manifest
,
""
,
" "
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -120,3 +125,15 @@ func main() {
...
@@ -120,3 +125,15 @@ func main() {
os
.
Stdout
.
Write
(
bs
)
os
.
Stdout
.
Write
(
bs
)
}
}
}
}
// parseDockerImage split a docker image name of the form [REGISTRYHOST/][USERNAME/]NAME[:TAG]
// TODO: handle the TAG
// Returns array of images name parts and base image name
func
parseDockerImage
(
imageName
string
)
(
parts
[]
string
,
baseName
string
)
{
// Parse docker image name
// IMAGE: [REGISTRYHOST/][USERNAME/]NAME[:TAG]
// NAME: [a-z0-9-_.]
parts
=
strings
.
Split
(
imageName
,
"/"
)
baseName
=
parts
[
len
(
parts
)
-
1
]
return
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment