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

Hack to get deployment manager credentials #34

Merged
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
36 changes: 33 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,53 @@ go 1.21
require (
github.com/PaesslerAG/jsonpath v0.1.1
github.com/gorilla/mux v1.8.0
github.com/imdario/mergo v0.3.11
github.com/itchyny/gojq v0.12.13
github.com/json-iterator/go v1.1.12
github.com/onsi/ginkgo/v2 v2.13.0
github.com/onsi/gomega v1.29.0
go.uber.org/mock v0.3.0
k8s.io/api v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/client-go v0.28.4
k8s.io/utils v0.0.0-20231121161247-cf03d44ff3cf
sigs.k8s.io/controller-runtime v0.16.3
)

require (
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/exp v0.0.0-20230118134722-a68e582fa157 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/term v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

require (
Expand All @@ -30,9 +60,9 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace
golang.org/x/net v0.18.0
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
143 changes: 139 additions & 4 deletions go.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions internal/cmd/server/start_deployment_manager_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ func (c *DeploymentManagerServerCommand) run(cmd *cobra.Command, argv []string)
"!token", backendToken,
)

// Create the transport wrapper:
transportWrapper, err := logging.NewTransportWrapper().
// Create the logging wrapper:
loggingWrapper, err := logging.NewTransportWrapper().
SetLogger(logger).
SetFlags(flags).
Build()
Expand Down Expand Up @@ -194,10 +194,11 @@ func (c *DeploymentManagerServerCommand) run(cmd *cobra.Command, argv []string)
// Create the handler:
handler, err := service.NewDeploymentManagerHandler().
SetLogger(logger).
SetTransportWrapper(transportWrapper).
SetLoggingWrapper(loggingWrapper).
SetCloudID(cloudID).
SetBackendURL(backendURL).
SetBackendToken(backendToken).
SetEnableHack(true).
Build()
if err != nil {
logger.Error(
Expand Down
28 changes: 28 additions & 0 deletions internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strings"

"github.com/PaesslerAG/jsonpath"
"github.com/itchyny/gojq"

"github.com/openshift-kni/oran-o2ims/internal/streaming"
)
Expand All @@ -41,6 +42,33 @@ func GetString(o Object, path string) (result string, err error) {
return
}

func JQString(o Object, source string) (result string, err error) {
query, err := gojq.Parse(source)
if err != nil {
return
}
code, err := gojq.Compile(query)
if err != nil {
return
}
iter := code.Run(o)
value, ok := iter.Next()
if !ok {
err = fmt.Errorf("query '%s' didn't return a value", source)
return
}
text, ok := value.(string)
if !ok {
err = fmt.Errorf(
"query '%s' returned a value of type '%T' instead of a string",
source, value,
)
return
}
result = text
return
}

func GetArray(o Object, path string) (result []any, err error) {
value, err := jsonpath.Get(path, o)
if err != nil {
Expand Down
219 changes: 219 additions & 0 deletions internal/jq/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
Copyright 2023 Red Hat Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions and limitations under the
License.
*/

package jq

import (
"fmt"
"log/slog"
"math"
"reflect"
"slices"
"strings"

"github.com/itchyny/gojq"
jsoniter "github.com/json-iterator/go"
"github.com/openshift-kni/oran-o2ims/internal/logging"
)

// Query contains the data and logic needed to evaluate a query. Instances are created using the
// Compile or Evaluate methods of the Tool type.
type Query struct {
logger *slog.Logger
source string
variables []string
code *gojq.Code
}

// Evaluate evaluates the query with on the given input. The output should be a pointer to a
// variable where the result will be stored. Optional named variables can be passed.
func (q *Query) Evaluate(input any, output any, variables ...Variable) error {
slices.SortFunc(variables, func(a, b Variable) int {
return strings.Compare(a.name, b.name)
})
names := make([]string, len(variables))
values := make([]any, len(variables))
for i, variable := range variables {
names[i] = variable.name
values[i] = variable.value
}
if !reflect.DeepEqual(names, q.variables) {
return fmt.Errorf(
"query was compiled with variables %s but used with %s",
logging.All(q.variables), logging.All(names),
)
}
return q.evaluate(input, output, values)
}

func (q *Query) evaluate(input any, output any, variables []any) error {
// Check that the output is a pointer:
outputType := reflect.TypeOf(output)
if outputType.Kind() != reflect.Pointer {
return fmt.Errorf("output should be a pointer, but it is of type '%T'", output)
}

// The library that we use expects the input to bo composed only of primitive types, slices
// and maps, but we want to support other input types, like structs. To achieve that we
// serialize the input and deserialize it again.
var tmp any
err := q.convert(input, &tmp)
if err != nil {
return err
}
input = tmp

// Run the query:
var results []any
iter := q.code.Run(input, variables...)
for {
result, ok := iter.Next()
if !ok {
break
}
err, ok = result.(error)
if ok {
return err
}
results = append(results, result)
}

// If the output isn't a slice then we take only the first result:
var result any
if outputType.Elem().Kind() == reflect.Slice {
result = results
} else {
length := len(results)
if length == 0 {
return fmt.Errorf("query produced no results")
}
if length > 1 {
q.logger.Warn(
"Query produced multiple results but output type isn't a slice, "+
"will return the first result",
slog.String("query", q.source),
slog.String("type", fmt.Sprintf("%T", output)),
slog.Any("results", results),
)
}
result = results[0]
}

// Copy the result to the output:
return q.convert(result, output)
}

func (q *Query) convert(input any, output any) error {
switch input := input.(type) {
case bool:
switch output := output.(type) {
case *bool:
*output = input
case *any:
*output = input
default:
return fmt.Errorf("can't convert boolean to %T", output)
}
case int:
switch output := output.(type) {
case *int:
*output = input
case *int32:
*output = int32(input)
case *int64:
*output = int64(input)
case *any:
*output = input
default:
return fmt.Errorf("can't convert integer to %T", output)
}
case float64:
switch output := output.(type) {
case *int:
result := math.Floor(input)
*output = int(result)
if result != input {
q.logger.Warn(
"Conversion from float to integer loses precision",
slog.Float64("input", input),
slog.Int("output", *output),
)
}
case *int32:
result := math.Floor(input)
*output = int32(result)
if result != input {
q.logger.Warn(
"Conversion from float to 32 bits integer loses precision",
slog.Float64("input", input),
slog.Int("output", int(*output)),
)
}
case *int64:
result := math.Floor(input)
*output = int64(result)
if result != input {
q.logger.Warn(
"Conversion from float to 64 bits integer loses precision",
slog.Float64("input", input),
slog.Int64("output", int64(*output)),
)
}
case *float64:
*output = input
case *any:
*output = input
default:
return fmt.Errorf("failed to convert float to %T", output)
}
case string:
switch output := output.(type) {
case *string:
*output = input
case *any:
*output = input
default:
return fmt.Errorf("failed to convert string to %T", output)
}
case []any:
switch output := output.(type) {
case *[]any:
*output = input
case *any:
*output = input
default:
return q.clone(input, output)
}
case map[string]any:
switch output := output.(type) {
case *map[string]any:
*output = input
case *any:
*output = input
default:
return q.clone(input, output)
}
default:
return q.clone(input, output)
}
return nil
}

func (q *Query) clone(input any, output any) error {
data, err := jsoniter.Marshal(input)
if err != nil {
return err
}
return jsoniter.Unmarshal(data, output)
}
41 changes: 41 additions & 0 deletions internal/jq/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 Red Hat Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing permissions and limitations under the
License.
*/

package jq

import (
"log/slog"
"testing"

. "github.com/onsi/ginkgo/v2/dsl/core"
. "github.com/onsi/gomega"
)

func TestJQ(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "JQ")
}

// Logger used for tests:
var logger *slog.Logger

var _ = BeforeSuite(func() {
// Create a logger that writes to the Ginkgo writer, so that the log messages will be
// attached to the output of the right test:
options := &slog.HandlerOptions{
Level: slog.LevelDebug,
}
handler := slog.NewJSONHandler(GinkgoWriter, options)
logger = slog.New(handler)
})
Loading