-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
config.go
106 lines (89 loc) · 3.79 KB
/
config.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
package kubeapiserver
import (
"os"
"path"
"github.com/docker/docker/api/types"
"github.com/golang/glog"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config/v1"
"github.com/openshift/origin/pkg/oc/clusteradd/componentinstall"
"github.com/openshift/origin/pkg/oc/clusterup/docker/dockerhelper"
"github.com/openshift/origin/pkg/oc/clusterup/docker/run"
"github.com/openshift/origin/pkg/oc/lib/errors"
"k8s.io/apimachinery/pkg/runtime"
)
const KubeAPIServerDirName = "kube-apiserver"
const OpenShiftAPIServerDirName = "openshift-apiserver"
const OpenShiftControllerManagerDirName = "openshift-controller-manager"
type KubeAPIServerStartConfig struct {
// MasterImage is the docker image for openshift start master
MasterImage string
Args []string
}
func NewKubeAPIServerStartConfig() *KubeAPIServerStartConfig {
return &KubeAPIServerStartConfig{}
}
// Start starts the OpenShift master as a Docker container
// and returns a directory in the local file system where
// the OpenShift configuration has been copied
func (opt KubeAPIServerStartConfig) MakeMasterConfig(dockerClient dockerhelper.Interface, basedir string) (string, error) {
componentName := "create-master-config"
imageRunHelper := run.NewRunHelper(dockerhelper.NewHelper(dockerClient)).New()
glog.Infof("Running %q", componentName)
createConfigCmd := []string{
"start", "master",
}
createConfigCmd = append(createConfigCmd, opt.Args...)
containerId, rc, err := imageRunHelper.Image(opt.MasterImage).
Privileged().
HostNetwork().
HostPid().
SaveContainerLogs(componentName, path.Join(basedir, "logs")).
Command(createConfigCmd...).Run()
defer func() {
if err = dockerClient.ContainerRemove(containerId, types.ContainerRemoveOptions{}); err != nil {
glog.Errorf("error removing %q: %v", containerId, err)
}
}()
if err != nil {
return "", errors.NewError("could not run %q: %v", componentName, err).WithCause(err)
}
if rc != 0 {
return "", errors.NewError("could not run %q: rc==%v", componentName, rc)
}
// TODO eliminate the linkage that other tasks have on this particular structure
masterDir := path.Join(basedir, KubeAPIServerDirName)
if err := os.MkdirAll(masterDir, 0755); err != nil {
return "", err
}
glog.V(1).Infof("Copying OpenShift config to local directory %s", masterDir)
if err = dockerhelper.DownloadDirFromContainer(dockerClient, containerId, "/var/lib/origin/openshift.local.config", masterDir); err != nil {
if removeErr := os.RemoveAll(masterDir); removeErr != nil {
glog.V(2).Infof("Error removing temporary config dir %s: %v", masterDir, removeErr)
}
return "", err
}
// update some listen information to include starting the DNS server
masterconfigFilename := path.Join(masterDir, "master-config.yaml")
masterconfig, err := componentinstall.ReadMasterConfig(masterconfigFilename)
if err != nil {
return "", err
}
addImagePolicyAdmission(&masterconfig.AdmissionConfig)
if err := componentinstall.WriteMasterConfig(masterconfigFilename, masterconfig); err != nil {
return "", err
}
return masterDir, nil
}
func addImagePolicyAdmission(admissionConfig *configapi.AdmissionConfig) {
// default openshift image policy admission
if admissionConfig.PluginConfig == nil {
admissionConfig.PluginConfig = map[string]*configapi.AdmissionPluginConfig{}
}
// Add default ImagePolicyConfig into openshift api master config
policyConfig := []byte(`{"kind":"ImagePolicyConfig","apiVersion":"v1","executionRules":[{"name":"execution-denied",
"onResources":[{"resource":"pods"},{"resource":"builds"}],"reject":true,"matchImageAnnotations":[{"key":"images.openshift.io/deny-execution",
"value":"true"}],"skipOnResolutionFailure":true}]}`)
admissionConfig.PluginConfig["openshift.io/ImagePolicy"] = &configapi.AdmissionPluginConfig{
Configuration: runtime.RawExtension{Raw: policyConfig},
}
}