|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package tests |
| 24 | + |
| 25 | +import ( |
| 26 | + "crypto/sha1" |
| 27 | + "fmt" |
| 28 | + "strings" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/dchest/uniuri" |
| 32 | + appsv1 "k8s.io/api/apps/v1" |
| 33 | + corev1 "k8s.io/api/core/v1" |
| 34 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 35 | + "k8s.io/client-go/kubernetes" |
| 36 | + |
| 37 | + "github.com/arangodb/kube-arangodb/pkg/util/retry" |
| 38 | +) |
| 39 | + |
| 40 | +// prepullImage runs a daemonset that pulls a given ArangoDB image on all nodes. |
| 41 | +func prepullArangoImage(cli kubernetes.Interface, image, namespace string) error { |
| 42 | + name := "prepuller-" + strings.ToLower(uniuri.NewLen(4)) |
| 43 | + dsLabels := map[string]string{ |
| 44 | + "app": "prepuller", |
| 45 | + "image-hash": fmt.Sprintf("%0x", sha1.Sum([]byte(image)))[:10], |
| 46 | + } |
| 47 | + ds := &appsv1.DaemonSet{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: name, |
| 50 | + Labels: dsLabels, |
| 51 | + }, |
| 52 | + Spec: appsv1.DaemonSetSpec{ |
| 53 | + Selector: &metav1.LabelSelector{ |
| 54 | + MatchLabels: dsLabels, |
| 55 | + }, |
| 56 | + Template: corev1.PodTemplateSpec{ |
| 57 | + ObjectMeta: metav1.ObjectMeta{ |
| 58 | + Labels: dsLabels, |
| 59 | + }, |
| 60 | + Spec: corev1.PodSpec{ |
| 61 | + Containers: []corev1.Container{ |
| 62 | + corev1.Container{ |
| 63 | + Name: "arango", |
| 64 | + Image: image, |
| 65 | + Env: []corev1.EnvVar{ |
| 66 | + corev1.EnvVar{ |
| 67 | + Name: "ARANGO_NO_AUTH", |
| 68 | + Value: "1", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + // Create DS |
| 78 | + if _, err := cli.AppsV1().DaemonSets(namespace).Create(ds); err != nil { |
| 79 | + return maskAny(err) |
| 80 | + } |
| 81 | + // Cleanup on exit |
| 82 | + defer func() { |
| 83 | + cli.AppsV1().DaemonSets(namespace).Delete(name, &metav1.DeleteOptions{}) |
| 84 | + }() |
| 85 | + // Now wait for it to be ready |
| 86 | + op := func() error { |
| 87 | + current, err := cli.AppsV1().DaemonSets(namespace).Get(name, metav1.GetOptions{}) |
| 88 | + if err != nil { |
| 89 | + return maskAny(err) |
| 90 | + } |
| 91 | + if current.Status.DesiredNumberScheduled > current.Status.NumberReady { |
| 92 | + return maskAny(fmt.Errorf("Expected %d pods to be ready, got %d", current.Status.DesiredNumberScheduled, current.Status.NumberReady)) |
| 93 | + } |
| 94 | + return nil |
| 95 | + } |
| 96 | + if err := retry.Retry(op, time.Hour); err != nil { |
| 97 | + return maskAny(err) |
| 98 | + } |
| 99 | + return nil |
| 100 | +} |
0 commit comments