-
Notifications
You must be signed in to change notification settings - Fork 1
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
envsvc: added pod spec env lookup #9
Open
dhowden
wants to merge
6
commits into
master
Choose a base branch
from
pod-spec-yaml
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bd88e6a
envsvc: added pod spec env lookup
dhowden e203f77
added missing files
dhowden 45912e5
fixup YAML conversion
dhowden d40bcc4
added test, need to fix YAML raw string formatting
dhowden eb702c9
fixed up YAML formatting
dhowden 2578b6f
improve flag docs
dhowden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package envsvc | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type pod struct { | ||
Spec struct { | ||
Containers []struct { | ||
Name string `yaml:"name"` | ||
Env []struct { | ||
Name string `yaml:"name"` | ||
Value string `yaml:"value"` | ||
} `yaml:"env"` | ||
} `yaml:"containers"` | ||
} `yaml:"spec"` | ||
} | ||
|
||
type Getter interface { | ||
Get(string) (string, bool) | ||
} | ||
|
||
// PodENVLookup creates a lookup Getter from the given Pod YAML. | ||
// Set name if there is more than one container in the pod. | ||
func PodENVLookup(r io.Reader, name string) (Getter, error) { | ||
dec := yaml.NewDecoder(r) | ||
p := &pod{} | ||
if err := dec.Decode(p); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(p.Spec.Containers) == 0 { | ||
return nil, errors.New("no containers in pod spec") | ||
} | ||
|
||
if name == "" { | ||
if len(p.Spec.Containers) != 1 { | ||
return nil, fmt.Errorf("name empty but %d containers in pod spec, must set name", len(p.Spec.Containers)) | ||
} | ||
return lookupFromContainerEnv(p, 0), nil | ||
} | ||
|
||
for i, c := range p.Spec.Containers { | ||
if c.Name == name { | ||
return lookupFromContainerEnv(p, i), nil | ||
} | ||
} | ||
return nil, fmt.Errorf("no container for name %q", name) | ||
} | ||
|
||
func lookupFromContainerEnv(p *pod, n int) *lookup { | ||
m := make(map[string]string) | ||
for _, x := range p.Spec.Containers[n].Env { | ||
m[x.Name] = x.Value | ||
} | ||
return &lookup{ | ||
g: osLookup{}, | ||
m: m, | ||
} | ||
} | ||
|
||
type lookup struct { | ||
g Getter | ||
m map[string]string | ||
} | ||
|
||
func (l lookup) Get(x string) (string, bool) { | ||
z, ok := l.g.Get(x) | ||
if ok { | ||
return z, ok | ||
} | ||
|
||
z, ok = l.m[x] | ||
return z, ok | ||
} | ||
|
||
type osLookup struct{} | ||
|
||
func (osLookup) Get(x string) (string, bool) { return os.LookupEnv(x) } | ||
|
||
func get(g Getter, x string) string { | ||
z, _ := g.Get(x) | ||
return z | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package envsvc_test | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"code.sajari.com/env/envsvc" | ||
) | ||
|
||
const singleInput = `apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: podname | ||
labels: | ||
name: podname | ||
spec: | ||
containers: | ||
- name: one | ||
image: one | ||
ports: | ||
- name: grpc | ||
protocol: TCP | ||
containerPort: 50551 | ||
resources: | ||
requests: | ||
cpu: 100m | ||
memory: 50Mi | ||
limits: | ||
cpu: "1" | ||
memory: 50Mi | ||
env: | ||
- name: ONE_BOOLEAN | ||
value: "false" | ||
- name: ONE_STRING | ||
value: "string value" | ||
- name: ONE_INTEGER | ||
value: "1"` | ||
|
||
const multipleInput = `apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: podname | ||
labels: | ||
name: podname | ||
spec: | ||
containers: | ||
- name: one | ||
image: one | ||
ports: | ||
- name: grpc | ||
protocol: TCP | ||
containerPort: 50551 | ||
resources: | ||
requests: | ||
cpu: 100m | ||
memory: 50Mi | ||
limits: | ||
cpu: "1" | ||
memory: 50Mi | ||
env: | ||
- name: ONE_BOOLEAN | ||
value: "false" | ||
- name: ONE_STRING | ||
value: "string value" | ||
- name: ONE_INTEGER | ||
value: "1" | ||
- name: two | ||
image: two | ||
ports: | ||
- name: grpc | ||
protocol: TCP | ||
containerPort: 50552 | ||
env: | ||
- name: TWO_BOOLEAN | ||
value: "true" | ||
- name: TWO_STRING | ||
value: "string value two" | ||
- name: TWO_INTEGER | ||
value: "2"` | ||
|
||
func TestPodENVLookup(t *testing.T) { | ||
t.Run("single_container", func(t *testing.T) { | ||
x, err := envsvc.PodENVLookup(strings.NewReader(singleInput), "") | ||
must(t, err) | ||
|
||
expectValue(t, x, "ONE_BOOLEAN", "false") | ||
expectValue(t, x, "ONE_STRING", "string value") | ||
expectValue(t, x, "ONE_INTEGER", "1") | ||
|
||
// Using the name should be fine (for 1 container also). | ||
x, err = envsvc.PodENVLookup(strings.NewReader(singleInput), "one") | ||
must(t, err) | ||
|
||
expectValue(t, x, "ONE_BOOLEAN", "false") | ||
expectValue(t, x, "ONE_STRING", "string value") | ||
expectValue(t, x, "ONE_INTEGER", "1") | ||
}) | ||
|
||
t.Run("multiple_containers", func(t *testing.T) { | ||
x, err := envsvc.PodENVLookup(strings.NewReader(multipleInput), "two") | ||
must(t, err) | ||
|
||
expectValue(t, x, "TWO_BOOLEAN", "true") | ||
expectValue(t, x, "TWO_STRING", "string value two") | ||
expectValue(t, x, "TWO_INTEGER", "2") | ||
}) | ||
|
||
} | ||
|
||
func expectValue(t *testing.T, g envsvc.Getter, name, value string) { | ||
t.Helper() | ||
|
||
x, ok := g.Get(name) | ||
if !ok { | ||
t.Errorf("expected value for %q", name) | ||
return | ||
} | ||
if x != value { | ||
t.Errorf("g.Get(%q) = %q, want %q", name, x, value) | ||
} | ||
} | ||
|
||
func must(t *testing.T, err error) { | ||
t.Helper() | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module code.sajari.com/env | ||
|
||
go 1.15 | ||
|
||
require gopkg.in/yaml.v3 v3.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why ENV instead of Env?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was in a shouty mood, presumably in anticipation of the YAML shenanigans. Will change it!