-
Notifications
You must be signed in to change notification settings - Fork 54
/
apply.go
167 lines (150 loc) · 5.13 KB
/
apply.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
// Licensed to Apache Software Foundation (ASF) under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Apache Software Foundation (ASF) licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package kubernetes
import (
"context"
"fmt"
"text/template"
"github.com/go-logr/logr"
l "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// Repo provides tools to access templates
type Repo interface {
ReadFile(path string) ([]byte, error)
GetFilesRecursive(path string) ([]string, error)
}
// Application contains the resource of one single component which is applied to api server
type Application struct {
client.Client
CR client.Object
FileRepo Repo
GVK schema.GroupVersionKind
TmplFunc template.FuncMap
Recorder record.EventRecorder
}
// ApplyAll manifests dependent a single CR
func (a *Application) ApplyAll(ctx context.Context, manifestFiles []string, log logr.Logger) error {
var changedFf []string
for _, f := range manifestFiles {
sl := log.WithName(f)
changed, err := a.Apply(ctx, f, sl, true)
if err != nil {
l.Error(err, "failed to apply resource")
a.Recorder.Eventf(a.CR, v1.EventTypeWarning, "failed to apply resource", "encountered err: %v", err)
return err
}
if changed {
changedFf = append(changedFf, f)
}
}
if len(changedFf) > 0 {
a.Recorder.Eventf(a.CR, v1.EventTypeNormal, "resources are created or updated", "resources: %v", changedFf)
}
return nil
}
// Apply a template represents a component to api server
func (a *Application) Apply(ctx context.Context, manifest string, log logr.Logger, needCompose bool) (bool, error) {
manifests, err := a.FileRepo.ReadFile(manifest)
if err != nil {
return false, err
}
proto := &unstructured.Unstructured{}
yaml, err := LoadTemplate(string(manifests), a.CR, a.TmplFunc, proto)
if err == ErrNothingLoaded {
log.Info("nothing is loaded")
return false, nil
}
if err != nil {
return false, fmt.Errorf("failed to load %s template: %w yaml: %v", manifest, err, string(yaml))
}
return a.apply(ctx, proto, log, needCompose)
}
// ApplyFromObject apply an object to api server
func (a *Application) ApplyFromObject(ctx context.Context, obj runtime.Object, log logr.Logger, needCompose bool) (bool, error) {
proto, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return false, fmt.Errorf("failed to convert object to unstructed: %v", err)
}
return a.apply(ctx, &unstructured.Unstructured{Object: proto}, log, needCompose)
}
func (a *Application) apply(ctx context.Context, obj *unstructured.Unstructured, log logr.Logger, needCompose bool) (bool, error) {
key := client.ObjectKeyFromObject(obj)
current := &unstructured.Unstructured{}
current.SetGroupVersionKind(obj.GetObjectKind().GroupVersionKind())
err := a.Client.Get(ctx, key, current)
if apierrors.IsNotFound(err) {
log.Info("could not find existing resource, creating one...")
if needCompose {
curr, errComp := a.compose(obj)
if errComp != nil {
return false, fmt.Errorf("failed to compose: %w", errComp)
}
obj = curr
}
if err = a.Client.Create(ctx, obj); err != nil {
return false, fmt.Errorf("failed to create: %w", err)
}
log.Info("created")
return true, nil
}
if err != nil {
return false, fmt.Errorf("failed to get %v : %w", key, err)
}
if needCompose {
object, err := a.compose(obj)
if err != nil {
return false, fmt.Errorf("failed to compose: %w", err)
}
obj = object
}
if getVersion(current, a.versionKey()) == getVersion(obj, a.versionKey()) {
log.Info("resource keeps the same as before")
return false, nil
}
if err := a.Client.Update(ctx, obj); err != nil {
return false, fmt.Errorf("failed to update: %w", err)
}
log.Info("updated")
return true, nil
}
func (a *Application) setVersionAnnotation(o *unstructured.Unstructured) error {
h, err := hash(o)
if err != nil {
return err
}
setVersion(o, a.versionKey(), h)
return nil
}
func (a *Application) versionKey() string {
return a.GVK.Group + "/version"
}
func (a *Application) compose(object *unstructured.Unstructured) (*unstructured.Unstructured, error) {
object.SetOwnerReferences([]metav1.OwnerReference{*metav1.NewControllerRef(a.CR, a.GVK)})
err := a.setVersionAnnotation(object)
if err != nil {
return nil, err
}
return object, nil
}