-
Notifications
You must be signed in to change notification settings - Fork 29
/
operations.go
103 lines (94 loc) · 3.03 KB
/
operations.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
//
// Copyright (c) 2019 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package utils
import (
"log"
"github.com/che-incubator/kubernetes-image-puller/cfg"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
// CacheImages creates the daemonset responsible for ensuring images are cached
func CacheImages(clientset *kubernetes.Clientset) {
log.Printf("Starting caching process")
// Create daemonset, wait for it to be ready
if err := createDaemonset(clientset); err != nil {
log.Printf("Could not create Daemonset: %v", err)
}
log.Printf("Daemonset ready.")
}
// RefreshCache forces a refresh of all pods in the daemonset, to ensure images
// with mutable tags (e.g. nightlies) are up-to-date.
func RefreshCache(clientset *kubernetes.Clientset) {
log.Printf("Refreshing cached images")
DeleteDaemonsetIfExists(clientset)
if err := createDaemonset(clientset); err != nil {
log.Printf("Could not create Daemonset: %v", err)
}
log.Printf("Refreshed images")
}
// EnsureDaemonsetExists checks that the daemonset is still present, and
// recreates it if necessary
func EnsureDaemonsetExists(clientset *kubernetes.Clientset) {
log.Printf("Checking that daemonset exists.")
cfg := cfg.GetConfig()
daemonset, err :=
clientset.
AppsV1().
DaemonSets(cfg.Namespace).
Get(cfg.DaemonsetName, metav1.GetOptions{})
if err != nil || daemonset == nil {
log.Printf("Recreating daemonset due to error")
DeleteDaemonsetIfExists(clientset)
CacheImages(clientset)
}
}
// DeleteDaemonsetIfExists first checks if the daemonset exists, and deletes
// it if it does. Useful for ensuring no daemonset is already present from a
// previous rollout.
func DeleteDaemonsetIfExists(clientset *kubernetes.Clientset) {
cfg := cfg.GetConfig()
daemonset, err :=
clientset.
AppsV1().
DaemonSets(cfg.Namespace).
Get(cfg.DaemonsetName, metav1.GetOptions{})
if errors.IsNotFound(err) {
return
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
log.Fatalf("Error getting daemonset: %v", statusError.ErrStatus.Message)
} else if err != nil {
log.Fatalf(err.Error())
}
if daemonset != nil {
deleteDaemonset(clientset)
log.Printf("Deleted existing daemonset")
}
}
// LogNumNodesScheduled logs the basic status of the daemonset.
func LogNumNodesScheduled(clientset *kubernetes.Clientset, user string) {
cfg := cfg.GetConfig()
daemonset, err :=
clientset.
AppsV1().
DaemonSets(cfg.Namespace).
Get(cfg.DaemonsetName, metav1.GetOptions{})
if err != nil {
log.Printf("Failed to get daemonset for user '%s': %s", user, err)
}
if daemonset != nil {
log.Printf("Daemonset for user '%s': Ready: %d, Desired: %d",
user,
daemonset.Status.NumberReady,
daemonset.Status.DesiredNumberScheduled)
}
}