File: factory.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 (119 lines) | stat: -rw-r--r-- 3,804 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package chartops

import (
	"net"
	"net/http"
	"time"

	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/gitops"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/gitops/rpc"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modagent"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/modshared"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/retry"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/tlstool"
	"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/pkg/agentcfg"
	"go.uber.org/zap"
	"helm.sh/helm/v3/pkg/action"
	"helm.sh/helm/v3/pkg/kube"
	"helm.sh/helm/v3/pkg/registry"
	"helm.sh/helm/v3/pkg/storage"
	"helm.sh/helm/v3/pkg/storage/driver"
)

const (
	getObjectsToSynchronizeInitBackoff   = 10 * time.Second
	getObjectsToSynchronizeMaxBackoff    = 5 * time.Minute
	getObjectsToSynchronizeResetDuration = 10 * time.Minute
	getObjectsToSynchronizeBackoffFactor = 2.0
	getObjectsToSynchronizeJitter        = 1.0

	defaultReinstallInterval = 5 * time.Minute
	installInitBackoff       = 10 * time.Second
	installMaxBackoff        = time.Minute
	installResetDuration     = 2 * time.Minute
	installBackoffFactor     = 2.0
	installJitter            = 1.0
)

type Factory struct {
}

func (f *Factory) New(config *modagent.Config) (modagent.Module, error) {
	clientset, err := config.K8sUtilFactory.KubernetesClientSet()
	if err != nil {
		return nil, err
	}
	// TODO support debug, credentials, output writer
	registryClient, err := registry.NewClient(
		registry.ClientOptEnableCache(true),
	)
	if err != nil {
		return nil, err
	}
	coreV1client := clientset.CoreV1()
	return &module{
		log: config.Log,
		workerFactory: &workerFactory{
			log: config.Log,
			helm: func(log *zap.Logger, chartCfg *agentcfg.ChartCF) Helm {
				infof := log.Sugar().Infof
				d := driver.NewSecrets(coreV1client.Secrets(*chartCfg.Namespace))
				d.Log = infof
				return &HelmActions{
					ActionCfg: &action.Configuration{
						RESTClientGetter: config.K8sUtilFactory,
						Releases: &storage.Storage{
							Driver:     d,
							MaxHistory: int(*chartCfg.MaxHistory),
							Log:        infof,
						},
						KubeClient: &kube.Client{
							Factory:   config.K8sUtilFactory,
							Log:       infof,
							Namespace: *chartCfg.Namespace,
						},
						RegistryClient: registryClient,
						Capabilities:   nil, // Empty to re-discover supported APIs.
						Log:            infof,
					},
				}
			},
			httpClient: &http.Transport{
				Proxy: http.ProxyFromEnvironment,
				DialContext: (&net.Dialer{
					Timeout:   30 * time.Second,
					KeepAlive: 30 * time.Second,
				}).DialContext,
				TLSClientConfig:       tlstool.DefaultClientTLSConfig(),
				MaxIdleConns:          10,
				IdleConnTimeout:       90 * time.Second,
				TLSHandshakeTimeout:   10 * time.Second,
				ResponseHeaderTimeout: 20 * time.Second,
				ForceAttemptHTTP2:     true,
			},
			gitopsClient: rpc.NewGitopsClient(config.KasConn),
			installPollConfig: retry.NewPollConfigFactory(defaultReinstallInterval, retry.NewExponentialBackoffFactory(
				installInitBackoff,
				installMaxBackoff,
				installResetDuration,
				installBackoffFactor,
				installJitter,
			)),
			watchPollConfig: retry.NewPollConfigFactory(0, retry.NewExponentialBackoffFactory(
				getObjectsToSynchronizeInitBackoff,
				getObjectsToSynchronizeMaxBackoff,
				getObjectsToSynchronizeResetDuration,
				getObjectsToSynchronizeBackoffFactor,
				getObjectsToSynchronizeJitter,
			)),
		},
	}, nil
}

func (f *Factory) Name() string {
	return gitops.AgentChartModuleName
}

func (f *Factory) StartStopPhase() modshared.ModuleStartStopPhase {
	return modshared.ModuleStartBeforeServers
}