-
Notifications
You must be signed in to change notification settings - Fork 70
/
daemon_set.go
169 lines (157 loc) · 4.65 KB
/
daemon_set.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
//
// DISCLAIMER
//
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
//
// Licensed 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package storage
import (
"context"
"fmt"
"strconv"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
api "github.com/arangodb/kube-arangodb/pkg/apis/storage/v1alpha"
"github.com/arangodb/kube-arangodb/pkg/storage/provisioner"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/constants"
"github.com/arangodb/kube-arangodb/pkg/util/errors"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil/kerrors"
)
const (
roleProvisioner = "provisioner"
)
// ensureDaemonSet ensures that a daemonset is created for the given local storage.
// If it already exists, it is updated.
func (ls *LocalStorage) ensureDaemonSet(apiObject *api.ArangoLocalStorage) error {
ns := ls.config.Namespace
c := core.Container{
Name: "provisioner",
Image: ls.image,
ImagePullPolicy: ls.imagePullPolicy,
Args: []string{
"storage",
"provisioner",
"--port=" + strconv.Itoa(provisioner.DefaultPort),
},
Ports: []core.ContainerPort{
core.ContainerPort{
ContainerPort: int32(provisioner.DefaultPort),
},
},
Env: []core.EnvVar{
core.EnvVar{
Name: constants.EnvOperatorNodeName,
ValueFrom: &core.EnvVarSource{
FieldRef: &core.ObjectFieldSelector{
FieldPath: "spec.nodeName",
},
},
},
},
}
if apiObject.Spec.GetPrivileged() {
c.SecurityContext = &core.SecurityContext{
Privileged: util.NewType[bool](true),
}
}
dsLabels := k8sutil.LabelsForLocalStorage(apiObject.GetName(), roleProvisioner)
dsSpec := apps.DaemonSetSpec{
Selector: &meta.LabelSelector{
MatchLabels: dsLabels,
},
Template: core.PodTemplateSpec{
ObjectMeta: meta.ObjectMeta{
Labels: dsLabels,
},
Spec: core.PodSpec{
Containers: []core.Container{
c,
},
NodeSelector: apiObject.Spec.NodeSelector,
ImagePullSecrets: ls.imagePullSecrets,
Priority: apiObject.Spec.PodCustomization.GetPriority(),
Tolerations: apiObject.Spec.Tolerations,
},
},
}
for i, lp := range apiObject.Spec.LocalPath {
volName := fmt.Sprintf("local-path-%d", i)
c := &dsSpec.Template.Spec.Containers[0]
c.VolumeMounts = append(c.VolumeMounts,
core.VolumeMount{
Name: volName,
MountPath: lp,
})
hostPathType := core.HostPathDirectoryOrCreate
dsSpec.Template.Spec.Volumes = append(dsSpec.Template.Spec.Volumes, core.Volume{
Name: volName,
VolumeSource: core.VolumeSource{
HostPath: &core.HostPathVolumeSource{
Path: lp,
Type: &hostPathType,
},
},
})
}
ds := &apps.DaemonSet{
ObjectMeta: meta.ObjectMeta{
Name: apiObject.GetName(),
Labels: dsLabels,
},
Spec: dsSpec,
}
// Attach DS to ArangoLocalStorage
ds.SetOwnerReferences(append(ds.GetOwnerReferences(), apiObject.AsOwner()))
// Create DS
if _, err := ls.deps.Client.Kubernetes().AppsV1().DaemonSets(ns).Create(context.Background(), ds, meta.CreateOptions{}); err != nil {
if kerrors.IsAlreadyExists(err) {
// Already exists, update it
} else {
return errors.WithStack(err)
}
} else {
// We're done
ls.log.Debug("Created DaemonSet")
return nil
}
// Update existing DS
attempt := 0
for {
attempt++
// Load current DS
current, err := ls.deps.Client.Kubernetes().AppsV1().DaemonSets(ns).Get(context.Background(), ds.GetName(), meta.GetOptions{})
if err != nil {
return errors.WithStack(err)
}
// Update it
current.Spec = dsSpec
if _, err := ls.deps.Client.Kubernetes().AppsV1().DaemonSets(ns).Update(context.Background(), current, meta.UpdateOptions{}); kerrors.IsConflict(err) && attempt < 10 {
ls.log.Err(err).Debug("failed to patch DaemonSet spec")
// Failed to update, try again
continue
} else if err != nil {
ls.log.Err(err).Debug("failed to patch DaemonSet spec")
return errors.WithStack(errors.Errorf("failed to patch DaemonSet spec: %v", err))
} else {
// Update was a success
ls.log.Debug("Updated DaemonSet")
return nil
}
}
}