Skip to content

Commit f6910dc

Browse files
committed
make registry installation a component
1 parent f91f8b0 commit f6910dc

File tree

3 files changed

+143
-100
lines changed

3 files changed

+143
-100
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package registry
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path"
7+
8+
"github.com/golang/glog"
9+
10+
apierrors "k8s.io/apimachinery/pkg/api/errors"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/client-go/kubernetes"
13+
"k8s.io/client-go/rest"
14+
15+
"github.com/openshift/origin/pkg/oc/bootstrap/clusterup/componentinstall"
16+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/dockerhelper"
17+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/openshift"
18+
"github.com/openshift/origin/pkg/oc/bootstrap/docker/run"
19+
"github.com/openshift/origin/pkg/oc/errors"
20+
securityclient "github.com/openshift/origin/pkg/security/generated/internalclientset/typed/security/internalversion"
21+
)
22+
23+
const (
24+
DefaultNamespace = "default"
25+
SvcDockerRegistry = "docker-registry"
26+
masterConfigDir = "/var/lib/origin/openshift.local.config/master"
27+
RegistryServiceIP = "172.30.1.1"
28+
)
29+
30+
type RegistryComponentOptions struct {
31+
ClusterAdminKubeConfig *rest.Config
32+
33+
OCImage string
34+
MasterConfigDir string
35+
Images string
36+
PVDir string
37+
}
38+
39+
func (r *RegistryComponentOptions) Name() string {
40+
return "openshift-image-registry"
41+
}
42+
43+
func (r *RegistryComponentOptions) Install(dockerClient dockerhelper.Interface, logdir string) error {
44+
kubeClient, err := kubernetes.NewForConfig(r.ClusterAdminKubeConfig)
45+
_, err = kubeClient.Core().Services(DefaultNamespace).Get(SvcDockerRegistry, metav1.GetOptions{})
46+
if err == nil {
47+
// If there's no error, the registry already exists
48+
return nil
49+
}
50+
if !apierrors.IsNotFound(err) {
51+
return errors.NewError("error retrieving docker registry service").WithCause(err)
52+
}
53+
54+
imageRunHelper := run.NewRunHelper(dockerhelper.NewHelper(dockerClient)).New()
55+
glog.Infof("Running %q", r.Name())
56+
57+
securityClient, err := securityclient.NewForConfig(r.ClusterAdminKubeConfig)
58+
if err != nil {
59+
return err
60+
}
61+
err = openshift.AddSCCToServiceAccount(securityClient, "privileged", "registry", "default", os.Stdout)
62+
if err != nil {
63+
return errors.NewError("cannot add privileged SCC to registry service account").WithCause(err)
64+
}
65+
66+
// Obtain registry markup. The reason it is not created outright is because
67+
// we need to modify the ClusterIP of the registry service. The command doesn't
68+
// have an option to set it.
69+
flags := []string{
70+
"adm",
71+
"registry",
72+
"--loglevel=8",
73+
"--config=" + masterConfigDir + "/admin.kubeconfig",
74+
fmt.Sprintf("--images=%s", r.Images),
75+
fmt.Sprintf("--mount-host=%s", path.Join(r.PVDir, "registry")),
76+
}
77+
_, stdout, stderr, rc, err := imageRunHelper.Image(r.OCImage).
78+
Privileged().
79+
DiscardContainer().
80+
HostNetwork().
81+
HostPid().
82+
Bind(r.MasterConfigDir + ":" + masterConfigDir).
83+
Entrypoint("oc").
84+
Command(flags...).Output()
85+
86+
if err := componentinstall.LogContainer(logdir, r.Name(), stdout, stderr); err != nil {
87+
glog.Errorf("error logging %q: %v", r.Name(), err)
88+
}
89+
if err != nil {
90+
return errors.NewError("could not run %q: %v", r.Name(), err).WithCause(err)
91+
}
92+
if rc != 0 {
93+
return errors.NewError("could not run %q: rc==%v", r.Name(), rc)
94+
}
95+
96+
svc, err := kubeClient.Core().Services(DefaultNamespace).Get(SvcDockerRegistry, metav1.GetOptions{})
97+
if err != nil {
98+
return err
99+
}
100+
svc.ResourceVersion = ""
101+
svc.Spec.ClusterIP = RegistryServiceIP
102+
if err := kubeClient.Core().Services(DefaultNamespace).Delete(svc.Name, nil); err != nil {
103+
return err
104+
}
105+
if _, err := kubeClient.Core().Services(DefaultNamespace).Create(svc); err != nil {
106+
return err
107+
}
108+
109+
return nil
110+
}

pkg/oc/bootstrap/docker/openshift/admin.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"io"
77
"os"
8-
"path"
98
"path/filepath"
109

1110
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -35,77 +34,6 @@ const (
3534
routerCertPath = masterConfigDir + "/router.pem"
3635
)
3736

38-
// InstallRegistry checks whether a registry is installed and installs one if not already installed
39-
func (h *Helper) InstallRegistry(dockerClient dockerhelper.Interface, ocImage string, kubeClient kclientset.Interface, f *clientcmd.Factory, configDir, logdir, images, pvDir string, out, errout io.Writer) error {
40-
_, err := kubeClient.Core().Services(DefaultNamespace).Get(SvcDockerRegistry, metav1.GetOptions{})
41-
if err == nil {
42-
// If there's no error, the registry already exists
43-
return nil
44-
}
45-
if !apierrors.IsNotFound(err) {
46-
return errors.NewError("error retrieving docker registry service").WithCause(err).WithDetails(h.OriginLog())
47-
}
48-
49-
componentName := "install-registry"
50-
imageRunHelper := run.NewRunHelper(dockerhelper.NewHelper(dockerClient)).New()
51-
glog.Infof("Running %q", componentName)
52-
53-
securityClient, err := f.OpenshiftInternalSecurityClient()
54-
if err != nil {
55-
return err
56-
}
57-
err = AddSCCToServiceAccount(securityClient.Security(), "privileged", "registry", "default", out)
58-
if err != nil {
59-
return errors.NewError("cannot add privileged SCC to registry service account").WithCause(err).WithDetails(h.OriginLog())
60-
}
61-
62-
masterDir := filepath.Join(configDir, "master")
63-
64-
// Obtain registry markup. The reason it is not created outright is because
65-
// we need to modify the ClusterIP of the registry service. The command doesn't
66-
// have an option to set it.
67-
flags := []string{
68-
"adm",
69-
"registry",
70-
"--loglevel=8",
71-
"--config=" + masterConfigDir + "/admin.kubeconfig",
72-
fmt.Sprintf("--images=%s", images),
73-
fmt.Sprintf("--mount-host=%s", path.Join(pvDir, "registry")),
74-
}
75-
_, stdout, stderr, rc, err := imageRunHelper.Image(ocImage).
76-
Privileged().
77-
DiscardContainer().
78-
HostNetwork().
79-
HostPid().
80-
Bind(masterDir + ":" + masterConfigDir).
81-
Entrypoint("oc").
82-
Command(flags...).Output()
83-
84-
if err := componentinstall.LogContainer(logdir, componentName, stdout, stderr); err != nil {
85-
glog.Errorf("error logging %q: %v", componentName, err)
86-
}
87-
if err != nil {
88-
return errors.NewError("could not run %q: %v", componentName, err).WithCause(err)
89-
}
90-
if rc != 0 {
91-
return errors.NewError("could not run %q: rc==%v", componentName, rc)
92-
}
93-
94-
svc, err := kubeClient.Core().Services(DefaultNamespace).Get(SvcDockerRegistry, metav1.GetOptions{})
95-
if err == nil {
96-
return err
97-
}
98-
svc.Spec.ClusterIP = RegistryServiceIP
99-
if err := kubeClient.Core().Services(DefaultNamespace).Delete(svc.Name, nil); err == nil {
100-
return err
101-
}
102-
if _, err := kubeClient.Core().Services(DefaultNamespace).Create(svc); err == nil {
103-
return err
104-
}
105-
106-
return nil
107-
}
108-
10937
// InstallRouter installs a default router on the OpenShift server
11038
func (h *Helper) InstallRouter(dockerClient dockerhelper.Interface, ocImage string, kubeClient kclientset.Interface, f *clientcmd.Factory, configDir, logdir, images, hostIP string, portForwarding bool, out, errout io.Writer) error {
11139
_, err := kubeClient.Core().Services(DefaultNamespace).Get(SvcRouter, metav1.GetOptions{})

pkg/oc/bootstrap/docker/up.go

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
cliconfig "github.com/docker/docker/cli/config"
1818
dockerclient "github.com/docker/docker/client"
1919
"github.com/golang/glog"
20-
"github.com/openshift/origin/pkg/oc/bootstrap/clusterup/tmpformac"
2120
"github.com/spf13/cobra"
2221
"github.com/spf13/pflag"
2322
"golang.org/x/net/context"
@@ -37,6 +36,8 @@ import (
3736
"github.com/openshift/origin/pkg/cmd/util/variable"
3837
"github.com/openshift/origin/pkg/oc/bootstrap"
3938
"github.com/openshift/origin/pkg/oc/bootstrap/clusterup/componentinstall"
39+
"github.com/openshift/origin/pkg/oc/bootstrap/clusterup/components/registry"
40+
"github.com/openshift/origin/pkg/oc/bootstrap/clusterup/tmpformac"
4041
"github.com/openshift/origin/pkg/oc/bootstrap/docker/dockerhelper"
4142
"github.com/openshift/origin/pkg/oc/bootstrap/docker/dockermachine"
4243
"github.com/openshift/origin/pkg/oc/bootstrap/docker/errors"
@@ -481,12 +482,35 @@ func (c *ClusterUpConfig) Start(out io.Writer) error {
481482
}
482483
taskPrinter.Success()
483484

484-
// Install a registry
485-
taskPrinter.StartTask("Installing registry")
486-
if err := c.InstallRegistry(out); err != nil {
487-
return taskPrinter.ToError(err)
485+
clusterAdminKubeConfigBytes, err := ioutil.ReadFile(path.Join(c.LocalConfigDir, "master", "admin.kubeconfig"))
486+
if err != nil {
487+
return err
488+
}
489+
clusterAdminKubeConfig, err := kclientcmd.RESTConfigFromKubeConfig(clusterAdminKubeConfigBytes)
490+
if err != nil {
491+
return err
492+
}
493+
494+
// TODO, now we build up a set of things to install here. We build the list so that we can install everything in
495+
// TODO parallel to avoid anyone accidentally introducing dependencies. We'll start with migrating what we have
496+
// TODO and then we'll try to clean it up.
497+
registryInstall := &registry.RegistryComponentOptions{
498+
ClusterAdminKubeConfig: clusterAdminKubeConfig,
499+
500+
OCImage: c.openshiftImage(),
501+
MasterConfigDir: path.Join(c.LocalConfigDir, "master"),
502+
Images: c.imageFormat(),
503+
PVDir: c.HostPersistentVolumesDir,
504+
}
505+
506+
componentsToInstall := []componentinstall.Component{}
507+
componentsToInstall = append(componentsToInstall, c.ImportInitialObjectsComponents(c.Out)...)
508+
componentsToInstall = append(componentsToInstall, registryInstall)
509+
510+
err = componentinstall.InstallComponents(componentsToInstall, c.GetDockerClient(), path.Join(c.BaseTempDir, "logs"))
511+
if err != nil {
512+
return err
488513
}
489-
taskPrinter.Success()
490514

491515
// Install a router
492516
taskPrinter.StartTask("Installing router")
@@ -504,13 +528,6 @@ func (c *ClusterUpConfig) Start(out io.Writer) error {
504528
taskPrinter.Success()
505529
}
506530

507-
// Import default image streams
508-
taskPrinter.StartTask("Importing default data router")
509-
if err := c.ImportInitialObjects(out); err != nil {
510-
return taskPrinter.ToError(err)
511-
}
512-
taskPrinter.Success()
513-
514531
// Install logging
515532
if c.ShouldInstallLogging {
516533
taskPrinter.StartTask("Installing logging")
@@ -882,19 +899,6 @@ func (c *ClusterUpConfig) imageFormat() string {
882899
return fmt.Sprintf("%s-${component}:%s", c.Image, c.ImageVersion)
883900
}
884901

885-
// InstallRegistry installs the OpenShift registry on the server
886-
func (c *ClusterUpConfig) InstallRegistry(out io.Writer) error {
887-
_, kubeClient, err := c.Clients()
888-
if err != nil {
889-
return err
890-
}
891-
f, err := c.Factory()
892-
if err != nil {
893-
return err
894-
}
895-
return c.OpenShiftHelper().InstallRegistry(c.GetDockerClient(), c.openshiftImage(), kubeClient, f, c.LocalConfigDir, path.Join(c.BaseTempDir, "logs"), c.imageFormat(), c.HostPersistentVolumesDir, out, os.Stderr)
896-
}
897-
898902
// InstallRouter installs a default router on the server
899903
func (c *ClusterUpConfig) InstallRouter(out io.Writer) error {
900904
_, kubeClient, err := c.Clients()
@@ -935,7 +939,8 @@ func (c *ClusterUpConfig) InstallWebConsole(out io.Writer) error {
935939
return c.OpenShiftHelper().InstallWebConsole(f, c.imageFormat(), c.ServerLogLevel, publicURL, masterURL, loggingURL, metricsURL)
936940
}
937941

938-
func (c *ClusterUpConfig) ImportInitialObjects(out io.Writer) error {
942+
// TODO this should become a separate thing we can install, like registry
943+
func (c *ClusterUpConfig) ImportInitialObjectsComponents(out io.Writer) []componentinstall.Component {
939944
componentsToInstall := []componentinstall.Component{}
940945
componentsToInstall = append(componentsToInstall,
941946
c.makeObjectImportInstallationComponentsOrDie(out, openshift.OpenshiftNamespace, map[string]string{
@@ -950,7 +955,7 @@ func (c *ClusterUpConfig) ImportInitialObjects(out io.Writer) error {
950955
componentsToInstall = append(componentsToInstall,
951956
c.makeObjectImportInstallationComponentsOrDie(out, openshift.OpenshiftInfraNamespace, internalCurrentTemplateLocations)...)
952957

953-
return componentinstall.InstallComponents(componentsToInstall, c.GetDockerClient(), path.Join(c.BaseTempDir, "logs"))
958+
return componentsToInstall
954959
}
955960

956961
// InstallLogging will start the installation of logging components

0 commit comments

Comments
 (0)