-
Notifications
You must be signed in to change notification settings - Fork 209
/
server.go
140 lines (120 loc) · 4.08 KB
/
server.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
// Copyright 2020 The Kubeflow Authors
//
// 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 app
import (
"bufio"
"context"
"io"
"os"
"path/filepath"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeinformers "k8s.io/client-go/informers"
kubeclientset "k8s.io/client-go/kubernetes"
restclientset "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"k8s.io/sample-controller/pkg/signals"
"github.com/kubeflow/mpi-operator/cmd/kubectl-delivery/app/options"
"github.com/kubeflow/mpi-operator/pkg/controllers/kubectl_delivery"
"github.com/kubeflow/mpi-operator/pkg/version"
)
const (
cmdVersion = "v1"
RecommendedKubeConfigPathEnv = "KUBECONFIG"
nsEnvironmentName = "NAMESPACE"
filename = "/etc/mpi/hostfile"
launcherPodSuffix = "launcher"
)
func Run(opt *options.ServerOption) error {
// Check if the -version flag was passed and, if so, print the version and exit.
if opt.PrintVersion {
version.PrintVersionAndExit(cmdVersion)
}
namespace := os.Getenv(nsEnvironmentName)
if len(namespace) == 0 {
klog.Infof("%s not set, use default namespace", nsEnvironmentName)
namespace = metav1.NamespaceDefault
}
if opt.Namespace != "" {
namespace = opt.Namespace
}
if namespace == corev1.NamespaceAll {
klog.Info("Using cluster scoped operator")
} else {
klog.Infof("Scoping operator to namespace %s", namespace)
}
// To help debugging, immediately log version.
klog.Infof("%+v", version.Info(cmdVersion))
// To help debugging, immediately log opts.
klog.Infof("Server options: %+v", opt)
// set up signals so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
// Note: ENV KUBECONFIG will overwrite user defined Kubeconfig option.
if len(os.Getenv(RecommendedKubeConfigPathEnv)) > 0 {
// use the current context in kubeconfig
// This is very useful for running locally.
opt.Kubeconfig = os.Getenv(RecommendedKubeConfigPathEnv)
}
cfg, err := clientcmd.BuildConfigFromFlags(opt.MasterURL, opt.Kubeconfig)
if err != nil {
klog.Fatalf("Error building kubeConfig: %v", err)
}
kubeClient, err := kubeclientset.NewForConfig(restclientset.AddUserAgent(cfg, "mpi-operator-kubectl-delivery"))
if err != nil {
klog.Fatalf("Error building kubeClient: %v", err)
}
kubeInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, 0, kubeinformers.WithNamespace(namespace))
fp, err := os.Open(filepath.Clean(filename))
if err != nil {
klog.Fatalf("Error open file[%s]: %v", filename, err)
}
defer func() {
if err := fp.Close(); err != nil {
klog.Fatalf("Error closing file[%s]: %v", filename, err)
}
}()
bufReader := bufio.NewReader(fp)
pods := []string{}
for {
line, _, err := bufReader.ReadLine()
if err != nil {
if err == io.EOF {
break
}
continue
}
lines := strings.SplitN(string(line), " ", 2)
// When using Intel MPI or MPICH, the hostfile format is hostname:slots, so need spliting the line by colon.
if strings.Contains(lines[0], ":") {
lines = strings.SplitN(string(line), ":", 2)
}
if !strings.HasSuffix(lines[0], launcherPodSuffix) {
pods = append(pods, lines[0])
}
}
controller := kubectl_delivery.NewKubectlDeliveryController(
namespace,
kubeClient,
kubeInformerFactory.Core().V1().Pods(),
pods)
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()
go kubeInformerFactory.Start(ctx.Done())
if err = controller.Run(opt.Threadiness, stopCh); err != nil {
klog.Fatalf("Error running controller: %s", err.Error())
}
return nil
}