-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
cell.go
93 lines (70 loc) · 2.09 KB
/
cell.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package apis
import (
"fmt"
"github.com/cilium/hive/cell"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/cilium/cilium/pkg/k8s/apis/cilium.io/client"
k8sClient "github.com/cilium/cilium/pkg/k8s/client"
)
// SkipCRDCreation specifies whether the CustomResourceDefinition will be
// created by the daemon
const SkipCRDCreation = "skip-crd-creation"
// RegisterCRDsCell is a cell that creates all the Cilium CRDs.
var RegisterCRDsCell = cell.Module(
"create-crds",
"Create Cilium CRDs",
cell.Config(defaultConfig),
cell.Invoke(createCRDs),
cell.ProvidePrivate(
newCiliumGroupCRDs,
),
)
type RegisterCRDsConfig struct {
// SkipCRDCreation disables creation of the CustomResourceDefinition
// for the operator
SkipCRDCreation bool
}
var defaultConfig = RegisterCRDsConfig{}
func (c RegisterCRDsConfig) Flags(flags *pflag.FlagSet) {
flags.Bool(SkipCRDCreation, false, "When true, Kubernetes Custom Resource Definitions will not be created")
}
// RegisterCRDsFunc is a function that register all the CRDs for a k8s group
type RegisterCRDsFunc func(k8sClient.Clientset) error
type params struct {
cell.In
Logger logrus.FieldLogger
Lifecycle cell.Lifecycle
Clientset k8sClient.Clientset
Config RegisterCRDsConfig
RegisterCRDsFuncs []RegisterCRDsFunc `group:"register-crd-funcs"`
}
func createCRDs(p params) {
p.Lifecycle.Append(cell.Hook{
OnStart: func(ctx cell.HookContext) error {
// Register the CRDs after validating that we are running on a supported
// version of K8s.
if !p.Clientset.IsEnabled() || p.Config.SkipCRDCreation {
p.Logger.Info("Skipping creation of CRDs")
return nil
}
for _, f := range p.RegisterCRDsFuncs {
if err := f(p.Clientset); err != nil {
return fmt.Errorf("unable to create CRDs: %w", err)
}
}
return nil
},
})
}
type registerCRDsFuncOut struct {
cell.Out
Func RegisterCRDsFunc `group:"register-crd-funcs"`
}
func newCiliumGroupCRDs() registerCRDsFuncOut {
return registerCRDsFuncOut{
Func: client.RegisterCRDs,
}
}