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
c29d89a5
Commit
c29d89a5
authored
Jan 30, 2015
by
Satnam Singh
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3357 from proppy/fix-podex
contrib/podex: fix ordering and manifest generation
parents
f04769a5
363ce1b3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
43 deletions
+81
-43
podex.go
contrib/podex/podex.go
+80
-43
test-go.sh
hack/test-go.sh
+1
-0
No files found.
contrib/podex/podex.go
View file @
c29d89a5
...
@@ -32,22 +32,29 @@ import (
...
@@ -32,22 +32,29 @@ import (
"encoding/json"
"encoding/json"
"flag"
"flag"
"fmt"
"fmt"
"io
/ioutil
"
"io"
"log"
"log"
"net/http"
"net/http"
"os"
"os"
"strconv"
"strconv"
"strings"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta1"
"github.com/ghodss/yaml"
"github.com/ghodss/yaml"
goyaml
"gopkg.in/v2/yaml"
)
)
const
usage
=
"
usage: podex [-json|-yaml] [-id PODNAME] IMAGES
"
const
usage
=
"
podex [-format=yaml|json] [-type=pod|container] [-id NAME] IMAGES...
"
var
generateJSON
=
flag
.
Bool
(
"json"
,
false
,
"generate json manifest"
)
var
manifestFormat
=
flag
.
String
(
"format"
,
"yaml"
,
"manifest format to output, `yaml` or `json`"
)
var
generateYAML
=
flag
.
Bool
(
"yaml"
,
false
,
"generate yaml manifest"
)
var
manifestType
=
flag
.
String
(
"type"
,
"pod"
,
"manifest type to output, `pod` or `container`"
)
var
podName
=
flag
.
String
(
"id"
,
""
,
"set pod name"
)
var
manifestName
=
flag
.
String
(
"name"
,
""
,
"manifest name, default to image base name"
)
func
init
()
{
flag
.
Usage
=
func
()
{
fmt
.
Fprintf
(
os
.
Stderr
,
"Usage: %s
\n
"
,
usage
)
flag
.
PrintDefaults
()
}
}
type
image
struct
{
type
image
struct
{
Host
string
Host
string
...
@@ -60,28 +67,25 @@ func main() {
...
@@ -60,28 +67,25 @@ func main() {
flag
.
Parse
()
flag
.
Parse
()
if
flag
.
NArg
()
<
1
{
if
flag
.
NArg
()
<
1
{
log
.
Fatal
(
usage
)
flag
.
Usage
()
log
.
Fatal
(
"pod: missing image argument"
)
}
}
if
*
pod
Name
==
""
{
if
*
manifest
Name
==
""
{
if
flag
.
NArg
()
>
1
{
if
flag
.
NArg
()
>
1
{
log
.
Print
(
usage
)
flag
.
Usage
(
)
log
.
Fatal
(
"podex: -id arg is required when passing more than one image"
)
log
.
Fatal
(
"podex: -id arg is required when passing more than one image"
)
}
}
_
,
_
,
*
pod
Name
,
_
=
splitDockerImageName
(
flag
.
Arg
(
0
))
_
,
_
,
*
manifest
Name
,
_
=
splitDockerImageName
(
flag
.
Arg
(
0
))
}
}
if
(
!*
generateJSON
&&
!*
generateYAML
)
||
(
*
generateJSON
&&
*
generateYAML
)
{
podContainers
:=
[]
goyaml
.
MapSlice
{}
log
.
Fatal
(
usage
)
}
podContainers
:=
[]
v1beta1
.
Container
{}
for
_
,
imageName
:=
range
flag
.
Args
()
{
for
_
,
imageName
:=
range
flag
.
Args
()
{
host
,
namespace
,
repo
,
tag
:=
splitDockerImageName
(
imageName
)
host
,
namespace
,
repo
,
tag
:=
splitDockerImageName
(
imageName
)
container
:=
v1beta1
.
Container
{
container
:=
goyaml
.
MapSlice
{
Name
:
repo
,
{
Key
:
"name"
,
Value
:
repo
}
,
Image
:
imageName
,
{
Key
:
"image"
,
Value
:
imageName
}
,
}
}
img
,
err
:=
getImageMetadata
(
host
,
namespace
,
repo
,
tag
)
img
,
err
:=
getImageMetadata
(
host
,
namespace
,
repo
,
tag
)
...
@@ -89,43 +93,79 @@ func main() {
...
@@ -89,43 +93,79 @@ func main() {
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"failed to get image metadata %q: %v"
,
imageName
,
err
)
log
.
Fatalf
(
"failed to get image metadata %q: %v"
,
imageName
,
err
)
}
}
portSlice
:=
[]
goyaml
.
MapSlice
{}
for
p
:=
range
img
.
ContainerConfig
.
ExposedPorts
{
for
p
:=
range
img
.
ContainerConfig
.
ExposedPorts
{
port
,
err
:=
strconv
.
Atoi
(
p
.
Port
())
port
,
err
:=
strconv
.
Atoi
(
p
.
Port
())
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"failed to parse port %q: %v"
,
p
.
Port
(),
err
)
log
.
Fatalf
(
"failed to parse port %q: %v"
,
p
.
Port
(),
err
)
}
}
container
.
Ports
=
append
(
container
.
Ports
,
v1beta1
.
Port
{
portEntry
:=
goyaml
.
MapSlice
{{
Name
:
strings
.
Join
([]
string
{
repo
,
p
.
Proto
(),
p
.
Port
()},
"-"
),
Key
:
"name"
,
ContainerPort
:
port
,
Value
:
strings
.
Join
([]
string
{
repo
,
p
.
Proto
(),
p
.
Port
()},
"-"
),
Protocol
:
v1beta1
.
Protocol
(
strings
.
ToUpper
(
p
.
Proto
())),
},
{
})
Key
:
"containerPort"
,
Value
:
port
,
}}
portSlice
=
append
(
portSlice
,
portEntry
)
if
p
.
Proto
()
!=
"tcp"
{
portEntry
=
append
(
portEntry
,
goyaml
.
MapItem
{
Key
:
"protocol"
,
Value
:
strings
.
ToUpper
(
p
.
Proto
())})
}
}
if
len
(
img
.
ContainerConfig
.
ExposedPorts
)
>
0
{
container
=
append
(
container
,
goyaml
.
MapItem
{
Key
:
"ports"
,
Value
:
portSlice
})
}
}
podContainers
=
append
(
podContainers
,
container
)
podContainers
=
append
(
podContainers
,
container
)
}
}
// TODO(proppy): add flag to handle multiple version
// TODO(proppy): add flag to handle multiple version
manifest
:=
v1beta1
.
ContainerManifest
{
containerManifest
:=
goyaml
.
MapSlice
{
Version
:
"v1beta1"
,
{
Key
:
"version"
,
Value
:
"v1beta2"
},
ID
:
*
podName
+
"-pod"
,
{
Key
:
"containers"
,
Value
:
podContainers
},
Containers
:
podContainers
,
RestartPolicy
:
v1beta1
.
RestartPolicy
{
Always
:
&
v1beta1
.
RestartPolicyAlways
{},
},
}
}
if
*
generateJSON
{
var
data
interface
{}
bs
,
err
:=
json
.
MarshalIndent
(
manifest
,
""
,
" "
)
if
err
!=
nil
{
switch
*
manifestType
{
log
.
Fatalf
(
"failed to render JSON container manifest: %v"
,
err
)
case
"container"
:
containerManifest
=
append
(
goyaml
.
MapSlice
{
{
Key
:
"id"
,
Value
:
*
manifestName
},
},
containerManifest
...
)
data
=
containerManifest
case
"pod"
:
data
=
goyaml
.
MapSlice
{
{
Key
:
"id"
,
Value
:
*
manifestName
},
{
Key
:
"kind"
,
Value
:
"Pod"
},
{
Key
:
"apiVersion"
,
Value
:
"v1beta1"
},
{
Key
:
"desiredState"
,
Value
:
goyaml
.
MapSlice
{
{
Key
:
"manifest"
,
Value
:
containerManifest
},
}},
}
}
os
.
Stdout
.
Write
(
bs
)
default
:
flag
.
Usage
()
log
.
Fatalf
(
"unsupported manifest type %q"
,
*
manifestType
)
}
}
if
*
generateYAML
{
bs
,
err
:=
yaml
.
Marshal
(
manifest
)
yamlBytes
,
err
:=
goyaml
.
Marshal
(
data
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed to marshal container manifest: %v"
,
err
)
}
switch
*
manifestFormat
{
case
"yaml"
:
os
.
Stdout
.
Write
(
yamlBytes
)
case
"json"
:
jsonBytes
,
err
:=
yaml
.
YAMLToJSON
(
yamlBytes
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Fatalf
(
"failed to render YAML container manifest: %v"
,
err
)
log
.
Fatalf
(
"failed to marshal container manifest into JSON: %v"
,
err
)
}
var
jsonPretty
bytes
.
Buffer
if
err
:=
json
.
Indent
(
&
jsonPretty
,
jsonBytes
,
""
,
" "
);
err
!=
nil
{
log
.
Fatalf
(
"failed to indent json %q: %v"
,
string
(
jsonBytes
),
err
)
}
}
os
.
Stdout
.
Write
(
bs
)
io
.
Copy
(
os
.
Stdout
,
&
jsonPretty
)
default
:
flag
.
Usage
()
log
.
Fatalf
(
"unsupported manifest format %q"
,
*
manifestFormat
)
}
}
}
}
...
@@ -215,11 +255,8 @@ func getImageMetadata(host, namespace, repo, tag string) (*imageMetadata, error)
...
@@ -215,11 +255,8 @@ func getImageMetadata(host, namespace, repo, tag string) (*imageMetadata, error)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error getting json for image %q: %v"
,
imageID
,
err
)
return
nil
,
fmt
.
Errorf
(
"error getting json for image %q: %v"
,
imageID
,
err
)
}
}
data
,
_
:=
ioutil
.
ReadAll
(
resp
.
Body
)
buf
:=
bytes
.
NewBuffer
(
data
)
log
.
Print
(
string
(
data
))
var
image
imageMetadata
var
image
imageMetadata
if
err
:=
json
.
NewDecoder
(
buf
)
.
Decode
(
&
image
);
err
!=
nil
{
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
image
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error decoding image %q metadata: %v"
,
imageID
,
err
)
return
nil
,
fmt
.
Errorf
(
"error decoding image %q metadata: %v"
,
imageID
,
err
)
}
}
return
&
image
,
nil
return
&
image
,
nil
...
...
hack/test-go.sh
View file @
c29d89a5
...
@@ -34,6 +34,7 @@ kube::test::find_dirs() {
...
@@ -34,6 +34,7 @@ kube::test::find_dirs() {
-o
-wholename
'./target'
\
-o
-wholename
'./target'
\
-o
-wholename
'*/third_party/*'
\
-o
-wholename
'*/third_party/*'
\
-o
-wholename
'*/Godeps/*'
\
-o
-wholename
'*/Godeps/*'
\
-o
-wholename
'*/contrib/podex/*'
\
\)
-prune
\
\)
-prune
\
\)
-name
'*_test.go'
-print0
| xargs
-0n1
dirname
|
sed
's|^\./||'
|
sort
-u
\)
-name
'*_test.go'
-print0
| xargs
-0n1
dirname
|
sed
's|^\./||'
|
sort
-u
)
)
...
...
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