Skip to content

Commit aa517d1

Browse files
committed
WIP: support multiple namespaces.
This particular commit makes the operator listen on all namespaces and introduces an optional namespace to the clusters RESET API endpoints.
1 parent 23011bd commit aa517d1

File tree

7 files changed

+35
-21
lines changed

7 files changed

+35
-21
lines changed

pkg/apiserver/apiserver.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ type controllerInformer interface {
3030
GetOperatorConfig() *config.Config
3131
GetStatus() *spec.ControllerStatus
3232
TeamClusterList() map[string][]spec.NamespacedName
33-
ClusterStatus(team, cluster string) (*spec.ClusterStatus, error)
34-
ClusterLogs(team, cluster string) ([]*spec.LogEntry, error)
35-
ClusterHistory(team, cluster string) ([]*spec.Diff, error)
33+
ClusterStatus(team, namespace, cluster string) (*spec.ClusterStatus, error)
34+
ClusterLogs(team, namespace, cluster string) ([]*spec.LogEntry, error)
35+
ClusterHistory(team, namespace, cluster string) ([]*spec.Diff, error)
3636
ClusterDatabasesMap() map[string][]string
3737
WorkerLogs(workerID uint32) ([]*spec.LogEntry, error)
3838
ListQueue(workerID uint32) (*spec.QueueDump, error)
@@ -48,9 +48,9 @@ type Server struct {
4848
}
4949

5050
var (
51-
clusterStatusURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/?$`)
52-
clusterLogsURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/logs/?$`)
53-
clusterHistoryURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/history/?$`)
51+
clusterStatusURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)(/(?P<namespace>[a-zA-Z][a-zA-Z0-9-]*))?/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/?$`)
52+
clusterLogsURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)(/(?P<namespace>[a-zA-Z][a-zA-Z0-9-]*))?/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/logs/?$`)
53+
clusterHistoryURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)(/(?P<namespace>[a-zA-Z][a-zA-Z0-9-]*))?/(?P<cluster>[a-zA-Z][a-zA-Z0-9-]*)/history/?$`)
5454
teamURL = regexp.MustCompile(`^/clusters/(?P<team>[a-zA-Z][a-zA-Z0-9]*)/?$`)
5555
workerLogsURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/logs/?$`)
5656
workerEventsQueueURL = regexp.MustCompile(`^/workers/(?P<id>\d+)/queue/?$`)
@@ -149,7 +149,8 @@ func (s *Server) clusters(w http.ResponseWriter, req *http.Request) {
149149
)
150150

151151
if matches := util.FindNamedStringSubmatch(clusterStatusURL, req.URL.Path); matches != nil {
152-
resp, err = s.controller.ClusterStatus(matches["team"], matches["cluster"])
152+
namespace, _ := matches["namespace"]
153+
resp, err = s.controller.ClusterStatus(matches["team"], namespace, matches["cluster"])
153154
} else if matches := util.FindNamedStringSubmatch(teamURL, req.URL.Path); matches != nil {
154155
teamClusters := s.controller.TeamClusterList()
155156
clusters, found := teamClusters[matches["team"]]
@@ -166,9 +167,11 @@ func (s *Server) clusters(w http.ResponseWriter, req *http.Request) {
166167
s.respond(clusterNames, nil, w)
167168
return
168169
} else if matches := util.FindNamedStringSubmatch(clusterLogsURL, req.URL.Path); matches != nil {
169-
resp, err = s.controller.ClusterLogs(matches["team"], matches["cluster"])
170+
namespace, _ := matches["namespace"]
171+
resp, err = s.controller.ClusterLogs(matches["team"], namespace, matches["cluster"])
170172
} else if matches := util.FindNamedStringSubmatch(clusterHistoryURL, req.URL.Path); matches != nil {
171-
resp, err = s.controller.ClusterHistory(matches["team"], matches["cluster"])
173+
namespace, _ := matches["namespace"]
174+
resp, err = s.controller.ClusterHistory(matches["team"], namespace, matches["cluster"])
172175
} else if req.URL.Path == clustersURL {
173176
res := make(map[string][]string)
174177
for team, clusters := range s.controller.TeamClusterList() {

pkg/cluster/cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func (c *Cluster) clusterName() spec.NamespacedName {
127127
return util.NameFromMeta(c.ObjectMeta)
128128
}
129129

130+
func (c *Cluster) clusterNamespace() string {
131+
return c.ObjectMeta.Namespace
132+
}
133+
130134
func (c *Cluster) teamName() string {
131135
// TODO: check Teams API for the actual name (in case the user passes an integer Id).
132136
return c.Spec.TeamID

pkg/cluster/resources.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (c *Cluster) preScaleDown(newStatefulSet *v1beta1.StatefulSet) error {
110110
}
111111

112112
podName := fmt.Sprintf("%s-0", c.Statefulset.Name)
113-
masterCandidatePod, err := c.KubeClient.Pods(c.OpConfig.Namespace).Get(podName, metav1.GetOptions{})
113+
masterCandidatePod, err := c.KubeClient.Pods(c.clusterNamespace()).Get(podName, metav1.GetOptions{})
114114
if err != nil {
115115
return fmt.Errorf("could not get master candidate pod: %v", err)
116116
}

pkg/controller/node.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (c *Controller) moveMasterPodsOffNode(node *v1.Node) {
8080
opts := metav1.ListOptions{
8181
LabelSelector: labels.Set(c.opConfig.ClusterLabels).String(),
8282
}
83-
podList, err := c.KubeClient.Pods(c.opConfig.Namespace).List(opts)
83+
podList, err := c.KubeClient.Pods("").List(opts)
8484
if err != nil {
8585
c.logger.Errorf("could not fetch list of the pods: %v", err)
8686
return

pkg/controller/pod.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (c *Controller) podListFunc(options metav1.ListOptions) (runtime.Object, er
1717
TimeoutSeconds: options.TimeoutSeconds,
1818
}
1919

20-
return c.KubeClient.Pods(c.opConfig.Namespace).List(opts)
20+
return c.KubeClient.Pods("").List(opts)
2121
}
2222

2323
func (c *Controller) podWatchFunc(options metav1.ListOptions) (watch.Interface, error) {
@@ -27,7 +27,7 @@ func (c *Controller) podWatchFunc(options metav1.ListOptions) (watch.Interface,
2727
TimeoutSeconds: options.TimeoutSeconds,
2828
}
2929

30-
return c.KubeClient.Pods(c.opConfig.Namespace).Watch(opts)
30+
return c.KubeClient.Pods("").Watch(opts)
3131
}
3232

3333
func (c *Controller) dispatchPodEvent(clusterName spec.NamespacedName, event spec.PodEvent) {

pkg/controller/postgresql.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ func (c *Controller) clusterListFunc(options metav1.ListOptions) (runtime.Object
4646

4747
req := c.KubeClient.CRDREST.
4848
Get().
49-
Namespace(c.opConfig.Namespace).
5049
Resource(constants.CRDResource).
5150
VersionedParams(&options, metav1.ParameterCodec)
5251

@@ -110,7 +109,6 @@ func (c *Controller) clusterWatchFunc(options metav1.ListOptions) (watch.Interfa
110109
options.Watch = true
111110
r, err := c.KubeClient.CRDREST.
112111
Get().
113-
Namespace(c.opConfig.Namespace).
114112
Resource(constants.CRDResource).
115113
VersionedParams(&options, metav1.ParameterCodec).
116114
FieldsSelectorParam(nil).

pkg/controller/status.go

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

1616
// ClusterStatus provides status of the cluster
17-
func (c *Controller) ClusterStatus(team, cluster string) (*spec.ClusterStatus, error) {
17+
func (c *Controller) ClusterStatus(team, namespace, cluster string) (*spec.ClusterStatus, error) {
18+
if namespace == "" {
19+
namespace = c.opConfig.Namespace
20+
}
1821
clusterName := spec.NamespacedName{
19-
Namespace: c.opConfig.Namespace,
22+
Namespace: namespace,
2023
Name: team + "-" + cluster,
2124
}
2225

@@ -90,9 +93,12 @@ func (c *Controller) GetStatus() *spec.ControllerStatus {
9093
}
9194

9295
// ClusterLogs dumps cluster ring logs
93-
func (c *Controller) ClusterLogs(team, name string) ([]*spec.LogEntry, error) {
96+
func (c *Controller) ClusterLogs(team, namespace, name string) ([]*spec.LogEntry, error) {
97+
if namespace == "" {
98+
namespace = c.opConfig.Namespace
99+
}
94100
clusterName := spec.NamespacedName{
95-
Namespace: c.opConfig.Namespace,
101+
Namespace: namespace,
96102
Name: team + "-" + name,
97103
}
98104

@@ -212,9 +218,12 @@ func (c *Controller) WorkerStatus(workerID uint32) (*spec.WorkerStatus, error) {
212218
}
213219

214220
// ClusterHistory dumps history of cluster changes
215-
func (c *Controller) ClusterHistory(team, name string) ([]*spec.Diff, error) {
221+
func (c *Controller) ClusterHistory(team, namespace, name string) ([]*spec.Diff, error) {
222+
if namespace == "" {
223+
namespace = c.opConfig.Namespace
224+
}
216225
clusterName := spec.NamespacedName{
217-
Namespace: c.opConfig.Namespace,
226+
Namespace: namespace,
218227
Name: team + "-" + name,
219228
}
220229

0 commit comments

Comments
 (0)