mirror of
https://github.com/outbackdingo/matchbox.git
synced 2026-01-27 18:19:36 +00:00
bootcfg/http: Add selectors to template data
* Ignition and Cloud config templates can reference selector key/value pairs by lowercase key name * Metadata endpoint will provide metadata and selector key/val pairs for the matching machine group
This commit is contained in:
@@ -49,6 +49,9 @@ func cloudHandler(srv server.Server) ContextHandler {
|
||||
return
|
||||
}
|
||||
}
|
||||
for key, value := range group.Selector {
|
||||
data[strings.ToLower(key)] = value
|
||||
}
|
||||
|
||||
// render the template of a cloud config with data
|
||||
var buf bytes.Buffer
|
||||
|
||||
@@ -14,7 +14,20 @@ import (
|
||||
)
|
||||
|
||||
func TestCloudHandler(t *testing.T) {
|
||||
content := "#cloud-config"
|
||||
content := `#cloud-config
|
||||
coreos:
|
||||
etcd2:
|
||||
name: {{.uuid}}
|
||||
units:
|
||||
- name: {{.service_name}}
|
||||
`
|
||||
expected := `#cloud-config
|
||||
coreos:
|
||||
etcd2:
|
||||
name: a1b2c3d4
|
||||
units:
|
||||
- name: etcd2
|
||||
`
|
||||
store := &fake.FixedStore{
|
||||
Profiles: map[string]*storagepb.Profile{fake.Group.Profile: fake.Profile},
|
||||
CloudConfigs: map[string]string{fake.Profile.CloudId: content},
|
||||
@@ -26,9 +39,9 @@ func TestCloudHandler(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
h.ServeHTTP(ctx, w, req)
|
||||
// assert that:
|
||||
// - Cloud config is rendered with Group metadata
|
||||
// - Cloud config is rendered with Group metadata and selectors
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, content, w.Body.String())
|
||||
assert.Equal(t, expected, w.Body.String())
|
||||
}
|
||||
|
||||
func TestCloudHandler_MissingCtxProfile(t *testing.T) {
|
||||
|
||||
@@ -47,6 +47,9 @@ func ignitionHandler(srv server.Server) ContextHandler {
|
||||
}
|
||||
}
|
||||
data["query"] = req.URL.RawQuery
|
||||
for key, value := range group.Selector {
|
||||
data[strings.ToLower(key)] = value
|
||||
}
|
||||
|
||||
// render the template for an Ignition config with data
|
||||
var buf bytes.Buffer
|
||||
|
||||
@@ -13,10 +13,10 @@ import (
|
||||
fake "github.com/coreos/coreos-baremetal/bootcfg/storage/testfakes"
|
||||
)
|
||||
|
||||
var expectedIgnition = `{"ignitionVersion":1,"storage":{},"systemd":{"units":[{"name":"etcd2.service","enable":true}]},"networkd":{},"passwd":{}}`
|
||||
var expectedIgnition = `{"ignitionVersion":1,"storage":{},"systemd":{"units":[{"name":"etcd2.service","enable":true},{"name":"a1b2c3d4.service","enable":true}]},"networkd":{},"passwd":{}}`
|
||||
|
||||
func TestIgnitionHandler(t *testing.T) {
|
||||
content := `{"ignitionVersion": 1,"systemd":{"units":[{"name":"{{.service_name}}.service","enable":true}]}}`
|
||||
content := `{"ignitionVersion": 1,"systemd":{"units":[{"name":"{{.service_name}}.service","enable":true},{"name":"{{.uuid}}.service","enable":true}]}}`
|
||||
store := &fake.FixedStore{
|
||||
Profiles: map[string]*storagepb.Profile{fake.Group.Profile: fake.Profile},
|
||||
IgnitionConfigs: map[string]string{fake.Profile.IgnitionId: content},
|
||||
@@ -28,7 +28,7 @@ func TestIgnitionHandler(t *testing.T) {
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
h.ServeHTTP(ctx, w, req)
|
||||
// assert that:
|
||||
// - Ignition template is rendered with Group metadata
|
||||
// - Ignition template is rendered with Group metadata and selectors
|
||||
// - Rendered Ignition template is parsed as JSON
|
||||
// - Ignition Config served as JSON
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
@@ -43,6 +43,8 @@ systemd:
|
||||
units:
|
||||
- name: {{.service_name}}.service
|
||||
enable: true
|
||||
- name: {{.uuid}}.service
|
||||
enable: true
|
||||
`
|
||||
store := &fake.FixedStore{
|
||||
Profiles: map[string]*storagepb.Profile{fake.Group.Profile: testProfileIgnitionYAML},
|
||||
@@ -55,7 +57,7 @@ systemd:
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
h.ServeHTTP(ctx, w, req)
|
||||
// assert that:
|
||||
// - Ignition template is rendered with Group metadata
|
||||
// - Ignition template is rendered with Group metadata and selectors
|
||||
// - Rendered Ignition template ending in .yaml is parsed as YAML
|
||||
// - Ignition Config served as JSON
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
@@ -29,14 +29,13 @@ func metadataHandler() ContextHandler {
|
||||
return
|
||||
}
|
||||
}
|
||||
for key, value := range group.Selector {
|
||||
data[key] = value
|
||||
}
|
||||
|
||||
for key, value := range data {
|
||||
fmt.Fprintf(w, "%s=%v\n", strings.ToUpper(key), value)
|
||||
}
|
||||
attrs := labelsFromRequest(req)
|
||||
for key, value := range attrs {
|
||||
fmt.Fprintf(w, "%s=%v\n", strings.ToUpper(key), value)
|
||||
}
|
||||
}
|
||||
return ContextHandlerFunc(fn)
|
||||
}
|
||||
|
||||
@@ -18,18 +18,16 @@ func TestMetadataHandler(t *testing.T) {
|
||||
h := metadataHandler()
|
||||
ctx := withGroup(context.Background(), fake.Group)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/?uuid=a1b2c3d4&mac="+validMACStr, nil)
|
||||
req, _ := http.NewRequest("GET", "/?uuid=a1b2c3d4", nil)
|
||||
h.ServeHTTP(ctx, w, req)
|
||||
// assert that:
|
||||
// - the Group's custom metadata is served
|
||||
// - query argument attributes are added to the metadata
|
||||
// - the Group's custom metadata and selectors are served
|
||||
// - key names are upper case
|
||||
expectedData := map[string]string{
|
||||
"K8S_VERSION": "v1.1.2",
|
||||
"POD_NETWORK": "10.2.0.0/16",
|
||||
"SERVICE_NAME": "etcd2",
|
||||
"UUID": "a1b2c3d4",
|
||||
"MAC": validMACStr,
|
||||
}
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
// convert response (random order) to map (tests compare in order)
|
||||
|
||||
Reference in New Issue
Block a user