-
Notifications
You must be signed in to change notification settings - Fork 53
/
install.go
116 lines (100 loc) · 3.6 KB
/
install.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package backend
import (
"context"
"time"
capabilityv1 "github.com/rancher/opni/pkg/apis/capability/v1"
opnicorev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/capabilities"
"github.com/rancher/opni/pkg/capabilities/wellknown"
"github.com/rancher/opni/pkg/storage"
"github.com/rancher/opni/plugins/logging/pkg/gateway/drivers"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
func (b *LoggingBackend) CanInstall(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) {
b.WaitForInit()
err := b.canInstall(ctx)
return &emptypb.Empty{}, err
}
func (b *LoggingBackend) canInstall(ctx context.Context) error {
installState := b.ClusterDriver.GetInstallStatus(ctx)
switch installState {
case drivers.Absent:
return status.Error(codes.Unavailable, "opensearch cluster is not installed")
case drivers.Pending, drivers.Installed:
return nil
case drivers.Error:
fallthrough
default:
return status.Error(codes.Internal, "unknown opensearch cluster state")
}
}
func (b *LoggingBackend) Install(ctx context.Context, req *capabilityv1.InstallRequest) (*capabilityv1.InstallResponse, error) {
b.WaitForInit()
var warningErr error
if err := b.canInstall(ctx); err != nil {
if !req.IgnoreWarnings {
return &capabilityv1.InstallResponse{
Status: capabilityv1.InstallResponseStatus_Error,
Message: err.Error(),
}, nil
}
warningErr = err
}
if err := b.ClusterDriver.CreateCredentials(ctx, req.GetCluster()); err != nil {
if !req.IgnoreWarnings {
return &capabilityv1.InstallResponse{
Status: capabilityv1.InstallResponseStatus_Error,
Message: err.Error(),
}, nil
}
warningErr = err
}
_, err := b.StorageBackend.UpdateCluster(ctx, req.Cluster,
storage.NewAddCapabilityMutator[*opnicorev1.Cluster](capabilities.Cluster(wellknown.CapabilityLogs)),
)
if err != nil {
return nil, err
}
b.requestNodeSync(ctx, req.Cluster)
if warningErr != nil {
return &capabilityv1.InstallResponse{
Status: capabilityv1.InstallResponseStatus_Warning,
Message: warningErr.Error(),
}, nil
}
addCtx, _ := context.WithTimeout(ctx, time.Minute*5)
if err := b.waitForOpensearchClient(addCtx); err != nil {
return &capabilityv1.InstallResponse{
Status: capabilityv1.InstallResponseStatus_Error,
Message: err.Error(),
}, nil
}
cluster, err := b.MgmtClient.GetCluster(addCtx, req.Cluster)
if err != nil {
return &capabilityv1.InstallResponse{
Status: capabilityv1.InstallResponseStatus_Error,
Message: err.Error(),
}, nil
}
if err := b.OpensearchManager.AddClusterMetadata(addCtx, cluster); err != nil {
return &capabilityv1.InstallResponse{
Status: capabilityv1.InstallResponseStatus_Error,
Message: err.Error(),
}, nil
}
return &capabilityv1.InstallResponse{
Status: capabilityv1.InstallResponseStatus_Success,
}, nil
}
func (b *LoggingBackend) InstallerTemplate(context.Context, *emptypb.Empty) (*capabilityv1.InstallerTemplateResponse, error) {
return &capabilityv1.InstallerTemplateResponse{
Template: `helm install opni-agent ` +
`{{ arg "input" "Namespace" "+omitEmpty" "+default:opni-agent" "+format:-n {{ value }}" }} ` +
`oci://docker.io/rancher/opni-agent --version=0.5.4 ` +
`--set monitoring.enabled=true,token={{ .Token }},pin={{ .Pin }},address={{ arg "input" "Gateway Hostname" "+default:{{ .Address }}" }}:{{ arg "input" "Gateway Port" "+default:{{ .Port }}" }} ` +
`{{ arg "toggle" "Install Prometheus Operator" "+omitEmpty" "+default:false" "+format:--set kube-prometheus-stack.enabled={{ value }}" }} ` +
`--create-namespace`,
}, nil
}