Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify API route registration #5851

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 57 additions & 57 deletions cmd/applications-rp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,75 +18,57 @@ package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/go-logr/logr"
"github.com/spf13/pflag"
etcdclient "go.etcd.io/etcd/client/v3"
runtimelog "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/radius-project/radius/pkg/armrpc/builder"
"github.com/radius-project/radius/pkg/armrpc/hostoptions"
"github.com/radius-project/radius/pkg/corerp/backend"
"github.com/radius-project/radius/pkg/corerp/frontend"
metricsservice "github.com/radius-project/radius/pkg/metrics/service"
profilerservice "github.com/radius-project/radius/pkg/profiler/service"
"github.com/radius-project/radius/pkg/recipes/controllerconfig"
"github.com/radius-project/radius/pkg/server"
"github.com/radius-project/radius/pkg/trace"

"github.com/radius-project/radius/pkg/logging"
pr_backend "github.com/radius-project/radius/pkg/portableresources/backend"
pr_frontend "github.com/radius-project/radius/pkg/portableresources/frontend"
"github.com/radius-project/radius/pkg/ucp/data"
"github.com/radius-project/radius/pkg/ucp/dataprovider"
"github.com/radius-project/radius/pkg/ucp/hosting"
"github.com/radius-project/radius/pkg/ucp/ucplog"

"github.com/go-logr/logr"
etcdclient "go.etcd.io/etcd/client/v3"
runtimelog "sigs.k8s.io/controller-runtime/pkg/log"
corerp_setup "github.com/radius-project/radius/pkg/corerp/setup"
)

const serviceName = "applications.core"

func newPortableResourceHosts(configFile string, enableAsyncWorker bool) ([]hosting.Service, *hostoptions.HostOptions, error) {
hostings := []hosting.Service{}
options, err := hostoptions.NewHostOptionsFromEnvironment(configFile)
if err != nil {
return nil, nil, err
}
hostings = append(hostings, pr_frontend.NewService(options))
if enableAsyncWorker {
hostings = append(hostings, pr_backend.NewService(options))
}

return hostings, &options, nil
}
const serviceName = "radius"

func main() {
var configFile string
var enableAsyncWorker bool

var runPortableResource bool
var portableResourceConfigFile string

defaultConfig := fmt.Sprintf("radius-%s.yaml", hostoptions.Environment())
flag.StringVar(&configFile, "config-file", defaultConfig, "The service configuration file.")
flag.BoolVar(&enableAsyncWorker, "enable-asyncworker", true, "Flag to run async request process worker (for private preview and dev/test purpose).")

flag.BoolVar(&runPortableResource, "run-portableresource", true, "Flag to run portable resources RPs(for private preview and dev/test purpose).")
defaultPortableRsConfig := fmt.Sprintf("portableresource-%s.yaml", hostoptions.Environment())
flag.StringVar(&portableResourceConfigFile, "portableresource-config", defaultPortableRsConfig, "The service configuration file for portable resource providers.")

pflag.StringVar(&configFile, "config-file", defaultConfig, "The service configuration file.")
if configFile == "" {
log.Fatal("config-file is empty.") //nolint:forbidigo // this is OK inside the main function.
}

flag.Parse()
var portableResourceConfigFile string
rynowak marked this conversation as resolved.
Show resolved Hide resolved
defaultPortableRsConfig := fmt.Sprintf("portableresource-%s.yaml", hostoptions.Environment())
pflag.StringVar(&portableResourceConfigFile, "portableresource-config", defaultPortableRsConfig, "The service configuration file for portable resource providers.")

pflag.Parse()

options, err := hostoptions.NewHostOptionsFromEnvironment(configFile)
if err != nil {
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
hostingSvc := []hosting.Service{frontend.NewService(options)}

hostingSvc := []hosting.Service{}

metricOptions := metricsservice.NewHostOptionsFromEnvironment(*options.Config)
metricOptions.Config.ServiceName = serviceName
Expand All @@ -99,7 +81,7 @@ func main() {
hostingSvc = append(hostingSvc, profilerservice.NewService(profilerOptions))
}

logger, flush, err := ucplog.NewLogger(logging.AppCoreLoggerName, &options.Config.Logging)
logger, flush, err := ucplog.NewLogger(serviceName, &options.Config.Logging)
if err != nil {
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
Expand All @@ -108,22 +90,10 @@ func main() {
// Must set the logger before using controller-runtime.
runtimelog.SetLogger(logger)

if enableAsyncWorker {
logger.Info("Enable AsyncRequestProcessWorker.")
hostingSvc = append(hostingSvc, backend.NewService(options))
}

// Configure Portable Resources to run it with Applications.Core RP.
var portableResourceOpts *hostoptions.HostOptions
if runPortableResource && portableResourceConfigFile != "" {
logger.Info("Run Service for Portable Resource Providers.")
var portableResourceSvcs []hosting.Service
var err error
portableResourceSvcs, portableResourceOpts, err = newPortableResourceHosts(portableResourceConfigFile, enableAsyncWorker)
if err != nil {
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}
hostingSvc = append(hostingSvc, portableResourceSvcs...)
// Load portable resource config.
prOptions, err := hostoptions.NewHostOptionsFromEnvironment(portableResourceConfigFile)
if err != nil {
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}

if options.Config.StorageProvider.Provider == dataprovider.TypeETCD &&
Expand All @@ -135,13 +105,31 @@ func main() {
client := hosting.NewAsyncValue[etcdclient.Client]()
options.Config.StorageProvider.ETCD.Client = client
options.Config.SecretProvider.ETCD.Client = client
if portableResourceOpts != nil {
portableResourceOpts.Config.StorageProvider.ETCD.Client = client
portableResourceOpts.Config.SecretProvider.ETCD.Client = client
}

// Portable resource options
rynowak marked this conversation as resolved.
Show resolved Hide resolved
prOptions.Config.StorageProvider.ETCD.Client = client
prOptions.Config.SecretProvider.ETCD.Client = client

hostingSvc = append(hostingSvc, data.NewEmbeddedETCDService(data.EmbeddedETCDServiceOptions{ClientConfigSink: client}))
}

builders, err := builders(options)
if err != nil {
log.Fatal(err) //nolint:forbidigo // this is OK inside the main function.
}

hostingSvc = append(
hostingSvc,
server.NewAPIService(options, builders),
server.NewAsyncWorker(options, builders),

// Configure Portable Resources to run it with Applications.Core RP.
//
// This is temporary until we migrate these resources to use the new registration model.
pr_frontend.NewService(prOptions),
pr_backend.NewService(prOptions),
)

loggerValues := []any{}
host := &hosting.Host{
Services: hostingSvc,
Expand Down Expand Up @@ -190,3 +178,15 @@ func main() {
panic(err)
}
}

func builders(options hostoptions.HostOptions) ([]builder.Builder, error) {
config, err := controllerconfig.New(options)
if err != nil {
return nil, err
}

return []builder.Builder{
corerp_setup.SetupNamespace(config).GenerateBuilder(),
// Add resource provider builders...
}, nil
}
1 change: 1 addition & 0 deletions cmd/applications-rp/portableresource-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ storageProvider:
inmemory: true
queueProvider:
provider: inmemory
name: radiusportable
profilerProvider:
enabled: true
port: 6060
Expand Down
1 change: 1 addition & 0 deletions cmd/applications-rp/portableresource-self-hosted.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ storageProvider:
namespace: 'radius-testing'
queueProvider:
provider: "apiserver"
name: radiusportable
apiserver:
context: ''
namespace: 'radius-testing'
Expand Down
1 change: 1 addition & 0 deletions cmd/applications-rp/radius-cloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ storageProvider:
masterKey: set-me-in-a-different-way
queueProvider:
provider: inmemory
name: radius
rynowak marked this conversation as resolved.
Show resolved Hide resolved
profilerProvider:
enabled: true
port: 6060
Expand Down
1 change: 1 addition & 0 deletions cmd/applications-rp/radius-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ storageProvider:
inmemory: true
queueProvider:
provider: inmemory
name: radius
profilerProvider:
enabled: true
port: 6060
Expand Down
1 change: 1 addition & 0 deletions cmd/applications-rp/radius-self-hosted.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ storageProvider:
namespace: 'radius-testing'
queueProvider:
provider: "apiserver"
name: radius
apiserver:
context: ''
namespace: 'radius-testing'
Expand Down
1 change: 1 addition & 0 deletions cmd/ucpd/ucp-self-hosted-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ secretProvider:

queueProvider:
provider: "apiserver"
name: 'ucp'
apiserver:
context: ''
namespace: 'radius-testing'
Expand Down
2 changes: 2 additions & 0 deletions deploy/Chart/templates/rp/configmaps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data:
namespace: "radius-system"
queueProvider:
provider: "apiserver"
name: "radius"
apiserver:
context: ""
namespace: "radius-system"
Expand Down Expand Up @@ -66,6 +67,7 @@ data:
namespace: "radius-system"
queueProvider:
provider: "apiserver"
name: "radiusportable"
apiserver:
context: ""
namespace: "radius-system"
Expand Down
1 change: 0 additions & 1 deletion deploy/Chart/templates/rp/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ spec:
image: "{{ .Values.rp.image }}:{{ .Values.rp.tag | default $appversion }}"
args:
- --config-file=/etc/config/radius-self-host.yaml
- --run-portableresource
- --portableresource-config=/etc/config/portableresource-self-host.yaml
env:
- name: SKIP_ARM
Expand Down
6 changes: 5 additions & 1 deletion deploy/Chart/templates/ucp/configmaps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ data:
provider: kubernetes

queueProvider:
provider: inmemory
provider: "apiserver"
name: "ucp"
apiserver:
context: ""
namespace: "radius-system"

profilerProvider:
enabled: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ storageProvider:
namespace: "radius-system"
queueProvider:
provider: "apiserver"
name: "radius"
apiserver:
context: ""
namespace: "radius-system"
Expand Down
34 changes: 15 additions & 19 deletions pkg/armrpc/api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,15 @@ var (
type OperationMethod string

var operationMethodToHTTPMethod = map[OperationMethod]string{
OperationList: http.MethodGet,
OperationGet: http.MethodGet,
OperationPut: http.MethodPut,
OperationPatch: http.MethodPatch,
OperationDelete: http.MethodDelete,
OperationPlaneScopeList: http.MethodGet,
OperationList: http.MethodGet,
OperationGet: http.MethodGet,
OperationPut: http.MethodPut,
OperationPatch: http.MethodPatch,
OperationDelete: http.MethodDelete,

// ARM RPC specific operations.
OperationGetOperations: http.MethodGet,
OperationGetOperationStatuses: http.MethodGet,
OperationGetOperationResult: http.MethodGet,
rynowak marked this conversation as resolved.
Show resolved Hide resolved
OperationPutSubscriptions: http.MethodPut,
OperationPutSubscriptions: http.MethodPut,

// Non-idempotent lifecycle operations.
OperationGetImperative: http.MethodPost,
Expand All @@ -80,16 +78,14 @@ func (o OperationMethod) HTTPMethod() string {

const (
// Predefined Operation methods.
OperationList OperationMethod = "LIST"
OperationGet OperationMethod = "GET"
OperationPut OperationMethod = "PUT"
OperationPatch OperationMethod = "PATCH"
OperationDelete OperationMethod = "DELETE"
OperationGetOperations OperationMethod = "GETOPERATIONS"
OperationGetOperationStatuses OperationMethod = "GETOPERATIONSTATUSES"
OperationGetOperationResult OperationMethod = "GETOPERATIONRESULT"
OperationPutSubscriptions OperationMethod = "PUTSUBSCRIPTIONS"
OperationPost OperationMethod = "POST"
OperationPlaneScopeList OperationMethod = "LISTPLANESCOPE"
OperationList OperationMethod = "LIST"
OperationGet OperationMethod = "GET"
OperationPut OperationMethod = "PUT"
OperationPatch OperationMethod = "PATCH"
OperationDelete OperationMethod = "DELETE"
OperationPutSubscriptions OperationMethod = "PUTSUBSCRIPTIONS"
OperationPost OperationMethod = "POST"

// Imperative operation methods for non-idempotent lifecycle operations.
// UCP extends the ARM resource lifecycle to support using POST for non-idempotent resource types.
Expand Down
Loading