diff --git a/bootcfg/http/cloud.go b/bootcfg/http/cloud.go index 4e4507ac..3395d97c 100644 --- a/bootcfg/http/cloud.go +++ b/bootcfg/http/cloud.go @@ -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 diff --git a/bootcfg/http/cloud_test.go b/bootcfg/http/cloud_test.go index 18aab6f2..ba109962 100644 --- a/bootcfg/http/cloud_test.go +++ b/bootcfg/http/cloud_test.go @@ -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) { diff --git a/bootcfg/http/ignition.go b/bootcfg/http/ignition.go index 09c63f85..cb07ff9f 100644 --- a/bootcfg/http/ignition.go +++ b/bootcfg/http/ignition.go @@ -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 diff --git a/bootcfg/http/ignition_test.go b/bootcfg/http/ignition_test.go index c81bd0bb..f2901ec1 100644 --- a/bootcfg/http/ignition_test.go +++ b/bootcfg/http/ignition_test.go @@ -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) diff --git a/bootcfg/http/metadata.go b/bootcfg/http/metadata.go index 6574ed7a..e6b4cca6 100644 --- a/bootcfg/http/metadata.go +++ b/bootcfg/http/metadata.go @@ -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) } diff --git a/bootcfg/http/metadata_test.go b/bootcfg/http/metadata_test.go index 03d0b6b9..035e1dd5 100644 --- a/bootcfg/http/metadata_test.go +++ b/bootcfg/http/metadata_test.go @@ -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)