Skip to content

Commit 2bd7608

Browse files
committed
feat: consistent base URL and fake SMD CLI
Add a --smd-simulator flag for the CLOUD_INIT_SMD_SIMULATOR and set the fakeSMDEnabled variable to it. Previously the envvar was used in some cases, and the variable in others, and nothing actually set the variable. Pass baseUrl to the VendorDataHandler initializer and use it if none is set in the store. Previously this was only used for logging, and was effectively inaccurate as such. Signed-off-by: Travis Raines <571832+rainest@users.noreply.github.com>
1 parent ded8403 commit 2bd7608

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

cmd/cloud-init-server/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ func setupFlags(flags *pflag.FlagSet) {
8989
flags.StringVar(&region, "region", getEnv("REGION", ""), "Region of the cluster")
9090
flags.StringVar(&availabilityZone, "az", getEnv("AZ", ""), "Availability zone of the cluster")
9191
flags.StringVar(&cloudProvider, "cloud-provider", getEnv("CLOUD_PROVIDER", ""), "Cloud provider of the cluster")
92-
flags.StringVar(&baseUrl, "base-url", getEnv("BASE_URL", ""), "Base URL for cloud-init-server including protocol and port (e.g. http://localhost:27777)")
92+
flags.StringVar(&baseUrl, "base-url", getEnv("BASE_URL", "http://cloud-init:27777"), "Base URL for cloud-init-server including protocol and port (e.g. http://localhost:27777)")
9393
flags.StringVar(&certPath, "cacert", getEnv("CACERT", ""), "Path to CA cert (defaults to system CAs)")
9494
flags.BoolVar(&insecure, "insecure", parseBool(getEnv("INSECURE", "false")), "Set to bypass TLS verification for requests")
9595
flags.BoolVar(&impersonationEnabled, "impersonation", parseBool(getEnv("IMPERSONATION", "false")), "Enable impersonation feature")
96+
flags.BoolVar(&fakeSMDEnabled, "smd-simulator", parseBool(getEnv("CLOUD_INIT_SMD_SIMULATOR", "false")), "Enable fake SMD")
9697
flags.StringVar(&wireguardServer, "wireguard-server", getEnv("WIREGUARD_SERVER", ""), "WireGuard server IP address and network (e.g. 100.97.0.1/16)")
9798
flags.BoolVar(&wireguardOnly, "wireguard-only", parseBool(getEnv("WIREGUARD_ONLY", "false")), "Only allow access to the cloud-init functions from the WireGuard subnet")
9899
flags.BoolVar(&debug, "debug", parseBool(getEnv("DEBUG", "false")), "Enable debug logging")
@@ -187,7 +188,7 @@ func startServer() error {
187188

188189
// Create SMD client
189190
var sm smdclient.SMDClientInterface
190-
if os.Getenv("CLOUD_INIT_SMD_SIMULATOR") == "true" {
191+
if fakeSMDEnabled {
191192
fmt.Printf("\n\n**********\n\n\tCLOUD_INIT_SMD_SIMULATOR is set to true in your environment.\n\n\tUsing the FakeSMDClient\n\n**********\n\n\n")
192193
sm = smdclient.NewFakeSMDClient(clusterName, 500)
193194
} else {
@@ -285,12 +286,12 @@ func initCiClientRouter(router chi.Router, handler *CiHandler, wgInterfaceManage
285286
if wireGuardMiddleware != nil {
286287
router.With(wireGuardMiddleware).Get("/user-data", UserDataHandler)
287288
router.With(wireGuardMiddleware).Get("/meta-data", MetaDataHandler(handler.sm, handler.store))
288-
router.With(wireGuardMiddleware).Get("/vendor-data", VendorDataHandler(handler.sm, handler.store))
289+
router.With(wireGuardMiddleware).Get("/vendor-data", VendorDataHandler(handler.sm, handler.store, baseUrl))
289290
router.With(wireGuardMiddleware).Get("/{group}.yaml", GroupUserDataHandler(handler.sm, handler.store))
290291
} else {
291292
router.Get("/user-data", UserDataHandler)
292293
router.Get("/meta-data", MetaDataHandler(handler.sm, handler.store))
293-
router.Get("/vendor-data", VendorDataHandler(handler.sm, handler.store))
294+
router.Get("/vendor-data", VendorDataHandler(handler.sm, handler.store, baseUrl))
294295
router.Get("/{group}.yaml", GroupUserDataHandler(handler.sm, handler.store))
295296
}
296297
router.Post("/phone-home/{id}", PhoneHomeHandler(wgInterfaceManager, handler.sm))
@@ -319,7 +320,7 @@ func initCiAdminRouter(router chi.Router, handler *CiHandler) {
319320
// impersonation API endpoints
320321
r.Get("/impersonation/{id}/user-data", UserDataHandler)
321322
r.Get("/impersonation/{id}/meta-data", MetaDataHandler(handler.sm, handler.store))
322-
r.Get("/impersonation/{id}/vendor-data", VendorDataHandler(handler.sm, handler.store))
323+
r.Get("/impersonation/{id}/vendor-data", VendorDataHandler(handler.sm, handler.store, baseUrl))
323324
r.Get("/impersonation/{id}/{group}.yaml", GroupUserDataHandler(handler.sm, handler.store))
324325
}
325326

cmd/cloud-init-server/vendordata_handlers.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ import (
2424
// @Param id path string false "Node ID"
2525
// @Router /vendor-data [get]
2626
// @Router /admin/impersonation/{id}/vendor-data [get]
27-
func VendorDataHandler(smd smdclient.SMDClientInterface, store cistore.Store) http.HandlerFunc {
27+
func VendorDataHandler(smd smdclient.SMDClientInterface, store cistore.Store, baseUrl string) http.HandlerFunc {
2828
return func(w http.ResponseWriter, r *http.Request) {
2929
urlId := chi.URLParam(r, "id")
30-
var baseUrl string
3130
var id = urlId
3231
var err error
3332
// If this request includes an id, it can be interrpreted as an impersonation request
@@ -75,9 +74,6 @@ func VendorDataHandler(smd smdclient.SMDClientInterface, store cistore.Store) ht
7574
if extendedInstanceData.CloudInitBaseURL != "" {
7675
baseUrl = extendedInstanceData.CloudInitBaseURL
7776
}
78-
if baseUrl == "" {
79-
baseUrl = "http://cloud-init:27777"
80-
}
8177

8278
payload := "#include\n"
8379
for _, group_name := range groups {

0 commit comments

Comments
 (0)