Skip to content

Commit 44ef254

Browse files
committed
refactor: make public networking flag negatable
1 parent 830f931 commit 44ef254

File tree

12 files changed

+209
-48
lines changed

12 files changed

+209
-48
lines changed

create/cloudvm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (cmd *cloudVMCmd) newCloudVM(namespace string) (*infrastructure.CloudVirtua
106106
}
107107

108108
b, err := io.ReadAll(file)
109-
if file != nil {
109+
if err != nil {
110110
return nil, fmt.Errorf("error reading public keys file: %w", err)
111111
}
112112
keys = append(keys, string(b))

create/keyvaluestore.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ import (
1515

1616
type keyValueStoreCmd struct {
1717
resourceCmd
18-
Location meta.LocationName `placeholder:"${keyvaluestore_location_default}" help:"Where the Key-Value Store instance is created. Available locations are: ${keyvaluestore_location_options}"`
19-
MemorySize *storage.KeyValueStoreMemorySize `placeholder:"${keyvaluestore_memorysize_default}" help:"Available amount of memory."`
20-
MaxMemoryPolicy storage.KeyValueStoreMaxMemoryPolicy `placeholder:"${keyvaluestore_maxmemorypolicy_default}" help:"Behaviour when the memory limit is reached."`
21-
AllowedCidrs []meta.IPv4CIDR `placeholder:"203.0.113.1/32" help:"IP addresses allowed to connect to the public endpoint."`
22-
PublicNetworkingEnabled *bool `help:"If public networking is \"false\", it is only possible to access the service by configuring a service connection."`
18+
Location meta.LocationName `placeholder:"${keyvaluestore_location_default}" help:"Where the Key-Value Store instance is created. Available locations are: ${keyvaluestore_location_options}"`
19+
MemorySize *storage.KeyValueStoreMemorySize `placeholder:"${keyvaluestore_memorysize_default}" help:"Available amount of memory."`
20+
MaxMemoryPolicy storage.KeyValueStoreMaxMemoryPolicy `placeholder:"${keyvaluestore_maxmemorypolicy_default}" help:"Behaviour when the memory limit is reached."`
21+
AllowedCidrs []meta.IPv4CIDR `placeholder:"203.0.113.1/32" help:"IP addresses allowed to connect to the public endpoint."`
22+
PublicNetworking *bool `negatable:"" help:"Enable or disable public networking. Enabled by default."`
23+
24+
// Deprecated Flags
25+
PublicNetworkingEnabled *bool `hidden:""`
2326
}
2427

2528
func (cmd *keyValueStoreCmd) Run(ctx context.Context, client *api.Client) error {
@@ -65,6 +68,11 @@ func KeyValueStoreKongVars() kong.Vars {
6568
func (cmd *keyValueStoreCmd) newKeyValueStore(namespace string) (*storage.KeyValueStore, error) {
6669
name := getName(cmd.Name)
6770

71+
publicNetworking := cmd.PublicNetworking
72+
if publicNetworking == nil {
73+
publicNetworking = cmd.PublicNetworkingEnabled
74+
}
75+
6876
keyValueStore := &storage.KeyValueStore{
6977
ObjectMeta: metav1.ObjectMeta{
7078
Name: name,
@@ -81,7 +89,7 @@ func (cmd *keyValueStoreCmd) newKeyValueStore(namespace string) (*storage.KeyVal
8189
Location: cmd.Location,
8290
MaxMemoryPolicy: cmd.MaxMemoryPolicy,
8391
AllowedCIDRs: cmd.AllowedCidrs,
84-
PublicNetworkingEnabled: cmd.PublicNetworkingEnabled,
92+
PublicNetworkingEnabled: publicNetworking,
8593
MemorySize: cmd.MemorySize,
8694
},
8795
},

create/keyvaluestore_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,51 @@ func TestKeyValueStore(t *testing.T) {
5454
AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("0.0.0.0/0")},
5555
},
5656
},
57+
{
58+
name: "publicNetworking-deprecated",
59+
create: keyValueStoreCmd{
60+
PublicNetworkingEnabled: ptr.To(true),
61+
},
62+
want: storage.KeyValueStoreParameters{
63+
PublicNetworkingEnabled: ptr.To(true),
64+
},
65+
},
5766
{
5867
name: "publicNetworking",
5968
create: keyValueStoreCmd{
69+
PublicNetworking: ptr.To(true),
70+
},
71+
want: storage.KeyValueStoreParameters{
6072
PublicNetworkingEnabled: ptr.To(true),
6173
},
74+
},
75+
{
76+
name: "publicNetworking-disabled-deprecated",
77+
create: keyValueStoreCmd{
78+
PublicNetworkingEnabled: ptr.To(false),
79+
},
80+
want: storage.KeyValueStoreParameters{
81+
PublicNetworkingEnabled: ptr.To(false),
82+
},
83+
},
84+
{
85+
name: "publicNetworking-disabled",
86+
create: keyValueStoreCmd{
87+
PublicNetworking: ptr.To(false),
88+
},
6289
want: storage.KeyValueStoreParameters{
90+
PublicNetworkingEnabled: ptr.To(false),
91+
},
92+
},
93+
{
94+
name: "publicNetworking-disabled-both",
95+
create: keyValueStoreCmd{
96+
PublicNetworking: ptr.To(false),
6397
PublicNetworkingEnabled: ptr.To(true),
6498
},
99+
want: storage.KeyValueStoreParameters{
100+
PublicNetworkingEnabled: ptr.To(false),
101+
},
65102
},
66103
}
67104
for _, tt := range tests {

create/opensearch.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import (
1616

1717
type openSearchCmd struct {
1818
resourceCmd
19-
Location meta.LocationName `placeholder:"${opensearch_location_default}" help:"Where the OpenSearch cluster is created. Available locations are: ${opensearch_location_options}"`
20-
ClusterType storage.OpenSearchClusterType `placeholder:"${opensearch_cluster_type_default}" help:"Type of cluster. Available types: ${opensearch_cluster_types}"`
21-
MachineType string `placeholder:"${opensearch_machine_type_default}" help:"Defines the sizing of an OpenSearch instance. Available types: ${opensearch_machine_types}"`
22-
AllowedCidrs []meta.IPv4CIDR `placeholder:"203.0.113.1/32" help:"IP addresses allowed to connect to the public endpoint."`
23-
PublicNetworkingEnabled *bool `help:"If public networking is \"false\", it is only possible to access the service by configuring a service connection."`
24-
BucketUsers []string `placeholder:"user1,user2" help:"BucketUsers specify the users who have read access to the OpenSearch snapshots bucket."`
19+
Location meta.LocationName `placeholder:"${opensearch_location_default}" help:"Where the OpenSearch cluster is created. Available locations are: ${opensearch_location_options}"`
20+
ClusterType storage.OpenSearchClusterType `placeholder:"${opensearch_cluster_type_default}" help:"Type of cluster. Available types: ${opensearch_cluster_types}"`
21+
MachineType string `placeholder:"${opensearch_machine_type_default}" help:"Defines the sizing of an OpenSearch instance. Available types: ${opensearch_machine_types}"`
22+
AllowedCidrs []meta.IPv4CIDR `placeholder:"203.0.113.1/32" help:"IP addresses allowed to connect to the public endpoint."`
23+
BucketUsers []string `placeholder:"user1,user2" help:"BucketUsers specify the users who have read access to the OpenSearch snapshots bucket."`
24+
PublicNetworking *bool `negatable:"" help:"Enable or disable public networking. Enabled by default."`
25+
26+
// Deprecated Flags
27+
PublicNetworkingEnabled *bool `hidden:"" help:"If public networking is \"false\", it is only possible to access the service by configuring a service connection."`
2528
}
2629

2730
func (cmd *openSearchCmd) Run(ctx context.Context, client *api.Client) error {
@@ -70,6 +73,11 @@ func OpenSearchKongVars() kong.Vars {
7073
func (cmd *openSearchCmd) newOpenSearch(namespace string) (*storage.OpenSearch, error) {
7174
name := getName(cmd.Name)
7275

76+
publicNetworking := cmd.PublicNetworking
77+
if publicNetworking == nil {
78+
publicNetworking = cmd.PublicNetworkingEnabled
79+
}
80+
7381
openSearch := &storage.OpenSearch{
7482
ObjectMeta: metav1.ObjectMeta{
7583
Name: name,
@@ -88,7 +96,7 @@ func (cmd *openSearchCmd) newOpenSearch(namespace string) (*storage.OpenSearch,
8896
ClusterType: cmd.ClusterType,
8997
AllowedCIDRs: cmd.AllowedCidrs,
9098
BucketUsers: LocalReferences(cmd.BucketUsers),
91-
PublicNetworkingEnabled: cmd.PublicNetworkingEnabled,
99+
PublicNetworkingEnabled: publicNetworking,
92100
},
93101
},
94102
}

create/opensearch_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ninech/nctl/internal/test"
1414
"github.com/stretchr/testify/require"
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
"k8s.io/utils/ptr"
1617
)
1718

1819
func TestOpenSearch(t *testing.T) {
@@ -57,6 +58,52 @@ func TestOpenSearch(t *testing.T) {
5758
AllowedCIDRs: []meta.IPv4CIDR{meta.IPv4CIDR("192.168.1.0/24")},
5859
},
5960
},
61+
{
62+
name: "publicNetworking-deprecated",
63+
create: openSearchCmd{
64+
PublicNetworkingEnabled: ptr.To(true),
65+
},
66+
want: storage.OpenSearchParameters{
67+
PublicNetworkingEnabled: ptr.To(true),
68+
},
69+
},
70+
{
71+
name: "publicNetworking",
72+
create: openSearchCmd{
73+
PublicNetworking: ptr.To(true),
74+
},
75+
want: storage.OpenSearchParameters{
76+
PublicNetworkingEnabled: ptr.To(true),
77+
},
78+
},
79+
{
80+
name: "publicNetworking-disabled-deprecated",
81+
create: openSearchCmd{
82+
PublicNetworkingEnabled: ptr.To(false),
83+
},
84+
want: storage.OpenSearchParameters{
85+
PublicNetworkingEnabled: ptr.To(false),
86+
},
87+
},
88+
{
89+
name: "publicNetworking-disabled",
90+
create: openSearchCmd{
91+
PublicNetworking: ptr.To(false),
92+
},
93+
want: storage.OpenSearchParameters{
94+
PublicNetworkingEnabled: ptr.To(false),
95+
},
96+
},
97+
{
98+
name: "publicNetworking-disabled-both",
99+
create: openSearchCmd{
100+
PublicNetworking: ptr.To(false),
101+
PublicNetworkingEnabled: ptr.To(true),
102+
},
103+
want: storage.OpenSearchParameters{
104+
PublicNetworkingEnabled: ptr.To(false),
105+
},
106+
},
60107
}
61108
for _, tt := range tests {
62109
t.Run(tt.name, func(t *testing.T) {

get/opensearch_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestOpenSearch(t *testing.T) {
190190

191191
objects := []client.Object{}
192192
for _, instance := range tt.instances {
193-
created := test.OpenSearch(instance.name, instance.project, string(meta.LocationNineES34))
193+
created := test.OpenSearch(instance.name, instance.project, meta.LocationNineES34)
194194
created.Spec.ForProvider.MachineType = instance.machineType
195195

196196
// Set cluster health status if provided

internal/test/opensearch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
)
99

10-
func OpenSearch(name, project, location string) *storage.OpenSearch {
10+
func OpenSearch(name, project string, location meta.LocationName) *storage.OpenSearch {
1111
return &storage.OpenSearch{
1212
ObjectMeta: metav1.ObjectMeta{
1313
Name: name,
@@ -21,7 +21,7 @@ func OpenSearch(name, project, location string) *storage.OpenSearch {
2121
},
2222
},
2323
ForProvider: storage.OpenSearchParameters{
24-
Location: meta.LocationName(location),
24+
Location: location,
2525
},
2626
},
2727
}

internal/test/redis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
)
99

10-
func KeyValueStore(name, project, location string) *storage.KeyValueStore {
10+
func KeyValueStore(name, project string, location meta.LocationName) *storage.KeyValueStore {
1111
return &storage.KeyValueStore{
1212
ObjectMeta: metav1.ObjectMeta{
1313
Name: name,
@@ -21,7 +21,7 @@ func KeyValueStore(name, project, location string) *storage.KeyValueStore {
2121
},
2222
},
2323
ForProvider: storage.KeyValueStoreParameters{
24-
Location: meta.LocationName(location),
24+
Location: location,
2525
},
2626
},
2727
}

update/keyvaluestore.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import (
1313

1414
type keyValueStoreCmd struct {
1515
resourceCmd
16-
MemorySize *storage.KeyValueStoreMemorySize `placeholder:"${keyvaluestore_memorysize_default}" help:"Available amount of memory."`
17-
MaxMemoryPolicy *storage.KeyValueStoreMaxMemoryPolicy `placeholder:"${keyvaluestore_maxmemorypolicy_default}" help:"Behaviour when the memory limit is reached."`
18-
AllowedCidrs *[]meta.IPv4CIDR `placeholder:"203.0.113.1/32" help:"IP addresses allowed to connect to the public endpoint."`
19-
PublicNetworkingEnabled *bool `help:"If public networking is \"false\", it is only possible to access the service by configuring a service connection."`
16+
MemorySize *storage.KeyValueStoreMemorySize `placeholder:"${keyvaluestore_memorysize_default}" help:"Available amount of memory."`
17+
MaxMemoryPolicy *storage.KeyValueStoreMaxMemoryPolicy `placeholder:"${keyvaluestore_maxmemorypolicy_default}" help:"Behaviour when the memory limit is reached."`
18+
AllowedCidrs *[]meta.IPv4CIDR `placeholder:"203.0.113.1/32" help:"IP addresses allowed to connect to the public endpoint."`
19+
PublicNetworking *bool `negatable:"" help:"Enable or disable public networking."`
20+
21+
// Deprecated Flags
22+
PublicNetworkingEnabled *bool `hidden:"" help:"If public networking is \"false\", it is only possible to access the service by configuring a service connection."`
2023
}
2124

2225
func (cmd *keyValueStoreCmd) Run(ctx context.Context, client *api.Client) error {
@@ -47,8 +50,13 @@ func (cmd *keyValueStoreCmd) applyUpdates(kvs *storage.KeyValueStore) error {
4750
if cmd.AllowedCidrs != nil {
4851
kvs.Spec.ForProvider.AllowedCIDRs = *cmd.AllowedCidrs
4952
}
50-
if cmd.PublicNetworkingEnabled != nil {
51-
kvs.Spec.ForProvider.PublicNetworkingEnabled = cmd.PublicNetworkingEnabled
53+
54+
publicNetworking := cmd.PublicNetworking
55+
if publicNetworking == nil {
56+
publicNetworking = cmd.PublicNetworkingEnabled
57+
}
58+
if publicNetworking != nil {
59+
kvs.Spec.ForProvider.PublicNetworkingEnabled = publicNetworking
5260
}
5361

5462
return nil

update/keyvaluestore_test.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package update
22

33
import (
44
"context"
5-
"reflect"
65
"testing"
76

87
meta "github.com/ninech/apis/meta/v1alpha1"
@@ -91,7 +90,7 @@ func TestKeyValueStore(t *testing.T) {
9190
},
9291
},
9392
{
94-
name: "update-public-networking",
93+
name: "disable-public-networking-deprecated",
9594
create: storage.KeyValueStoreParameters{
9695
PublicNetworkingEnabled: ptr.To(true),
9796
},
@@ -100,15 +99,37 @@ func TestKeyValueStore(t *testing.T) {
10099
PublicNetworkingEnabled: ptr.To(false),
101100
},
102101
},
102+
{
103+
name: "disable-public-networking",
104+
create: storage.KeyValueStoreParameters{
105+
PublicNetworkingEnabled: ptr.To(true),
106+
},
107+
update: keyValueStoreCmd{PublicNetworking: ptr.To(false)},
108+
want: storage.KeyValueStoreParameters{
109+
PublicNetworkingEnabled: ptr.To(false),
110+
},
111+
},
112+
{
113+
name: "disable-public-networking-both",
114+
create: storage.KeyValueStoreParameters{
115+
PublicNetworkingEnabled: ptr.To(true),
116+
},
117+
update: keyValueStoreCmd{PublicNetworking: ptr.To(false), PublicNetworkingEnabled: ptr.To(true)},
118+
want: storage.KeyValueStoreParameters{
119+
PublicNetworkingEnabled: ptr.To(false),
120+
},
121+
},
103122
}
104123
for _, tt := range tests {
105124
t.Run(tt.name, func(t *testing.T) {
125+
is := require.New(t)
126+
106127
tt.update.Name = "test-" + t.Name()
107128

108129
apiClient, err := test.SetupClient()
109-
require.NoError(t, err)
130+
is.NoError(err)
110131

111-
created := test.KeyValueStore(tt.update.Name, apiClient.Project, "nine-es34")
132+
created := test.KeyValueStore(tt.update.Name, apiClient.Project, meta.LocationNineES34)
112133
created.Spec.ForProvider = tt.create
113134
if err := apiClient.Create(ctx, created); err != nil {
114135
t.Fatalf("keyvaluestore create error, got: %s", err)
@@ -125,9 +146,7 @@ func TestKeyValueStore(t *testing.T) {
125146
t.Fatalf("expected keyvaluestore to exist, got: %s", err)
126147
}
127148

128-
if !reflect.DeepEqual(updated.Spec.ForProvider, tt.want) {
129-
t.Fatalf("expected KeyValueStore.Spec.ForProvider = %v, got: %v", updated.Spec.ForProvider, tt.want)
130-
}
149+
is.Equal(tt.want, updated.Spec.ForProvider)
131150
})
132151
}
133152
}

0 commit comments

Comments
 (0)