This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
186 lines (160 loc) · 7.47 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"crypto/x509"
"encoding/pem"
"flag"
"io/ioutil"
"log"
"net/url"
"os"
vaultapi "github.com/hashicorp/vault/api"
"github.com/puppetlabs/leg/instrumentation/alerts"
"github.com/puppetlabs/leg/storage"
_ "github.com/puppetlabs/leg/storage/file"
_ "github.com/puppetlabs/leg/storage/gcs"
"github.com/puppetlabs/relay-core/pkg/model"
"github.com/puppetlabs/relay-core/pkg/operator/admission"
"github.com/puppetlabs/relay-core/pkg/operator/config"
"github.com/puppetlabs/relay-core/pkg/operator/controller/run"
"github.com/puppetlabs/relay-core/pkg/operator/controller/tenant"
"github.com/puppetlabs/relay-core/pkg/operator/controller/trigger"
"github.com/puppetlabs/relay-core/pkg/operator/dependency"
jose "gopkg.in/square/go-jose.v2"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
func main() {
// We use a custom flag set because Tekton's API has the default flag set
// configured too, which makes our command help make no sense.
fs := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
environment := fs.String("environment", "dev", "the environment this operator is running in")
standalone := fs.Bool("standalone", false, "enables standalone mode which runs workflows without multi-tenant network security protections")
kubeconfig := fs.String("kubeconfig", "", "path to kubeconfig file. Only required if running outside of a cluster.")
kubeMasterURL := fs.String("kube-master-url", "", "url to the kubernetes master")
kubeNamespace := fs.String("kube-namespace", "", "an optional working namespace to restrict to for watching CRDs")
imagePullSecret := fs.String("image-pull-secret", "", "the optionally namespaced name of the image pull secret to use for system images")
storageAddr := fs.String("storage-addr", "", "the storage URL to upload logs into")
numWorkers := fs.Int("num-workers", 2, "the number of worker threads to spawn that process Workflow resources")
_ = fs.Bool("metrics-enabled", false, "enables the metrics collection and server")
_ = fs.String("metrics-server-bind-addr", "localhost:3050", "the host:port to bind the metrics server to")
jwtSigningKeyFile := fs.String("jwt-signing-key-file", "", "path to a PEM-encoded RSA JWT key to use for signing step tokens")
vaultTransitPath := fs.String("vault-transit-path", "transit", "path to the Vault secrets engine to use for encrypting step tokens")
vaultTransitKey := fs.String("vault-transit-key", "metadata-api", "the Vault transit key to use")
metadataAPIURLStr := fs.String("metadata-api-url", "", "URL to the metadata API")
webhookServerPort := fs.Int("webhook-server-port", 443, "the port to listen on for webhook requests")
webhookServerKeyDir := fs.String("webhook-server-key-dir", "", "path to a directory containing two files, tls.key and tls.crt, to secure the webhook server")
tenantSandboxing := fs.Bool("tenant-sandboxing", false, "enables gVisor sandbox for tenant pods")
tenantSandboxRuntimeClassName := fs.String("tenant-sandbox-runtime-class-name", "runsc", "name of the runtime class providing the gVisor containerd runtime")
sentryDSN := fs.String("sentry-dsn", "", "the Sentry DSN to use for error reporting")
dynamicRBACBinding := fs.Bool("dynamic-rbac-binding", false, "enable if RBAC rules are set up dynamically for the operator to reduce unhelpful reported errors")
runtimeToolsImage := fs.String("runtime-tools-image", model.ToolsImage, "the image to use for the runtime tools")
err := fs.Parse(os.Args[1:])
if err != nil {
log.Fatal("Error parsing flags", err)
}
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)
var storageUrl *url.URL
if *storageAddr != "" {
storageUrl, err = url.Parse(*storageAddr)
if err != nil {
log.Fatal("Error parsing the -storage-addr", err)
}
}
metadataAPIURL, err := url.Parse(*metadataAPIURLStr)
if err != nil {
log.Fatal("Error parsing -metadata-api-url", err)
}
var blobStore storage.BlobStore
if storageUrl != nil {
blobStore, err = storage.NewBlobStore(*storageUrl)
if err != nil {
log.Fatal("Error initializing the storage client from the -storage-addr", err)
}
}
if *webhookServerKeyDir == "" {
log.Fatal("The webhook server key directory -webhook-server-key-dir must be specified")
}
kcfg := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: *kubeconfig},
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: *kubeMasterURL}},
)
kcc, err := kcfg.ClientConfig()
if err != nil {
log.Fatal("Error creating kubernetes config", err)
}
vc, err := vaultapi.NewClient(vaultapi.DefaultConfig())
if err != nil {
log.Fatal("Error creating Vault client", err)
}
jwtSigningKeyBytes, err := ioutil.ReadFile(*jwtSigningKeyFile)
if err != nil {
log.Fatal("Error reading JWT signing key file", err)
}
jwtSigningKeyBlock, _ := pem.Decode(jwtSigningKeyBytes)
if jwtSigningKeyBlock == nil {
log.Fatal("Error parsing PEM")
} else if jwtSigningKeyBlock.Type != "RSA PRIVATE KEY" {
log.Fatal("PEM file does not contain an RSA private key")
}
jwtSigningKey, err := x509.ParsePKCS1PrivateKey(jwtSigningKeyBlock.Bytes)
if err != nil {
log.Fatal("Error parsing RSA private key", err)
}
jwtSigner, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS512, Key: jwtSigningKey}, &jose.SignerOptions{})
if err != nil {
log.Fatal("Error creating signer for JWTs", err)
}
alertsDelegate, _ := alerts.DelegateToPassthrough()
if *sentryDSN != "" {
var err error
alertsDelegate, err = alerts.DelegateToSentry(*sentryDSN)
if err != nil {
log.Fatal("Error initializing Sentry", err)
}
}
cfg := &config.WorkflowControllerConfig{
Environment: *environment,
Standalone: *standalone,
Namespace: *kubeNamespace,
ImagePullSecret: *imagePullSecret,
MaxConcurrentReconciles: *numWorkers,
MetadataAPIURL: metadataAPIURL,
VaultTransitPath: *vaultTransitPath,
VaultTransitKey: *vaultTransitKey,
WebhookServerPort: *webhookServerPort,
WebhookServerKeyDir: *webhookServerKeyDir,
AlertsDelegate: alertsDelegate,
DynamicRBACBinding: *dynamicRBACBinding,
RuntimeToolsImage: *runtimeToolsImage,
}
dm, err := dependency.NewDependencyManager(cfg, kcc, vc, jwtSigner, blobStore)
if err != nil {
log.Fatal("Error creating controller dependency builder", err)
}
if err := run.Add(dm); err != nil {
log.Fatal("Could not add all controllers to operator manager", err)
}
if err := tenant.Add(dm.Manager, cfg); err != nil {
log.Fatal("Could not add all controllers to operator manager", err)
}
if err := trigger.Add(dm); err != nil {
log.Fatal("Could not add all controllers to operator manager", err)
}
var podEnforcementHandlerOpts []admission.PodEnforcementHandlerOption
if *tenantSandboxing {
podEnforcementHandlerOpts = append(podEnforcementHandlerOpts, admission.PodEnforcementHandlerWithRuntimeClassName(*tenantSandboxRuntimeClassName))
}
if *standalone {
podEnforcementHandlerOpts = append(podEnforcementHandlerOpts, admission.PodEnforcementHandlerWithStandaloneMode(true))
}
dm.Manager.GetWebhookServer().Register("/mutate/pod-enforcement", &webhook.Admission{
Handler: admission.NewPodEnforcementHandler(podEnforcementHandlerOpts...),
})
if err := dm.Manager.Start(signals.SetupSignalHandler()); err != nil {
log.Fatal("Manager exited non-zero", err)
}
}