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

[WIP] Demo scripts #35

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.log
/__debug*
/oran-o2ims
/template.json
/token
/vendor
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"start",
"metadata-server",
"--log-level=debug",
"--cloud-id=6575154c-72fc-4ed8-9a87-a81885ab38bb"
"--cloud-id=6575154c-72fc-4ed8-9a87-a81885ab38bb",
"--external-address=https://o2ims.example.com"
]
},
{
Expand All @@ -36,7 +37,8 @@
"--log-level=debug",
"--cloud-id=6575154c-72fc-4ed8-9a87-a81885ab38bb",
"--backend-url=${env:BACKEND_URL}",
"--backend-token=${env:BACKEND_TOKEN}"
"--backend-token=${env:BACKEND_TOKEN}",
"--extensions={ \"country\": .metadata.labels[\"country\"], \"version\": .metadata.labels[\"openshiftVersion\"] }"
]
},
{
Expand Down
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ deps-update:
hack/update_deps.sh
hack/install_test_deps.sh

.PHONY: template.json
template.json: template.yaml
oc process \
--filename="$<" \
--local="true" \
--ignore-unknown-parameters="true" \
--param="IMAGE=$(image_repo):$(image_tag)" \
--param="INGRESS_CLASS=openshift-default" \
--param="INGRESS_HOST=$$(oc get ingresscontroller -n openshift-ingress-operator default -o json | jq -r '.status.domain')" \
--param="BACKEND_TOKEN=$$(oc create token -n multicluster-global-hub multicluster-global-hub-manager --duration=24h)" \
> "$@"

.PHONY: deploy
deploy: template.json
oc project "o2ims" || oc new-project "o2ims"
oc apply --filename="$<"

.PHONY: undeploy
undeploy: template.json
oc delete --filename="$<" --ignore-not-found="true"

.PHONY: ci-job
ci-job: deps-update lint fmt test

Expand Down
16 changes: 16 additions & 0 deletions get
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# Make sure we have a bearer token:
if [ ! -f "token" ]
then
oc create token -n o2ims client --duration=24h > token
fi

# Send the request:
curl \
--insecure \
--silent \
--header "Authorization: Bearer $(cat token)" \
"https://o2ims.apps.global-hub.oran.home.arpa$1" \
| \
jq
4 changes: 3 additions & 1 deletion internal/cmd/server/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ package server

// Names of command line flags:
const (
cloudIDFlagName = "cloud-id"
cloudIDFlagName = "cloud-id"
externalAddressFlag = "external-address"
extensionsFlagName = "extensions"
)
20 changes: 18 additions & 2 deletions internal/cmd/server/start_deployment_manager_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ func DeploymentManagerServer() *cobra.Command {
"",
"Token for authenticating to the backend server.",
)
_ = flags.StringArray(
extensionsFlagName,
[]string{},
"Extension to add to deployment managers.",
)
return result
}

Expand Down Expand Up @@ -138,10 +143,20 @@ func (c *DeploymentManagerServerCommand) run(cmd *cobra.Command, argv []string)
)
return exit.Error(1)
}
extensions, err := flags.GetStringArray(extensionsFlagName)
if err != nil {
logger.Error(
"Failed to extension flag",
"flag", extensionsFlagName,
"error", err.Error(),
)
return exit.Error(1)
}
logger.Info(
"Backend details",
"url", backendURL,
"!token", backendToken,
slog.String("url", backendURL),
slog.String("!token", backendToken),
slog.Any("extensions", extensions),
)

// Create the logging wrapper:
Expand Down Expand Up @@ -196,6 +211,7 @@ func (c *DeploymentManagerServerCommand) run(cmd *cobra.Command, argv []string)
SetLogger(logger).
SetLoggingWrapper(loggingWrapper).
SetCloudID(cloudID).
SetExtensions(extensions...).
SetBackendURL(backendURL).
SetBackendToken(backendToken).
SetEnableHack(true).
Expand Down
23 changes: 22 additions & 1 deletion internal/cmd/server/start_metadata_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func MetadataServer() *cobra.Command {
"",
"O-Cloud identifier.",
)
_ = flags.String(
externalAddressFlag,
"",
"External address.",
)
return result
}

Expand Down Expand Up @@ -87,7 +92,22 @@ func (c *MetadataServerCommand) run(cmd *cobra.Command, argv []string) error {
}
logger.Info(
"Cloud identifier",
"value", cloudID,
slog.String("value", cloudID),
)

// Get the external address:
externalAddress, err := flags.GetString(externalAddressFlag)
if err != nil {
logger.Error(
"Failed to get external address flag",
slog.String("flag", externalAddressFlag),
slog.String("error", err.Error()),
)
return exit.Error(1)
}
logger.Info(
"External address",
slog.String("value", externalAddress),
)

// Create the router:
Expand Down Expand Up @@ -135,6 +155,7 @@ func (c *MetadataServerCommand) run(cmd *cobra.Command, argv []string) error {
cloudInfoHandler, err := service.NewCloudInfoHandler().
SetLogger(logger).
SetCloudID(cloudID).
SetExternalAddress(externalAddress).
Build()
if err != nil {
logger.Error(
Expand Down
3 changes: 1 addition & 2 deletions internal/jq/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"errors"
"fmt"
"log/slog"
"reflect"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -98,7 +97,7 @@ func (t *Tool) lookup(source string, variables []string) (result *Query, err err
if !ok {
return
}
if !reflect.DeepEqual(variables, query.variables) {
if !slices.Equal(variables, query.variables) {
err = fmt.Errorf(
"query was compiled with variables %s but used with %s",
logging.All(query.variables), logging.All(variables),
Expand Down
1 change: 0 additions & 1 deletion internal/search/projector_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (e *ProjectorEvaluator) setPath(path []string, object map[string]any, value
if ok {
switch next := next.(type) {
case map[string]any:
next[head] = value
return e.setPath(tail, next, value)
default:
empty := map[string]any{}
Expand Down
22 changes: 22 additions & 0 deletions internal/search/projector_evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,27 @@ var _ = Describe("Projector evaluator", func() {
},
},
),
Entry(
"Multiple subfields",
"extensions/country,extensions/version,extensions/hub",
func() any {
return map[string]any{
"extensions": map[string]any{
"cert": "my-cert",
"key": "my-key",
"country": "ES",
"version": "1.2.3",
"hub": "hub0",
},
}
},
map[string]any{
"extensions": map[string]any{
"country": "ES",
"version": "1.2.3",
"hub": "hub0",
},
},
),
)
})
3 changes: 3 additions & 0 deletions internal/search/selector_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func (e *SelectorEvaluator) evaluateEq(value any, args []any) (result bool,
)
return
}
if value == nil {
return
}
args, err = e.convertArgs(value, args)
if err != nil {
return
Expand Down
23 changes: 16 additions & 7 deletions internal/service/cloud_info_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ import (
// CloudInfoHandlerBuilder contains the data and logic needed to create a new handler for the application
// root. Don't create instances of this type directly, use the NewCloudInfoHandler function instead.
type CloudInfoHandlerBuilder struct {
logger *slog.Logger
cloudID string
logger *slog.Logger
cloudID string
externalAddress string
}

// RootHander knows how to respond to requests for the application root. Don't create instances of
// this type directly, use the NewCloudInfoHandler function instead.
type CloudInfoHandler struct {
logger *slog.Logger
cloudID string
logger *slog.Logger
cloudID string
externalAddress string
}

// NewCloudInfoHandler creates a builder that can then be used to configure and create a handler for the
Expand All @@ -54,6 +56,12 @@ func (b *CloudInfoHandlerBuilder) SetCloudID(value string) *CloudInfoHandlerBuil
return b
}

// SetExternalAddress set the URL of the service as seen by external users.
func (b *CloudInfoHandlerBuilder) SetExternalAddress(value string) *CloudInfoHandlerBuilder {
b.externalAddress = value
return b
}

// Build uses the data stored in the builder to create and configure a new handler.
func (b *CloudInfoHandlerBuilder) Build() (result *CloudInfoHandler, err error) {
// Check parameters:
Expand All @@ -68,8 +76,9 @@ func (b *CloudInfoHandlerBuilder) Build() (result *CloudInfoHandler, err error)

// Create and populate the object:
result = &CloudInfoHandler{
logger: b.logger,
cloudID: b.cloudID,
logger: b.logger,
cloudID: b.cloudID,
externalAddress: b.externalAddress,
}
return
}
Expand All @@ -83,7 +92,7 @@ func (h *CloudInfoHandler) Get(ctx context.Context, request *GetRequest) (respon
"globalCloudId": h.cloudID,
"name": "OpenShift O-Cloud",
"description": "OpenShift O-Cloud",
"serviceUri": "https://localhost:8080",
"serviceUri": h.externalAddress,
"extensions": data.Object{},
},
}
Expand Down
50 changes: 50 additions & 0 deletions internal/service/deployment_manager_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"maps"
"net/http"
neturl "net/url"
"slices"
"sync"

"github.com/imdario/mergo"
Expand All @@ -46,6 +47,7 @@ type DeploymentManagerHandlerBuilder struct {
logger *slog.Logger
loggingWrapper func(http.RoundTripper) http.RoundTripper
cloudID string
extensions []string
backendURL string
backendToken string
enableHack bool
Expand All @@ -58,6 +60,7 @@ type DeploymentManagerHandler struct {
logger *slog.Logger
loggingWrapper func(http.RoundTripper) http.RoundTripper
cloudID string
extensions []string
backendURL string
backendToken string
backendClient *http.Client
Expand Down Expand Up @@ -98,6 +101,12 @@ func (b *DeploymentManagerHandlerBuilder) SetCloudID(
return b
}

// SetExtensions sets the fields that will be added to the extensions.
func (b *DeploymentManagerHandlerBuilder) SetExtensions(values ...string) *DeploymentManagerHandlerBuilder {
b.extensions = values
return b
}

// SetBackendURL sets the URL of the backend server This is mandatory..
func (b *DeploymentManagerHandlerBuilder) SetBackendToken(
value string) *DeploymentManagerHandlerBuilder {
Expand Down Expand Up @@ -186,6 +195,14 @@ func (b *DeploymentManagerHandlerBuilder) Build() (
return
}

// Check that extensions are at least sintactically valid:
for _, extension := range b.extensions {
_, err = jqTool.Compile(extension)
if err != nil {
return
}
}

// Create the Kubernetes API client only if the hack is enabled:
var globalHubClient *k8s.Client
if b.enableHack {
Expand All @@ -203,6 +220,7 @@ func (b *DeploymentManagerHandlerBuilder) Build() (
logger: b.logger,
loggingWrapper: b.loggingWrapper,
cloudID: b.cloudID,
extensions: slices.Clone(b.extensions),
backendURL: b.backendURL,
backendToken: b.backendToken,
backendClient: backendClient,
Expand Down Expand Up @@ -366,6 +384,38 @@ func (h *DeploymentManagerHandler) mapItem(ctx context.Context,
return
}

// Add the extensions:
for _, extension := range h.extensions {
var value any
err = h.jqTool.Evaluate(extension, input, &value)
if err != nil {
h.logger.Error(
"Failed to evaluate extension",
slog.String("cluster", cluster),
slog.String("extension", extension),
slog.String("error", err.Error()),
)
}
if value != nil {
err = mergo.Merge(
&output,
data.Object{
"extensions": value,
},
mergo.WithOverride,
)
if err != nil {
h.logger.Warn(
"Failed to merge extension",
slog.String("cluster", cluster),
slog.String("extension", extension),
slog.String("error", err.Error()),
)
err = nil
}
}
}

// Add the profile:
profile, err := h.getProfile(ctx, hub, cluster)
if err != nil {
Expand Down
Loading