Skip to content

Commit a92038f

Browse files
Ace EldeibandyliulimingUbuntu
authored
setup one entrypoint for the agentbakersvc. (#2145) (#2148)
Co-authored-by: Ubuntu <andliu@devbox.rws0oxdtctoulkaanznumdrtch.ix.internal.cloudapp.net> Co-authored-by: Liming Liu <andyliuliming@outlook.com> Co-authored-by: Ubuntu <andliu@devbox.rws0oxdtctoulkaanznumdrtch.ix.internal.cloudapp.net>
1 parent 3dd463b commit a92038f

File tree

2 files changed

+79
-71
lines changed

2 files changed

+79
-71
lines changed

cmd/main.go

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +2,11 @@
22
package main
33

44
import (
5-
"context"
6-
"fmt"
7-
"log"
8-
"os"
9-
"os/signal"
10-
"syscall"
11-
12-
"github.com/Azure/agentbaker/apiserver"
13-
"github.com/spf13/cobra"
5+
"github.com/Azure/agentbaker/cmd/starter"
146
)
157

8+
// NOTE: please make the main.go only contains the starter.Execute()
9+
// that's because other components is reference the same function "starter.Execute()" to start the process
1610
func main() {
17-
Execute()
18-
}
19-
20-
// Execute adds all child commands to the root command and sets flags appropriately.
21-
func Execute() {
22-
rootCmd.AddCommand(startCmd)
23-
startCmd.Flags().StringVar(&options.Addr, "addr", ":8080", "the addr to serve the api on")
24-
25-
if err := rootCmd.Execute(); err != nil {
26-
fmt.Println(err)
27-
os.Exit(1)
28-
}
29-
}
30-
31-
// rootCmd represents the base command when called without any subcommands
32-
var rootCmd = &cobra.Command{
33-
Use: "agentbaker",
34-
Short: "Agent baker is responsible for generating all the data necessary to allow Nodes to join an AKS cluster.",
35-
}
36-
37-
var (
38-
options = &apiserver.Options{}
39-
)
40-
41-
// startCmd represents the start command
42-
var startCmd = &cobra.Command{
43-
Use: "start",
44-
Short: "Starts the server that hosts agentbaker",
45-
Run: startHelper,
46-
}
47-
48-
func startHelper(cmd *cobra.Command, args []string) {
49-
ctx, shutdown := context.WithCancel(context.Background())
50-
defer shutdown()
51-
52-
// setup signal handling to cancel the context
53-
go func() {
54-
signals := make(chan os.Signal, 1)
55-
signal.Notify(signals, syscall.SIGTERM)
56-
<-signals
57-
log.Println("received SIGTERM. Terminating...")
58-
shutdown()
59-
}()
60-
61-
api, err := apiserver.NewAPIServer(options)
62-
if err != nil {
63-
log.Fatal(ctx, err.Error())
64-
}
65-
66-
errorPipeline := make(chan error)
67-
68-
go func() {
69-
log.Printf("Serving API on %s\n", options.Addr)
70-
errorPipeline <- api.ListenAndServe(ctx)
71-
}()
72-
73-
select {
74-
case <-ctx.Done():
75-
return
76-
case err := <-errorPipeline:
77-
log.Fatal(ctx, err.Error())
78-
}
11+
starter.Execute()
7912
}

cmd/starter/start.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//This has been generated using akservice version: v0.0.1
2+
package starter
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"log"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
12+
"github.com/Azure/agentbaker/apiserver"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
// Execute adds all child commands to the root command and sets flags appropriately.
17+
func Execute() {
18+
rootCmd.AddCommand(startCmd)
19+
startCmd.Flags().StringVar(&options.Addr, "addr", ":8080", "the addr to serve the api on")
20+
21+
if err := rootCmd.Execute(); err != nil {
22+
fmt.Println(err)
23+
os.Exit(1)
24+
}
25+
}
26+
27+
// rootCmd represents the base command when called without any subcommands
28+
var rootCmd = &cobra.Command{
29+
Use: "agentbaker",
30+
Short: "Agent baker is responsible for generating all the data necessary to allow Nodes to join an AKS cluster.",
31+
}
32+
33+
var (
34+
options = &apiserver.Options{}
35+
)
36+
37+
// startCmd represents the start command
38+
var startCmd = &cobra.Command{
39+
Use: "start",
40+
Short: "Starts the server that hosts agentbaker",
41+
Run: startHelper,
42+
}
43+
44+
func startHelper(cmd *cobra.Command, args []string) {
45+
ctx, shutdown := context.WithCancel(context.Background())
46+
defer shutdown()
47+
48+
// setup signal handling to cancel the context
49+
go func() {
50+
signals := make(chan os.Signal, 1)
51+
signal.Notify(signals, syscall.SIGTERM)
52+
<-signals
53+
log.Println("received SIGTERM. Terminating...")
54+
shutdown()
55+
}()
56+
57+
api, err := apiserver.NewAPIServer(options)
58+
if err != nil {
59+
log.Fatal(ctx, err.Error())
60+
}
61+
62+
errorPipeline := make(chan error)
63+
64+
go func() {
65+
log.Printf("Serving API on %s\n", options.Addr)
66+
errorPipeline <- api.ListenAndServe(ctx)
67+
}()
68+
69+
select {
70+
case <-ctx.Done():
71+
return
72+
case err := <-errorPipeline:
73+
log.Fatal(ctx, err.Error())
74+
}
75+
}

0 commit comments

Comments
 (0)