/
submarine_tensorboard.go
198 lines (173 loc) · 7.21 KB
/
submarine_tensorboard.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 controllers
import (
"context"
"fmt"
"github.com/apache/submarine/submarine-cloud-v3/controllers/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
submarineapacheorgv1 "github.com/apache/submarine/submarine-cloud-v3/api/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
func (r *SubmarineReconciler) newSubmarineTensorboardPersistentVolumeClaim(ctx context.Context, submarine *submarineapacheorgv1.Submarine) *corev1.PersistentVolumeClaim {
pvc, err := util.ParsePersistentVolumeClaimYaml(tensorboardYamlPath)
if err != nil {
r.Log.Error(err, "ParsePersistentVolumeClaimYaml")
}
pvc.Namespace = submarine.Namespace
err = controllerutil.SetControllerReference(submarine, pvc, r.Scheme)
if err != nil {
r.Log.Error(err, "Set PersistentVolumeClaim ControllerReference")
}
return pvc
}
func (r *SubmarineReconciler) newSubmarineTensorboardDeployment(ctx context.Context, submarine *submarineapacheorgv1.Submarine) *appsv1.Deployment {
deployment, err := util.ParseDeploymentYaml(tensorboardYamlPath)
if err != nil {
r.Log.Error(err, "ParseDeploymentYaml")
}
deployment.Namespace = submarine.Namespace
err = controllerutil.SetControllerReference(submarine, deployment, r.Scheme)
if err != nil {
r.Log.Error(err, "Set Deployment ControllerReference")
}
// tensorboard image
tensorboardImage := submarine.Spec.Tensorboard.Image
if tensorboardImage != "" {
deployment.Spec.Template.Spec.Containers[0].Image = tensorboardImage
}
// pull secrets
pullSecrets := util.GetSubmarineCommonImage(submarine).PullSecrets
if pullSecrets != nil {
deployment.Spec.Template.Spec.ImagePullSecrets = r.CreatePullSecrets(&pullSecrets)
}
return deployment
}
func (r *SubmarineReconciler) newSubmarineTensorboardService(ctx context.Context, submarine *submarineapacheorgv1.Submarine) *corev1.Service {
service, err := util.ParseServiceYaml(tensorboardYamlPath)
if err != nil {
r.Log.Error(err, "ParseServiceYaml")
}
service.Namespace = submarine.Namespace
err = controllerutil.SetControllerReference(submarine, service, r.Scheme)
if err != nil {
r.Log.Error(err, "Set Service ControllerReference")
}
return service
}
// createSubmarineTensorboard is a function to create submarine-tensorboard.
// Reference: https://github.com/apache/submarine/blob/master/submarine-cloud-v3/artifacts/submarine-tensorboard.yaml
func (r *SubmarineReconciler) createSubmarineTensorboard(ctx context.Context, submarine *submarineapacheorgv1.Submarine) error {
r.Log.Info("Enter createSubmarineTensorboard")
// Step 1: Create PersistentVolumeClaim
pvc := &corev1.PersistentVolumeClaim{}
err := r.Get(ctx, types.NamespacedName{Name: tensorboardPvcName, Namespace: submarine.Namespace}, pvc)
// If the resource doesn't exist, we'll create it
if errors.IsNotFound(err) {
pvc = r.newSubmarineTensorboardPersistentVolumeClaim(ctx, submarine)
err = r.Create(ctx, pvc)
r.Log.Info("Create PersistentVolumeClaim", "name", pvc.Name)
}
// If an error occurs during Get/Create, we'll requeue the item so we can
// attempt processing again later. This could have been caused by a
// temporary network failure, or any other transient reason.
if err != nil {
return err
}
if !metav1.IsControlledBy(pvc, submarine) {
msg := fmt.Sprintf(MessageResourceExists, pvc.Name)
r.Recorder.Event(submarine, corev1.EventTypeWarning, ErrResourceExists, msg)
return fmt.Errorf(msg)
}
// Step 2: Create Deployment
deployment := &appsv1.Deployment{}
err = r.Get(ctx, types.NamespacedName{Name: tensorboardName, Namespace: submarine.Namespace}, deployment)
if errors.IsNotFound(err) {
// If an error occurs during Get/Create, we'll requeue the item so we can
// attempt processing again later. This could have been caused by a
// temporary network failure, or any other transient reason.
deployment = r.newSubmarineTensorboardDeployment(ctx, submarine)
err = r.Create(ctx, deployment)
r.Log.Info("Create Deployment", "name", deployment.Name)
} else {
newDeployment := r.newSubmarineTensorboardDeployment(ctx, submarine)
// compare if there are same
if !r.compareTensorboardDeployment(deployment, newDeployment) {
// update meta with uid
newDeployment.ObjectMeta = deployment.ObjectMeta
err = r.Update(ctx, newDeployment)
r.Log.Info("Update Deployment", "name", deployment.Name)
}
}
// If an error occurs during Get/Create, we'll requeue the item so we can
// attempt processing again later. This could have been caused by a
// temporary network failure, or any other transient reason.
if err != nil {
return err
}
if !metav1.IsControlledBy(deployment, submarine) {
msg := fmt.Sprintf(MessageResourceExists, deployment.Name)
r.Recorder.Event(submarine, corev1.EventTypeWarning, ErrResourceExists, msg)
return fmt.Errorf(msg)
}
// Step 3: Create Service
service := &corev1.Service{}
err = r.Get(ctx, types.NamespacedName{Name: tensorboardServiceName, Namespace: submarine.Namespace}, service)
// If the resource doesn't exist, we'll create it
if errors.IsNotFound(err) {
service = r.newSubmarineTensorboardService(ctx, submarine)
err = r.Create(ctx, service)
r.Log.Info("Create Service", "name", service.Name)
}
// If an error occurs during Get/Create, we'll requeue the item so we can
// attempt processing again later. This could have been caused by a
// temporary network failure, or any other transient reason.
if err != nil {
return err
}
if !metav1.IsControlledBy(service, submarine) {
msg := fmt.Sprintf(MessageResourceExists, service.Name)
r.Recorder.Event(submarine, corev1.EventTypeWarning, ErrResourceExists, msg)
return fmt.Errorf(msg)
}
return nil
}
// compareTensorboardDeployment will determine if two Deployments are equal
func (r *SubmarineReconciler) compareTensorboardDeployment(oldDeployment, newDeployment *appsv1.Deployment) bool {
// spec.replicas
if *oldDeployment.Spec.Replicas != *newDeployment.Spec.Replicas {
return false
}
if len(oldDeployment.Spec.Template.Spec.Containers) != 1 {
return false
}
// spec.template.spec.containers[0].image
if oldDeployment.Spec.Template.Spec.Containers[0].Image !=
newDeployment.Spec.Template.Spec.Containers[0].Image {
return false
}
// spec.template.spec.imagePullSecrets
if !util.ComparePullSecrets(oldDeployment.Spec.Template.Spec.ImagePullSecrets,
newDeployment.Spec.Template.Spec.ImagePullSecrets) {
return false
}
return true
}