-
Notifications
You must be signed in to change notification settings - Fork 2
/
crd.go
161 lines (131 loc) · 4 KB
/
crd.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
package crd
import (
"context"
"encoding/json"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
apixv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apixv1client "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
ctrlzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/yaml"
"github.com/dominodatalab/hephaestus/deployments/crds"
"github.com/dominodatalab/hephaestus/pkg/kubernetes"
)
// crdProcessor uses a CRD client to process the provided resource definition.
type crdProcessor func(
context.Context,
apixv1client.CustomResourceDefinitionInterface,
*apixv1.CustomResourceDefinition,
) error
var (
log = ctrlzap.New(
ctrlzap.UseDevMode(true),
ctrlzap.Encoder(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())),
)
crdClientFn = crdClient
applyFn = createOrUpdate
deleteFn = deleteWhenPresent
)
// Apply will create or update all project CRDs inside a Kubernetes cluster.
//
// The latest available version of the CRD will be used to perform this operation.
func Apply(ctx context.Context) error {
return operate(ctx, applyFn)
}
// Delete will remove all project CRDs from a Kubernetes cluster.
func Delete(ctx context.Context) error {
return operate(ctx, deleteFn)
}
// Exists will check for the existence of a specific groupversion.
func Exists(gv metav1.GroupVersion) (bool, error) {
config, err := kubernetes.RestConfig()
if err != nil {
return false, err
}
client, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
return false, err
}
_, err = client.ServerResourcesForGroupVersion(gv.String())
if err != nil {
if apierrors.IsNotFound(err) {
return false, nil
}
return false, err
}
return true, nil
}
// operate will read all available CRDS and apply state changes to the cluster using the processor func.
func operate(ctx context.Context, processor crdProcessor) error {
log.Info("Loading all CRDs")
definitions, err := crds.ReadAll()
if err != nil {
return err
}
client, err := crdClientFn()
if err != nil {
return err
}
for _, def := range definitions {
bs, err := yaml.YAMLToJSON(def.Contents)
if err != nil {
return err
}
resource := new(apixv1.CustomResourceDefinition)
if err := json.Unmarshal(bs, resource); err != nil {
return err
}
if err := processor(ctx, client, resource); err != nil {
return err
}
}
return nil
}
// crdClient returns a client configured to work with custom resource definitions.
func crdClient() (apixv1client.CustomResourceDefinitionInterface, error) {
log.Info("Initializing Kubernetes V1 CRD client")
config, err := kubernetes.RestConfig()
if err != nil {
return nil, err
}
client, err := apixv1client.NewForConfig(config)
if err != nil {
return nil, err
}
return client.CustomResourceDefinitions(), nil
}
// createOrUpdate provided CRD.
func createOrUpdate(
ctx context.Context,
client apixv1client.CustomResourceDefinitionInterface,
crd *apixv1.CustomResourceDefinition,
) error {
log.Info("Fetching CRD", "Name", crd.Name)
found, err := client.Get(ctx, crd.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
log.Info("CRD not found, creating", "Name", crd.Name)
_, err = client.Create(ctx, crd, metav1.CreateOptions{})
} else if err == nil {
log.Info("CRD found, updating", "Name", crd.Name)
crd.SetResourceVersion(found.ResourceVersion)
_, err = client.Update(ctx, crd, metav1.UpdateOptions{})
}
return err
}
// deleteWhenPresent will remove a CRD if it has been applied to the k8s API, otherwise it's a noop.
func deleteWhenPresent(
ctx context.Context,
client apixv1client.CustomResourceDefinitionInterface,
crd *apixv1.CustomResourceDefinition,
) error {
log.Info("Deleting CRD", "Name", crd.Name)
err := client.Delete(ctx, crd.Name, metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
log.Info("CRD not found, ignoring", "Name", crd.Name)
return nil
}
return err
}