-
Notifications
You must be signed in to change notification settings - Fork 124
/
manager.go
94 lines (79 loc) · 2.75 KB
/
manager.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
/*
Copyright 2019 The Machine Controller 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.
*/
//
// UserData plugin manager.
//
// Package manager provides the instantiation and
// running of the plugins on machine controller side.
package manager
import (
"errors"
"flag"
providerconfigtypes "github.com/kubermatic/machine-controller/pkg/providerconfig/types"
"k8s.io/klog"
)
var (
// ErrLocatingPlugins is returned when a new manager cannot locate
// the plugins for the supported operating systems.
ErrLocatingPlugins = errors.New("one or more user data plugins not found")
// ErrPluginNotFound describes an invalid operating system for
// a user data plugin. Here directory has to be checked if
// correct ones are installed.
ErrPluginNotFound = errors.New("no user data plugin for the given operating system found")
// supportedOS contains a list of operating systems the machine
// controller supports.
supportedOS = []providerconfigtypes.OperatingSystem{
providerconfigtypes.OperatingSystemAmazonLinux2,
providerconfigtypes.OperatingSystemCentOS,
providerconfigtypes.OperatingSystemFlatcar,
providerconfigtypes.OperatingSystemRHEL,
providerconfigtypes.OperatingSystemSLES,
providerconfigtypes.OperatingSystemUbuntu,
}
)
// Manager inits and manages the userdata plugins.
type Manager struct {
debug bool
plugins map[providerconfigtypes.OperatingSystem]*Plugin
}
// New returns an initialised plugin manager.
func New() (*Manager, error) {
m := &Manager{
plugins: make(map[providerconfigtypes.OperatingSystem]*Plugin),
}
flag.BoolVar(&m.debug, "plugin-debug", false, "Switch for enabling the plugin debugging")
m.locatePlugins()
if len(m.plugins) < len(supportedOS) {
return nil, ErrLocatingPlugins
}
return m, nil
}
// ForOS returns the plugin for the given operating system.
func (m *Manager) ForOS(os providerconfigtypes.OperatingSystem) (p *Plugin, err error) {
var found bool
if p, found = m.plugins[os]; !found {
return nil, ErrPluginNotFound
}
return p, nil
}
// locatePlugins tries to find the plugins and inits their wrapper.
func (m *Manager) locatePlugins() {
for _, os := range supportedOS {
plugin, err := newPlugin(os, m.debug)
if err != nil {
klog.Errorf("cannot use plugin '%v': %v", os, err)
continue
}
m.plugins[os] = plugin
}
}