File: factory.go

package info (click to toggle)
gitlab-agent 16.11.5-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 7,072 kB
  • sloc: makefile: 193; sh: 55; ruby: 3
file content (81 lines) | stat: -rw-r--r-- 2,376 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
//go:build !fips

package agent

import (
	"fmt"
	"os"
	"strconv"

	"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/module/starboard_vulnerability"
)

const (
	envVarOcsServiceAccountName = "OCS_SERVICE_ACCOUNT_NAME"
	envVarOcsEnabled            = "OCS_ENABLED"
)

type Factory struct{}

func (f *Factory) IsProducingLeaderModules() bool {
	return true
}

func (f *Factory) New(cfg *modagent.Config) (modagent.Module, error) {
	// In case of an error ocsEnabled is already false
	ocsEnabled, _ := strconv.ParseBool(os.Getenv(envVarOcsEnabled))
	if !ocsEnabled {
		cfg.Log.Sugar().Infof("Module is disabled. Set the '%s' environment variable to 'true` to enable it", envVarOcsEnabled)
		return nil, nil
	}

	ocsServiceAccountName := os.Getenv(envVarOcsServiceAccountName)
	if ocsServiceAccountName == "" {
		return nil, fmt.Errorf("%s environment variable is required but is empty", envVarOcsServiceAccountName)
	}

	kubeClientset, err := cfg.K8sUtilFactory.KubernetesClientSet()
	if err != nil {
		return nil, fmt.Errorf("could not create kubernetes clientset: %w", err)
	}

	return &module{
		log:                       cfg.Log,
		api:                       cfg.API,
		policiesUpdateDataChannel: make(chan configurationToUpdateData),
		workerFactory: (&workerFactory{
			log:                       cfg.Log,
			api:                       cfg.API,
			kubeClientset:             kubeClientset,
			gitlabAgentNamespace:      getAgentNamespace(cfg),
			gitlabAgentServiceAccount: getAgentServiceAccount(cfg),
			ocsServiceAccountName:     ocsServiceAccountName,
		}).New,
	}, nil
}

func (f *Factory) Name() string {
	return starboard_vulnerability.ModuleName
}

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

func getAgentNamespace(cfg *modagent.Config) string {
	namespace := cfg.AgentMeta.PodNamespace
	if namespace == "" {
		namespace = defaultGitlabAgentNamespace
	}
	return namespace
}

func getAgentServiceAccount(cfg *modagent.Config) string {
	serviceAccountName := cfg.ServiceAccountName
	if serviceAccountName == "" {
		serviceAccountName = defaultGitlabAgentServiceAccount
	}
	return serviceAccountName
}