forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conversion.go
186 lines (166 loc) · 6.61 KB
/
conversion.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
package openshift_controller_manager
import (
"fmt"
"io/ioutil"
"time"
"github.com/ghodss/yaml"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
kyaml "k8s.io/apimachinery/pkg/util/yaml"
kcmoptions "k8s.io/kubernetes/cmd/kube-controller-manager/app/options"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
configv1 "github.com/openshift/origin/pkg/cmd/server/apis/config/v1"
"github.com/openshift/origin/pkg/cmd/server/cm"
cmdflags "github.com/openshift/origin/pkg/cmd/util/flags"
)
func ConvertMasterConfigToOpenshiftControllerConfig(input *configapi.MasterConfig) *configapi.OpenshiftControllerConfig {
// this is the old flag binding logic
flagOptions, err := kcmoptions.NewKubeControllerManagerOptions()
if err != nil {
// coder error
panic(err)
}
flagOptions.GenericComponent.LeaderElection.RetryPeriod = metav1.Duration{Duration: 3 * time.Second}
flagFunc := cm.OriginControllerManagerAddFlags(flagOptions)
errors := cmdflags.Resolve(input.KubernetesMasterConfig.ControllerArguments, flagFunc)
if len(errors) > 0 {
// this can't happen since we only run this on configs we have validated
panic(errors)
}
// deep copy to make sure no linger references are shared
in := input.DeepCopy()
registryURLs := []string{}
if len(in.ImagePolicyConfig.ExternalRegistryHostname) > 0 {
registryURLs = append(registryURLs, in.ImagePolicyConfig.ExternalRegistryHostname)
}
if len(in.ImagePolicyConfig.InternalRegistryHostname) > 0 {
registryURLs = append(registryURLs, in.ImagePolicyConfig.InternalRegistryHostname)
}
buildDefaults, err := getBuildDefaults(in.AdmissionConfig.PluginConfig)
if err != nil {
// this should happen on scrubbed input
panic(err)
}
buildOverrides, err := getBuildOverrides(in.AdmissionConfig.PluginConfig)
if err != nil {
// this should happen on scrubbed input
panic(err)
}
ret := &configapi.OpenshiftControllerConfig{
ClientConnectionOverrides: in.MasterClients.OpenShiftLoopbackClientConnectionOverrides,
ServingInfo: &in.ServingInfo,
Controllers: in.ControllerConfig.Controllers,
LeaderElection: configapi.LeaderElectionConfig{
RetryPeriod: flagOptions.GenericComponent.LeaderElection.RetryPeriod,
RenewDeadline: flagOptions.GenericComponent.LeaderElection.RenewDeadline,
LeaseDuration: flagOptions.GenericComponent.LeaderElection.LeaseDuration,
},
ResourceQuota: configapi.ResourceQuotaControllerConfig{
ConcurrentSyncs: flagOptions.ResourceQuotaController.ConcurrentResourceQuotaSyncs,
SyncPeriod: flagOptions.ResourceQuotaController.ResourceQuotaSyncPeriod,
MinResyncPeriod: flagOptions.GenericComponent.MinResyncPeriod,
},
ServiceServingCert: in.ControllerConfig.ServiceServingCert,
Deployer: configapi.DeployerControllerConfig{
ImageTemplateFormat: in.ImageConfig,
},
Build: configapi.BuildControllerConfig{
ImageTemplateFormat: in.ImageConfig,
BuildDefaults: buildDefaults,
BuildOverrides: buildOverrides,
},
ServiceAccount: configapi.ServiceAccountControllerConfig{
ManagedNames: in.ServiceAccountConfig.ManagedNames,
},
DockerPullSecret: configapi.DockerPullSecretControllerConfig{
RegistryURLs: registryURLs,
},
Network: configapi.NetworkControllerConfig{
ClusterNetworks: in.NetworkConfig.ClusterNetworks,
NetworkPluginName: in.NetworkConfig.NetworkPluginName,
ServiceNetworkCIDR: in.NetworkConfig.ServiceNetworkCIDR,
VXLANPort: in.NetworkConfig.VXLANPort,
},
Ingress: configapi.IngressControllerConfig{
IngressIPNetworkCIDR: in.NetworkConfig.IngressIPNetworkCIDR,
},
SecurityAllocator: *in.ProjectConfig.SecurityAllocator,
ImageImport: configapi.ImageImportControllerConfig{
DisableScheduledImport: in.ImagePolicyConfig.DisableScheduledImport,
MaxScheduledImageImportsPerMinute: in.ImagePolicyConfig.MaxScheduledImageImportsPerMinute,
ScheduledImageImportMinimumIntervalSeconds: in.ImagePolicyConfig.ScheduledImageImportMinimumIntervalSeconds,
},
}
return ret
}
// getBuildDefaults creates a new BuildDefaults that will apply the defaults specified in the plugin config
func getBuildDefaults(pluginConfig map[string]*configapi.AdmissionPluginConfig) (*configapi.BuildDefaultsConfig, error) {
const buildDefaultsPlugin = "BuildDefaults"
scheme := runtime.NewScheme()
configv1.InstallLegacy(scheme)
uncastConfig, err := getPluginConfigObj(pluginConfig, buildDefaultsPlugin, scheme)
if err != nil {
return nil, err
}
if uncastConfig == nil {
return nil, nil
}
config, ok := uncastConfig.(*configapi.BuildDefaultsConfig)
if !ok {
return nil, fmt.Errorf("expected BuildDefaultsConfig, not %T", uncastConfig)
}
return config, nil
}
// getBuildOverrides creates a new BuildOverrides that will apply the overrides specified in the plugin config
func getBuildOverrides(pluginConfig map[string]*configapi.AdmissionPluginConfig) (*configapi.BuildOverridesConfig, error) {
const buildOverridesPlugin = "BuildOverrides"
scheme := runtime.NewScheme()
configv1.InstallLegacy(scheme)
uncastConfig, err := getPluginConfigObj(pluginConfig, buildOverridesPlugin, scheme)
if err != nil {
return nil, err
}
if uncastConfig == nil {
return nil, err
}
config, ok := uncastConfig.(*configapi.BuildOverridesConfig)
if !ok {
return nil, fmt.Errorf("expected BuildDefaultsConfig, not %T", uncastConfig)
}
return config, nil
}
func getPluginConfigObj(pluginConfig map[string]*configapi.AdmissionPluginConfig, pluginName string, scheme *runtime.Scheme) (runtime.Object, error) {
yamlContent, err := getPluginConfigYAML(pluginConfig, pluginName, scheme)
if err != nil {
return nil, err
}
if len(yamlContent) == 0 {
return nil, nil
}
internalDecoder := serializer.NewCodecFactory(scheme).UniversalDecoder()
jsonData, err := kyaml.ToJSON(yamlContent)
if err != nil {
return nil, err
}
return runtime.Decode(internalDecoder, jsonData)
}
// getPluginConfigYAML gives the byte content of the config for a given plugin
func getPluginConfigYAML(pluginConfig map[string]*configapi.AdmissionPluginConfig, pluginName string, scheme *runtime.Scheme) ([]byte, error) {
// Check whether a config is specified for this plugin. If not, default to the
// global plugin config file (if any).
cfg, hasConfig := pluginConfig[pluginName]
if !hasConfig {
return nil, nil
}
obj := cfg.Configuration
if obj == nil {
return ioutil.ReadFile(cfg.Location)
}
codec := serializer.NewCodecFactory(scheme).LegacyCodec(scheme.PrioritizedVersionsAllGroups()...)
json, err := runtime.Encode(codec, obj)
if err != nil {
return nil, err
}
return yaml.JSONToYAML(json)
}