Skip to content

Commit

Permalink
feat: [cxl-host] attempt to publish as avahi service for auto discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
HJ-Fan committed Oct 8, 2024
1 parent 88fe381 commit 81d6333
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cmd/cxl-host/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"

"github.com/godbus/dbus/v5"
"github.com/holoplot/go-avahi"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -53,9 +56,61 @@ func main() {
accounts.AccountsHandler().InitLogger(logger)
accounts.AccountsHandler().Restore()

// avahi publish
err = AvahiPublish(ctx, settings.Port)
if err != nil {
fmt.Printf("ERROR: avahi publish failed, err=%v\nContinue...\n", err)
}
DefaultApiService := cxl_host.NewCxlHostApiService()
DefaultApiController := redfishapi.NewDefaultAPIController(DefaultApiService)
OverrideAPIController := cxl_host.NewOverrideAPIController(DefaultApiService)
router := cxl_host.NewCxlHostRouter(ctx, OverrideAPIController, DefaultApiController)
log.Fatal(http.ListenAndServe(":"+settings.Port, router))
}

// AvahiPublish: Publish the service with avahi
func AvahiPublish(ctx context.Context, port string) error {
txt := [][]byte{}
txt = append(txt, []byte("cxl-host=true"))

conn, err := dbus.SystemBus()
if err != nil {
return fmt.Errorf("avahi: Cannot get system bus: %v", err)
}

a, err := avahi.ServerNew(conn)
if err != nil {
return fmt.Errorf("avahi: Avahi new failed: %v", err)
}

eg, err := a.EntryGroupNew()
if err != nil {
return fmt.Errorf("avahi: EntryGroupNew() failed: %v", err)
}

hostname, err := a.GetHostName()
if err != nil {
return fmt.Errorf("avahi: GetHostName() failed: %v", err)
}

fqdn, err := a.GetHostNameFqdn()
if err != nil {
return fmt.Errorf("avahi: GetHostNameFqdn() failed: %v", err)
}

p, err := strconv.Atoi(port)
if err != nil {
return fmt.Errorf("avahi: Cannot convert port to Integer: %v", err)
}
err = eg.AddService(avahi.InterfaceUnspec, avahi.ProtoInet, 0, hostname, "_obmc_redfish._tcp", "local", fqdn, uint16(p), txt)
if err != nil {
return fmt.Errorf("avahi: AddService() failed: %v", err)
}

err = eg.Commit()
if err != nil {
return fmt.Errorf("avahi: Commit() failed: %v", err)
}

return nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ require (
k8s.io/klog/v2 v2.120.1
)

require github.com/godbus/dbus/v5 v5.0.4 // indirect

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holoplot/go-avahi v1.0.1
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand All @@ -21,6 +23,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/holoplot/go-avahi v1.0.1 h1:XcqR2keL4qWRnlxHD5CAOdWpLFZJ+EOUK0vEuylfvvk=
github.com/holoplot/go-avahi v1.0.1/go.mod h1:qH5psEKb0DK+BRplMfc+RY4VMOlbf6mqfxgpMy6aP0M=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down

0 comments on commit 81d6333

Please sign in to comment.