Skip to content

Commit

Permalink
asset/manifests: add openshift-install configmap
Browse files Browse the repository at this point in the history
This adds support for generating the openshift-install ConfigMap when
using a UPI installation flow. Before this, the ConfigMap was generated
during the "cluster" target rather than the manifests target, so that we
could distinguish between an IPI and a UPI installation. The user had
the option of overriding the "invoker" field using the environment
variable OPENSHIFT_INSTALL_INVOKER. Even if the environment variable was
used, the ConfigMap would not be present in a UPI installation (because
it wasn't generated in the "manifests" target). This change causes the
installer to generate that ConfigMap in the "manifests" target (in
addition to the "cluster" target) when it sees the environment variable.

We will make use of this functionality in CI, where we specify the job
as the invoker.
  • Loading branch information
crawford committed Jan 27, 2020
1 parent ee1b793 commit 27de643
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 15 deletions.
31 changes: 17 additions & 14 deletions pkg/asset/cluster/tfvars.go
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net"
"os"
"path/filepath"

igntypes "github.com/coreos/ignition/config/v2_2/types"
gcpprovider "github.com/openshift/cluster-api-provider-gcp/pkg/apis/gcpprovider/v1beta1"
Expand All @@ -28,6 +29,7 @@ import (
openstackconfig "github.com/openshift/installer/pkg/asset/installconfig/openstack"
ovirtconfig "github.com/openshift/installer/pkg/asset/installconfig/ovirt"
"github.com/openshift/installer/pkg/asset/machines"
"github.com/openshift/installer/pkg/asset/openshiftinstall"
"github.com/openshift/installer/pkg/asset/rhcos"
"github.com/openshift/installer/pkg/tfvars"
awstfvars "github.com/openshift/installer/pkg/tfvars/aws"
Expand All @@ -49,7 +51,6 @@ import (
openstackdefaults "github.com/openshift/installer/pkg/types/openstack/defaults"
"github.com/openshift/installer/pkg/types/ovirt"
"github.com/openshift/installer/pkg/types/vsphere"
"github.com/openshift/installer/pkg/version"
)

const (
Expand Down Expand Up @@ -491,26 +492,28 @@ func (t *TerraformVariables) Load(f asset.FileFetcher) (found bool, err error) {
// injectInstallInfo adds information about the installer and its invoker as a
// ConfigMap to the provided bootstrap Ignition config.
func injectInstallInfo(bootstrap []byte) (string, error) {
openshiftInstallPath := filepath.Join("/", "opt", "openshift", openshiftinstall.ConfigPath)
config := &igntypes.Config{}
if err := json.Unmarshal(bootstrap, &config); err != nil {
return "", errors.Wrap(err, "failed to unmarshal bootstrap Ignition config")
}

invoker := "user"
if env := os.Getenv("OPENSHIFT_INSTALL_INVOKER"); env != "" {
invoker = env
// If the openshift-install ConfigMap is already present, don't bother
// injecting another. In fact, while it's okay in Ignition v0s2 for a file
// to be defined multiple times (the last occurrence takes precedence), it
// is an error in Ignition v2s3, which is used by OKD.
for _, file := range config.Storage.Files {
if file.Path == openshiftInstallPath {
return string(bootstrap), nil
}
}

cm, err := openshiftinstall.CreateInstallConfig("user")
if err != nil {
return "", errors.Wrap(err, "failed to generate openshift-install config")
}

config.Storage.Files = append(config.Storage.Files, ignition.FileFromString("/opt/openshift/manifests/openshift-install.yml", "root", 0644, fmt.Sprintf(`---
apiVersion: v1
kind: ConfigMap
metadata:
name: openshift-install
namespace: openshift-config
data:
version: "%s"
invoker: "%s"
`, version.Raw, invoker)))
config.Storage.Files = append(config.Storage.Files, ignition.FileFromString(openshiftInstallPath, "root", 0644, cm))

ign, err := json.Marshal(config)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion pkg/asset/manifests/openshift.go
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/openshift/installer/pkg/asset/installconfig/ovirt"
"github.com/openshift/installer/pkg/asset/machines"
openstackmanifests "github.com/openshift/installer/pkg/asset/manifests/openstack"
"github.com/openshift/installer/pkg/asset/openshiftinstall"

osmachine "github.com/openshift/installer/pkg/asset/machines/openstack"
"github.com/openshift/installer/pkg/asset/password"
Expand Down Expand Up @@ -56,6 +57,7 @@ func (o *Openshift) Dependencies() []asset.Asset {
&installconfig.InstallConfig{},
&installconfig.ClusterID{},
&password.KubeadminPassword{},
&openshiftinstall.Config{},

&openshift.CloudCredsSecret{},
&openshift.KubeadminPasswordSecret{},
Expand All @@ -70,7 +72,8 @@ func (o *Openshift) Generate(dependencies asset.Parents) error {
installConfig := &installconfig.InstallConfig{}
clusterID := &installconfig.ClusterID{}
kubeadminPassword := &password.KubeadminPassword{}
dependencies.Get(installConfig, kubeadminPassword, clusterID)
openshiftInstall := &openshiftinstall.Config{}
dependencies.Get(installConfig, kubeadminPassword, clusterID, openshiftInstall)
var cloudCreds cloudCredsSecretData
platform := installConfig.Config.Platform.Name()
switch platform {
Expand Down Expand Up @@ -218,6 +221,10 @@ func (o *Openshift) Generate(dependencies asset.Parents) error {
})
}

if openshiftInstall.File != nil {
o.FileList = append(o.FileList, openshiftInstall.Files()...)
}

asset.SortFiles(o.FileList)

return nil
Expand Down
115 changes: 115 additions & 0 deletions pkg/asset/openshiftinstall/openshiftinstall.go
@@ -0,0 +1,115 @@
package openshiftinstall

import (
"os"
"path/filepath"

"github.com/ghodss/yaml"
"github.com/pkg/errors"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/version"
)

var (
// ConfigPath is the relative path of openshift-install within the asset
// directory.
ConfigPath = filepath.Join("openshift", "openshift-install.yaml")
)

// Config generates the openshift-install ConfigMap.
type Config struct {
File *asset.File
}

var _ asset.WritableAsset = (*Config)(nil)

// Name returns a human friendly name for the asset.
func (*Config) Name() string {
return "OpenShift Install"
}

// Dependencies returns all of the dependencies directly needed to generate
// the asset.
func (*Config) Dependencies() []asset.Asset {
return []asset.Asset{}
}

// Generate generates the openshift-install ConfigMap.
func (i *Config) Generate(dependencies asset.Parents) error {
cm, err := CreateInstallConfig("")
if err != nil {
return err
}

if cm != "" {
i.File = &asset.File{
Filename: ConfigPath,
Data: []byte(cm),
}
}

return nil
}

// Files returns the files generated by the asset.
func (i *Config) Files() []*asset.File {
if i.File != nil {
return []*asset.File{i.File}
}
return []*asset.File{}
}

// Load loads the already-rendered files back from disk.
func (i *Config) Load(f asset.FileFetcher) (bool, error) {
file, err := f.FetchByName(ConfigPath)
if os.IsNotExist(err) {
return false, nil
} else if err != nil {
return false, err
}
i.File = file
return true, nil
}

// CreateInstallConfig creates the openshift-install ConfigMap from the
// OPENSHIFT_INSTALL_INVOKER environment variable, and if not present, from the
// provided default invoker. If both the environment variable and the default
// are the empty string, this returns an empty string (indicting that no
// ConfigMap should be created. This returns an error if the marshalling to
// YAML fails.
func CreateInstallConfig(defaultInvoker string) (string, error) {
var invoker string
if env := os.Getenv("OPENSHIFT_INSTALL_INVOKER"); env != "" {
invoker = env
} else if defaultInvoker != "" {
invoker = defaultInvoker
} else {
return "", nil
}

cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "openshift-config",
Name: "openshift-install",
},
Data: map[string]string{
"version": version.Raw,
"invoker": invoker,
},
}

cmData, err := yaml.Marshal(cm)
if err != nil {
return "", errors.Wrapf(err, "failed to create install-config ConfigMap")
}

return string(cmData), nil
}

0 comments on commit 27de643

Please sign in to comment.