Skip to content

Commit cfdb3fb

Browse files
committed
make registry installation a component
1 parent c67a2f9 commit cfdb3fb

File tree

4 files changed

+160
-102
lines changed

4 files changed

+160
-102
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.Spec.ClusterIP = RegistryServiceIP
101+
if err := kubeClient.Core().Services(DefaultNamespace).Delete(svc.Name, nil); err != nil {
102+
return err
103+
}
104+
if _, err := kubeClient.Core().Services(DefaultNamespace).Create(svc); err != nil {
105+
return err
106+
}
107+
108+
return nil
109+
}

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: 39 additions & 30 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"
@@ -479,12 +480,39 @@ func (c *ClusterUpConfig) Start(out io.Writer) error {
479480
}
480481
taskPrinter.Success()
481482

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

489517
// Install a router
490518
taskPrinter.StartTask("Installing router")
@@ -502,13 +530,6 @@ func (c *ClusterUpConfig) Start(out io.Writer) error {
502530
taskPrinter.Success()
503531
}
504532

505-
// Import default image streams
506-
taskPrinter.StartTask("Importing default data router")
507-
if err := c.ImportInitialObjects(out); err != nil {
508-
return taskPrinter.ToError(err)
509-
}
510-
taskPrinter.Success()
511-
512533
// Install logging
513534
if c.ShouldInstallLogging {
514535
taskPrinter.StartTask("Installing logging")
@@ -530,7 +551,7 @@ func (c *ClusterUpConfig) Start(out io.Writer) error {
530551
// Install template service broker if our version is high enough
531552
if c.ShouldInstallServiceCatalog {
532553
taskPrinter.StartTask("Installing template service broker")
533-
if err := c.InstallServiceCatalog(out); err != nil {
554+
if err := c.InstallTemplateServiceBroker(out); err != nil {
534555
return taskPrinter.ToError(err)
535556
}
536557
taskPrinter.Success()
@@ -540,7 +561,7 @@ func (c *ClusterUpConfig) Start(out io.Writer) error {
540561
// the TSB registration is not persisted.
541562
if c.ShouldInstallServiceCatalog {
542563
taskPrinter.StartTask("Registering template service broker with service catalog")
543-
if err := c.InstallServiceCatalog(out); err != nil {
564+
if err := c.RegisterTemplateServiceBroker(out); err != nil {
544565
return taskPrinter.ToError(err)
545566
}
546567
taskPrinter.Success()
@@ -884,19 +905,6 @@ func (c *ClusterUpConfig) imageFormat() string {
884905
return fmt.Sprintf("%s-${component}:%s", c.Image, c.ImageVersion)
885906
}
886907

887-
// InstallRegistry installs the OpenShift registry on the server
888-
func (c *ClusterUpConfig) InstallRegistry(out io.Writer) error {
889-
_, kubeClient, err := c.Clients()
890-
if err != nil {
891-
return err
892-
}
893-
f, err := c.Factory()
894-
if err != nil {
895-
return err
896-
}
897-
return c.OpenShiftHelper().InstallRegistry(c.GetDockerClient(), c.openshiftImage(), kubeClient, f, c.LocalConfigDir, path.Join(c.BaseTempDir, "logs"), c.imageFormat(), c.HostPersistentVolumesDir, out, os.Stderr)
898-
}
899-
900908
// InstallRouter installs a default router on the server
901909
func (c *ClusterUpConfig) InstallRouter(out io.Writer) error {
902910
_, kubeClient, err := c.Clients()
@@ -937,7 +945,8 @@ func (c *ClusterUpConfig) InstallWebConsole(out io.Writer) error {
937945
return c.OpenShiftHelper().InstallWebConsole(f, c.imageFormat(), c.ServerLogLevel, publicURL, masterURL, loggingURL, metricsURL)
938946
}
939947

940-
func (c *ClusterUpConfig) ImportInitialObjects(out io.Writer) error {
948+
// TODO this should become a separate thing we can install, like registry
949+
func (c *ClusterUpConfig) ImportInitialObjectsComponents(out io.Writer) []componentinstall.Component {
941950
componentsToInstall := []componentinstall.Component{}
942951
componentsToInstall = append(componentsToInstall,
943952
c.makeObjectImportInstallationComponentsOrDie(out, openshift.OpenshiftNamespace, map[string]string{
@@ -952,7 +961,7 @@ func (c *ClusterUpConfig) ImportInitialObjects(out io.Writer) error {
952961
componentsToInstall = append(componentsToInstall,
953962
c.makeObjectImportInstallationComponentsOrDie(out, openshift.OpenshiftInfraNamespace, internalCurrentTemplateLocations)...)
954963

955-
return componentinstall.InstallComponents(componentsToInstall, c.GetDockerClient(), path.Join(c.BaseTempDir, "logs"))
964+
return componentsToInstall
956965
}
957966

958967
// InstallLogging will start the installation of logging components

vendor/k8s.io/kubernetes/staging/src/k8s.io/client-go/tools/clientcmd/client_config.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)