2018-02-28 01:35:35 +00:00
|
|
|
/*
|
2019-03-13 17:23:00 +00:00
|
|
|
Copyright 2018, 2019 the Velero contributors.
|
2018-02-28 01:35:35 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet {
|
|
|
|
c := &podTemplateConfig{
|
2019-01-25 03:33:07 +00:00
|
|
|
image: "gcr.io/heptio-images/velero:latest",
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
pullPolicy := corev1.PullAlways
|
|
|
|
imageParts := strings.Split(c.image, ":")
|
|
|
|
if len(imageParts) == 2 && imageParts[1] != "latest" {
|
|
|
|
pullPolicy = corev1.PullIfNotPresent
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-04-15 19:02:51 +00:00
|
|
|
userID := int64(0)
|
|
|
|
mountPropagationMode := corev1.MountPropagationHostToContainer
|
|
|
|
|
2018-02-28 01:35:35 +00:00
|
|
|
daemonSet := &appsv1.DaemonSet{
|
|
|
|
ObjectMeta: objectMeta(namespace, "restic"),
|
2019-03-13 17:23:00 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "DaemonSet",
|
|
|
|
APIVersion: appsv1.SchemeGroupVersion.String(),
|
|
|
|
},
|
2018-02-28 01:35:35 +00:00
|
|
|
Spec: appsv1.DaemonSetSpec{
|
|
|
|
Selector: &metav1.LabelSelector{
|
|
|
|
MatchLabels: map[string]string{
|
|
|
|
"name": "restic",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Template: corev1.PodTemplateSpec{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Labels: map[string]string{
|
2019-04-15 21:10:11 +00:00
|
|
|
"name": "restic",
|
|
|
|
"component": "velero",
|
2018-02-28 01:35:35 +00:00
|
|
|
},
|
2019-07-03 22:22:34 +00:00
|
|
|
Annotations: c.annotations,
|
2018-02-28 01:35:35 +00:00
|
|
|
},
|
|
|
|
Spec: corev1.PodSpec{
|
2019-01-25 03:33:07 +00:00
|
|
|
ServiceAccountName: "velero",
|
2019-04-15 19:02:51 +00:00
|
|
|
SecurityContext: &corev1.PodSecurityContext{
|
|
|
|
RunAsUser: &userID,
|
|
|
|
},
|
2018-02-28 01:35:35 +00:00
|
|
|
Volumes: []corev1.Volume{
|
|
|
|
{
|
|
|
|
Name: "host-pods",
|
|
|
|
VolumeSource: corev1.VolumeSource{
|
|
|
|
HostPath: &corev1.HostPathVolumeSource{
|
|
|
|
Path: "/var/lib/kubelet/pods",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-15 19:02:51 +00:00
|
|
|
{
|
|
|
|
Name: "scratch",
|
|
|
|
VolumeSource: corev1.VolumeSource{
|
|
|
|
EmptyDir: new(corev1.EmptyDirVolumeSource),
|
|
|
|
},
|
|
|
|
},
|
2018-02-28 01:35:35 +00:00
|
|
|
},
|
|
|
|
Containers: []corev1.Container{
|
|
|
|
{
|
|
|
|
Name: "restic",
|
|
|
|
Image: c.image,
|
|
|
|
ImagePullPolicy: pullPolicy,
|
2019-04-15 21:10:11 +00:00
|
|
|
Command: []string{
|
|
|
|
"/velero",
|
|
|
|
},
|
|
|
|
Args: []string{
|
|
|
|
"restic",
|
|
|
|
"server",
|
|
|
|
},
|
|
|
|
|
2018-02-28 01:35:35 +00:00
|
|
|
VolumeMounts: []corev1.VolumeMount{
|
|
|
|
{
|
2019-04-15 19:02:51 +00:00
|
|
|
Name: "host-pods",
|
|
|
|
MountPath: "/host_pods",
|
|
|
|
MountPropagation: &mountPropagationMode,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "scratch",
|
|
|
|
MountPath: "/scratch",
|
2018-02-28 01:35:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Env: []corev1.EnvVar{
|
|
|
|
{
|
|
|
|
Name: "NODE_NAME",
|
|
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
|
|
FieldRef: &corev1.ObjectFieldSelector{
|
|
|
|
FieldPath: "spec.nodeName",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-01-25 03:33:07 +00:00
|
|
|
Name: "VELERO_NAMESPACE",
|
2018-02-28 01:35:35 +00:00
|
|
|
ValueFrom: &corev1.EnvVarSource{
|
|
|
|
FieldRef: &corev1.ObjectFieldSelector{
|
|
|
|
FieldPath: "metadata.namespace",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-15 19:02:51 +00:00
|
|
|
{
|
|
|
|
Name: "VELERO_SCRATCH_DIR",
|
|
|
|
Value: "/scratch",
|
|
|
|
},
|
2018-02-28 01:35:35 +00:00
|
|
|
},
|
2019-07-31 19:24:47 +00:00
|
|
|
Resources: c.resources,
|
2018-02-28 01:35:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-01 22:57:36 +00:00
|
|
|
if c.withSecret {
|
2018-02-28 01:35:35 +00:00
|
|
|
daemonSet.Spec.Template.Spec.Volumes = append(
|
|
|
|
daemonSet.Spec.Template.Spec.Volumes,
|
|
|
|
corev1.Volume{
|
|
|
|
Name: "cloud-credentials",
|
|
|
|
VolumeSource: corev1.VolumeSource{
|
|
|
|
Secret: &corev1.SecretVolumeSource{
|
|
|
|
SecretName: "cloud-credentials",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2019-04-15 19:02:51 +00:00
|
|
|
|
|
|
|
daemonSet.Spec.Template.Spec.Containers[0].VolumeMounts = append(
|
|
|
|
daemonSet.Spec.Template.Spec.Containers[0].VolumeMounts,
|
|
|
|
corev1.VolumeMount{
|
|
|
|
Name: "cloud-credentials",
|
|
|
|
MountPath: "/credentials",
|
|
|
|
},
|
|
|
|
)
|
2019-08-01 22:57:36 +00:00
|
|
|
|
|
|
|
daemonSet.Spec.Template.Spec.Containers[0].Env = append(daemonSet.Spec.Template.Spec.Containers[0].Env, []corev1.EnvVar{
|
|
|
|
{
|
|
|
|
Name: "GOOGLE_APPLICATION_CREDENTIALS",
|
|
|
|
Value: "/credentials/cloud",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "AWS_SHARED_CREDENTIALS_FILE",
|
|
|
|
Value: "/credentials/cloud",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "AZURE_CREDENTIALS_FILE",
|
|
|
|
Value: "/credentials/cloud",
|
|
|
|
},
|
|
|
|
}...)
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
daemonSet.Spec.Template.Spec.Containers[0].Env = append(daemonSet.Spec.Template.Spec.Containers[0].Env, c.envVars...)
|
|
|
|
|
|
|
|
return daemonSet
|
|
|
|
}
|