Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Allow plugins to apply yaml #2775

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
1 change: 1 addition & 0 deletions changelogs/unreleased/2775-xtreme-vikram-yadav
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
allowed Go plugins to apply yaml
1 change: 0 additions & 1 deletion internal/octant/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const (
ActionOverviewServiceEditor = "action.octant.dev/serviceEditor"
ActionDeploymentConfiguration = "action.octant.dev/deploymentConfiguration"
ActionUpdateObject = "action.octant.dev/update"
ActionApplyYaml = "action.octant.dev/apply"
)

func sendAlert(alerter action.Alerter, alertType action.AlertType, message string, expiration *time.Time) {
Expand Down
2 changes: 1 addition & 1 deletion internal/octant/apply_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewApplyYaml(logger log.Logger, objectStore store.Store) *ApplyYaml {

// ActionName returns the name of this action
func (p *ApplyYaml) ActionName() string {
return ActionApplyYaml
return action.ActionApplyYaml
}

// Handle applies the requested yaml to the cluster
Expand Down
10 changes: 5 additions & 5 deletions internal/octant/apply_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ data:

ctx := context.Background()

payload := action.CreatePayload(ActionApplyYaml, map[string]interface{}{
payload := action.CreatePayload(action.ActionApplyYaml, map[string]interface{}{
"update": update,
"namespace": "default",
})
Expand Down Expand Up @@ -164,7 +164,7 @@ data:

ctx := context.Background()

payload := action.CreatePayload(ActionApplyYaml, map[string]interface{}{
payload := action.CreatePayload(action.ActionApplyYaml, map[string]interface{}{
"update": update,
"namespace": "default",
})
Expand Down Expand Up @@ -228,7 +228,7 @@ metadata:

ctx := context.Background()

payload := action.CreatePayload(ActionApplyYaml, map[string]interface{}{
payload := action.CreatePayload(action.ActionApplyYaml, map[string]interface{}{
"update": update,
"namespace": "default",
})
Expand Down Expand Up @@ -286,7 +286,7 @@ metadata:

ctx := context.Background()

payload := action.CreatePayload(ActionApplyYaml, map[string]interface{}{
payload := action.CreatePayload(action.ActionApplyYaml, map[string]interface{}{
"update": update,
"namespace": "default",
})
Expand Down Expand Up @@ -326,7 +326,7 @@ data: {

ctx := context.Background()

payload := action.CreatePayload(ActionApplyYaml, map[string]interface{}{
payload := action.CreatePayload(action.ActionApplyYaml, map[string]interface{}{
"update": update,
"namespace": "default",
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/action/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ const (
// The ActionRequest.Payload for this action contains a single string entry `contextName` with a value
// of the new context.
RequestSetContext = "action.octant.dev/setContext"

// ActionApplyYaml is the action to apply a yaml resource configuration.
// The ActionRequest.Payload for this action contains string entry `namespace` and a string entry
// `update` containing the yaml configuration for a resource.
ActionApplyYaml = "action.octant.dev/apply"
)
19 changes: 19 additions & 0 deletions pkg/plugin/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ func TestAPI(t *testing.T) {
require.NoError(t, err)
},
},
{
name: "applyYAML",
initFunc: func(t *testing.T, mocks *apiMocks) {
mocks.objectStore.EXPECT().
CreateOrUpdateFromYAML(contextType, "default", "---\nyaml").
Return([]string{}, nil).
Do(func(ctx context.Context, namespace, yaml string) {
require.Equal(t, "bar", ctx.Value(api.DashboardMetadataKey("foo")))
})
},
doFunc: func(t *testing.T, client *api.Client) {
clientCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()

clientCtx = metadata.AppendToOutgoingContext(clientCtx, "x-octant-foo", "bar")
_, err := client.ApplyYAML(clientCtx, "default", "---\nyaml")
require.NoError(t, err)
},
},
{
name: "get",
initFunc: func(t *testing.T, mocks *apiMocks) {
Expand Down
14 changes: 14 additions & 0 deletions pkg/plugin/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ func (c *Client) Update(ctx context.Context, object *unstructured.Unstructured)
return err
}

// ApplyYAML creates or updates resource(s) based on input yaml string.
func (c *Client) ApplyYAML(ctx context.Context, namespace, yaml string) ([]string, error) {
client := c.DashboardConnection.Client()

req := &proto.ApplyYAMLRequest{
Namespace: namespace,
Yaml: yaml,
}

res, err := client.ApplyYAML(ctx, req)

return res.Resources, err
}

func (c *Client) Create(ctx context.Context, object *unstructured.Unstructured) error {
client := c.DashboardConnection.Client()

Expand Down
26 changes: 26 additions & 0 deletions pkg/plugin/api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ func TestClient_Update(t *testing.T) {
require.NoError(t, err)
}

func TestClient_ApplyYAML(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

ctx := context.Background()
namespace := "foo-namespace"
yamlString := "---\napiVersion:apps/v1"

dashboardClient := fake.NewMockDashboardClient(controller)
req := &proto.ApplyYAMLRequest{
Namespace: namespace,
Yaml: yamlString,
}
dashboardClient.EXPECT().ApplyYAML(gomock.Any(), req).Return(&proto.ApplyYAMLResponse{}, nil)

conn := fake.NewMockDashboardConnection(controller)
conn.EXPECT().Client().Return(dashboardClient)

connOpt := MockDashboardConnection(conn)

client, err := api.NewClient("address", connOpt)
require.NoError(t, err)

_, err = client.ApplyYAML(ctx, namespace, yamlString)
require.NoError(t, err)
}
func TestClient_ForceFrontendUpdate(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
Expand Down
15 changes: 15 additions & 0 deletions pkg/plugin/api/fake/mock_dash_service.go

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

20 changes: 20 additions & 0 deletions pkg/plugin/api/fake/mock_dashboard_client.go

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

Loading