File: module.go

package info (click to toggle)
gitlab-agent 16.1.3-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 6,324 kB
  • sloc: makefile: 175; sh: 52; ruby: 3
file content (86 lines) | stat: -rw-r--r-- 2,916 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package agent

import (
	"context"
	"time"

	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modagent"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/remote_development"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/errz"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/logz"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/prototool"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/syncz"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/pkg/agentcfg"
	"go.uber.org/zap"
)

const (
	defaultFullSyncInterval    = 1 * time.Hour
	defaultPartialSyncInterval = 10 * time.Second
)

// remote dev module is expected to only run on the leader agentk replica
// as such module is expected to implement modagent.LeaderModule and
// the following has been added to ensure this compliance
var _ modagent.LeaderModule = (*module)(nil)

type remoteDevReconciler interface {
	Run(context.Context) error
	Stop()
}

type module struct {
	log               *zap.Logger
	api               modagent.Api
	reconcilerFactory func(ctx context.Context) (remoteDevReconciler, error)
}

func (m *module) IsRunnableConfiguration(cfg *agentcfg.AgentConfiguration) bool {
	return cfg.RemoteDevelopment.Enabled
}

func (m *module) Run(ctx context.Context, cfg <-chan *agentcfg.AgentConfiguration) error {
	wh := syncz.NewProtoWorkerHolder[*agentcfg.RemoteCF](
		func(config *agentcfg.RemoteCF) syncz.Worker {
			return syncz.WorkerFunc(func(ctx context.Context) {
				m.log.Debug("Remote Development - starting reconciler run")
				defer m.log.Debug("Remote Development - reconciler run ended")

				w := &worker{
					log:                 m.log,
					api:                 m.api,
					fullSyncInterval:    config.GetFullSyncInterval().AsDuration(),
					partialSyncInterval: config.GetPartialSyncInterval().AsDuration(),
					reconcilerFactory:   m.reconcilerFactory,
				}

				err := w.Run(ctx)
				if err != nil && !errz.ContextDone(err) {
					m.log.Error("Error running reconciler", logz.Error(err))
				}
			})
		},
	)
	defer wh.StopAndWait()

	// This loop reacts to configuration changes stopping and starting workers.
	for config := range cfg {
		wh.ApplyConfig(ctx, config.RemoteDevelopment)
	}
	return nil
}

//goland:noinspection GoUnusedParameter
func (m *module) DefaultAndValidateConfiguration(config *agentcfg.AgentConfiguration) error {
	prototool.NotNil(&config.RemoteDevelopment)

	// config.RemoteDevelopment.Enabled will default to false if not provided which is expected
	prototool.Duration(&config.RemoteDevelopment.PartialSyncInterval, defaultPartialSyncInterval)
	prototool.Duration(&config.RemoteDevelopment.FullSyncInterval, defaultFullSyncInterval)

	return nil
}

func (m *module) Name() string {
	return remote_development.ModuleName
}