Skip to content

Commit

Permalink
feat: allow config-specified host prefix instead of 'nid'
Browse files Browse the repository at this point in the history
  • Loading branch information
synackd committed Nov 26, 2024
1 parent 1f70d65 commit 6f1bfe3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
19 changes: 14 additions & 5 deletions coresmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
cache *Cache
baseURL *url.URL
bootScriptBaseURL *url.URL
hostnamePrefix string
leaseDuration time.Duration
)

Expand All @@ -48,8 +49,8 @@ func setup4(args ...string) (handler.Handler4, error) {
log.Infof("initializing coresmd/coresmd %s (%s), built %s", version.Version, version.GitCommit, version.BuildTime)

// Ensure all required args were passed
if len(args) != 5 {
return nil, errors.New("expected 5 arguments: base URL, boot script base URL, CA certificate path, cache duration, lease duration")
if len(args) != 6 {
return nil, errors.New("expected 6 arguments: base URL, boot script base URL, CA certificate path, hostname prefix, cache duration, lease duration")
}

// Create new SmdClient using first argument (base URL)
Expand Down Expand Up @@ -81,17 +82,25 @@ func setup4(args ...string) (handler.Handler4, error) {
log.Infof("CA certificate path was empty, not setting")
}

// Parse hostname prefix
hostnamePrefix = strings.Trim(args[3], `"'`)
log.Infof("hostname prefix: %s", hostnamePrefix)
if hostnamePrefix == "" {
log.Info("no hostname prefix set, defaulting to 'nid'")
hostnamePrefix = "nid"
}

// Create new Cache using fourth argument (cache validity duration) and new SmdClient
// pointer
log.Debug("generating new Cache")
cache, err = NewCache(args[3], smdClient)
cache, err = NewCache(args[4], smdClient)
if err != nil {
return nil, fmt.Errorf("failed to create new cache: %w", err)
}

// Set lease duration from fifth argument
log.Debug("setting lease duration")
leaseDuration, err = time.ParseDuration(args[4])
leaseDuration, err = time.ParseDuration(args[5])
if err != nil {
return nil, fmt.Errorf("failed to parse lease duration: %w", err)
}
Expand Down Expand Up @@ -131,7 +140,7 @@ func Handler4(req, resp *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, bool) {

// Set client hostname
if ifaceInfo.Type == "Node" {
resp.Options.Update(dhcpv4.OptHostName(fmt.Sprintf("nid%04d", ifaceInfo.CompNID)))
resp.Options.Update(dhcpv4.OptHostName(fmt.Sprintf("%s%04d", hostnamePrefix, ifaceInfo.CompNID)))
}

// Set root path to this server's IP
Expand Down
9 changes: 6 additions & 3 deletions resources/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ server4:
# address if name servers are not configured.
# 3. (OPTIONAL) Path to CA cert used for TLS with the SMD base URL. If
# there is already a trusted certificate, this can be blank ("").
# 4. Cache validity duration. Coresmd uses a pull-through cache to store
# 4. Node hostname prefix. The hostnames given to nodes will be of the
# format "<prefix><nid>". If this argument is blank, "nid" is used as
# the prefix. <nid> is the node ID number, 0-padded to four places.
# 5. Cache validity duration. Coresmd uses a pull-through cache to store
# network information and this is the duration to refresh that cache.
# 5. Lease duration.
- coresmd: https://foobar.openchami.cluster http://172.16.0.253:8081 /root_ca/root_ca.crt 30s 1h
# 6. Lease duration.
- coresmd: https://foobar.openchami.cluster http://172.16.0.253:8081 /root_ca/root_ca.crt '' 30s 1h

# Any requests reaching this point are unknown to SMD and it is up to the
# administrator to decide how to handle unknown packets.
Expand Down

0 comments on commit 6f1bfe3

Please sign in to comment.