Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Use a struct for GenericMaps
Browse files Browse the repository at this point in the history
Switch GenericMap from a named `map[string]interface{}` type
to a struct with a Data field. This allows us to override the openapi
spec once, at the type level, instead of retroactively at runtime for
each new field of type GenericMap added to our API structs.

This prevents the "managedFields" log that occurs when we forget to add
new fields to the OpenAPI modification logic.

Signed-off-by: Nick Hale <4175918+njhale@users.noreply.github.com>
  • Loading branch information
njhale committed Sep 5, 2023
1 parent a8ddbcc commit fd55e32
Show file tree
Hide file tree
Showing 39 changed files with 382 additions and 325 deletions.
34 changes: 21 additions & 13 deletions integration/client/apps/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ func TestAppUpdate(t *testing.T) {
Service: "other-service2",
},
},
DeployArgs: map[string]any{
"param1": "val1",
"param2": "val2",
DeployArgs: &v1.GenericMap{

Check failure on line 168 in integration/client/apps/apps_test.go

View workflow job for this annotation

GitHub Actions / validate

cannot use &v1.GenericMap{…} (value of type *"github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1".GenericMap) as map[string]any value in struct literal
Data: map[string]any{
"param1": "val1",
"param2": "val2",
},
},
})
if err != nil {
Expand Down Expand Up @@ -227,9 +229,11 @@ func TestAppUpdate(t *testing.T) {
Service: "other-service3",
},
},
DeployArgs: map[string]any{
"param2": "val3",
"param3": "val3",
DeployArgs: &v1.GenericMap{

Check failure on line 232 in integration/client/apps/apps_test.go

View workflow job for this annotation

GitHub Actions / validate

cannot use &v1.GenericMap{…} (value of type *"github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1".GenericMap) as map[string]any value in struct literal
Data: map[string]any{
"param2": "val3",
"param3": "val3",
},
},
})
if err != nil {
Expand Down Expand Up @@ -302,10 +306,12 @@ func TestAppUpdate(t *testing.T) {
},
}, thirdApp.Spec.Links)

assert.Equal(t, v1.GenericMap{
"param1": "val1",
"param2": "val3",
"param3": "val3",
assert.Equal(t, &v1.GenericMap{
Data: map[string]any{
"param1": "val1",
"param2": "val3",
"param3": "val3",
},
}, thirdApp.Spec.DeployArgs)

assert.Equal(t, imageID2, thirdApp.Spec.Image)
Expand Down Expand Up @@ -447,8 +453,10 @@ func TestAppRun(t *testing.T) {
Target: "secretRequest",
},
},
DeployArgs: map[string]any{
"key": "value",
DeployArgs: &v1.GenericMap{

Check failure on line 456 in integration/client/apps/apps_test.go

View workflow job for this annotation

GitHub Actions / validate

cannot use &v1.GenericMap{…} (value of type *"github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1".GenericMap) as map[string]any value in struct literal (typecheck)
Data: map[string]any{
"key": "value",
},
},
PublishMode: v1.PublishModeAll,
})
Expand All @@ -461,7 +469,7 @@ func TestAppRun(t *testing.T) {
assert.Equal(t, v1.PublishModeAll, app.Spec.PublishMode)
assert.Equal(t, "volume", app.Spec.Volumes[0].Volume)
assert.Equal(t, "secret", app.Spec.Secrets[0].Secret)
assert.Equal(t, "value", app.Spec.DeployArgs["key"])
assert.Equal(t, "value", app.Spec.DeployArgs.Data["key"])
}

func TestAppRunImageVariations(t *testing.T) {
Expand Down
12 changes: 8 additions & 4 deletions integration/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,10 @@ func TestDeployParam(t *testing.T) {
},
Spec: v1.AppInstanceSpec{
Image: image.ID,
DeployArgs: map[string]any{
"someInt": 5,
DeployArgs: &v1.GenericMap{
Data: map[string]any{
"someInt": 5,
},
},
},
})
Expand Down Expand Up @@ -1470,8 +1472,10 @@ func TestCrossProjectNetworkConnection(t *testing.T) {
// run curl to test the connection
app, err := check.client.AppRun(ctx, check.imageID, &client.AppRunOptions{
Name: check.name,
DeployArgs: map[string]any{
"address": check.podIP,
DeployArgs: &v1.GenericMap{

Check failure on line 1475 in integration/run/run_test.go

View workflow job for this annotation

GitHub Actions / validate

cannot use &v1.GenericMap{…} (value of type *"github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1".GenericMap) as map[string]any value in struct literal (typecheck)
Data: map[string]any{
"address": check.podIP,
},
},
})
if err != nil {
Expand Down
18 changes: 12 additions & 6 deletions integration/secrets/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,10 @@ func TestEncryptionEndToEnd(t *testing.T) {
}

app, err := c1.AppRun(context.Background(), image.ID, &client.AppRunOptions{
DeployArgs: map[string]any{
"encdata": output,
DeployArgs: &v1.GenericMap{

Check failure on line 169 in integration/secrets/secret_test.go

View workflow job for this annotation

GitHub Actions / validate

cannot use &v1.GenericMap{…} (value of type *"github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1".GenericMap) as map[string]any value in struct literal
Data: map[string]any{
"encdata": output,
},
},
})
if err != nil {
Expand Down Expand Up @@ -201,8 +203,10 @@ func TestNamespacedDecryption(t *testing.T) {
}

app, err := c2.AppRun(context.Background(), image.ID, &client.AppRunOptions{
DeployArgs: map[string]any{
"encdata": encdata,
DeployArgs: &v1.GenericMap{

Check failure on line 206 in integration/secrets/secret_test.go

View workflow job for this annotation

GitHub Actions / validate

cannot use &v1.GenericMap{…} (value of type *"github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1".GenericMap) as map[string]any value in struct literal
Data: map[string]any{
"encdata": encdata,
},
},
})
if err != nil {
Expand Down Expand Up @@ -237,8 +241,10 @@ func TestMultiKeyDecryptionEndToEnd(t *testing.T) {
}

app, err := c1.AppRun(context.Background(), image.ID, &client.AppRunOptions{
DeployArgs: map[string]any{
"encdata": encdata,
DeployArgs: &v1.GenericMap{

Check failure on line 244 in integration/secrets/secret_test.go

View workflow job for this annotation

GitHub Actions / validate

cannot use &v1.GenericMap{…} (value of type *"github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1".GenericMap) as map[string]any value in struct literal (typecheck)
Data: map[string]any{
"encdata": encdata,
},
},
})
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions integration/services/services_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package services

import (
"encoding/json"
"fmt"
"testing"

"github.com/acorn-io/runtime/integration/helper"
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/client"
"github.com/acorn-io/runtime/pkg/labels"
"github.com/acorn-io/z"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -25,6 +28,10 @@ func TestServiceInfo(t *testing.T) {
t.Fatal(err)
}

imageJSON := string(z.MustBe(json.MarshalIndent(image, "", " ")))

fmt.Println("imageJSON:", imageJSON)

app, err := c.AppRun(ctx, image.ID, nil)
if err != nil {
t.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/api.acorn.io/v1/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ const (
EventSeverityError = internalv1.EventSeverityError
)

func Mapify(v any) (internalv1.GenericMap, error) {
return internalv1.Mapify(v)
func Mapify(data any) (internalv1.GenericMap, error) {
return internalv1.Mapify(data)
}
12 changes: 6 additions & 6 deletions pkg/apis/api.acorn.io/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,12 @@ type ImageDetails struct {
metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

// Input Params
ImageName string `json:"imageName,omitempty"`
NestedDigest string `json:"nestedDigest,omitempty"`
DeployArgs v1.GenericMap `json:"deployArgs,omitempty"`
Profiles []string `json:"profiles,omitempty"`
Auth *RegistryAuth `json:"auth,omitempty"`
IncludeNested bool `json:"includeNested,omitempty"`
ImageName string `json:"imageName,omitempty"`
NestedDigest string `json:"nestedDigest,omitempty"`
DeployArgs *v1.GenericMap `json:"deployArgs,omitempty"`
Profiles []string `json:"profiles,omitempty"`
Auth *RegistryAuth `json:"auth,omitempty"`
IncludeNested bool `json:"includeNested,omitempty"`
// NoDefaultRegistry - if true, do not assume a default registry on the image if none is specified
NoDefaultRegistry bool `json:"noDefaultRegistry,omitempty"`

Expand Down
10 changes: 8 additions & 2 deletions pkg/apis/api.acorn.io/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pkg/apis/internal.acorn.io/v1/appimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ type AppImage struct {
// ImageInstance.Name
ID string `json:"id,omitempty"`
// Name is the image name requested by the user of any format
Name string `json:"name,omitempty"`
Digest string `json:"digest,omitempty"`
Acornfile string `json:"acornfile,omitempty"`
ImageData ImagesData `json:"imageData,omitempty"`
BuildArgs GenericMap `json:"buildArgs,omitempty"`
VCS VCS `json:"vcs,omitempty"`
Name string `json:"name,omitempty"`
Digest string `json:"digest,omitempty"`
Acornfile string `json:"acornfile,omitempty"`
ImageData ImagesData `json:"imageData,omitempty"`
BuildArgs *GenericMap `json:"buildArgs,omitempty"`
VCS VCS `json:"vcs,omitempty"`
}

type VCS struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/internal.acorn.io/v1/appinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type AppInstanceSpec struct {
PublishMode PublishMode `json:"publishMode,omitempty"`
Links []ServiceBinding `json:"services,omitempty"`
Publish []PortBinding `json:"ports,omitempty"`
DeployArgs GenericMap `json:"deployArgs,omitempty"`
DeployArgs *GenericMap `json:"deployArgs,omitempty"`
Permissions []Permissions `json:"permissions,omitempty"`
ImageGrantedPermissions []Permissions `json:"imageGrantedPermissions,omitempty"`
AutoUpgrade *bool `json:"autoUpgrade,omitempty"`
Expand Down
16 changes: 8 additions & 8 deletions pkg/apis/internal.acorn.io/v1/appspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ const (
type ChangeType string

type AcornBuild struct {
OriginalImage string `json:"originalImage,omitempty"`
Context string `json:"context,omitempty"`
Acornfile string `json:"acornfile,omitempty"`
BuildArgs GenericMap `json:"buildArgs,omitempty"`
OriginalImage string `json:"originalImage,omitempty"`
Context string `json:"context,omitempty"`
Acornfile string `json:"acornfile,omitempty"`
BuildArgs *GenericMap `json:"buildArgs,omitempty"`
}

type Build struct {
Expand Down Expand Up @@ -726,7 +726,7 @@ type Acorn struct {
Image string `json:"image,omitempty"`
Build *AcornBuild `json:"build,omitempty"`
Profiles []string `json:"profiles,omitempty"`
DeployArgs GenericMap `json:"deployArgs,omitempty"`
DeployArgs *GenericMap `json:"deployArgs,omitempty"`
Publish PortBindings `json:"publish,omitempty"`
PublishMode PublishMode `json:"publishMode,omitempty"`
Environment NameValues `json:"environment,omitempty"`
Expand All @@ -749,7 +749,7 @@ type Secret struct {
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"type,omitempty"`
Params GenericMap `json:"params,omitempty"`
Params *GenericMap `json:"params,omitempty"`
Data map[string]string `json:"data,omitempty"`
}

Expand Down Expand Up @@ -791,11 +791,11 @@ type Service struct {
Address string `json:"address,omitempty"`
Ports Ports `json:"ports,omitempty"`
Container string `json:"container,omitempty"`
Data GenericMap `json:"data,omitempty"`
Data *GenericMap `json:"data,omitempty"`
Generated *GeneratedService `json:"generated,omitempty"`
Image string `json:"image,omitempty"`
Build *AcornBuild `json:"build,omitempty"`
ServiceArgs GenericMap `json:"serviceArgs,omitempty"`
ServiceArgs *GenericMap `json:"serviceArgs,omitempty"`
Environment NameValues `json:"environment,omitempty"`
Secrets SecretBindings `json:"secrets,omitempty"`
Links ServiceBindings `json:"links,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/internal.acorn.io/v1/appstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type ServiceStatus struct {
CommonStatus `json:",inline"`
Default bool `json:"default,omitempty"`
Ports Ports `json:"ports,omitempty"`
Data GenericMap `json:"data,omitempty"`
Data *GenericMap `json:"data,omitempty"`
Consumer *ServiceConsumer `json:"consumer,omitempty"`
Secrets []string `json:"secrets,omitempty"`
Address string `json:"address,omitempty"`
Expand Down
14 changes: 7 additions & 7 deletions pkg/apis/internal.acorn.io/v1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ type AcornImageBuildInstance struct {
}

type AcornImageBuildInstanceSpec struct {
ContextCacheKey string `json:"contextCacheKey,omitempty"`
BuilderName string `json:"builderName,omitempty" wrangler:"required"`
Acornfile string `json:"acornfile,omitempty"`
Profiles []string `json:"profiles,omitempty"`
Platforms []Platform `json:"platforms,omitempty"`
Args GenericMap `json:"args,omitempty"`
VCS VCS `json:"vcs,omitempty"`
ContextCacheKey string `json:"contextCacheKey,omitempty"`
BuilderName string `json:"builderName,omitempty" wrangler:"required"`
Acornfile string `json:"acornfile,omitempty"`
Profiles []string `json:"profiles,omitempty"`
Platforms []Platform `json:"platforms,omitempty"`
Args *GenericMap `json:"args,omitempty"`
VCS VCS `json:"vcs,omitempty"`
}

type AcornImageBuildInstanceStatus struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/internal.acorn.io/v1/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type EventInstance struct {
// but can be used to hold any data related to the event.
//
// +optional
Details GenericMap `json:"details,omitempty"`
Details *GenericMap `json:"details,omitempty"`
}

// GetObserved returns the time that the Event was first observed.
Expand Down

0 comments on commit fd55e32

Please sign in to comment.