Skip to content

Commit 479d1cf

Browse files
committed
Add microshift preset support
After this patch user can use `crc config set preset microshift` and provision the cluster. For microshift preset we need to update the server detail to kubeconfig because default it uses `127.0.0.1` when microshift service start.
1 parent 632676d commit 479d1cf

File tree

8 files changed

+103
-17
lines changed

8 files changed

+103
-17
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ SHELL := /bin/bash
55
OPENSHIFT_VERSION ?= 4.12.5
66
PODMAN_VERSION ?= 4.3.1
77
OKD_VERSION ?= 4.12.0-0.okd-2023-02-18-033438
8+
MICROSHIFT_VERSION ?= 4.12.5
89
BUNDLE_EXTENSION = crcbundle
910
CRC_VERSION = 2.15.0
1011
COMMIT_SHA=$(shell git rev-parse --short HEAD)
@@ -58,6 +59,7 @@ VERSION_VARIABLES := -X $(REPOPATH)/pkg/crc/version.crcVersion=$(CRC_VERSION) \
5859
-X $(REPOPATH)/pkg/crc/version.ocpVersion=$(OPENSHIFT_VERSION) \
5960
-X $(REPOPATH)/pkg/crc/version.okdVersion=$(OKD_VERSION) \
6061
-X $(REPOPATH)/pkg/crc/version.podmanVersion=$(PODMAN_VERSION) \
62+
-X $(REPOPATH)/pkg/crc/version.microshiftVersion=$(MICROSHIFT_VERSION) \
6163
-X $(REPOPATH)/pkg/crc/version.commitSha=$(COMMIT_SHA)
6264
RELEASE_VERSION_VARIABLES := -X $(REPOPATH)/pkg/crc/segment.WriteKey=cvpHsNcmGCJqVzf6YxrSnVlwFSAZaYtp
6365

pkg/crc/config/validations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func ValidateYesNo(value interface{}) (bool, string) {
123123
func validatePreset(value interface{}) (bool, string) {
124124
_, err := crcpreset.ParsePresetE(cast.ToString(value))
125125
if err != nil {
126-
return false, fmt.Sprintf("Unknown preset. Only %s and %s are valid.", crcpreset.Podman, crcpreset.OpenShift)
126+
return false, fmt.Sprintf("Unknown preset. Only %s, %s, %s and %s are valid.", crcpreset.Podman, crcpreset.OpenShift, crcpreset.OKD, crcpreset.Microshift)
127127
}
128128
return true, ""
129129
}

pkg/crc/machine/bundle/constants.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@ var bundleLocations = map[string]bundlesDownloadInfo{
1010
"darwin": {
1111
preset.OpenShift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/openshift/4.12.5/crc_vfkit_4.12.5_amd64.crcbundle",
1212
"7a691040952b138b200d8bd7dd57ba352ba99f0c4006c976b3660bf7e2d5ccc0"),
13+
preset.Microshift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/microshift/4.12.5/crc_microshift_vfkit_4.12.5_amd64.crcbundle",
14+
"18fa7827c5a15318cac75feae2aba40da5ae2c83703301276190b6212e6d93a8"),
1315
},
14-
1516
"linux": {
1617
preset.OpenShift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/openshift/4.12.5/crc_libvirt_4.12.5_amd64.crcbundle",
1718
"12797290dda62930f65e74fd12b8d634ef4d265f50ec708563f37b91ad68edc6"),
19+
preset.Microshift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/microshift/4.12.5/crc_microshift_libvirt_4.12.5_amd64.crcbundle",
20+
"aa79ee3c88d5855f864096512e78627b153d896b8aa07782799a6cfdfef77322"),
1821
},
1922
"windows": {
2023
preset.OpenShift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/openshift/4.12.5/crc_hyperv_4.12.5_amd64.crcbundle",
2124
"f49945f40c9f219845d2086b66fcc29cbca3b8490bf6319d2a40f4e3e7bd1ad8"),
25+
preset.Microshift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/microshift/4.12.5/crc_microshift_hyperv_4.12.5_amd64.crcbundle",
26+
"ed8eba68baba622011ceb9fd08a985097777c53ad941b3493b398c95512542c1"),
2227
},
2328
},
2429
"arm64": {
2530
"darwin": {
2631
preset.OpenShift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/openshift/4.12.5/crc_vfkit_4.12.5_arm64.crcbundle",
2732
"f89fc8fa21c762e8e483575950bef5b2aa62e575df6ed0fb76051258ef6352e5"),
33+
preset.Microshift: download.NewRemoteFile("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/microshift/4.12.5/crc_microshift_vfkit_4.12.5_arm64.crcbundle",
34+
"97fc903ce29ca4af99c8f834d4dc5b55366244e53375179063bd12111e251a73"),
2835
},
2936
},
3037
}

pkg/crc/machine/bundle/constants_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,22 @@ import (
1212
"github.com/stretchr/testify/require"
1313
)
1414

15-
func TestURIOpenShiftVersion(t *testing.T) {
16-
openshiftVersion := version.GetBundleVersion(preset.OpenShift)
17-
require.NotEqual(t, openshiftVersion, "0.0.0-unset", fmt.Sprintf("OpenShift version is unset (%s), build flags are incorrect", openshiftVersion))
15+
func TestBundleVersionURI(t *testing.T) {
16+
checkBundleVersionURI(t, preset.OpenShift)
17+
checkBundleVersionURI(t, preset.Microshift)
18+
}
19+
20+
func checkBundleVersionURI(t *testing.T, p preset.Preset) {
21+
bundleVersion := version.GetBundleVersion(p)
22+
require.NotEqual(t, bundleVersion, "0.0.0-unset", fmt.Sprintf("%s version is unset (%s), build flags are incorrect", p, bundleVersion))
1823

19-
uriPrefix := fmt.Sprintf("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/openshift/%s", openshiftVersion)
24+
uriPrefix := fmt.Sprintf("https://mirror.openshift.com/pub/openshift-v4/clients/crc/bundles/%s/%s", p, bundleVersion)
2025
for _, osInfo := range bundleLocations {
2126
for _, presetInfo := range osInfo {
22-
for _, remoteFile := range presetInfo {
23-
assert.True(t, strings.HasPrefix(remoteFile.URI, uriPrefix), fmt.Sprintf("URI %s does not match OpenShift version %s", remoteFile.URI, openshiftVersion))
27+
for preset, remoteFile := range presetInfo {
28+
if preset == p {
29+
assert.True(t, strings.HasPrefix(remoteFile.URI, uriPrefix), fmt.Sprintf("URI %s does not match %s version %s", remoteFile.URI, p, bundleVersion))
30+
}
2431
}
2532
}
2633
}

pkg/crc/machine/console.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (client *client) GetConsoleURL() (*types.ConsoleResult, error) {
1818
}
1919
defer vm.Close()
2020

21-
if !vm.bundle.IsOpenShift() {
21+
if vm.bundle.IsPodman() {
2222
return nil, fmt.Errorf("Only supported with OpenShift bundles")
2323
}
2424

pkg/crc/machine/kubeconfig.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,16 @@ func contains(arr []string, str string) bool {
227227
}
228228
return false
229229
}
230+
231+
func updateServerDetailsToKubeConfig(input, output string) error {
232+
cfg, err := clientcmd.LoadFromFile(input)
233+
if err != nil {
234+
return err
235+
}
236+
for name, cluster := range cfg.Clusters {
237+
if name == "microshift" && cluster.Server == "https://127.0.0.1:6443" {
238+
cluster.Server = fmt.Sprintf("https://api%s:6443", constants.ClusterDomain)
239+
}
240+
}
241+
return clientcmd.WriteToFile(*cfg, output)
242+
}

pkg/crc/machine/start.go

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ func (client *client) updateVMConfig(startConfig types.StartConfig, vm *virtualM
110110
return nil
111111
}
112112

113-
func growRootFileSystem(sshRunner *crcssh.Runner) error {
113+
func growRootFileSystem(sshRunner *crcssh.Runner, preset crcPreset.Preset) error {
114+
if preset == crcPreset.Microshift {
115+
logging.Debugf("growRootFileSystem does not support LVM which is used by %s images", preset)
116+
return nil
117+
}
114118
// With 4.7, this is quite a manual process until https://github.com/openshift/installer/pull/4746 gets fixed
115119
// See https://github.com/crc-org/crc/issues/2104 for details
116120
rootPart, _, err := sshRunner.Run("realpath", "/dev/disk/by-label/root")
@@ -370,7 +374,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
370374
}
371375

372376
// Trigger disk resize, this will be a no-op if no disk size change is needed
373-
if err := growRootFileSystem(sshRunner); err != nil {
377+
if err := growRootFileSystem(sshRunner, startConfig.Preset); err != nil {
374378
return nil, errors.Wrap(err, "Error updating filesystem size")
375379
}
376380

@@ -408,7 +412,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
408412
return nil, errors.Wrap(err, "Error getting proxy configuration")
409413
}
410414

411-
if !vm.bundle.IsOpenShift() {
415+
if vm.bundle.IsPodman() {
412416
// **************************
413417
// END OF PODMAN START CODE
414418
// **************************
@@ -476,6 +480,31 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
476480
logging.Warn(fmt.Sprintf("Failed to query DNS from host: %v", err))
477481
}
478482

483+
if vm.bundle.IsMicroshift() {
484+
// **************************
485+
// END OF MICROSHIFT START CODE
486+
// **************************
487+
// Start the microshift and copy the generated kubeconfig file
488+
ocConfig := oc.UseOCWithSSH(sshRunner)
489+
ocConfig.Context = "microshift"
490+
ocConfig.Cluster = "microshift"
491+
492+
if err := startMicroshift(ctx, sshRunner, ocConfig, startConfig.PullSecret); err != nil {
493+
return nil, err
494+
}
495+
496+
if client.useVSock() {
497+
if err := ensureRoutesControllerIsRunning(sshRunner, ocConfig, startConfig.Preset, vm.bundle); err != nil {
498+
return nil, err
499+
}
500+
}
501+
502+
return &types.StartResult{
503+
ClusterConfig: types.ClusterConfig{ClusterType: startConfig.Preset},
504+
Status: vmState,
505+
}, nil
506+
}
507+
479508
// Check the certs validity inside the vm
480509
logging.Info("Verifying validity of the kubelet certificates...")
481510
certsExpired, err := cluster.CheckCertsValidity(sshRunner)
@@ -882,11 +911,8 @@ func updateKubeconfig(ctx context.Context, ocConfig oc.Config, sshRunner *crcssh
882911
}
883912

884913
func bundleMismatchWithPreset(preset crcPreset.Preset, bundleMetadata *bundle.CrcBundleInfo) error {
885-
if preset == crcPreset.Podman && bundleMetadata.IsOpenShift() {
886-
return errors.Errorf("Preset %s is used but bundle is provided for %s preset", crcPreset.Podman, crcPreset.OpenShift)
887-
}
888-
if preset != crcPreset.Podman && !bundleMetadata.IsOpenShift() {
889-
return errors.Errorf("Preset %s is used but bundle is provided for %s preset", crcPreset.OpenShift, crcPreset.Podman)
914+
if preset != bundleMetadata.GetBundleType() {
915+
return errors.Errorf("Preset %s is used but bundle is provided for %s preset", preset, bundleMetadata.GetBundleType())
890916
}
891917
return nil
892918
}
@@ -922,3 +948,32 @@ func addPodmanSystemConnections(c *types.ConnectionDetails) error {
922948
}
923949
return nil
924950
}
951+
952+
func startMicroshift(ctx context.Context, sshRunner *crcssh.Runner, ocConfig oc.Config, pullSec cluster.PullSecretLoader) error {
953+
logging.Infof("Starting Microshift service... [takes around 1min]")
954+
content, err := pullSec.Value()
955+
if err != nil {
956+
return err
957+
}
958+
if err := sshRunner.CopyDataPrivileged([]byte(content), "/etc/crio/openshift-pull-secret", 0600); err != nil {
959+
return err
960+
}
961+
if _, _, err := sshRunner.RunPrivileged("Starting microshift service", "systemctl", "start", "microshift"); err != nil {
962+
return err
963+
}
964+
if err := sshRunner.CopyFileFromVM("/var/lib/microshift/resources/kubeadmin/kubeconfig", constants.KubeconfigFilePath, 0600); err != nil {
965+
return err
966+
}
967+
if err := sshRunner.CopyFile(constants.KubeconfigFilePath, "/opt/kubeconfig", 0644); err != nil {
968+
return err
969+
}
970+
if err := updateServerDetailsToKubeConfig(constants.KubeconfigFilePath, constants.KubeconfigFilePath); err != nil {
971+
return err
972+
}
973+
974+
if err := cluster.WaitForAPIServer(ctx, ocConfig); err != nil {
975+
return err
976+
}
977+
978+
return nil
979+
}

pkg/crc/preset/preset.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func ParsePresetE(input string) (Preset, error) {
3737
return OpenShift, nil
3838
case OKD.String():
3939
return OKD, nil
40+
case Microshift.String():
41+
return Microshift, nil
4042
default:
4143
return OpenShift, fmt.Errorf("Cannot parse preset '%s'", input)
4244
}

0 commit comments

Comments
 (0)