Skip to content

Commit

Permalink
Refactor frontend to use cobra
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Shen <[email protected]>
  • Loading branch information
mjlshen committed May 30, 2024
1 parent 1308b8c commit f2ee70c
Show file tree
Hide file tree
Showing 33 changed files with 324 additions and 142 deletions.
125 changes: 125 additions & 0 deletions frontend/cmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package cmd

import (
"context"
"fmt"
"net"
"os"
"os/signal"
"runtime/debug"
"syscall"

sdk "github.com/openshift-online/ocm-sdk-go"
"github.com/spf13/cobra"

"github.com/Azure/ARO-HCP/frontend/pkg/config"
"github.com/Azure/ARO-HCP/frontend/pkg/database"
"github.com/Azure/ARO-HCP/frontend/pkg/frontend"
)

type FrontendOpts struct {
clustersServiceURL string
insecure bool

region string
port int

databaseName string
databaseURL string
}

func NewRootCmd() *cobra.Command {
opts := &FrontendOpts{}
rootCmd := &cobra.Command{
Use: "aro-hcp-frontend",
Args: cobra.NoArgs,
Short: "Serve the ARO HCP Frontend",
Long: `Serve the ARO HCP Frontend
This command runs the ARO HCP Frontend. It communicates with Clusters Service and a CosmosDB
# Run ARO HCP Frontend locally to connect to a local Clusters Service at http://localhost:8000
./aro-hcp-frontend --database-name ${DB_NAME} --database-url ${DB_URL} --region ${REGION} \
--clusters-service-url "http://localhost:8000"
`,
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

rootCmd.Flags().StringVar(&opts.databaseName, "database-name", os.Getenv("DB_NAME"), "database name")
rootCmd.Flags().StringVar(&opts.databaseURL, "database-url", os.Getenv("DB_URL"), "database url")
rootCmd.Flags().StringVar(&opts.region, "region", os.Getenv("REGION"), "Azure region")
rootCmd.Flags().IntVar(&opts.port, "port", 8443, "port to listen on")

rootCmd.Flags().StringVar(&opts.clustersServiceURL, "clusters-service-url", "https://api.openshift.com", "URL of the OCM API gateway.")
rootCmd.Flags().BoolVar(&opts.insecure, "insecure", false, "Skip validating TLS for clusters-service.")

return rootCmd
}

func (opts *FrontendOpts) Run() error {
version := "unknown"
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
version = setting.Value
break
}
}
}

logger := config.DefaultLogger()
logger.Info(fmt.Sprintf("%s (%s) started", frontend.ProgramName, version))

// Init prometheus emitter
prometheusEmitter := frontend.NewPrometheusEmitter()

// Configure database configuration and client
dbConfig := database.NewDatabaseConfig(opts.databaseName, opts.databaseURL)
dbClient, err := database.NewDatabaseClient(dbConfig)
if err != nil {
return fmt.Errorf("creating the database client failed: %v", err)
}

listener, err := net.Listen("tcp4", fmt.Sprintf(":%d", opts.port))
if err != nil {
return err
}

logger.Info(fmt.Sprintf("Application running in region: %s", opts.region))

// Initialize Clusters Service Client
conn, err := sdk.NewUnauthenticatedConnectionBuilder().
URL(opts.clustersServiceURL).
Insecure(opts.insecure).
Build()
if err != nil {
return err
}

f := frontend.NewFrontend(logger, listener, prometheusEmitter, dbClient, opts.region, conn)

// Verify the Async DB is available and accessible
logger.Info("Testing DB Access")
result, err := dbClient.DBConnectionTest(context.Background())
if err != nil {
logger.Error(fmt.Sprintf("Database test failed to fetch properties: %v", err))
} else {
logger.Info(fmt.Sprintf("Database check completed - %s", result))
}

stop := make(chan struct{})
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, syscall.SIGINT, syscall.SIGTERM)
go f.Run(context.Background(), stop)

sig := <-signalChannel
logger.Info(fmt.Sprintf("caught %s signal", sig))
close(stop)

f.Join()
logger.Info(fmt.Sprintf("%s (%s) stopped", frontend.ProgramName, version))

return nil
}
2 changes: 1 addition & 1 deletion frontend/deploy/aro-hcp-frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ objects:
containers:
- name: aro-hcp-frontend
image: ${ARO_HCP_FRONTEND_IMAGE}
imagePullPolicy: IfNotPresent
imagePullPolicy: Always
env:
- name: DB_NAME
value: ${DB_NAME}
Expand Down
15 changes: 15 additions & 0 deletions frontend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/google/uuid v1.6.0
github.com/prometheus/client_golang v1.19.0
github.com/segmentio/ksuid v1.0.4
github.com/spf13/cobra v1.8.0
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
)

Expand All @@ -20,33 +21,47 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.11.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.7.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/microcosm-cc/bluemonday v1.0.18 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/openshift-online/ocm-sdk-go v0.1.421
github.com/openshift/api v0.0.0-20240429104249-ac9356ba1784 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.0 // indirect
k8s.io/apimachinery v0.30.0 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
Expand Down
Loading

0 comments on commit f2ee70c

Please sign in to comment.