Skip to content

Commit b7b4b0b

Browse files
feat: support a single namespace deployment
Signed-off-by: Ricky Moorhouse <[email protected]>
1 parent 555822f commit b7b4b0b

File tree

9 files changed

+67
-65
lines changed

9 files changed

+67
-65
lines changed

deployment/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ logging:
77
nets:
88
apiconnect:
99
enabled: true
10+
namespace: apic-namespace
1011
analytics:
1112
enabled: true
1213
namespace: analytics-namespace
@@ -27,4 +28,4 @@ nets:
2728
manager:
2829
enabled: true
2930
process_org_metrics: false
30-
namespace: manager-apic
31+
namespace: manager-namespace

deployment/deployment.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ spec:
2222
- env:
2323
- name: MGMT_CREDS
2424
value: /app/management
25+
- name: DP_CREDS
26+
value: /app/datapower
2527
- name: ANALYTICS_CERTS
2628
value: /app/analytics
2729
- name: SECURE
2830
value: 'true'
2931
- name: CERT_PATH
3032
value: /app/certs
31-
- name: CONFIG_PATH
32-
value: /app/config/config.yaml
33-
image: $IMAGE
33+
image: ghcr.io/ibm/apiconnect-trawler/trawler:main
3434
imagePullPolicy: Always
3535
name: trawler
3636
ports:
@@ -49,10 +49,10 @@ spec:
4949
readOnlyRootFilesystem: true
5050
runAsNonRoot: true
5151
volumeMounts:
52-
- mountPath: /app/config
52+
- mountPath: /config
5353
name: trawler-config
5454
- mountPath: /app/management
55-
name: trawler-creds
55+
name: mgmt-creds
5656
- mountPath: /app/datapower
5757
name: datapower-creds
5858
# Update the following for your analytics deployment (replace analytics with your subsystem name)
@@ -71,7 +71,7 @@ spec:
7171
name: trawler-config
7272
optional: true
7373
name: trawler-config
74-
- name: trawler-creds
74+
- name: mgmt-creds
7575
secret:
7676
optional: true
7777
secretName: trawler-mgmt-creds

deployment/kustomization.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ resources:
88
- certificate.yaml
99
- deployment.yaml
1010
- serviceaccount.yaml
11-
- clusterrole.yaml
12-
- clusterrolebinding.yaml
1311
- networkpolicy_trawler-a7s.yaml
1412
- networkpolicy_trawler.yaml
1513
- secret-mgmt.yaml
1614
- secret-dp.yaml
15+
# Uncomment cluster role and comment out role if you want to give cluster wide permissions
16+
- clusterrole.yaml
17+
- clusterrolebinding.yaml
18+
# - role.yaml
19+
# - rolebinding.yaml
1720
# Uncomment the following if you are using prometheus-operator
1821
# - service.yaml
1922
# - servicemonitor.yaml

deployment/role.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: Role
4+
metadata:
5+
name: trawler
6+
rules:
7+
- apiGroups:
8+
- ''
9+
resources:
10+
- pods
11+
- services
12+
- secrets
13+
verbs:
14+
- get
15+
- list
16+
- apiGroups:
17+
- management.apiconnect.ibm.com
18+
- portal.apiconnect.ibm.com
19+
- gateway.apiconnect.ibm.com
20+
- analytics.apiconnect.ibm.com
21+
resources:
22+
- managementclusters
23+
- analyticsclusters
24+
- portalclusters
25+
- gatewayclusters
26+
verbs:
27+
- get
28+
- list

deployment/rolebinding.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
kind: RoleBinding
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
metadata:
4+
name: trawler
5+
subjects:
6+
- kind: ServiceAccount
7+
name: trawler
8+
roleRef:
9+
kind: Role
10+
name: trawler
11+
apiGroup: rbac.authorization.k8s.io

nets/analytics/analytics.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package analytics
22

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
"nets"
@@ -12,8 +11,6 @@ import (
1211
"github.com/IBM/alchemy-logging/src/go/alog"
1312
"github.com/prometheus/client_golang/prometheus"
1413
"github.com/prometheus/client_golang/prometheus/promauto"
15-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16-
"k8s.io/apimachinery/pkg/runtime/schema"
1714
"k8s.io/client-go/dynamic"
1815
)
1916

@@ -26,6 +23,7 @@ type AnalyticsNetConfig struct {
2623
Enabled bool `yaml:"enabled"`
2724
Frequency int `yaml:"frequency"`
2825
Host string `yaml:"host"`
26+
Namespace string `yaml:"namespace"`
2927
}
3028

3129
type ClusterHealth struct {
@@ -204,17 +202,8 @@ func (a *Analytics) ingestionStats(analytics_url string, analyticsName string, a
204202
}
205203

206204
func (a *Analytics) findAnalytics(dynamicClient dynamic.DynamicClient) error {
207-
a7s_gvr := schema.GroupVersionResource{
208-
Group: "analytics.apiconnect.ibm.com",
209-
Version: "v1beta1",
210-
Resource: "analyticsclusters",
211-
}
205+
a7ss := nets.GetCustomResourceList("analytics.apiconnect.ibm.com", "v1beta1", "analyticsclusters", a.Config.Namespace)
212206

213-
a7ss, err := dynamicClient.Resource(a7s_gvr).List(context.Background(), v1.ListOptions{})
214-
if err != nil {
215-
log.Log(alog.ERROR, "error getting analyticscluster: %v\n", err)
216-
return err
217-
}
218207
for _, a7s := range a7ss.Items {
219208
analyticsName := a7s.Object["metadata"].(map[string]interface{})["name"].(string)
220209
analyticsNamespace := a7s.Object["metadata"].(map[string]interface{})["namespace"].(string)

nets/apiconnect/apiconnect.go

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package apiconnect
22

33
import (
4-
"context"
54
"fmt"
65
"nets"
76
"strings"
@@ -10,9 +9,6 @@ import (
109
"github.com/IBM/alchemy-logging/src/go/alog"
1110
"github.com/prometheus/client_golang/prometheus"
1211
"github.com/prometheus/client_golang/prometheus/promauto"
13-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14-
"k8s.io/apimachinery/pkg/runtime/schema"
15-
"k8s.io/client-go/dynamic"
1612
)
1713

1814
type APIConnect struct {
@@ -28,6 +24,7 @@ type APIConnectNetConfig struct {
2824
Host string `yaml:"host"`
2925
HealthPrefix string `yaml:"health_prefix"`
3026
HealthLabel HealthLabel `yaml:"health_label"`
27+
Namespace string `yaml:"namespace"`
3128
}
3229
type HealthLabel struct {
3330
Name string `yaml:"name"`
@@ -36,12 +33,8 @@ type HealthLabel struct {
3633

3734
var log = alog.UseChannel("apic")
3835

39-
func (a *APIConnect) crdStatusMetrics(dynamicClient dynamic.DynamicClient, gvr schema.GroupVersionResource, crdStatus prometheus.GaugeVec) {
40-
subsystems, err := dynamicClient.Resource(gvr).List(context.Background(), v1.ListOptions{})
41-
if err != nil {
42-
log.Log(alog.ERROR, "error getting subsystems %v", err)
43-
return
44-
}
36+
func (a *APIConnect) crdStatusMetrics(group, version, resource string, crdStatus prometheus.GaugeVec) {
37+
subsystems := nets.GetCustomResourceList(group, version, resource, a.Config.Namespace)
4538

4639
for _, subsystem := range subsystems.Items {
4740
subsystemName := subsystem.Object["metadata"].(map[string]interface{})["name"].(string)
@@ -66,9 +59,9 @@ func (a *APIConnect) crdStatusMetrics(dynamicClient dynamic.DynamicClient, gvr s
6659
}
6760
if version != "" {
6861
if a.Config.HealthLabel.Name != "" {
69-
a.healthStatus.WithLabelValues(gvr.Resource+"_"+subsystemName, version, a.Config.HealthLabel.Value).Set(healthValue)
62+
a.healthStatus.WithLabelValues(resource+"_"+subsystemName, version, a.Config.HealthLabel.Value).Set(healthValue)
7063
} else {
71-
a.healthStatus.WithLabelValues(gvr.Resource+"_"+subsystemName, version).Set(healthValue)
64+
a.healthStatus.WithLabelValues(resource+"_"+subsystemName, version).Set(healthValue)
7265
}
7366
}
7467
}
@@ -136,31 +129,8 @@ func (a *APIConnect) registerMetrics() {
136129
[]string{"name", "namespace", "type"})
137130
}
138131
func (a *APIConnect) Fish() {
139-
dynamicClient := nets.GetDynamicKubeClient()
140-
141-
var a7s_gvr = schema.GroupVersionResource{
142-
Group: "analytics.apiconnect.ibm.com",
143-
Version: "v1beta1",
144-
Resource: "analyticsclusters",
145-
}
146-
var ptl_gvr = schema.GroupVersionResource{
147-
Group: "portal.apiconnect.ibm.com",
148-
Version: "v1beta1",
149-
Resource: "portalclusters",
150-
}
151-
var mgmt_gvr = schema.GroupVersionResource{
152-
Group: "management.apiconnect.ibm.com",
153-
Version: "v1beta1",
154-
Resource: "managementclusters",
155-
}
156-
var gw_gvr = schema.GroupVersionResource{
157-
Group: "gateway.apiconnect.ibm.com",
158-
Version: "v1beta1",
159-
Resource: "gatewayclusters",
160-
}
161-
162-
a.crdStatusMetrics(*dynamicClient, gw_gvr, *a.metrics["gwCrdStatus"])
163-
a.crdStatusMetrics(*dynamicClient, mgmt_gvr, *a.metrics["mgmtCrdStatus"])
164-
a.crdStatusMetrics(*dynamicClient, ptl_gvr, *a.metrics["ptlCrdStatus"])
165-
a.crdStatusMetrics(*dynamicClient, a7s_gvr, *a.metrics["a7sCrdStatus"])
132+
a.crdStatusMetrics("gateway.apiconnect.ibm.com", "v1beta1", "gatewayclusters", *a.metrics["gwCrdStatus"])
133+
a.crdStatusMetrics("management.apiconnect.ibm.com", "v1beta1", "managementclusters", *a.metrics["mgmtCrdStatus"])
134+
a.crdStatusMetrics("portal.apiconnect.ibm.com", "v1beta1", "portalclusters", *a.metrics["ptlCrdStatus"])
135+
a.crdStatusMetrics("analytics.apiconnect.ibm.com", "v1beta1", "analyticsclusters", *a.metrics["a7sCrdStatus"])
166136
}

nets/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,15 @@ func InvokeAPI(url string, certPath string, token string) (*http.Response, error
163163

164164
}
165165

166-
func GetCustomResourceList(group, version, resource string) *unstructured.UnstructuredList {
166+
func GetCustomResourceList(group, version, resource, namespace string) *unstructured.UnstructuredList {
167167
dynamicClient := GetDynamicKubeClient()
168168
gvr := schema.GroupVersionResource{
169169
Group: group,
170170
Version: version,
171171
Resource: resource,
172172
}
173173

174-
items, err := dynamicClient.Resource(gvr).List(context.Background(), v1.ListOptions{})
174+
items, err := dynamicClient.Resource(gvr).Namespace(namespace).List(context.Background(), v1.ListOptions{})
175175
if err != nil {
176176
log.Log(alog.ERROR, "Failed to find %s: %v", resource, err)
177177
return nil

nets/manager/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type ManagerNetConfig struct {
2525
Host string `yaml:"host"`
2626
ProcessOrgMetrics bool `yaml:"process_org_metrics"`
2727
MoreStats bool `yaml:"more_stats"`
28+
Namespace string `yaml:"namespace"`
2829
}
2930

3031
type CountStruct struct {
@@ -98,7 +99,6 @@ type ConfiguredGatewayService struct {
9899

99100
var version string
100101

101-
var crlList = nets.GetCustomResourceList
102102
var invokeAPI = nets.InvokeAPI
103103
var getToken = nets.GetToken
104104

@@ -197,7 +197,7 @@ func (m *Manager) publishTopologyMetrics(topologyCount CountStruct, managementNa
197197

198198
func (m *Manager) findAPIM() error {
199199

200-
apims := crlList("management.apiconnect.ibm.com", "v1beta1", "managementclusters")
200+
apims := nets.GetCustomResourceList("management.apiconnect.ibm.com", "v1beta1", "managementclusters", m.Config.Namespace)
201201
if apims != nil {
202202
for _, apim := range apims.Items {
203203
managementName := apim.Object["metadata"].(map[string]interface{})["name"].(string)

0 commit comments

Comments
 (0)