Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Plugin Kubernetes Operator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Maksim Gorelik
Plugin Kubernetes Operator
Commits
3129b4aa
Commit
3129b4aa
authored
1 year ago
by
Maksim Gorelik
Browse files
Options
Downloads
Patches
Plain Diff
ViperGetStringSlice
parent
e0a86644
No related branches found
No related tags found
No related merge requests found
Pipeline
#46984
failed
1 year ago
Stage: build
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/utils.go
+18
-0
18 additions, 0 deletions
common/utils.go
kong/kongSync.go
+35
-19
35 additions, 19 deletions
kong/kongSync.go
with
53 additions
and
19 deletions
common/utils.go
0 → 100644
+
18
−
0
View file @
3129b4aa
package
common
import
(
"errors"
"fmt"
"github.com/spf13/viper"
)
func
ViperGetStringSlice
(
key
string
)
([]
string
,
error
)
{
// viper.GetStringSlice does not work properly https://github.com/spf13/viper/issues/380#issuecomment-445244338
var
res
[]
string
err
:=
viper
.
UnmarshalKey
(
key
,
&
res
)
if
err
!=
nil
{
return
nil
,
errors
.
Join
(
err
,
fmt
.
Errorf
(
"error reading %s from env"
,
key
))
}
else
{
return
res
,
nil
}
}
This diff is collapsed.
Click to expand it.
kong/kongSync.go
+
35
−
19
View file @
3129b4aa
...
@@ -2,6 +2,7 @@ package kong
...
@@ -2,6 +2,7 @@ package kong
import
(
import
(
"fmt"
"fmt"
"gitlab.eclipse.org/eclipse/xfsc/personal-credential-manager-cloud/plugin-kubernetes-operator/common"
"strconv"
"strconv"
"strings"
"strings"
...
@@ -18,7 +19,12 @@ var log = logger.GetLogger()
...
@@ -18,7 +19,12 @@ var log = logger.GetLogger()
func
SyncKongServices
(
services
*
[]
types
.
Metadata
)
{
func
SyncKongServices
(
services
*
[]
types
.
Metadata
)
{
adminApi
:=
viper
.
GetString
(
"KONG_ADMIN_API"
)
adminApi
:=
viper
.
GetString
(
"KONG_ADMIN_API"
)
tags
:=
viper
.
GetStringSlice
(
"PLUGIN_TAGS"
)
tags
,
err
:=
common
.
ViperGetStringSlice
(
"PLUGIN_TAGS"
)
if
err
!=
nil
{
log
.
Sugar
()
.
Errorln
(
err
)
return
}
kr
,
err
:=
kongListRoutes
(
adminApi
,
""
,
tags
)
kr
,
err
:=
kongListRoutes
(
adminApi
,
""
,
tags
)
if
err
!=
nil
||
kr
==
nil
{
if
err
!=
nil
||
kr
==
nil
{
...
@@ -56,32 +62,29 @@ func SyncKongServices(services *[]types.Metadata) {
...
@@ -56,32 +62,29 @@ func SyncKongServices(services *[]types.Metadata) {
}
}
}
}
func
getPort
(
svc
*
v1types
.
Service
)
(
string
,
error
)
{
postfix
:=
viper
.
GetString
(
"PLUGIN_PORT_NAME_POSTFIX"
)
for
_
,
port
:=
range
svc
.
Spec
.
Ports
{
if
strings
.
HasSuffix
(
port
.
Name
,
postfix
)
{
return
strconv
.
Itoa
(
int
(
port
.
Port
)),
nil
}
}
// If no port with the postfix is found, return an error
err
:=
fmt
.
Errorf
(
"port with postfix `%s` not found for service: %s"
,
postfix
,
svc
.
Name
)
return
""
,
err
}
func
SyncKongService
(
event
watch
.
EventType
,
svc
*
v1types
.
Service
,
metadata
*
types
.
Metadata
)
{
func
SyncKongService
(
event
watch
.
EventType
,
svc
*
v1types
.
Service
,
metadata
*
types
.
Metadata
)
{
adminApi
:=
viper
.
GetString
(
"KONG_ADMIN_API"
)
adminApi
:=
viper
.
GetString
(
"KONG_ADMIN_API"
)
str
:=
[]
string
{
metadata
.
Name
}
str
:=
[]
string
{
metadata
.
Name
}
name
:=
strings
.
ReplaceAll
(
strings
.
Join
(
str
,
"-"
),
" "
,
"-"
)
name
:=
strings
.
ReplaceAll
(
strings
.
Join
(
str
,
"-"
),
" "
,
"-"
)
service
,
err
:=
kongListService
(
adminApi
,
metadata
.
ServiceGuid
)
service
,
err
:=
kongListService
(
adminApi
,
metadata
.
ServiceGuid
)
tags
:=
viper
.
GetStringSlice
(
"PLUGIN_TAGS"
)
methods
:=
viper
.
GetStringSlice
(
"PLUGIN_HTTP_METHODS"
)
tags
,
err
:=
common
.
ViperGetStringSlice
(
"PLUGIN_TAGS"
)
if
err
!=
nil
{
log
.
Sugar
()
.
Errorln
(
err
)
return
}
methods
,
err
:=
common
.
ViperGetStringSlice
(
"PLUGIN_HTTP_METHODS"
)
if
err
!=
nil
{
log
.
Sugar
()
.
Errorln
(
err
)
return
}
port
,
err
:=
getPort
(
svc
)
port
,
err
:=
getPort
(
svc
)
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Sugar
()
.
Errorln
(
err
)
log
.
Sugar
()
.
Errorln
(
err
)
return
return
}
}
host
:=
svc
.
Name
+
"."
+
svc
.
Namespace
+
".svc.cluster.local"
host
:=
svc
.
Name
+
"."
+
svc
.
Namespace
+
".svc.cluster.local"
if
err
==
nil
{
if
err
==
nil
{
...
@@ -93,7 +96,7 @@ func SyncKongService(event watch.EventType, svc *v1types.Service, metadata *type
...
@@ -93,7 +96,7 @@ func SyncKongService(event watch.EventType, svc *v1types.Service, metadata *type
err
=
kongDeleteService
(
metadata
.
ServiceGuid
,
adminApi
)
err
=
kongDeleteService
(
metadata
.
ServiceGuid
,
adminApi
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Sugar
()
.
Errorln
(
"Deletion not successful
l
."
)
log
.
Sugar
()
.
Errorln
(
"Deletion not successful."
)
return
return
}
}
}
}
...
@@ -104,7 +107,7 @@ func SyncKongService(event watch.EventType, svc *v1types.Service, metadata *type
...
@@ -104,7 +107,7 @@ func SyncKongService(event watch.EventType, svc *v1types.Service, metadata *type
err
=
kongCreateRoute
(
metadata
.
ServiceGuid
,
metadata
.
RouteGuid
,
name
,
metadata
.
Route
,
adminApi
,
"PATCH"
,
tags
,
methods
)
err
=
kongCreateRoute
(
metadata
.
ServiceGuid
,
metadata
.
RouteGuid
,
name
,
metadata
.
Route
,
adminApi
,
"PATCH"
,
tags
,
methods
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Sugar
()
.
Errorln
(
"Dele
tion not successful
l
."
)
log
.
Sugar
()
.
Errorln
(
err
,
"Modifica
tion not successful."
)
return
return
}
}
}
}
...
@@ -116,9 +119,22 @@ func SyncKongService(event watch.EventType, svc *v1types.Service, metadata *type
...
@@ -116,9 +119,22 @@ func SyncKongService(event watch.EventType, svc *v1types.Service, metadata *type
err
=
kongCreateRoute
(
metadata
.
ServiceGuid
,
metadata
.
RouteGuid
,
name
,
metadata
.
Route
,
adminApi
,
"POST"
,
tags
,
methods
)
err
=
kongCreateRoute
(
metadata
.
ServiceGuid
,
metadata
.
RouteGuid
,
name
,
metadata
.
Route
,
adminApi
,
"POST"
,
tags
,
methods
)
}
}
if
err
!=
nil
{
if
err
!=
nil
{
log
.
Sugar
()
.
Errorln
(
"
Dele
tion not successful
l
."
)
log
.
Sugar
()
.
Errorln
(
"
Crea
tion not successful."
)
return
return
}
}
}
}
}
}
}
}
func
getPort
(
svc
*
v1types
.
Service
)
(
string
,
error
)
{
postfix
:=
viper
.
GetString
(
"PLUGIN_PORT_NAME_POSTFIX"
)
for
_
,
port
:=
range
svc
.
Spec
.
Ports
{
if
strings
.
HasSuffix
(
port
.
Name
,
postfix
)
{
return
strconv
.
Itoa
(
int
(
port
.
Port
)),
nil
}
}
// If no port with the postfix is found, return an error
err
:=
fmt
.
Errorf
(
"port with postfix `%s` not found for service: %s"
,
postfix
,
svc
.
Name
)
return
""
,
err
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment