Skip to content

Commit

Permalink
Use oc executable embedded in the bundle
Browse files Browse the repository at this point in the history
oc executable is now in the bundle. This removes the hard link between
an openshift/snc release and a crc release. A crc release can use any
bundle compatible: for instance an okd bundle.
  • Loading branch information
guillaumerose authored and gbraad committed Nov 9, 2020
1 parent 329ec11 commit 2feb01f
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 95 deletions.
5 changes: 0 additions & 5 deletions Makefile
@@ -1,10 +1,6 @@
SHELL := /bin/bash

BUNDLE_VERSION = 4.6.1
# OC_VERSION and BUNDLE_VERSION are going to same for release artifacts but
# different for nightly and CI bits where bundle version would be any random
# string or dd-mm-yyyy format.
OC_VERSION ?= ${BUNDLE_VERSION}
BUNDLE_EXTENSION = crcbundle
CRC_VERSION = 1.18.0
COMMIT_SHA=$(shell git rev-parse --short HEAD)
Expand Down Expand Up @@ -49,7 +45,6 @@ __check_defined = \
# Linker flags
VERSION_VARIABLES := -X $(REPOPATH)/pkg/crc/version.crcVersion=$(CRC_VERSION) \
-X $(REPOPATH)/pkg/crc/version.bundleVersion=$(BUNDLE_VERSION) \
-X $(REPOPATH)/pkg/crc/version.ocVersion=$(OC_VERSION) \
-X $(REPOPATH)/pkg/crc/version.commitSha=$(COMMIT_SHA)

# https://golang.org/cmd/link/
Expand Down
3 changes: 0 additions & 3 deletions cmd/crc-embedder/cmd/embed.go
Expand Up @@ -89,17 +89,14 @@ var (
"darwin": {
hyperkit.MachineDriverDownloadURL,
hyperkit.HyperKitDownloadURL,
constants.GetOcURLForOs("darwin"),
constants.GetCRCMacTrayDownloadURL(),
constants.GetGoodhostsURLForOs("darwin"),
},
"linux": {
libvirt.MachineDriverDownloadURL,
constants.GetOcURLForOs("linux"),
constants.GetGoodhostsURLForOs("linux"),
},
"windows": {
constants.GetOcURLForOs("windows"),
constants.GetGoodhostsURLForOs("windows"),
constants.GetCRCWindowsTrayDownloadURL(),
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/crc/cmd/root.go
Expand Up @@ -40,7 +40,7 @@ var (
)

func init() {
if err := constants.EnsureBaseDirExists(); err != nil {
if err := constants.EnsureBaseDirectoriesExist(); err != nil {
logging.Fatal(err.Error())
}
var err error
Expand Down
11 changes: 1 addition & 10 deletions pkg/crc/cache/cache.go
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/crc/pkg/crc/version"
"github.com/code-ready/crc/pkg/download"
"github.com/code-ready/crc/pkg/embed"
"github.com/code-ready/crc/pkg/extract"
Expand Down Expand Up @@ -39,10 +38,6 @@ func New(executableName string, archiveURL string, destDir string, version strin
return &Cache{executableName: executableName, archiveURL: archiveURL, destDir: destDir, version: version, getVersion: getVersion}
}

func getCurrentOcVersion(executablePath string) (string, error) {
return getVersionGeneric(executablePath, "version", "--client")
}

func (c *Cache) GetExecutablePath() string {
return filepath.Join(c.destDir, c.executableName)
}
Expand All @@ -57,7 +52,7 @@ func (c *Cache) GetExecutableName() string {
*
* It returns <version> as a string
*/
func getVersionGeneric(executablePath string, args ...string) (string, error) {
func getVersionGeneric(executablePath string, args ...string) (string, error) { //nolint:deadcode,unused
stdOut, _, err := crcos.RunWithDefaultLocale(executablePath, args...)
parsedOutput := strings.Split(stdOut, ":")
if len(parsedOutput) < 2 {
Expand All @@ -66,10 +61,6 @@ func getVersionGeneric(executablePath string, args ...string) (string, error) {
return strings.TrimSpace(parsedOutput[1]), err
}

func NewOcCache() *Cache {
return New(constants.OcExecutableName, constants.GetOcURL(), constants.CrcOcBinDir, version.GetOcVersion(), getCurrentOcVersion)
}

func NewPodmanCache() *Cache {
return New(constants.PodmanExecutableName, constants.GetPodmanURL(), constants.CrcBinDir, "", nil)
}
Expand Down
28 changes: 9 additions & 19 deletions pkg/crc/constants/constants.go
Expand Up @@ -37,20 +37,6 @@ const (
DefaultContext = "admin"
)

var ocURLForOs = map[string]string{
"darwin": fmt.Sprintf("%s/%s/%s", DefaultOcURLBase, version.GetOcVersion(), "openshift-client-mac.tar.gz"),
"linux": fmt.Sprintf("%s/%s/%s", DefaultOcURLBase, version.GetOcVersion(), "openshift-client-linux.tar.gz"),
"windows": fmt.Sprintf("%s/%s/%s", DefaultOcURLBase, version.GetOcVersion(), "openshift-client-windows.zip"),
}

func GetOcURLForOs(os string) string {
return ocURLForOs[os]
}

func GetOcURL() string {
return GetOcURLForOs(runtime.GOOS)
}

var podmanURLForOs = map[string]string{
"darwin": fmt.Sprintf("%s/%s", DefaultPodmanURLBase, "podman-remote-latest-master-darwin-amd64.zip"),
"linux": fmt.Sprintf("%s/%s", DefaultPodmanURLBase, "podman-remote-latest-master-linux---amd64.zip"),
Expand Down Expand Up @@ -126,11 +112,15 @@ func GetHomeDir() string {
return os.Getenv("HOME")
}

// EnsureBaseDirExists create the ~/.crc dir if its not there
func EnsureBaseDirExists() error {
_, err := os.Stat(CrcBaseDir)
if err != nil {
return os.Mkdir(CrcBaseDir, 0750)
// EnsureBaseDirectoriesExist create the ~/.crc and ~/.crc/oc dirs if they are not present
func EnsureBaseDirectoriesExist() error {
if _, err := os.Stat(CrcBaseDir); err != nil {
if !os.IsNotExist(err) {
return err
}
if err := os.Mkdir(CrcBaseDir, 0750); err != nil {
return err
}
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/crc/logging/logging.go
Expand Up @@ -15,7 +15,7 @@ var (
)

func OpenLogFile(path string) (*os.File, error) {
err := constants.EnsureBaseDirExists()
err := constants.EnsureBaseDirectoriesExist()
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/crc/machine/bundle/metadata.go
Expand Up @@ -6,12 +6,14 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/extract"
crcos "github.com/code-ready/crc/pkg/os"
)

// Metadata structure to unmarshal the crc-bundle-info.json file
Expand Down Expand Up @@ -87,9 +89,30 @@ func GetCachedBundleInfo(bundleName string) (*CrcBundleInfo, error) {
if err != nil {
return nil, err
}
if err := bundleInfo.installSymlinkOrCopy(); err != nil {
return nil, err
}
return &bundleInfo, nil
}

func (bundle *CrcBundleInfo) installSymlinkOrCopy() error {
ocInBundle := filepath.Join(bundle.cachedPath, constants.OcExecutableName)
ocInBinDir := filepath.Join(constants.CrcOcBinDir, constants.OcExecutableName)
if _, err := os.Stat(constants.CrcOcBinDir); err != nil {
if !os.IsNotExist(err) {
return err
}
if err := os.MkdirAll(constants.CrcOcBinDir, 0750); err != nil {
return err
}
}
_ = os.Remove(ocInBinDir)
if runtime.GOOS == "windows" {
return crcos.CopyFileContents(ocInBundle, ocInBinDir, 0750)
}
return os.Symlink(ocInBundle, ocInBinDir)
}

func (bundle *CrcBundleInfo) resolvePath(filename string) string {
return filepath.Join(bundle.cachedPath, filename)
}
Expand Down
33 changes: 0 additions & 33 deletions pkg/crc/preflight/preflight_checks_common.go
Expand Up @@ -15,13 +15,6 @@ import (
)

var genericPreflightChecks = [...]Check{
{
configKeySuffix: "check-oc-cached",
checkDescription: "Checking if oc executable is cached",
check: checkOcExecutableCached,
fixDescription: "Caching oc executable",
fix: fixOcExecutableCached,
},
{
configKeySuffix: "check-podman-cached",
checkDescription: "Checking if podman remote executable is cached",
Expand Down Expand Up @@ -84,32 +77,6 @@ func fixBundleCached() error {
return fmt.Errorf("CRC bundle is not embedded in the executable")
}

// Check if oc executable is cached or not
func checkOcExecutableCached() error {
// Remove oc executable from older location and ignore the error
// We should remove this code after 3-4 releases. (after 2020-07-10)
os.Remove(filepath.Join(constants.CrcBinDir, "oc"))

oc := cache.NewOcCache()
if !oc.IsCached() {
return errors.New("oc executable is not cached")
}
if err := oc.CheckVersion(); err != nil {
return err
}
logging.Debug("oc executable already cached")
return nil
}

func fixOcExecutableCached() error {
oc := cache.NewOcCache()
if err := oc.EnsureIsCached(); err != nil {
return fmt.Errorf("Unable to download oc %v", err)
}
logging.Debug("oc executable cached")
return nil
}

// Check if podman executable is cached or not
func checkPodmanExecutableCached() error {
// Disable the podman cache until further notice
Expand Down
6 changes: 3 additions & 3 deletions pkg/crc/preflight/preflight_darwin_test.go
Expand Up @@ -10,10 +10,10 @@ import (
func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage())
RegisterSettings(cfg)
assert.Len(t, cfg.AllConfigs(), 20)
assert.Len(t, cfg.AllConfigs(), 18)
}

func TestCountPreflights(t *testing.T) {
assert.Len(t, getPreflightChecks(false), 10)
assert.Len(t, getPreflightChecks(true), 16)
assert.Len(t, getPreflightChecks(false), 9)
assert.Len(t, getPreflightChecks(true), 15)
}
14 changes: 7 additions & 7 deletions pkg/crc/preflight/preflight_linux_test.go
Expand Up @@ -12,16 +12,16 @@ func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage())
RegisterSettings(cfg)
options := len(cfg.AllConfigs())
assert.True(t, options == 40 || options == 32)
assert.True(t, options == 38 || options == 30)
}

func TestCountPreflights(t *testing.T) {
assert.Len(t, getPreflightChecksForDistro(crcos.RHEL, false), 21)
assert.Len(t, getPreflightChecksForDistro(crcos.RHEL, true), 21)
assert.Len(t, getPreflightChecksForDistro(crcos.RHEL, false), 20)
assert.Len(t, getPreflightChecksForDistro(crcos.RHEL, true), 20)

assert.Len(t, getPreflightChecksForDistro("unexpected", false), 21)
assert.Len(t, getPreflightChecksForDistro("unexpected", true), 21)
assert.Len(t, getPreflightChecksForDistro("unexpected", false), 20)
assert.Len(t, getPreflightChecksForDistro("unexpected", true), 20)

assert.Len(t, getPreflightChecksForDistro(crcos.Ubuntu, false), 17)
assert.Len(t, getPreflightChecksForDistro(crcos.Ubuntu, true), 17)
assert.Len(t, getPreflightChecksForDistro(crcos.Ubuntu, false), 16)
assert.Len(t, getPreflightChecksForDistro(crcos.Ubuntu, true), 16)
}
6 changes: 3 additions & 3 deletions pkg/crc/preflight/preflight_windows_test.go
Expand Up @@ -10,10 +10,10 @@ import (
func TestCountConfigurationOptions(t *testing.T) {
cfg := config.New(config.NewEmptyInMemoryStorage())
RegisterSettings(cfg)
assert.Len(t, cfg.AllConfigs(), 24)
assert.Len(t, cfg.AllConfigs(), 22)
}

func TestCountPreflights(t *testing.T) {
assert.Len(t, getPreflightChecks(false), 14)
assert.Len(t, getPreflightChecks(true), 16)
assert.Len(t, getPreflightChecks(false), 13)
assert.Len(t, getPreflightChecks(true), 15)
}
7 changes: 0 additions & 7 deletions pkg/crc/version/version.go
Expand Up @@ -20,9 +20,6 @@ var (

// Bundle version which used for the release.
bundleVersion = "0.0.0-unset"

// OC version which should be used.
ocVersion = "0.0.0-unset"
)

const (
Expand Down Expand Up @@ -51,10 +48,6 @@ func GetBundleVersion() string {
return bundleVersion
}

func GetOcVersion() string {
return ocVersion
}

func GetCRCMacTrayVersion() string {
return crcMacTrayVersion
}
Expand Down
3 changes: 0 additions & 3 deletions test/integration/features/basic.feature
Expand Up @@ -37,7 +37,6 @@ Feature: Basic test
@linux
Scenario: CRC setup on Linux
When executing "crc setup" succeeds
Then stderr should contain "Caching oc executable"
And stderr should contain "Checking if CRC bundle is cached in '$HOME/.crc'"
And stderr should contain "Checking if running as non-root"
And stderr should contain "Checking if Virtualization is enabled"
Expand Down Expand Up @@ -67,7 +66,6 @@ Feature: Basic test
@darwin
Scenario: CRC setup on Mac
When executing "crc setup" succeeds
Then stderr should contain "Caching oc executable"
And stderr should contain "Checking if running as non-root"
And stderr should contain "Checking if HyperKit is installed"
And stderr should contain "Checking if crc-driver-hyperkit is installed"
Expand All @@ -79,7 +77,6 @@ Feature: Basic test
@windows
Scenario: CRC setup on Windows
When executing "crc setup" succeeds
Then stderr should contain "Caching oc executable"
Then stderr should contain "Unpacking bundle from the CRC executable" if bundle is embedded
Then stderr should contain "Checking Windows 10 release"
Then stderr should contain "Checking if Hyper-V is installed"
Expand Down

0 comments on commit 2feb01f

Please sign in to comment.