velero/pkg/install/daemonset.go

192 lines
4.7 KiB
Go
Raw Normal View History

/*
Allow registry to be configured at build time This adds a new `buildinfo` variable `ImageRegistry` that can set at build time like the `Version` variable. This allows us to customise the Velero binary to use different registries. If the variable is set, this variable wille be used when creating the URIs for both the main `velero` and `velero-restic-restore-helper` images. If it is not set, default to using Dockerhub (`velero/velero`, `velero/velero-restic-restore-helper`). There are numerous ways in which the Velero binary can be built so all of them have been updated to add the new link time flag to set the variable: * `make local` (used for local developer builds to build for the local OS and ARCH) * `make build` (used by developers and also VMware internal builds to build a specific OS and ARCH) * Goreleaser config (used when creating OSS release binaries) * Dockerfile (used to build the Velero binary used within the image) All of these workflows are currently triggered from our Makefile where the variable `REGISTRY` is already available with the default value of `velero` and used to build the image tag. Where the new `ImageRegistry` build variable is needed, we pass through this Makefile variable to those tasks so it can be used accordingly. The GitHub action and the `./hack/docker-push.sh` script used to push container images has not been modified. This will continue to use the default registry specified in the Makefile and will not explicitly pass it in. Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-07-06 20:14:46 +00:00
Copyright the Velero contributors.
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 (
"fmt"
"strings"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Allow registry to be configured at build time This adds a new `buildinfo` variable `ImageRegistry` that can set at build time like the `Version` variable. This allows us to customise the Velero binary to use different registries. If the variable is set, this variable wille be used when creating the URIs for both the main `velero` and `velero-restic-restore-helper` images. If it is not set, default to using Dockerhub (`velero/velero`, `velero/velero-restic-restore-helper`). There are numerous ways in which the Velero binary can be built so all of them have been updated to add the new link time flag to set the variable: * `make local` (used for local developer builds to build for the local OS and ARCH) * `make build` (used by developers and also VMware internal builds to build a specific OS and ARCH) * Goreleaser config (used when creating OSS release binaries) * Dockerfile (used to build the Velero binary used within the image) All of these workflows are currently triggered from our Makefile where the variable `REGISTRY` is already available with the default value of `velero` and used to build the image tag. Where the new `ImageRegistry` build variable is needed, we pass through this Makefile variable to those tasks so it can be used accordingly. The GitHub action and the `./hack/docker-push.sh` script used to push container images has not been modified. This will continue to use the default registry specified in the Makefile and will not explicitly pass it in. Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-07-06 20:14:46 +00:00
"github.com/vmware-tanzu/velero/internal/velero"
)
func DaemonSet(namespace string, opts ...podTemplateOption) *appsv1.DaemonSet {
c := &podTemplateConfig{
Allow registry to be configured at build time This adds a new `buildinfo` variable `ImageRegistry` that can set at build time like the `Version` variable. This allows us to customise the Velero binary to use different registries. If the variable is set, this variable wille be used when creating the URIs for both the main `velero` and `velero-restic-restore-helper` images. If it is not set, default to using Dockerhub (`velero/velero`, `velero/velero-restic-restore-helper`). There are numerous ways in which the Velero binary can be built so all of them have been updated to add the new link time flag to set the variable: * `make local` (used for local developer builds to build for the local OS and ARCH) * `make build` (used by developers and also VMware internal builds to build a specific OS and ARCH) * Goreleaser config (used when creating OSS release binaries) * Dockerfile (used to build the Velero binary used within the image) All of these workflows are currently triggered from our Makefile where the variable `REGISTRY` is already available with the default value of `velero` and used to build the image tag. Where the new `ImageRegistry` build variable is needed, we pass through this Makefile variable to those tasks so it can be used accordingly. The GitHub action and the `./hack/docker-push.sh` script used to push container images has not been modified. This will continue to use the default registry specified in the Makefile and will not explicitly pass it in. Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-07-06 20:14:46 +00:00
image: velero.DefaultVeleroImage(),
}
for _, opt := range opts {
opt(c)
}
pullPolicy := corev1.PullAlways
imageParts := strings.Split(c.image, ":")
if len(imageParts) == 2 && imageParts[1] != "latest" {
pullPolicy = corev1.PullIfNotPresent
}
daemonSetArgs := []string{
"node-agent",
"server",
}
if len(c.features) > 0 {
daemonSetArgs = append(daemonSetArgs, fmt.Sprintf("--features=%s", strings.Join(c.features, ",")))
}
userID := int64(0)
mountPropagationMode := corev1.MountPropagationHostToContainer
daemonSet := &appsv1.DaemonSet{
ObjectMeta: objectMeta(namespace, "node-agent"),
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
APIVersion: appsv1.SchemeGroupVersion.String(),
},
Spec: appsv1.DaemonSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"name": "node-agent",
},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: podLabels(c.labels, map[string]string{
"name": "node-agent",
}),
Annotations: c.annotations,
},
Spec: corev1.PodSpec{
ServiceAccountName: c.serviceAccountName,
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: &userID,
},
Volumes: []corev1.Volume{
{
Name: "host-pods",
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: "/var/lib/kubelet/pods",
},
},
},
{
Name: "scratch",
VolumeSource: corev1.VolumeSource{
EmptyDir: new(corev1.EmptyDirVolumeSource),
},
},
},
Containers: []corev1.Container{
{
Name: "node-agent",
Image: c.image,
ImagePullPolicy: pullPolicy,
Command: []string{
"/velero",
},
Args: daemonSetArgs,
VolumeMounts: []corev1.VolumeMount{
{
Name: "host-pods",
MountPath: "/host_pods",
MountPropagation: &mountPropagationMode,
},
{
Name: "scratch",
MountPath: "/scratch",
},
},
Env: []corev1.EnvVar{
{
Name: "NODE_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "spec.nodeName",
},
},
},
{
Name: "VELERO_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "VELERO_SCRATCH_DIR",
Value: "/scratch",
},
},
Resources: c.resources,
},
},
},
},
},
}
if c.withSecret {
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",
},
},
},
)
daemonSet.Spec.Template.Spec.Containers[0].VolumeMounts = append(
daemonSet.Spec.Template.Spec.Containers[0].VolumeMounts,
corev1.VolumeMount{
Name: "cloud-credentials",
MountPath: "/credentials",
},
)
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",
},
{
Name: "ALIBABA_CLOUD_CREDENTIALS_FILE",
Value: "/credentials/cloud",
},
}...)
}
daemonSet.Spec.Template.Spec.Containers[0].Env = append(daemonSet.Spec.Template.Spec.Containers[0].Env, c.envVars...)
return daemonSet
}