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
|
package agent
import (
"context"
"fmt"
"time"
"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/remote_development"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/module/remote_development/agent/k8s"
"gitlab.com/gitlab-org/cluster-integration/gitlab-agent/v16/internal/tool/retry"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/dynamic/dynamicinformer"
)
const (
interval = 10 * time.Second
initBackoff = 10 * time.Second
maxBackoff = time.Minute
resetDuration = 2 * time.Minute
backoffFactor = 2.0
jitter = 1.0
agentIdLabelSelector = "agent.gitlab.com/id"
resyncDuration = 5 * time.Minute
)
var (
deploymentGVR = schema.GroupVersionResource{
Group: "apps",
Version: "v1",
Resource: "deployments",
}
)
type Factory struct {
}
func (f *Factory) New(config *modagent.Config) (modagent.Module, error) {
restConfig, err := config.K8sUtilFactory.ToRESTConfig()
if err != nil {
return nil, err
}
client, err := dynamic.NewForConfig(restConfig)
if err != nil {
return nil, err
}
k8sClient, err := k8s.New(config.Log, config.K8sUtilFactory)
if err != nil {
return nil, err
}
pollFactory := retry.NewPollConfigFactory(interval, retry.NewExponentialBackoffFactory(
initBackoff,
maxBackoff,
resetDuration,
backoffFactor,
jitter,
))
return &module{
log: config.Log,
api: config.Api,
reconcilerFactory: func(ctx context.Context) (remoteDevReconciler, error) {
agentId, err := config.Api.GetAgentId(ctx)
if err != nil {
return nil, err
}
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(client, resyncDuration, corev1.NamespaceAll, func(opts *metav1.ListOptions) {
opts.LabelSelector = fmt.Sprintf("%s=%d", agentIdLabelSelector, agentId)
})
inf, err := newK8sInformer(config.Log, factory.ForResource(deploymentGVR).Informer())
if err != nil {
return nil, err
}
r := &reconciler{
log: config.Log,
agentId: agentId,
api: config.Api,
pollConfig: pollFactory,
pollFunction: retry.PollWithBackoff,
stateTracker: newPersistedStateTracker(),
terminatingTracker: newPersistedTerminatingWorkspacesTracker(),
informer: inf,
k8sClient: k8sClient,
}
err = r.informer.Start(ctx)
if err != nil {
return nil, err
}
return r, nil
},
}, nil
}
func (f *Factory) Name() string {
return remote_development.ModuleName
}
func (f *Factory) StartStopPhase() modshared.ModuleStartStopPhase {
return modshared.ModuleStartBeforeServers
}
|