-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (120 loc) · 3.86 KB
/
main.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"github.com/renderedtext/agent-k8s-stack/pkg/config"
"github.com/renderedtext/agent-k8s-stack/pkg/controller"
"github.com/renderedtext/agent-k8s-stack/pkg/semaphore"
"github.com/renderedtext/agent-k8s-stack/pkg/signals"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
klog.InitFlags(nil)
flag.Parse()
ctx := signals.SetupSignalHandler()
endpoint := os.Getenv("SEMAPHORE_ENDPOINT")
if endpoint == "" {
klog.Error("invalid configuration: no SEMAPHORE_ENDPOINT specified")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
cfg, err := config.NewConfigFromEnv(endpoint)
if err != nil {
klog.Errorf("error building config: %v", err)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
clientset, err := newK8sClientset()
if err != nil {
klog.Errorf("error creating kube client: %v", err)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
semaphoreClient := semaphore.NewClient(endpoint)
informerFactory, err := NewInformerFactory(clientset, cfg)
if err != nil {
klog.Errorf("error creating informer factory: %v", err)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
controller, err := controller.New(
ctx,
informerFactory,
cfg,
semaphoreClient,
clientset,
)
if err != nil {
klog.Errorf("error creating controller: %v", err)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
// Start method is non-blocking and runs all registered informers in a dedicated goroutine.
informerFactory.Start(ctx.Done())
if err = controller.Run(ctx); err != nil {
klog.Errorf("Error running controller: %v", err)
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}
// Returns an informer factory configured to watch resources
// (secrets, pods, jobs) labeled with a semaphoreci.com/resource-type label.
func NewInformerFactory(clientset kubernetes.Interface, cfg *config.Config) (informers.SharedInformerFactory, error) {
requirements := []labels.Requirement{}
hasResourceType, err := labels.NewRequirement(config.ResourceTypeLabel, selection.Exists, nil)
if err != nil {
return nil, fmt.Errorf("failed to resource type requirement: %w", err)
}
requirements = append(requirements, *hasResourceType)
return informers.NewSharedInformerFactoryWithOptions(
clientset,
time.Minute,
informers.WithNamespace(cfg.Namespace),
informers.WithTweakListOptions(func(options *metav1.ListOptions) {
options.LabelSelector = labels.NewSelector().Add(requirements...).String()
}),
), nil
}
func newK8sClientset() (kubernetes.Interface, error) {
clientset, err := newInClusterClientset()
if err != nil {
log.Printf("No in-cluster configuration found - using ~/.kube/config...\n")
clientset, err = newClientsetFromConfig()
if err != nil {
return nil, fmt.Errorf("error creating kubernetes clientset: %v", err)
}
}
return clientset, nil
}
func newClientsetFromConfig() (kubernetes.Interface, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("error getting user home directory: %v", err)
}
kubeConfigPath := filepath.Join(homeDir, ".kube", "config")
kubeConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
return nil, fmt.Errorf("error getting Kubernetes config: %v", err)
}
clientset, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return nil, fmt.Errorf("error creating kubernetes clientset from config file: %v", err)
}
return clientset, nil
}
func newInClusterClientset() (kubernetes.Interface, error) {
k8sConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(k8sConfig)
if err != nil {
return nil, err
}
return clientset, nil
}