Skip to content

Commit d3fde22

Browse files
committed
chore: a very fat refactor
1 parent 6df5faf commit d3fde22

File tree

12 files changed

+518
-484
lines changed

12 files changed

+518
-484
lines changed

cmd/root.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import (
1010

1111
"github.com/home-operations/gatus-sidecar/internal/config"
1212
"github.com/home-operations/gatus-sidecar/internal/controller"
13-
"github.com/home-operations/gatus-sidecar/internal/manager"
13+
"github.com/home-operations/gatus-sidecar/internal/resources/httproute"
14+
"github.com/home-operations/gatus-sidecar/internal/resources/ingress"
15+
"github.com/home-operations/gatus-sidecar/internal/resources/service"
16+
"github.com/home-operations/gatus-sidecar/internal/state"
1417
"k8s.io/client-go/dynamic"
1518
"k8s.io/client-go/rest"
1619
)
@@ -21,7 +24,7 @@ func main() {
2124
defer cancel()
2225

2326
// Create a single shared state manager
24-
stateManager := manager.NewManager(cfg.Output)
27+
stateManager := state.NewManager(cfg.Output)
2528

2629
restCfg, err := rest.InClusterConfig()
2730
if err != nil {
@@ -35,11 +38,11 @@ func main() {
3538
os.Exit(1)
3639
}
3740

38-
// Create all controllers
41+
// Register default resource types
3942
controllers := []*controller.Controller{
40-
controller.NewHTTPRouteController(stateManager, dc),
41-
controller.NewIngressController(stateManager, dc),
42-
controller.NewServiceController(stateManager, dc),
43+
controller.New(ingress.Definition(), stateManager, dc),
44+
controller.New(httproute.Definition(), stateManager, dc),
45+
controller.New(service.Definition(), stateManager, dc),
4346
}
4447

4548
// Run all controllers concurrently

internal/controller/controller.go

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,31 @@ import (
1717

1818
"github.com/home-operations/gatus-sidecar/internal/config"
1919
"github.com/home-operations/gatus-sidecar/internal/endpoint"
20-
"github.com/home-operations/gatus-sidecar/internal/handler"
21-
"github.com/home-operations/gatus-sidecar/internal/manager"
22-
)
23-
24-
const (
25-
httpsPrefix = "https://"
26-
httpPrefix = "http://"
27-
httpProtocol = "http"
28-
httpsProtocol = "https"
29-
dnsTestURL = "1.1.1.1"
30-
dnsEmptyBodyCondition = "len([BODY]) == 0"
31-
dnsQueryType = "A"
32-
ingressCondition = "[STATUS] == 200"
20+
"github.com/home-operations/gatus-sidecar/internal/resources"
21+
"github.com/home-operations/gatus-sidecar/internal/state"
3322
)
3423

3524
type Controller struct {
3625
gvr schema.GroupVersionResource
3726
options metav1.ListOptions
38-
handler handler.ResourceHandler
27+
handler resources.ResourceHandler
3928
convert func(*unstructured.Unstructured) (metav1.Object, error)
40-
stateManager *manager.Manager
29+
stateManager *state.Manager
4130
dynamicClient dynamic.Interface
4231
}
4332

33+
// New creates a controller using a ResourceDefinition
34+
func New(definition *resources.ResourceDefinition, stateManager *state.Manager, dynamicClient dynamic.Interface) *Controller {
35+
return &Controller{
36+
gvr: definition.GVR,
37+
options: metav1.ListOptions{},
38+
handler: resources.NewHandler(definition, dynamicClient),
39+
stateManager: stateManager,
40+
dynamicClient: dynamicClient,
41+
convert: definition.ConvertFunc,
42+
}
43+
}
44+
4445
func (c *Controller) GetResource() string {
4546
return c.gvr.Resource
4647
}
@@ -227,24 +228,3 @@ func (c *Controller) deepMergeTemplates(parent, child map[string]any) map[string
227228

228229
return result
229230
}
230-
231-
func hasRequiredAnnotations(obj metav1.Object, cfg *config.Config) bool {
232-
annotations := obj.GetAnnotations()
233-
if annotations == nil {
234-
return false
235-
}
236-
237-
_, hasEnabledAnnotation := annotations[cfg.EnabledAnnotation]
238-
_, hasTemplateAnnotation := annotations[cfg.TemplateAnnotation]
239-
240-
return hasEnabledAnnotation || hasTemplateAnnotation
241-
}
242-
243-
func applyGuardedTemplate(dnsQueryName string, endpoint *endpoint.Endpoint) {
244-
endpoint.URL = dnsTestURL
245-
endpoint.DNS = map[string]any{
246-
"query-name": dnsQueryName,
247-
"query-type": dnsQueryType,
248-
}
249-
endpoint.Conditions = []string{dnsEmptyBodyCondition}
250-
}

internal/controller/httproute.go

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)