Skip to content

Commit 1dc3faf

Browse files
committed
make registry installation a component
1 parent 89b43e5 commit 1dc3faf

File tree

4 files changed

+159
-100
lines changed

4 files changed

+159
-100
lines changed
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

-72
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

+37-28
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,39 @@ 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+
kubeconfigLoader, err := kclientcmd.NewClientConfigFromBytes(clusterAdminKubeConfigBytes)
490+
if err != nil {
491+
return err
492+
}
493+
clusterAdminKubeConfig, err := kubeconfigLoader.ClientConfig()
494+
if err != nil {
495+
return err
496+
}
497+
498+
// TODO, now we build up a set of things to install here. We build the list so that we can install everything in
499+
// TODO parallel to avoid anyone accidentally introducing dependencies. We'll start with migrating what we have
500+
// TODO and then we'll try to clean it up.
501+
registryInstall := &registry.RegistryComponentOptions{
502+
ClusterAdminKubeConfig: clusterAdminKubeConfig,
503+
504+
OCImage: c.openshiftImage(),
505+
MasterConfigDir: path.Join(c.LocalConfigDir, "master"),
506+
Images: c.imageFormat(),
507+
PVDir: c.HostPersistentVolumesDir,
508+
}
509+
510+
componentsToInstall := []componentinstall.Component{}
511+
componentsToInstall = append(componentsToInstall, c.ImportInitialObjectsComponents(c.Out)...)
512+
componentsToInstall = append(componentsToInstall, registryInstall)
513+
514+
err = componentinstall.InstallComponents(componentsToInstall, c.GetDockerClient(), path.Join(c.BaseTempDir, "logs"))
515+
if err != nil {
516+
return err
488517
}
489-
taskPrinter.Success()
490518

491519
// Install a router
492520
taskPrinter.StartTask("Installing router")
@@ -504,13 +532,6 @@ func (c *ClusterUpConfig) Start(out io.Writer) error {
504532
taskPrinter.Success()
505533
}
506534

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-
514535
// Install logging
515536
if c.ShouldInstallLogging {
516537
taskPrinter.StartTask("Installing logging")
@@ -882,19 +903,6 @@ func (c *ClusterUpConfig) imageFormat() string {
882903
return fmt.Sprintf("%s-${component}:%s", c.Image, c.ImageVersion)
883904
}
884905

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-
898906
// InstallRouter installs a default router on the server
899907
func (c *ClusterUpConfig) InstallRouter(out io.Writer) error {
900908
_, kubeClient, err := c.Clients()
@@ -935,7 +943,8 @@ func (c *ClusterUpConfig) InstallWebConsole(out io.Writer) error {
935943
return c.OpenShiftHelper().InstallWebConsole(f, c.imageFormat(), c.ServerLogLevel, publicURL, masterURL, loggingURL, metricsURL)
936944
}
937945

938-
func (c *ClusterUpConfig) ImportInitialObjects(out io.Writer) error {
946+
// TODO this should become a separate thing we can install, like registry
947+
func (c *ClusterUpConfig) ImportInitialObjectsComponents(out io.Writer) []componentinstall.Component {
939948
componentsToInstall := []componentinstall.Component{}
940949
componentsToInstall = append(componentsToInstall,
941950
c.makeObjectImportInstallationComponentsOrDie(out, openshift.OpenshiftNamespace, map[string]string{
@@ -950,7 +959,7 @@ func (c *ClusterUpConfig) ImportInitialObjects(out io.Writer) error {
950959
componentsToInstall = append(componentsToInstall,
951960
c.makeObjectImportInstallationComponentsOrDie(out, openshift.OpenshiftInfraNamespace, internalCurrentTemplateLocations)...)
952961

953-
return componentinstall.InstallComponents(componentsToInstall, c.GetDockerClient(), path.Join(c.BaseTempDir, "logs"))
962+
return componentsToInstall
954963
}
955964

956965
// InstallLogging will start the installation of logging components

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

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)