@@ -2,14 +2,14 @@ package cloudmap
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model"
8
7
"github.com/aws/aws-sdk-go-v2/aws"
9
8
sd "github.com/aws/aws-sdk-go-v2/service/servicediscovery"
10
9
"github.com/aws/aws-sdk-go-v2/service/servicediscovery/types"
11
10
"github.com/go-logr/logr"
12
11
"k8s.io/apimachinery/pkg/util/cache"
12
+ "k8s.io/apimachinery/pkg/util/wait"
13
13
ctrl "sigs.k8s.io/controller-runtime"
14
14
"time"
15
15
)
@@ -86,11 +86,28 @@ func (sdc *serviceDiscoveryClient) CreateService(ctx context.Context, service *m
86
86
sdc .log .Info ("creating a new service" , "namespace" , service .Namespace , "name" , service .Name )
87
87
88
88
nsId , nsErr := sdc .getNamespaceId (ctx , service .Namespace )
89
-
90
89
if nsErr != nil {
91
90
return nsErr
92
91
}
93
92
93
+ if nsId == "" {
94
+ nsOutput , nsErr := sdc .sdApi .CreateHttpNamespace (ctx , & sd.CreateHttpNamespaceInput {
95
+ Name : & service .Namespace ,
96
+ })
97
+
98
+ if nsErr != nil {
99
+ return nsErr
100
+ }
101
+ opResult , opErr := sdc .WaitUntilSuccessOperation (ctx , nsOutput .OperationId )
102
+ if opErr != nil {
103
+ return opErr
104
+ }
105
+ nsId = opResult .Operation .Targets ["NAMESPACE" ]
106
+ sdc .namespaceIdCache .Add (
107
+ service .Namespace ,
108
+ nsId , defaultNamespaceIdCacheTTL )
109
+ }
110
+
94
111
//TODO: Handle non-http namespaces
95
112
sdSrv , srvErr := sdc .sdApi .CreateService (ctx , & sd.CreateServiceInput {
96
113
Name : & service .Name ,
@@ -106,6 +123,32 @@ func (sdc *serviceDiscoveryClient) CreateService(ctx context.Context, service *m
106
123
107
124
return sdc .RegisterEndpoints (ctx , service )
108
125
}
126
+ func (sdc * serviceDiscoveryClient ) WaitUntilSuccessOperation (ctx context.Context , operationId * string ) (* sd.GetOperationOutput , error ) {
127
+ opResult := & sd.GetOperationOutput {}
128
+ var opErr error
129
+ err := wait .PollUntil (defaultOperationPollInterval , func () (bool , error ) {
130
+ opResult , opErr = sdc .sdApi .GetOperation (ctx , & sd.GetOperationInput {
131
+ OperationId : operationId ,
132
+ })
133
+ if opErr != nil {
134
+ return true , opErr
135
+ }
136
+
137
+ if opResult .Operation .Status == types .OperationStatusFail {
138
+ return true , fmt .Errorf ("failed to create namespace.Reason: %s" , * opResult .Operation .ErrorMessage )
139
+ }
140
+
141
+ if opResult .Operation .Status == types .OperationStatusSuccess {
142
+ return true , nil
143
+ }
144
+
145
+ return false , nil
146
+ }, ctx .Done ())
147
+ if err != nil {
148
+ return nil , err
149
+ }
150
+ return opResult , nil
151
+ }
109
152
110
153
func (sdc * serviceDiscoveryClient ) GetService (ctx context.Context , namespaceName string , serviceName string ) (* model.Service , error ) {
111
154
sdc .log .Info ("fetching a service" , "namespaceName" , namespaceName , "serviceName" , serviceName )
@@ -179,7 +222,9 @@ func (sdc *serviceDiscoveryClient) getNamespaceId(ctx context.Context, nsName st
179
222
return "" , err
180
223
}
181
224
182
- sdc .namespaceIdCache .Add (nsName , nsId , defaultNamespaceIdCacheTTL )
225
+ if nsId != "" {
226
+ sdc .namespaceIdCache .Add (nsName , nsId , defaultNamespaceIdCacheTTL )
227
+ }
183
228
184
229
return nsId , err
185
230
}
@@ -201,7 +246,7 @@ func (sdc *serviceDiscoveryClient) getNamespaceIdFromCloudMap(ctx context.Contex
201
246
}
202
247
}
203
248
204
- return "" , errors . New ( fmt . Sprintf ( "namespace %s not found" , nsName ))
249
+ return "" , nil
205
250
}
206
251
207
252
func (sdc * serviceDiscoveryClient ) getServiceId (ctx context.Context , nsName string , svcName string ) (string , error ) {
@@ -244,7 +289,7 @@ func (sdc *serviceDiscoveryClient) listServicesFromCloudMap(ctx context.Context,
244
289
svcs := make ([]* types.ServiceSummary , 0 )
245
290
246
291
nsId , nsErr := sdc .getNamespaceId (ctx , nsName )
247
- if nsErr != nil {
292
+ if nsErr != nil || nsId == "" {
248
293
return svcs , nil
249
294
}
250
295
0 commit comments