This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
/
service_configuration_editor.go
82 lines (66 loc) · 2.18 KB
/
service_configuration_editor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright (c) 2019 the Octant contributors. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package octant
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/vmware-tanzu/octant/internal/log"
"github.com/vmware-tanzu/octant/pkg/action"
"github.com/vmware-tanzu/octant/pkg/store"
)
// ServiceConfigurationEditor edits editors.
type ServiceConfigurationEditor struct {
store store.Store
}
var _ action.Dispatcher = (*ServiceConfigurationEditor)(nil)
// NewServiceConfigurationEditor creates an instance of ServiceConfigurationEditor.
func NewServiceConfigurationEditor(objectStore store.Store) *ServiceConfigurationEditor {
editor := &ServiceConfigurationEditor{store: objectStore}
return editor
}
// ActionName returns the name of this action.
func (s *ServiceConfigurationEditor) ActionName() string {
return "action.octant.dev/serviceEditor"
}
// Handle edits a service: Supported edits:
// * selector
func (s *ServiceConfigurationEditor) Handle(ctx context.Context, alerter action.Alerter, payload action.Payload) error {
logger := log.From(ctx).With("actionName", s.ActionName())
logger.
With("payload", payload).
Debugf("received action payload")
key, err := store.KeyFromPayload(payload)
if err != nil {
return err
}
name := key.Name
selectorsList, err := payload.StringSlice("selectors")
if err != nil {
return err
}
selector := make(map[string]string)
for i := range selectorsList {
parts := strings.SplitN(selectorsList[i], ":", 2)
if len(parts) != 2 {
return errors.Errorf("invalid selector %s", selectorsList[i])
}
selector[parts[0]] = parts[1]
}
fn := func(object *unstructured.Unstructured) error {
return unstructured.SetNestedStringMap(object.Object, selector, "spec", "selector")
}
alertType := action.AlertTypeInfo
message := fmt.Sprintf("Updated Service %q", name)
if err := s.store.Update(ctx, key, fn); err != nil {
alertType = action.AlertTypeWarning
message = fmt.Sprintf("Unable to update Service %q: %s", name, err)
}
alert := action.CreateAlert(alertType, message, action.DefaultAlertExpiration)
alerter.SendAlert(alert)
return nil
}