-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
main.go
198 lines (155 loc) · 6.9 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
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
restclient "k8s.io/client-go/rest"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
// Uncomment the following line to load the gcp plugin (only required to authenticate against GKE clusters).
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
clientset "github.com/inlets/inlets-operator/pkg/generated/clientset/versioned"
informers "github.com/inlets/inlets-operator/pkg/generated/informers/externalversions"
"github.com/inlets/inlets-operator/pkg/signals"
"github.com/inlets/inlets-operator/pkg/version"
// required for generating code from CRD
_ "k8s.io/code-generator/cmd/client-gen/generators"
)
var (
masterURL string
kubeconfig string
)
func init() {
flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
flag.StringVar(&masterURL, "master", "", "The address of the Kubernetes API server. Overrides any value in kubeconfig. Only required if out-of-cluster.")
}
const defaultRelease = "0.9.31"
func main() {
infra := &InfraConfig{
ProConfig: InletsProConfig{},
}
flag.StringVar(&infra.Provider, "provider", "", "Your infrastructure provider - 'equinix-metal', 'digitalocean', 'scaleway', 'gce', 'linode', 'azure', 'ec2' or 'hetzner'")
flag.StringVar(&infra.Region, "region", "", "The region to provision hosts into")
flag.StringVar(&infra.Zone, "zone", "us-central1-a", "The zone where the exit node is to be provisioned")
flag.StringVar(&infra.AccessKey, "access-key", "", "The access key for your infrastructure provider")
flag.StringVar(&infra.AccessKeyFile, "access-key-file", "", "Read the access key for your infrastructure provider from a file (recommended)")
flag.StringVar(&infra.SecretKey, "secret-key", "", "The secret key if using scaleway or ec2 as the provider")
flag.StringVar(&infra.SecretKeyFile, "secret-key-file", "", "Read the access key for your infrastructure provider from a file (recommended)")
flag.StringVar(&infra.SubscriptionID, "subscription-id", "", "The azure Subscription ID")
flag.StringVar(&infra.OrganizationID, "organization-id", "", "The organization id if using scaleway as the provider")
flag.StringVar(&infra.VpcID, "vpc-id", "", "The VPC ID to create the exit-server in (ec2)")
flag.StringVar(&infra.SubnetID, "subnet-id", "", "The Subnet ID where the exit-server should be placed (ec2)")
flag.StringVar(&infra.ProjectID, "project-id", "", "The project ID if using equinix-metal, or gce as the provider")
flag.StringVar(&infra.ProConfig.License, "license", "", "Supply a license for use with inlets-pro")
flag.StringVar(&infra.ProConfig.LicenseFile, "license-file", "", "Supply a file to read for the inlets-pro license")
flag.StringVar(&infra.ProConfig.ClientImage, "client-image", "ghcr.io/inlets/inlets-pro:"+defaultRelease, "Container image for inlets tunnel clients run in the cluster")
flag.StringVar(&infra.ProConfig.InletsRelease, "inlets-release", defaultRelease, "Inlets version to use to create tunnel servers")
flag.StringVar(&infra.MaxClientMemory, "max-client-memory", "128Mi", "Maximum memory limit for the tunnel clients")
flag.StringVar(&infra.Plan, "plan", "", "Plan code for cloud host")
flag.BoolVar(&infra.AnnotatedOnly, "annotated-only", false, "Only create a tunnel for annotated services. Annotate with operator.inlets.dev/manage=1.")
flag.Parse()
log.Printf("Inlets Operator version: %s SHA: %s\n", version.Release, version.SHA)
create := "all tunnels"
if infra.AnnotatedOnly {
create = "tunnels annotated with: operator.inlets.dev/manage=1"
}
log.Printf("Creating tunnels for: %s ", create)
err := validateFlags(*infra)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
log.Printf("Inlets client image: %s\tInlets server version: %s\n",
infra.GetInletsClientImage(),
infra.GetInletsRelease())
if _, err := infra.ProConfig.GetLicenseKey(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
os.Exit(1)
}
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
cfg, err := getClientCmdConfig(masterURL, kubeconfig)
if err != nil {
log.Fatalf("Error building kubeconfig: %s", err.Error())
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building kubernetes clientset: %s", err.Error())
}
operatorClient, err := clientset.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building example clientset: %s", err.Error())
}
kubeInformerFactory := kubeinformers.NewSharedInformerFactory(kubeClient, time.Second*30)
tunnelsInformerFactory := informers.NewSharedInformerFactory(operatorClient, time.Second*30)
controller := NewController(kubeClient, operatorClient,
kubeInformerFactory.Apps().V1().Deployments(),
tunnelsInformerFactory.Operator().V1alpha1().Tunnels(),
kubeInformerFactory.Core().V1().Services(),
infra)
// notice that there is no need to run Start methods in a separate goroutine. (i.e. go kubeInformerFactory.Start(stopCh)
// Start method is non-blocking and runs all registered informers in a dedicated goroutine.
kubeInformerFactory.Start(stopCh)
tunnelsInformerFactory.Start(stopCh)
if err = controller.Run(2, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err.Error())
}
}
// GetInletsClientImage returns the image for the client-side tunnel
func (i *InfraConfig) GetInletsClientImage() string {
if i.ProConfig.ClientImage == "" {
return fmt.Sprintf("ghcr.io/inlets/inlets-pro:%s", defaultRelease)
}
return strings.TrimSpace(i.ProConfig.ClientImage)
}
func (i *InfraConfig) GetInletsRelease() string {
if i.ProConfig.InletsRelease == "" {
return defaultRelease
}
return strings.TrimSpace(i.ProConfig.InletsRelease)
}
// GetAccessKey from parameter or file trimming
// any whitespace found.
func (i *InfraConfig) GetAccessKey() string {
if len(i.AccessKeyFile) > 0 {
data, err := ioutil.ReadFile(i.AccessKeyFile)
if err != nil {
log.Fatalln(err)
}
return strings.TrimSpace(string(data))
}
return strings.TrimSpace(i.AccessKey)
}
// GetSecretKey from parameter or file trimming
// any whitespace found.
func (i *InfraConfig) GetSecretKey() string {
if len(i.SecretKeyFile) > 0 {
data, err := ioutil.ReadFile(i.SecretKeyFile)
if err != nil {
log.Fatalln(err)
}
return strings.TrimSpace(string(data))
}
return strings.TrimSpace(i.SecretKey)
}
func getClientCmdConfig(masterURL, kubeconfig string) (*restclient.Config, error) {
var err error
var cfg *restclient.Config
if len(kubeconfig) == 0 {
cfg, err = restclient.InClusterConfig()
if err != nil {
if _, statErr := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token"); os.IsNotExist(statErr) {
err = fmt.Errorf("set the -kubeconfig flag, if running outside of a cluster")
}
}
} else {
cfg, err = clientcmd.BuildConfigFromFlags(masterURL, kubeconfig)
}
return cfg, err
}