-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathopensearch.go
More file actions
89 lines (76 loc) · 2.81 KB
/
Copy pathopensearch.go
File metadata and controls
89 lines (76 loc) · 2.81 KB
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
package create
import (
"context"
runtimev1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
infra "github.com/ninech/apis/infrastructure/v1alpha1"
meta "github.com/ninech/apis/meta/v1alpha1"
storage "github.com/ninech/apis/storage/v1alpha1"
"github.com/ninech/nctl/api"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
)
type openSearchCmd struct {
resourceCmd
Location string `help:"Location where the OpenSearch cluster is created." placeholder:"nine-es34"`
ClusterType storage.OpenSearchClusterType `help:"ClusterType specifies the type of OpenSearch cluster to create. Options: single, multi" placeholder:"single"`
MachineType string `help:"MachineType specifies the type of machine to use for the OpenSearch cluster." placeholder:"nine-search-s"`
AllowedCidrs []meta.IPv4CIDR `help:"AllowedCIDRs specify the allowed IP addresses, connecting to the cluster." placeholder:"203.0.113.1/32"`
BucketUsers []string `help:"BucketUsers specify the users who have read access to the OpenSearch snapshots bucket." placeholder:"user1,user2"`
}
func (cmd *openSearchCmd) Run(ctx context.Context, client *api.Client) error {
openSearch, err := cmd.newOpenSearch(client.Project)
if err != nil {
return err
}
c := newCreator(client, openSearch, "opensearch")
ctx, cancel := context.WithTimeout(ctx, cmd.WaitTimeout)
defer cancel()
if err := c.createResource(ctx); err != nil {
return err
}
if !cmd.Wait {
return nil
}
return c.wait(ctx, waitStage{
objectList: &storage.OpenSearchList{},
onResult: func(event watch.Event) (bool, error) {
if c, ok := event.Object.(*storage.OpenSearch); ok {
return isAvailable(c), nil
}
return false, nil
},
})
}
func (cmd *openSearchCmd) newOpenSearch(namespace string) (*storage.OpenSearch, error) {
name := getName(cmd.Name)
openSearch := &storage.OpenSearch{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: storage.OpenSearchSpec{
ResourceSpec: runtimev1.ResourceSpec{
WriteConnectionSecretToReference: &runtimev1.SecretReference{
Name: "opensearch-" + name,
Namespace: namespace,
},
},
ForProvider: storage.OpenSearchParameters{
Location: meta.LocationName(cmd.Location),
MachineType: infra.NewMachineType(cmd.MachineType),
ClusterType: cmd.ClusterType,
AllowedCIDRs: cmd.AllowedCidrs,
BucketUsers: LocalReferences(cmd.BucketUsers),
},
},
}
return openSearch, nil
}
// LocalReferences converts a slice of strings to []meta.LocalReference.
func LocalReferences(s []string) []meta.LocalReference {
references := make([]meta.LocalReference, len(s))
for i, user := range s {
references[i] = meta.LocalReference{Name: user}
}
return references
}