Merge pull request #437 from marpaia/install-api

Add library code to install required server components
This commit is contained in:
Andy Goldstein
2018-05-18 16:48:42 -04:00
committed by GitHub
33 changed files with 9832 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package install
import (
"time"
arkv1 "github.com/heptio/ark/pkg/apis/ark/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func Config(
namespace string,
pvCloudProviderName string,
pvCloudProviderConfig map[string]string,
backupCloudProviderName string,
backupCloudProviderConfig map[string]string,
bucket string,
) *arkv1.Config {
return &arkv1.Config{
ObjectMeta: objectMeta(namespace, "default"),
PersistentVolumeProvider: &arkv1.CloudProviderConfig{
Name: pvCloudProviderName,
Config: pvCloudProviderConfig,
},
BackupStorageProvider: arkv1.ObjectStorageProviderConfig{
CloudProviderConfig: arkv1.CloudProviderConfig{
Name: backupCloudProviderName,
Config: backupCloudProviderConfig,
},
Bucket: bucket,
},
BackupSyncPeriod: metav1.Duration{
Duration: 30 * time.Minute,
},
GCSyncPeriod: metav1.Duration{
Duration: 30 * time.Minute,
},
ScheduleSyncPeriod: metav1.Duration{
Duration: time.Minute,
},
RestoreOnlyMode: false,
}
}
+39
View File
@@ -0,0 +1,39 @@
package install
import (
"fmt"
arkv1 "github.com/heptio/ark/pkg/apis/ark/v1"
apiextv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// CRDs returns a list of the CRD types for all of the required Ark CRDs
func CRDs() []*apiextv1beta1.CustomResourceDefinition {
return []*apiextv1beta1.CustomResourceDefinition{
crd("Backup", "backups"),
crd("Schedule", "schedules"),
crd("Restore", "restores"),
crd("Config", "configs"),
crd("DownloadRequest", "downloadrequests"),
crd("DeleteBackupRequest", "deletebackuprequests"),
}
}
func crd(kind, plural string) *apiextv1beta1.CustomResourceDefinition {
return &apiextv1beta1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s.%s", plural, arkv1.GroupName),
},
Spec: apiextv1beta1.CustomResourceDefinitionSpec{
Group: arkv1.GroupName,
Version: arkv1.SchemeGroupVersion.Version,
Scope: apiextv1beta1.NamespaceScoped,
Names: apiextv1beta1.CustomResourceDefinitionNames{
Plural: plural,
Kind: kind,
},
},
}
}
+121
View File
@@ -0,0 +1,121 @@
package install
import (
"strings"
appsv1beta1 "k8s.io/api/apps/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type deploymentOption func(*config)
type config struct {
image string
withoutCredentialsVolume bool
}
func WithImage(image string) deploymentOption {
return func(c *config) {
c.image = image
}
}
func WithoutCredentialsVolume() deploymentOption {
return func(c *config) {
c.withoutCredentialsVolume = true
}
}
func Deployment(namespace string, opts ...deploymentOption) *appsv1beta1.Deployment {
c := &config{
image: "gcr.io/heptio-images/ark:latest",
}
for _, opt := range opts {
opt(c)
}
pullPolicy := corev1.PullAlways
imageParts := strings.Split(c.image, ":")
if len(imageParts) == 2 && imageParts[1] != "latest" {
pullPolicy = corev1.PullIfNotPresent
}
deployment := &appsv1beta1.Deployment{
ObjectMeta: objectMeta(namespace, "ark"),
Spec: appsv1beta1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels(),
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyAlways,
ServiceAccountName: "ark",
Containers: []corev1.Container{
{
Name: "ark",
Image: c.image,
ImagePullPolicy: pullPolicy,
Command: []string{
"/ark",
},
Args: []string{
"server",
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "plugins",
MountPath: "/plugins",
},
},
Env: []corev1.EnvVar{
{
Name: "GOOGLE_APPLICATION_CREDENTIALS",
Value: "/credentials/cloud",
},
{
Name: "AWS_SHARED_CREDENTIALS_FILE",
Value: "/credentials/cloud",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "plugins",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
},
},
},
},
},
},
}
if !c.withoutCredentialsVolume {
deployment.Spec.Template.Spec.Volumes = append(
deployment.Spec.Template.Spec.Volumes,
corev1.Volume{
Name: "cloud-credentials",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "cloud-credentials",
},
},
},
)
deployment.Spec.Template.Spec.Containers[0].VolumeMounts = append(
deployment.Spec.Template.Spec.Containers[0].VolumeMounts,
corev1.VolumeMount{
Name: "cloud-credentials",
MountPath: "/credentials",
},
)
}
return deployment
}
+55
View File
@@ -0,0 +1,55 @@
package install
import (
corev1 "k8s.io/api/core/v1"
rbacv1beta1 "k8s.io/api/rbac/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func labels() map[string]string {
return map[string]string{
"component": "ark",
}
}
func objectMeta(namespace, name string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels(),
}
}
func ServiceAccount(namespace string) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
ObjectMeta: objectMeta(namespace, "ark"),
}
}
func ClusterRoleBinding(namespace string) *rbacv1beta1.ClusterRoleBinding {
return &rbacv1beta1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "ark",
},
Subjects: []rbacv1beta1.Subject{
{
Kind: "ServiceAccount",
Namespace: namespace,
Name: "ark",
},
},
RoleRef: rbacv1beta1.RoleRef{
Kind: "ClusterRole",
Name: "cluster-admin",
APIGroup: "rbac.authorization.k8s.io",
},
}
}
func Namespace(namespace string) *corev1.Namespace {
return &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
}