-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
173 lines (139 loc) · 4.54 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
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
clientset "github.com/openfaas/ingress-operator/pkg/client/clientset/versioned"
informers "github.com/openfaas/ingress-operator/pkg/client/informers/externalversions"
controllerv1 "github.com/openfaas/ingress-operator/pkg/controller/v1"
"github.com/openfaas/ingress-operator/pkg/signals"
"github.com/openfaas/ingress-operator/pkg/version"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
klog "k8s.io/klog"
// required for generating code from CRD
_ "k8s.io/code-generator/cmd/client-gen/generators"
// required to authenticate against GKE clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
var (
masterURL string
kubeconfig string
)
const defaultResync = time.Hour * 10
func init() {
klog.InitFlags(nil)
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.")
}
func main() {
// TODO: remove
flag.Set("logtostderr", "true")
flag.Parse()
setupLogging()
sha, release := version.GetReleaseInfo()
klog.Infof("Starting FunctionIngress controller version: %s commit: %s", release, sha)
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
cfg, err := getClientCmdConfig(masterURL, kubeconfig)
if err != nil {
klog.Fatalf("Error building kubeconfig: %s", err.Error())
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building Kubernetes clientset: %s", err.Error())
}
faasClient, err := clientset.NewForConfig(cfg)
if err != nil {
klog.Fatalf("Error building FunctionIngress clientset: %s", err.Error())
}
ingressNamespace := "openfaas"
if namespace, exists := os.LookupEnv("ingress_namespace"); exists {
ingressNamespace = namespace
}
kubeInformerOpt := kubeinformers.WithNamespace(ingressNamespace)
kubeInformerFactory := kubeinformers.
NewSharedInformerFactoryWithOptions(kubeClient, defaultResync, kubeInformerOpt)
faasInformerOpt := informers.WithNamespace(ingressNamespace)
faasInformerFactory := informers.
NewSharedInformerFactoryWithOptions(faasClient, defaultResync, faasInformerOpt)
ctrl := controllerv1.NewController(
kubeClient,
faasClient,
kubeInformerFactory,
faasInformerFactory,
)
go kubeInformerFactory.Start(stopCh)
go faasInformerFactory.Start(stopCh)
if err = ctrl.Run(1, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err.Error())
}
}
func setupLogging() {
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)
// Sync the klog and klog flags.
flag.CommandLine.VisitAll(func(f1 *flag.Flag) {
f2 := klogFlags.Lookup(f1.Name)
if f2 != nil {
value := f1.Value.String()
f2.Value.Set(value)
}
})
}
type Capabilities map[string]bool
func (c Capabilities) Has(wanted string) bool {
return c[wanted]
}
func (c Capabilities) String() string {
keys := make([]string, 0, len(c))
for k := range c {
keys = append(keys, k)
}
return strings.Join(keys, ", ")
}
// getPreferredAvailableAPIs queries the cluster for the preferred resources information and returns a Capabilities
// instance containing those api groups that support the specified kind.
//
// kind should be the title case singular name of the kind. For example, "Ingress" is the kind for a resource "ingress".
func getPreferredAvailableAPIs(client kubernetes.Interface, kind string) (Capabilities, error) {
discoveryclient := client.Discovery()
lists, err := discoveryclient.ServerPreferredResources()
if err != nil {
return nil, err
}
caps := Capabilities{}
for _, list := range lists {
if len(list.APIResources) == 0 {
continue
}
for _, resource := range list.APIResources {
if len(resource.Verbs) == 0 {
continue
}
if resource.Kind == kind {
caps[list.GroupVersion] = true
}
}
}
return caps, nil
}
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
}